Fine-Grained Locking
A primary-source survey of how locks, condition variables and their asynchronous cousins are built today: the futex-word locks of Rust, Zig and glibc; the parking lots of WebKit, parking_lot and Go's runtime; the queue locks behind Abseil, nsync, Java's AbstractQueuedSynchronizer and Linux; the operating systems' wait-on-address primitives; the task-level primitives of tokio, smol, Seastar, Trio and kotlinx.coroutines; and the papers, from Concurrent ML in 1991 to combine-and-exchange locks in 2025, that bear on waiting across threads and fibers. Thirteen lock implementations, four OS primitives, four async runtimes, twelve papers, and two runnable benchmarks.
It is the evidence base for sparkles.base.sync — wait-on-address-based Mutex, Condition, Once, Event and Semaphore for D, @nogc nothrow, on Linux, Windows, macOS and FreeBSD — and for event-horizon's WaitWord, which lets fibers on a completion-based loop wait on a word another thread wakes.
This survey answers four questions:
- What should the mutex word be? A parking-lot lock — two bits plus a designated-waker bit — over a fixed table of address-keyed queues: faster than the three-state futex word once one waiter at a time is woken, and the only design that permits wait morphing and handoff. See comparison § (a).
- What condition-variable algorithm? Wait morphing: requeue waiters onto the mutex's queue, with exact notifications. See comparison § (b).
- How fair, and how much spinning? Barge, bound starvation by a count of failed wakes with a handoff to the designated waker, and spin briefly before parking. See comparison § (c).
- How do loop-resident fibers wait on a word? In the same parking lot: a queue node carries a thread's parker or a fiber's loop waker, so every primitive becomes fiber-aware on every OS. See comparison § (d).
Last reviewed: October 12, 2026
Master catalog
| Subject | Category | Language / OS | Primitive / contribution | Link |
|---|---|---|---|---|
Rust std::sync | thread locks | Rust | 3-state futex mutex, sequence-counter condvar, queue-based fallbacks | → |
Zig std.Io sync | thread locks | Zig | futex locks through the Io vtable; a parking futex on Windows | → |
| glibc NPTL | thread locks | C | lll_lock, adaptive mutexes, the 2016 G1/G2 condvar | → |
parking_lot | thread locks | Rust | one-byte locks over a global hashed parking table; condvar requeue | → |
WebKit WTF::Lock | thread locks | C++ | two-bit locks, ParkingLot, randomized eventual fairness | → |
Go sync and sema.go | thread locks | Go | starvation mode after 1 ms; the runtime's address-keyed semaphore treap | → |
Abseil absl::Mutex | thread locks | C++ | queue pointer in the word, Await predicates, Fer condvar transfer | → |
| nsync | thread locks | C | predicate waits without condvars; wake-count starvation bound; nsync_wait_n | → |
Folly synchronization | thread locks | C++ | DistributedMutex combining, ParkingLot, fibers::Baton | → |
Java AbstractQueuedSynchronizer | thread locks | Java | CLH-style queue, LockSupport.park, virtual threads unchanged | → |
os_unfair_lock | thread locks | Darwin | owner in the word, priority inheritance, no spinning | → |
SRWLOCK and CONDITION_VARIABLE | thread locks | Windows | pointer-sized, zero-initialized; the 2005–2024 shared-mode bug | → |
Queue locks (MCS, CLH, qspinlock) | thread locks | Linux, papers | local spinning, the MCS tail in a 32-bit word, HANDOFF, CNA | → |
| Linux futexes | OS wait | Linux | futex, PI, requeue, futex2, io_uring futex ops | → |
Windows WaitOnAddress | OS wait | Windows | user-mode waiter table over thread alerts; IOCP bridges | → |
Darwin __ulock and os_sync | OS wait | Darwin | private SPI, the macOS 14.4 public API, EVFILT_USER bridging | → |
FreeBSD _umtx_op | OS wait | FreeBSD | UMTX_OP_WAIT_UINT_PRIVATE; libthr's user-space condvar queue | → |
tokio sync | async | Rust | intrusive FIFO semaphore; cancellation that passes the permit on | → |
smol event-listener / async-lock | async | Rust | one list for task wakers and thread unparkers; 500 µs starvation mode | → |
| Seastar | async | C++ | shard-local primitives; membarrier wakeups | → |
| Trio | async | Python | a per-object ParkingLot; FIFO handoff; from_thread | → |
| Reppy 1991 — CML | paper | ML | first-class synchronous events; choose; the Parallel CML protocol | → |
| von Behren et al. 2003 — Capriccio | paper | C | user-level threads over async I/O; the cost of the idle path | → |
| Li & Zdancewic 2007 | paper | Haskell | threads as a monad over an event loop | → |
| Ziarek et al. 2011 | paper | ML | composable asynchronous events: register now, resume when matched | → |
| Tardieu, Wang & Lin 2012 | paper | X10 | work stealing with suspendable tasks | → |
| Seo et al. 2018 — Argobots | paper | C | user-level threads and tasklets for HPC runtimes | → |
| Enberg, Rao & Tarkoma 2019 | paper | C++ | thread-per-core; the cross-core wakeup protocol and its lost-wakeup hazard | → |
| Karsten & Barghi 2020 | paper | C++ | M:N threading; benaphore idle sleep; one wakeup function | → |
| Koval, Khalanskiy & Alistarh 2023 — CQS | paper | Kotlin | fair, abortable, verified waiter queues; kotlinx.coroutines' Mutex, Semaphore | → |
| Skazhenik et al. 2025 | paper | C++ | spin → yield → suspend for locks under cooperative scheduling | → |
| Skazhenik et al. 2026 | paper (Russian) | C++ | the journal study: BackoffType for TTAS, MCS and HMCS on coroutines | → |
| König, Epple & Becker 2025 — CES | paper | C++ | combine-and-exchange: resume the waiter locally, requeue the releaser | → |
The concepts page defines the shared vocabulary, and the comparison answers the four questions.
Taxonomy
By where waiters live
| Where | Subjects |
|---|---|
| The kernel, keyed by the lock word | Rust std, Zig (Linux), glibc, os_unfair_lock |
| A user-space table keyed by address | parking_lot, WebKit, Go, Folly ParkingLot, WaitOnAddress |
| A queue hanging off the lock | Abseil, nsync, Java AQS, queue locks, tokio, event-listener |
| The waiters' own stacks | Folly DistributedMutex, Linux qspinlock |
| One thread's data structures (no sharing) | Seastar, Trio, Enberg 2019 |
By what a fiber or task does while it waits
| Strategy | Subjects |
|---|---|
| Kernel wait as a completion | Zig Io.Uring, Linux io_uring futex ops |
| One park call that knows the waiter's kind | Java virtual threads, Go gopark, event-listener, Folly Baton |
| Task-level primitives only | tokio, Seastar, Trio, kotlinx (CQS) |
| Blocks the OS thread | Rust std, glibc, parking_lot, WebKit, Abseil |
Milestones
| Year | Milestone |
|---|---|
| 1991 | MCS queue locks; Concurrent ML's first-class events |
| 2002 | Linux futexes merged (2.5.7); Drepper's "Futexes Are Tricky" follows in 2004 |
| 2003 | Capriccio: user-level threads over asynchronous I/O |
| 2004 | Java 5 java.util.concurrent and AbstractQueuedSynchronizer |
| 2012 | Windows 8 adds WaitOnAddress |
| 2015–2016 | WebKit's ParkingLot and WTF::Lock; Rust's parking_lot follows |
| 2017 | glibc 2.25 ships Riegel's 2016 condition variable, which drops requeue |
| 2016 | macOS 10.12 adds os_unfair_lock |
| 2021 | Linux 5.16 adds futex_waitv |
| 2022 | Rust std replaces pthread with futex locks on Linux |
| 2023 | Linux 6.7: futex2 syscalls and io_uring futex ops; Java 21 virtual threads; CQS at PLDI |
| 2024 | macOS 14.4 publishes os_sync_wait_on_address; the SRWLOCK shared-mode fix |
| 2025 | JDK 24 unpins virtual threads in synchronized (JEP 491); combine-and-exchange locks |
| 2026 | parking_lot drops automatic eventual fairness (unreleased at the time of reading) |
Dates are as the deep-dives establish them; the 2002, 2004 and 2012 entries come from the operating systems' documentation, the others from the cited sources.
Suggested reading paths
- Designing
sparkles.base.sync: concepts → Linux futexes → Rust std → glibc →parking_lot→ comparison. - Designing
WaitWord: Zig → Java AQS § async → event-listener →WaitOnAddress§ event-loop integration → CQS → comparison § (d). - Fairness: Go → WebKit → nsync → queue locks → CES.
- Locks under fibers: Capriccio → Karsten & Barghi → Skazhenik 2025 → Argobots.
Examples
Two runnable programs, compiled and run by ci examples standalone:
mutex-throughput.d— throughput, fairness, and futex waits and wakes per acquisition for a three-state futex word, a parking lock with and without a designated waker, a pthread mutex and a spin lock, from one thread to twice the core count.handoff-latency.d— the round trip of handing a turn between two sleeping threads over a futex word, aneventfdand a pthread condition variable.
Sources
Every deep-dive cites its source trees pinned to a commit, and its papers by DOI and full text; the PDFs are kept beside the clones, under $REPOS/papers/fine-grained-locking/.