Systems study
Offline — showing the cached copy.
Home / Operating Systems / 6. Synchronization primitives

6. Synchronization primitives

One-liner. Atomics order single words; locks protect invariants; futexes make waiting cheap.

Memorize

  • Atomic ops (CAS, fetch-add) are lock-free but only protect one word.
  • Spinlock: busy-waits — correct only for very short critical sections, never across sleep.
  • Mutex: sleeps via futex (fast userspace check, kernel wait queue on contention).
  • Semaphore: counts permits; RW lock: many readers or one writer.
  • RCU: readers lock-free, writers wait for a grace period — great for read-mostly kernel data.
  • Memory barriers (smp_mb, acquire/release) constrain reordering; atomics imply ordering only if you use the right variant.
  • Contention shows up as PSI stall time and involuntary context switches.
FUTEX FAST/SLOW PATH lock uncontended:atomic CAS in u… done contended futex_wait kernel queue unlock futex_wake retry
Futex fast/slow path

Trap. Using a spinlock or atomic to protect a multi-step invariant. Do this. Protect the invariant with one lock; atomics are for counters and flags.

Check

What makes a futex-based mutex cheap when uncontended?