A Linux mutex is usually one atomic instruction. The kernel only gets involved when a thread actually has to sleep, and the piece that makes this possible is the futex (fast user-space mutex).
Problem
A lock has two jobs. It has to let exactly one thread in, and it has to make the others wait. The first job is easy in user space: a compare-and-swap on a shared integer does it. The second job is not. Only the kernel can take a thread off the CPU and put it back later.
So we have two bad options:
- Spin. Loop on the compare-and-swap until it succeeds. No kernel involvement, but a waiting thread burns a whole core, and if the lock holder gets descheduled, everyone spins for nothing.
- Ask the kernel every time. System V semaphores worked this way: every
semop()is a system call, even when nobody else wants the lock. Most locks are uncontended most of the time, so this pays the syscall price for nothing.
What we want is the first option when there is no contention and the second only when there is. That is the goal of the original futex paper from 2002 1.
The idea
A futex is just a 32-bit integer in your own memory, plus two system calls that take its address 2:
FUTEX_WAIT(addr, val): put me to sleep, but only if*addrstill equalsval. Otherwise returnEAGAINimmediately.FUTEX_WAKE(addr, n): wake up tonthreads sleeping onaddr.
That’s it. The kernel doesn’t know what the integer means. It doesn’t know it’s a lock. It keeps no state for it at all until a thread calls FUTEX_WAIT, and it forgets it again when the last waiter leaves. All the meaning lives in user space.
A lock in three states
glibc builds its internal lock (the one under pthread_mutex_t for the default mutex type) on a futex word with three values 3:
0: free1: locked, nobody waiting2: locked, maybe someone waiting
This is the code, trimmed from lowlevellock.h and lowlevellock.c 4:
void lock(int *futex) {
if (cas(futex, 0, 1)) // 0 -> 1: got it, no syscall
return;
while (xchg(futex, 2) != 0) // mark "maybe waiters"
futex_wait(futex, 2); // sleep only if it is still 2
}
void unlock(int *futex) {
if (xchg(futex, 0) > 1) // was it 2?
futex_wake(futex, 1); // then wake one
}
Uncontended, lock() is one cas and unlock() is one xchg. No kernel.
The value 2 is what keeps unlock() cheap. Without it, the unlocking thread can’t tell if anyone is asleep, so it would have to call futex_wake every time just in case. With it, the syscall only happens when someone has announced they might be waiting.
Notice that a thread that wakes up writes 2 again, not 1. It has no idea whether other threads are still asleep, so it assumes they are. The cost is an occasional FUTEX_WAKE that finds nobody. That’s much cheaper than a thread sleeping forever.
The race FUTEX_WAIT exists for
Why does FUTEX_WAIT take a value at all? Why not just “sleep on this address”?
Consider this order of events:
- T1 holds the lock. T2 calls
lock(), sets the word to2, and decides to sleep. - Before T2’s syscall reaches the kernel, T1 calls
unlock(). It sets the word to0, sees it was2, and callsFUTEX_WAKE. Nobody is on the queue yet, so the wake does nothing. - T2’s
FUTEX_WAITfinally lands in the kernel and goes to sleep.
Now T2 sleeps on a free lock and nobody will ever wake it. This is the classic lost wakeup.
The value argument closes it. Inside the kernel, futex_wait_setup() takes the lock of the hash bucket for this address, re-reads the word from user memory, and only queues the thread if the word still equals val 5. FUTEX_WAKE takes the same bucket lock. So in step 3 the kernel reads 0, sees it isn’t 2, and returns EAGAIN. T2 goes around the loop, does xchg(2), gets back 0, and owns the lock. The man page calls this an atomic “compare-and-block” 2.
Where the waiters live
The kernel keeps a hash table of buckets, each with a spinlock and a list of sleeping threads (recent kernels can also give a process its own private table 6). The key for a private futex is (mm, address): your process’s address space plus the virtual address. For a futex in shared memory, which can sit at different addresses in different processes, the key is built from the underlying file or page instead 6. Since Linux 2.6.22 you can pass FUTEX_PRIVATE_FLAG to tell the kernel the futex is not shared, which lets it skip that more expensive lookup 2. glibc does this for process-private mutexes.
An uncontended lock never touches this table. That’s the whole point.
Try it
Start with two threads and a short hold time: the locks, no syscall counter does nearly all the work. Then add threads or hold the lock longer and watch the word sit at 2 while the kernel queue fills up. Keep an eye on EAGAIN: those are the lost wakeups from the section above, prevented.
pthread_mutex_t · glibc's three-state lock word · slowed down a lotEach thread works on its own for a while, then takes the lock. With one or two threads and a short hold, almost every lock and unlock is a single atomic instruction.
What this leaves out
- glibc has no wrapper for
futex(). You call it throughsyscall(SYS_futex, ...)2, and you almost never should. - Priority inheritance (
FUTEX_LOCK_PI) changes the deal: the word holds the owner’s thread ID so the kernel can boost the owner’s priority 2. - The newer
futex_waitv()call waits on up to 128 futexes at once and wakes on any of them 7.
So the kernel only shows up when a thread actually has to sleep. Everything else is one atomic instruction on an integer you own.
Footnotes
-
Franke, Russell, Kirkwood, Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux, Ottawa Linux Symposium 2002. ↩
-
Ulrich Drepper, Futexes Are Tricky. Walks through why the simpler versions are broken. ↩
-
glibc sysdeps/nptl/lowlevellock.h and nptl/lowlevellock.c. ↩
-
Linux kernel/futex/waitwake.c,
futex_wait_setup()and the ordering comment above it. ↩ -
Linux kernel/futex/core.c,
get_futex_key(). ↩ ↩2 -
futex2 documentation, kernel.org. ↩