The unlock does need to have release semantics to properly protect the critical section. But it doesn't need sequential-consistency. Atomicity isn't really the issue (see below).
So yes, on x86 a simple store is safe, and glibc's pthread_spin_unlock
does so::
movl $1, (%rdi)
xorl %eax, %eax
retq
See also a simple but maybe usable x86 spinlock implementation I wrote in this answer, using a read-only spin loop with a pause
instruction.
Possibly this code was adapted from a bit-field version.
Unlocking with btr
to zero one flag in a bitfield isn't safe, because it's a non-atomic read-modify-write of the containing byte (or the containing naturally-aligned 4 byte dword or 2 byte word).
So maybe whoever wrote it didn't realize that simple stores to aligned addresses are atomic on x86, like on most ISAs. But what x86 has that weakly-ordered ISAs don't is that every store has release semantics. An xchg
to release the lock makes every unlock a full memory barrier, which goes beyond normal locking semantics. (Although on x86, taking a lock will be a full barrirer, because there's no way to do an atomic RMW or atomic compare-and-swap without an xchg
or other lock
ed instruction, and those are full barriers like mfence
.)
The unlocking store doesn't technically need to be atomic, since we only ever store zero or 1, so only the lower byte matters. e.g. I think it would still work if the lock was unaligned and split across a cache-line boundary. Tearing can happen but doesn't matter, and what's really happening is that the low byte of the lock is modified atomically, with operations that always put zeros into the upper 3 bytes.
If you wanted to return the old value to catch double-unlocking bugs, a better implementation would separately load and store:
spin_unlock:
;; pre-condition: [locked] is non-zero
mov eax, [locked] ; old value, for debugging
mov dword [locked], 0 ; On x86, this is an atomic store with "release" semantics.
;test eax,eax
;jz double_unlocking_detected ; or leave this to the caller
ret
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…