Basic synchronization problem, need to be able to test a value and set it
atomically.
The IA32 hardware provides a special instruction:
xchg
If this instruction accesses memory, during execution the bus is locked to that
no other process or thread can access memory until the instruction completes.
Variations:
xchgb, xchgw, xchgl
Example: using
xchgb
Suppose
%ebp contains the address of a byte of memory that is used
to lock a critical section.
The byte contains 1 if the critical section is in use, and 0 if it is available.
Consider:
movb $1,%al
loop:
xchgb %al, (%ebp)
testb %al,%al
jne loop
<Critical Section>
movb $0, (%ebp)
Note that this only works because the
xchgb instruction is atomic.
This could fail if the
xchgb instruction were replaced by:
movb %al, %ah
movb (%ebp), %al
movb %ah, (%ebp)
swap using xchgb
This example is similar to the swap example in the text.
Next Notes
Back to CS 3733 Notes Table of Contents