Skip to content

Day61 - Futex, Mutex Internals and Condition Variable Internals

Objective

Understand how Linux userspace synchronization primitives are built.

This lab focuses on:

  • pthread mutex fast path
  • futex wait and wake behavior
  • expected value semantics
  • condition variable internals

Environment

Platform:

Raspberry Pi 5
Debian GNU/Linux
Kernel: 6.x

Tools:

gcc
pthread
strace

Build:

gcc <file>.c -pthread -Wall -Wextra -O2

Lab 1 — Observe pthread mutex fast path

Source:

mutex_fast_path.c

Run:

strace -e futex ./mutex_fast

Expected:

(no futex syscall)

Observation:

  • uncontended lock path remains in userspace
  • no kernel scheduling involved

Architecture:

pthread_mutex


atomic


success


return

Lab 2 — Observe mutex contention

Source:

mutex_contention.c

Run:

strace -f -e futex ./mutex_contention

Expected:

FUTEX_WAIT_PRIVATE
FUTEX_WAKE_PRIVATE

Observed:

lock fail


futex wait


wake


retry

Conclusion:

mutex only enters kernel under contention.


Lab 3 — Implement mini futex mutex

Source:

mini_futex_mutex.c

Implemented:

futex_wait()
futex_wake_one()
mini_mutex_lock()
mini_mutex_unlock()

Pseudo flow:

lock()

CAS

success → return

fail → futex wait

Unlock:

store unlock


wake

Conclusion:

futex is not a lock.

It only coordinates sleeping.


Lab 4 — Validate futex expected behavior

Source:

broken_cond_wait.c

Scenario:

producer wake


consumer wait later

Observed:

FUTEX_WAKE = 0


FUTEX_WAIT


EAGAIN

Meaning:

Kernel checks:

*addr == expected

before sleeping.

Conclusion:

wake can be missed.

state must not be missed.


Lab 5 — Build minimal condition variable

Source:

test_my_cond.c

Implemented:

struct my_cond {
    atomic_int seq;
};

Wait:

snapshot seq

unlock

futex wait

relock

Signal:

seq++

wake

Usage:

while (!ready)
    my_cond_wait()

Producer:

ready = true

my_cond_signal()

Conclusion:

condition state and notification state are separate.


Comparison

Component Responsibility
ready Condition
mutex Protect state
seq Notification
futex Sleep and wake

Key Takeaways

  • mutex fast path stays in userspace
  • futex uses expected value validation
  • condition variable stores no data
  • wait queue and condition are separate concepts