Day61 - Futex, Mutex Internals and Condition Variable Internals¶
Date¶
2026-05-27
Goal¶
Understand how Linux userspace synchronization primitives are implemented.
Focus areas:
- Futex fundamentals
- pthread mutex internals
- Condition variable internals
- Userspace fast path vs kernel slow path
- Relationship between condition state and notification
Learning Summary¶
Today focused on understanding the synchronization stack beneath pthread.
The goal was not API usage but understanding how synchronization behaves internally.
Conceptually:
Lab 1 — Observe pthread mutex fast path¶
Implemented:
- uncontended mutex loop
- strace verification
Observation:
Result:
- no futex syscall observed
- lock/unlock path remained in userspace
Conclusion:
Linux mutex avoids syscall overhead in uncontended cases.
Lab 2 — Implement mini futex mutex¶
Implemented:
- atomic compare exchange
- futex wait
- futex wake
Architecture:
Observation:
Mutex itself is not implemented by futex.
Futex only provides sleeping and wakeup capability.
Conclusion:
Futex is a synchronization primitive, not a lock.
Lab 3 — Understand futex expected semantics¶
Experiment:
Producer wakes before consumer enters wait.
Observed:
Key learning:
Conclusion:
FUTEX_WAIT(expected) validates state before sleeping.
Lab 4 — Build a minimal condition variable¶
Implemented:
- ready as condition state
- seq as notification sequence
- mutex for coordination
- futex for waiting
Structure:
Key insight:
Condition state and notification state are independent concepts.
Condition variable does not store events.
It only coordinates waiting.
Important Concepts¶
Mutex¶
Condition Variable¶
Futex¶
Reflection¶
The most difficult concept today was understanding:
Using ready and seq separately helped reveal how pthread condition variables operate internally.
Another important realization:
Most pthread synchronization objects are userspace state.
Kernel participation only happens during contention.
Files Produced¶
Implemented:
- mutex_fast_path.c
- mutex_contention.c
- mini_futex_mutex.c
- broken_cond_wait.c
- test_my_cond.c
Helper:
- futex_wait()
- futex_wake_one()
Next¶
Potential directions:
- pthread_rwlock internals
- glibc mutex internals
- eventfd and synchronization
- wait queue and scheduler internals