operating-systems hardware concurrency
Definition
Exchange Instruction
The exchange instruction (or swap) is an atomic hardware operation that interchanges the contents of a register and a memory location in a single, uninterruptible step. Like Test-and-Set, it implements mutual exclusion.
Mechanism
exchange(Key, Lock)
temp = Key
Key = Lock
Lock = temp
Mutual Exclusion Protocol
int lock = 0
Void process()
int key = 1
do
exchange(key, lock)
while (key != 0) // busy wait for lock
/* --- Critical Section --- */
exchange(key, lock) // release lock
/* --- Remainder Section --- */
Comparison with Test-and-Set
Functionally similar; exchange is often more flexible on architectures with multiple registers. Both rely on busy waiting, making them less efficient for long waits than kernel-level semaphores.