X-Git-Url: https://pd.if.org/git/?p=zos;a=blobdiff_plain;f=spinlock.s;fp=spinlock.s;h=37849b2233dfc8aa341566d9d7fa112e862b2e73;hp=0000000000000000000000000000000000000000;hb=5a6d8336c11140500330da69cd74490a07876fe1;hpb=5c0e560e481e2e97b793f7574e849a5882781df9 diff --git a/spinlock.s b/spinlock.s new file mode 100644 index 0000000..37849b2 --- /dev/null +++ b/spinlock.s @@ -0,0 +1,30 @@ +global spinlock_acquire:function +global spinlock_release:function +global spinlock_init:function + +; spinlock addr in rdi +; low four bytes is ticket number +; high four bytes is turn counter + +spinlock_init: + mov rax, 0 + mov [rdi], rax + ret + +spinlock_acquire: + mov eax, 1 + lock xadd [rdi], eax ; eax = my ticket number + + cmp [rdi+4], eax ; is it my turn? + je .acquired ; yes + +.retry: + pause + cmp [rdi+4], eax ; is it my turn? + jne .retry ; keep waiting +.acquired: + ret + +spinlock_release: + lock inc dword [rdi+4] + ret