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 the incl (%ecx)
instruction which inc
rements the l
ong word at the address held in the ecx
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 does lock incl (%ecx)
to atomically increment that variable by 1. The next two instructions set the eax
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).