Skip to content

Plan Futex foundation

ebenali edited this page Dec 4, 2026 · 5 revisions

Plan: Futex foundation (C1)

Scope of roadmap milestone C1: process-shared and robust synchronization objects in libpthread, and the clock-selectable waits of POSIX 2024. Written before implementation; steps are updated as they land. See the Roadmap for where C1 sits and Threads, processes and tasks for LWPs.

What exists

In the kernel, everything C1 needs. __futex(2) (sys_futex.c) implements the Linux operations that matter here: wait and wake, with and without a bitset, requeue, wake-op; private futexes keyed by address space and address, shared ones by the backing memory object (uvm_voaddr), so two processes mapping the same page meet on the same futex. A timed wait takes an absolute or relative deadline on CLOCK_MONOTONIC or, with FUTEX_CLOCK_REALTIME, on CLOCK_REALTIME. __futex_set_robust_list(2) registers a thread's list of held robust locks; when the thread exits or execs (lwp_thread_cleanup, also reached from exit1), the kernel walks the list, sets FUTEX_OWNER_DIED in each futex word the thread still owns, and wakes one waiter. LWP IDs come from the PID space, so an owner's ID in a futex word is unique system-wide, as the robust protocol requires. The priority-inheritance operations (FUTEX_LOCK_PI and the rest) are not implemented.

In libpthread, none of it is used. A mutex is a pointer-sized owner word (the owning pthread_t, with type bits in the low bits) and a list of waiting threads linked through their own stacks; waiters park with _lwp_park(2). Condition variables, reader-writer locks and barriers work the same way. Every one of these holds pointers into one process's memory, so none can be shared between processes: pthread_*attr_setpshared(PTHREAD_PROCESS_SHARED) fails with ENOSYS, and the prototypes are hidden behind _PTHREAD_PSHARED. Robust mutexes (pthread_mutexattr_setrobust, pthread_mutex_consistent) do not exist, nor do the 2024 waits that take a clock argument. The conformance runs list these as the largest group of threads failures.

Design

Binary compatibility first. The sizes and layouts of pthread_mutex_t, pthread_cond_t, pthread_rwlock_t and pthread_barrier_t do not change, nor do their static initializers, nor the fast paths of the existing process-private objects. A shared or robust object is a second representation inside the same structure, chosen when it is initialized with such an attribute; its owner word holds a value the private fast paths never match, so they fall through to the slow path, which dispatches on it. Existing binaries never created such objects (the attribute failed), so none can be in memory in the old form.

Shared and robust mutexes use the Linux futex word protocol, the one the kernel's robust-list code implements: a 32-bit word holding the owner's LWP ID, FUTEX_WAITERS when a thread sleeps on it, and FUTEX_OWNER_DIED once the kernel has released it for a dead owner. The word lives in the mutex's spare field; the list link for the robust list lives in the waiters field, which this representation does not otherwise use. Locking is a compare-and-swap from 0 to the caller's ID; contention sets FUTEX_WAITERS and waits on the word. Robust waits always use shared futex operations, because the kernel's release wakes through the shared key.

Robust semantics as POSIX states them. Acquiring a word marked FUTEX_OWNER_DIED succeeds and returns EOWNERDEAD; the mutex is then inconsistent until pthread_mutex_consistent(); unlocking it while inconsistent makes it permanently unusable, and every later lock returns ENOTRECOVERABLE. Each thread registers its robust-list head on its first robust lock, not at creation, so threads that never use one pay nothing. The list head and the pending-operation slot live in the thread's own structure.

Mutex types and protocols. Normal, error-checking and recursive types work in the shared representation (owner compared by LWP ID); PTHREAD_PRIO_PROTECT works through _sched_protect as it does now. PTHREAD_PRIO_INHERIT needs the kernel's PI operations and stays ENOTSUP; it is a later step (see below).

Condition variables in the shared representation are a 32-bit sequence word: a waiter reads it, releases the mutex, waits on the word while it is unchanged, and relocks; signal and broadcast advance it and wake one or all. The condition attribute's clock applies as now. Reader-writer locks become one futex word holding the reader count, a writer bit and waiter bits, with writers preferred as in the private implementation. Barriers are a count and a generation word.

Clock-selectable waits. pthread_mutex_clocklock, pthread_cond_clockwait, pthread_rwlock_clockrdlock, pthread_rwlock_clockwrlock for both representations (private objects pass the clock to _lwp_park, shared ones to the futex), and sem_clockwait, which needs the clock passed through the semaphore system call. Accepted clocks: CLOCK_REALTIME and CLOCK_MONOTONIC; others fail with EINVAL.

Headers. The pshared and robust prototypes become visible under the POSIX feature macros; <unistd.h> defines _POSIX_THREAD_PROCESS_SHARED and _POSIX_THREAD_ROBUST_PRIO_PROTECT, and sysconf() answers for them, once the tests below pass.

Tests

ATF tests in tests/lib/libpthread, each also run against shared memory between two processes (mmap(MAP_SHARED|MAP_ANON) before fork, and a shm_open object between unrelated processes): lock and unlock across processes; every mutex type; timed and clock waits on both clocks; robust recovery after the owner exits, is killed, and execs, with the owner holding several robust mutexes; the pending slot (owner killed between acquiring and linking); ENOTRECOVERABLE; condition variables, reader-writer locks and barriers across processes; a stress test of many processes and threads on one shared object. The existing private-object tests run unchanged, and a benchmark of the uncontended and contended private paths shows no regression. The conformance suites' threads failures for these interfaces are the acceptance list.

Order

  1. Mutexes: shared representation, robust list, consistent, clock lock; tests. Done; it needed a kernel fix first: a shared futex key on anonymous shared memory now names the amap and slot, not the page's anon, whose extra reference made writes copy the page.
  2. Condition variables; pthread_cond_clockwait for both representations; tests. Done.
  3. Reader-writer locks and barriers; tests. Done, with the man pages for steps 1 to 3; each later step documents itself.
  4. sem_clockwait (kernel and librt). Done: a new system call, _ksem_clockwait, takes the clock.
  5. Headers, sysconf, option macros. Done. The conformance rerun joins the next bulk conformance run.
  6. Later, separately: priority inheritance (the kernel's FUTEX_*_PI operations, then PTHREAD_PRIO_INHERIT in both representations).

Clone this wiki locally