以下代码来自维基百科(http://en.wikipedia.org/wiki/Test-and-set)
enter_region: ; A "jump to" tag; function entry point.
tsl reg, flag ; Test and Set Lock; flag is the
; shared variable; it is copied
; into the register reg and flag
; then atomically set to 1.
cmp reg, #0 ; Was flag zero on entry_region?
jnz enter_region ; Jump to enter_region if
; reg is non-zero; i.e.,
; flag was non-zero on entry.
ret ; Exit; i.e., flag was zero on
; entry. If we get here, tsl
; will have set it non-zero; thus,
; we have claimed the resource as-
; sociated with flag.
leave_region:
move flag, #0 ;store 0 in flag
ret ;return to caller
据我所知,这两个命令tsl reg, flag
和cmp reg, #0
可以合并为一个命令tsl reg, flag
,它共同完成三件事:(1)将flag
复制到{{} 1}}(2)将reg
设置为flag
(3)测试1
是否为零。从reg
命令中分离(3)
有什么好处或必要性?有没有人有这方面的想法?
答案 0 :(得分:0)
tsl
实际上并没有测试flag
的内容(除了它的名字);它只是原子地将flag
复制到reg
。在x86中,它通常使用xchg
或比较和交换指令实现,当后者可用时。