]> pd.if.org Git - zos/blob - spinlock.s
add a readme with a public domain note
[zos] / spinlock.s
1 global spinlock_acquire:function
2 global spinlock_release:function
3 global spinlock_init:function
4
5 ; spinlock addr in rdi
6 ; low four bytes is ticket number
7 ; high four bytes is turn counter
8
9 spinlock_init:
10         mov rax, 0
11         mov [rdi], rax
12         ret
13
14 spinlock_acquire:
15         mov eax, 1
16         lock xadd [rdi], eax   ; eax = my ticket number
17
18         cmp [rdi+4], eax        ; is it my turn?
19         je .acquired             ; yes
20
21 .retry:
22         pause
23         cmp [rdi+4], eax        ; is it my turn?
24         jne .retry                ; keep waiting
25 .acquired:
26         ret
27
28 spinlock_release:
29         lock inc dword [rdi+4]
30         ret