I read the Intel manual and found that there is a lock prefix for instructions that can prevent processors from moving to the same memory location at the same time. I am delighted with this. I guess it can be used as a hardware mutex. So I wrote a code snippet to take a snapshot. The result is pretty frustrating. Lock does not support MOV or LEA instructions. The manual says that LOCK only supports ADD, ADC, AND, BTC, BTR, BTS, CMPXCHG, CMPXCH8B, DEC, INC, NEG, NOT, OR, SBB, SUB, XOR, XADD and XCHG. Moreover, if the LOCK prefix is used with one of these instructions, and the original operand is a memory operand, an undefined code exception (#UD) may be thrown.
I wonder why there are so many restrictions, so many restrictions make LOCK useless. I cannot use it to ensure that the general write operation does not contain dirty data or other problems caused by parallelism.
eg. I wrote ++ (* p) code in C. p is a pointer to shared memory. The corresponding assembly is similar:
movl 28(%esp), %eax
movl (%eax), %eax
leal 1(%eax), %edx
movl 28(%esp), %eax
movl %edx, (%eax)
I added a “lock” before “movl” and “leal”, but the processor complains about “Invalid Instruction.” :-( I assume that the only way to serialize write operations is to use a software mutex, right?