Sub topic 8
Hardware Support: Test-and-Set and Compare-and-Swap
Prerequisites and the hardware problem
Assume you know processes, threads, preemption, and the critical-section problem. A critical section is code that accesses shared data. Its synchronization solution should provide mutual exclusion, progress, and bounded waiting.
Software algorithms such as Peterson’s algorithm use shared variables to coordinate processes. Hardware instructions provide a simpler foundation. They perform a read and a write as one atomic operation. Atomic means that no other thread can observe the operation halfway through.
Test-and-set
A test-and-set instruction returns the old value of a lock and sets the lock to true in one indivisible step.
#include <stdatomic.h>
#include <stdio.h>
atomic_flag lock = ATOMIC_FLAG_INIT;
atomic_int counter = 0;
void acquire(void) {
while (atomic_flag_test_and_set_explicit(
&lock, memory_order_acquire)) {
/* Spin while another thread owns the lock. */
}
}
void release(void) {
atomic_flag_clear_explicit(&lock, memory_order_release);
}
int main(void) {
acquire();
counter++;
printf("counter = %d\n", atomic_load(&counter));
release();
return 0;
}If the old value is false, the thread acquires the lock. If the old value is true, another thread owns it, so the thread repeats the operation. This repeated checking is called spinning. A lock using this technique is a spinlock.
The acquire operation prevents later memory operations from moving before lock acquisition. The release operation makes earlier writes visible before unlocking. These memory-order guarantees matter on modern processors.
Compare-and-swap
Compare-and-swap (CAS) takes a memory location, an expected value, and a new value. It replaces the location only if the current value equals the expected value. It reports whether the replacement happened.
Conceptually:
if (memory == expected) {
memory = new_value;
return success;
}
return failure;A CAS-based lock can use zero for unlocked and one for locked:
while (!CAS(lock, 0, 1))
;If CAS changes zero to one, the thread enters. If another thread has already changed the value, CAS fails and the thread retries.
Comparing the instructions
| Property | Test-and-set | Compare-and-swap |
|---|---|---|
| Main action | Set a flag and return its old value | Replace a value if it matches an expected value |
| Common use | Simple spinlocks | Locks, counters, and lock-free structures |
| Fairness | Not guaranteed | Not guaranteed |
| Failure behavior | Repeated spinning | Repeated CAS attempts |
A mutex may put a waiting thread to sleep instead of making it consume CPU cycles. Spinlocks are more suitable when the critical section is very short and the processor has multiple cores.
Exercise
A CAS lock stores 0 for unlocked and 1 for locked. The current value is 1. A thread calls CAS(lock, 0, 1). What happens?
Show answer
CAS fails because the current value does not equal the expected value 0. The lock remains 1, and the thread must retry or take another action.