]> git.corax.cc Git - corax/commitdiff
Add spinlock implementation (should have been in commit aa0e4586dd3efe434b34ae7956a4c...
authorMatthias Kruk <m@m10k.eu>
Sat, 23 Nov 2019 06:32:04 +0000 (15:32 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 23 Nov 2019 06:32:04 +0000 (15:32 +0900)
kernel/klibc/spinlock.S [new file with mode: 0644]

diff --git a/kernel/klibc/spinlock.S b/kernel/klibc/spinlock.S
new file mode 100644 (file)
index 0000000..a82c2a5
--- /dev/null
@@ -0,0 +1,54 @@
+       .section .text
+
+       .global spinlock_lock
+       .global spinlock_trylock
+       .global spinlock_unlock
+
+spinlock_lock:
+       movl            4(%esp), %edx
+0:     xorl            %eax, %eax
+       movl            $1, %ecx
+       lock
+       cmpxchgl        %ecx, (%edx)
+       jnz                     0b /* lock failed */
+       ret                     /* lock succeeded */
+
+spinlock_trylock:
+       movl            4(%esp), %edx
+       xorl            %eax, %eax
+       movl            $1, %ecx
+       lock
+       cmpxchgl        %ecx, (%edx)
+       ret
+
+spinlock_unlock:
+       movl            4(%esp), %edx
+       xorl            %eax, %eax
+       lock
+       xchgl           %eax, (%edx)
+       ret
+
+       /* 序でにamd64の実装
+
+spinlock_lock:
+       xorq            %rax, %rax
+       movl            $1, %ecx
+       lock
+       cmpxchgl        %ecx, (%rdi)
+       jnz                     spinlock_lock
+       ret
+
+spinlock_trylock:
+       xorl            %rax, %rax
+       movl            $1, %ecx
+       lock
+       cmpxchgl        %ecx, (%rdi)
+       ret
+
+spinlock_unlock:
+       xorq            %rax, %rax
+       lock
+       xchgl           %eax, (%rdi)
+       ret
+
+       */