Test-and-Set and Swap
Test-and-Set and Swap
Recorded by Dr. Krithika L.B. Slot m3-tas-swap
Atomic instructions that make locks possible on multiprocessors, how to build mutual exclusion and bounded waiting from them, and the mutex lock that programmers actually use.
By the end of this session you should be able to:
On a uniprocessor the simplest fix is to disable interrupts during the critical section: no timer interrupt → no context switch → no interleaving. This does not work on multiprocessors (other CPUs keep running), disabling interrupts on every CPU is slow, and user programs must not be allowed to do it. Instead, modern CPUs provide special atomic instructions: they read and modify a memory word as one uninterruptible unit, even with several CPUs.
bool test_and_set(bool *target) { /* atomic */
bool rv = *target; /* remember old value */
*target = true; /* always set to true */
return rv; /* return old value */
}
bool lock = false; /* shared */
do {
while (test_and_set(&lock))
; /* spin while it was already true */
/* critical section */
lock = false;
/* remainder section */
} while (true);The other classic instruction exchanges two values atomically. The textbook Swap(&a, &b) swaps two words;
modern CPUs provide compare-and-swap (CAS, x86 cmpxchg), which writes a new value only if the word
still holds an expected value.
/* Swap version (each process has a local key) */
void Swap(bool *a, bool *b) { bool t = *a; *a = *b; *b = t; } /* atomic */
do {
key = true;
while (key == true)
Swap(&lock, &key); /* exits when the old lock value was false */
/* critical section */
lock = false;
/* remainder section */
} while (true);
/* compare_and_swap version */
int compare_and_swap(int *value, int expected, int new_value) { /* atomic */
int temp = *value;
if (*value == expected)
*value = new_value;
return temp;
}
while (compare_and_swap(&lock, 0, 1) != 0)
; /* spin */
/* critical section */
lock = 0;They give mutual exclusion and progress but not bounded waiting — an unlucky process can lose the race forever. A bounded-waiting version uses a waiting[n] array: on exit, a process scans (i+1) … (i−1) and passes the lock directly to the next waiting process, so each waits at most n − 1 turns.
do {
waiting[i] = true;
key = true;
while (waiting[i] && key)
key = test_and_set(&lock);
waiting[i] = false;
/* critical section */
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n; /* find next waiting process */
if (j == i)
lock = false; /* nobody waiting: release */
else
waiting[j] = false; /* hand over directly to Pj */
/* remainder section */
} while (true);Atomic variables (C11 atomic_int, Java AtomicInteger) are built on CAS and make single updates such
as count++ safe without a lock.
Hardware instructions are awkward for application programmers, so OS and library designers wrap them in a
mutex lock (mutual exclusion). A process must acquire() the lock before entering its
critical section and release() it on exit. A boolean available says whether the lock is free; calls to
acquire and release must be atomic (implemented with CAS).
acquire() {
while (!available)
; /* busy wait */
available = false;
}
release() {
available = true;
}
do {
acquire();
/* critical section */
release();
/* remainder section */
} while (true);pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&m);
count++; /* critical section */
pthread_mutex_unlock(&m);The waiting process loops, wasting CPU. Good on multiprocessors when locks are held for a very short time — no context switch needed. Bad on a single CPU or for long critical sections.
The waiting process is put to sleep in a queue and woken on release. No wasted cycles, but each wait costs two context switches. Semaphores (Session 3.4) do this.
Test-and-Set and Swap
Recorded by Dr. Krithika L.B. Slot m3-tas-swap
Mutex locks and spinlocks
Recorded by Dr. Krithika L.B. Slot m3-mutex
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0If two CPUs could both read false before either wrote true, both would enter the critical section.
No — a process can keep losing the race. The waiting[] array version passes the lock in cyclic order, giving bounded waiting of n − 1.
On a multiprocessor when the lock is held for less time than two context switches would take.
0 (the old value), and it atomically sets lock to 1 — the caller has acquired the lock.
A user program could disable interrupts and never re-enable them, taking over the machine; it also does not stop other CPUs.
A mutex says “one at a time”. What if we need “at most N at a time”, or “B must happen after A”? Semaphores generalise the lock.
Continue to 3.4 — Semaphores →