Chapter 1 · Section 5 of 8 · 6 min read
In the wild
Three mutexes, three sets of choices about the same questions: how do waiters wait, who goes next, who may unlock. Each case follows the five questions from Chapter 0. Every quote comes from the page linked in the case, opened on 2026-11-24. When a project's documents do not say why, the case says so.
Go's sync.Mutex: fast by default, fair when it has to be
What they chose. A hybrid. Normal mode lets a running goroutine take a free lock even when others are waiting. A waiter that fails for more than 1 ms switches the mutex to starvation mode, where the lock is handed straight to the first in the queue. (Source: the comment in src/internal/sync/mutex.go, Go 1.25.2, lines 31-55, quoted in the previous section.)
Why, in their words. The same comment gives two reasons, one for each mode: "Normal mode has considerably better performance as a goroutine can acquire a mutex several times in a row even if there are blocked waiters. Starvation mode is important to prevent pathological cases of tail latency." The Go 1.9 release notes list, under the sync package: "Mutex is now more fair." (Go 1.9 Release Notes.)
What else was possible. The two pure policies, always-fair handoff or always-let-the-runner-win, are exactly the two modes, so the comment itself describes what each alone would cost. The comment also says starvation mode "is so inefficient, that two goroutines can go lock-step infinitely."
The original discussion. I did not find the issue or code review for the Go 1.9 change in the sources I opened, so I do not describe the original discussion. The release note and the source comment are what I can cite.
What it costs. Complexity: the state packs a lock bit, a woken bit, a starving bit and a waiter count into one word, and the slow path has to keep them consistent. The source throws a fatal error on inconsistent state ("sync: inconsistent mutex state").
What to copy. When you need a lock to be both fast and fair, a policy that watches how long the unluckiest waiter has waited and changes behaviour when a limit is passed. You will meet the same idea, a timeout that flips a system into a safer mode, in later chapters.
The Linux kernel mutex: spin only while the owner runs
What they chose. A kernel mutex has three paths. In the kernel documentation's words (Generic Mutex Subsystem, The Linux Kernel documentation):
- the fastpath "tries to atomically acquire the lock by cmpxchg() ing the owner with the current task", which "only works in the uncontended case";
- the midpath, "aka optimistic spinning, tries to spin for acquisition while the lock owner is running";
- the slowpath, "last resort", where "the task is added to the wait-queue and sleeps until woken up by the unlock path."
Why, in their words. For spinning: "The rationale is that if the lock owner is running, it is likely to release the lock soon." And: "By simply not interrupting a task and busy-waiting for a few cycles instead of immediately sleeping, the performance of this lock has been seen to significantly improve a number of workloads." The page notes that mutexes were "introduced in 2006" as an alternative to binary semaphores, with "simpler interfaces".
What else was possible, and what differs from Go. Go decides whether to spin from the state of the processors (only a few spins, only if another processor is busy). The kernel decides from the state of the owner: spin only while the owner is running. The kernel also queues spinners so that "only one spinner can compete for the mutex", using an MCS lock, described as "a simple spinlock with the desirable properties of being fair" and without "expensive cacheline bouncing". Chapter 15 covers queue locks.
Rules that differ from Go. The kernel enforces: "Only one task can hold the mutex at a time. Only the owner can unlock the mutex. Multiple unlocks are not permitted. Recursive locking/unlocking is not permitted." Go has the same rule about recursion in practice, as you will see in the Pitfalls section, but the opposite rule about who may unlock: a Go mutex is "not associated with a particular goroutine."
What it costs. The page calls mutexes "sleeping locks", and its rules say they "may not be used in hardware or software interrupt contexts such as tasklets and timers" (same page, Semantics).
What to copy. Spin only when you have evidence the wait will be short, such as "the holder is running right now."
Java's ReentrantLock: let the caller choose fairness
What they chose. The caller chooses at construction time. The JDK documentation says: "The constructor for this class accepts an optional fairness parameter. When set true, under contention, locks favor granting access to the longest-waiting thread. Otherwise this lock does not guarantee any particular access order." (ReentrantLock, Java SE 21 & JDK 21.)
Why, in their words. The page states the trade-off: "Programs using fair locks accessed by many threads may display lower overall throughput (i.e., are slower; often much slower) than those using the default setting, but have smaller variances in times to obtain locks and guarantee lack of starvation."
What it does not promise. "Note however, that fairness of locks does not guarantee fairness of thread scheduling." One thread using a fair lock may still get it several times in a row while other active threads are not holding it. The untimed tryLock() "does not honor the fairness setting."
What else was possible. The page itself describes the class as having "the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities", so the implicit lock is the alternative, and the extras include the fairness choice. I did not find a design document explaining why fairness is a constructor argument rather than adaptive, so I will not offer a reason.
What it costs. The programmer must pick, and the documentation warns that the fair setting can be "often much slower".
What to copy. If your callers know their workload, expose the choice. If they do not, do what Go does and make the lock adapt.
Side by side
Go sync.Mutex | Linux kernel mutex | Java ReentrantLock | |
|---|---|---|---|
| Fairness policy | Adaptive: normal mode, then starvation mode after a 1 ms wait | The page describes a fair queue for spinners (MCS lock) and a wait-queue for sleepers | Caller chooses at construction |
| Waiting | Spin a few times if the processors allow, then sleep | Spin while the owner runs, then sleep | The page does not describe the mechanism |
| Who may unlock | Any goroutine | Only the owner | The thread that locked it: "owned by the thread last successfully locking, but not yet unlocking it" |
| Recursive locking | Blocks forever (see Pitfalls) | Not permitted | Allowed: returns immediately if the current thread already owns it |
Sources: the three pages linked above, and the Go source comment and go doc text quoted in the previous sections.