什么是"锁定"在x86汇编指令是什么意思? [英] What does the "lock" instruction mean in x86 assembly?
问题描述
我看到一些x86汇编在Qt的源代码:
q_atomic_increment:
MOVL 4(%尤),%ecx中
锁
包括(ECX%)
MOV $ 0,%EAX
setne%人
RET .align伪4,0x90
.TYPE q_atomic_increment,@功能
.size q_atomic_increment,.- q_atomic_increment
-
从谷歌搜索,我就知道
锁定
指令将导致CPU锁定总线,但是,当CPU释放总线,我不知道吗? -
关于整个高于code,我不明白这code如何实现
添加
?
-
锁定
不是指令本身:它是一个指令preFIX,它适用于下面的指令。该指令必须是东西做内存(INC
,XCHG
,<$ C $读 - 修改 - 写C> CMPXCHG 等)---在这种情况下,它是含(%ECX)
指令,INC
rements的→
翁词在保存的地址的ECX
注册。的
锁定
preFIX确保CPU具有操作的持续时间适当的高速缓存线的独占所有权,并提供了一些额外的顺序保证。这可以通过产生一个总线锁定来实现,但是CPU将避免这个,其中可能的。如果总线被锁定那么它仅用于该锁定指令的持续时间。 -
这code将变量的地址递增从栈到
ECX
注册,然后它锁定包括(ECX%)
由1原子方式增加该变量接下来的两个指令设置EAX
注册(持有从返回的值功能),以0,如果变量的新值是0,否则为1。该操作是一个递增,不是一个附加(因此得名)。
I saw some x86 assembly in Qt's source:
q_atomic_increment:
movl 4(%esp), %ecx
lock
incl (%ecx)
mov $0,%eax
setne %al
ret
.align 4,0x90
.type q_atomic_increment,@function
.size q_atomic_increment,.-q_atomic_increment
From Googling, I knew
lock
instruction will cause CPU to lock the bus, but I don't know when CPU frees the bus?About the whole above code, I don't understand how this code implements the
Add
?
LOCK
is not an instruction itself: it is an instruction prefix, which applies to the following instruction. That instruction must be something that does a read-modify-write on memory (INC
,XCHG
,CMPXCHG
etc.) --- in this case it is theincl (%ecx)
instruction whichinc
rements thel
ong word at the address held in theecx
register.The
LOCK
prefix ensures that the CPU has exclusive ownership of the appropriate cache line for the duration of the operation, and provides certain additional ordering guarantees. This may be achieved by asserting a bus lock, but the CPU will avoid this where possible. If the bus is locked then it is only for the duration of the locked instruction.This code copies the address of the variable to be incremented off the stack into the
ecx
register, then it doeslock incl (%ecx)
to atomically increment that variable by 1. The next two instructions set theeax
register (which holds the return value from the function) to 0 if the new value of the variable is 0, and 1 otherwise. The operation is an increment, not an add (hence the name).
这篇关于什么是&QUOT;锁定&QUOT;在x86汇编指令是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!