Skip to content

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:

pthread_mutex
=
atomic
+
futex

pthread_cond
=
condition state
+
mutex
+
futex

Lab 1 — Observe pthread mutex fast path

Implemented:

  • uncontended mutex loop
  • strace verification

Observation:

pthread_mutex_lock()


CAS success


userspace only

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:

lock

atomic CAS


futex wait


wake

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:

FUTEX_WAKE → 0

consumer later calls wait


EAGAIN

Key learning:

wake may be missed

but state must not

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:

ready
+
seq
+
mutex
+
futex

Key insight:

Condition state and notification state are independent concepts.

Condition variable does not store events.

It only coordinates waiting.


Important Concepts

Mutex

atomic
+
futex

Condition Variable

condition
+
wait queue
+
mutex coordination

Futex

sleep if value == expected
otherwise return EAGAIN

Reflection

The most difficult concept today was understanding:

condition
notification

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