以下是 ARM 手册中的示例自旋锁实现。请在此处查看:http: //infocenter.arm.com/help/topic/com.arm.doc.genc007826/Barrier_Litmus_Tests_and_Cookbook_A08.pdf 。关于“使用带有锁的等待事件 (WFE) 和发送事件 (SEV)”的部分。
锁码:
Loop:
LDREX R5, [R1] ; read lock
CMP R5, #0 ; check if 0
WFENE ; sleep if the lock is held
STREXEQ R5, R0, [R1] ; attempt to store new value
CMPEQ R5, #0 ; test if store suceeded
BNE Loop ; retry if not
DMB ; ensures that all subsequent accesses are observed after the
; gaining of the lock is observed
; loads and stores in the critical region can now be performed
解锁代码:
MOV R0, #0
DMB ; ensure all previous accesses are observed before the lock is cleared
STR R0, [R1] ; clear the lock.
DSB ; ensure completion of the store that cleared the lock before sending the event
SEV
问题:为什么使用 STREXEQ?如果使用 STREX 会怎样?据我了解,strexeq 只有在设置了 EQ 标志时才会执行。但是不管怎样,如果没有设置 EQ,那么 WFENE 会确保我们在等待事件吗?那么 STREX EQ是不是不必要的呢?
提前致谢!