Skip to content

Contents: concurrent C and free-threaded CPython

This index covers the primer in reading order and links to every chapter section. Chapters 1–8 explain general concurrent C concepts; chapters 9–13 explain CPython's mechanisms; chapter 14 brings them together into a decision guide.

For targeted lookup, start with the topic index below. For edits, read the linked section in context and check related chapters before adding an explanation that may already exist. Section links are preferable to line numbers, which change as the guide is edited.

For definitions of concurrency terms, see the glossary. For external books, articles, talks, and references, see further reading.

Topic index

Topic or question Primary section Related material
Data race versus race condition Definitions and check-then-act example Compound container operations
Undefined behavior, compiler optimization, as-if rule Compiler transformations Undefined behavior
Memory locations, bit-fields, hardware atomicity C data races and memory locations Hardware versus C atomicity
Why volatile is not synchronization volatile Source order
Happens-before and synchronization proof Happens-before Explaining safe access
Release/acquire publication and reuse constraints Complete publication example Memory-order options
Atomic RMW, exchange, CAS, weak versus strong CAS Atomic operations CAS loops
_Py_atomic_*, FT_ATOMIC_*, C11 API mapping CPython atomic API Build versus runtime GIL
Fences and versioned optimistic snapshots Fence basics Advanced snapshot example and proof
Which fields a lock protects; complete transitions Mutex ownership of state Protecting invariants
Condition variables, wakeups, events, spin locks Condition variables Events, spin locks
Deadlocks and ordering multiple mutexes General lock ordering PyMutex lock ordering
Pointer lifetime and reference acquisition under a lock Lifetime after unlock Borrowed versus strong references
Cache coherence, false sharing, atomic instruction cost Cache-line granularity Atomic cost
Sharding, per-thread state, immutable snapshots Sharding Per-thread state, snapshots
Lock-free, wait-free, obstruction-free, starvation Progress guarantees Fairness
Linearizability, retries, multi-field commit, ABA Linearization points Multi-field commit, ABA
Hazard pointers, epochs, RCU, QSBR Reclamation reader scope Hazard pointers, epochs, RCU, QSBR
Thread attachment, detachment, suspension, foreign threads Thread-state contracts PyMutex waiting
Extension support declaration and shared module state Runtime GIL and module declarations Extension state
PyMutex initialization, address stability, diagnostics PyMutex chapter Stable address, lock-state query
Critical-section suspension and revalidation Detachment and suspension Restore invariants, reacquisition
Two-object critical sections, recursion, ob_mutex Two-object form Recursive entry, ob_mutex
Mutex-backed critical sections, limited API, Clinic helpers Mutex-backed forms Build and API availability, generated code
Stop-the-world scope and hazards Interpreter versus runtime scope Lock hazards, callbacks and cleanup
CPython QSBR sequences, deferred frees and decrefs Sequence tracking Delayed-work helpers, batching
QSBR pointer scope, retained memory, allocator reuse, try-incref Quiescent-state obligations Retention, allocator and reference acquisition
Selecting or combining mechanisms; final review checklist Decision order Combining mechanisms, checklist

Detailed chapter contents

01. The basic problem: shared mutable memory

Introduces conflicting accesses, logical races, related fields, and the separate pointer-lifetime problem.

02. The C abstract machine and undefined behavior

Explains why correctness depends on the C rules, not observed machine instructions or tests on one CPU.

03. The C memory model

Defines ordering relationships and gives the complete release/acquire publication example and proof.

04. Atomic operations

Explains atomic operations and memory orders using C11 examples, then maps them to CPython's interfaces.

05. Synchronization

Teaches mutex-based protection of complete operations, lock ownership and ordering, and waiting primitives.

06. Concurrency and performance

Covers coherence, contention, false sharing, instruction costs, and structural ways to reduce sharing.

07. Lock-free algorithms

Develops progress guarantees, CAS and linearization, optimistic validation, ABA, and reclamation obligations. The advanced versioned-snapshot example is explicitly not lock-free.

08. Safe memory reclamation

Surveys reader protection, grace periods, hazard pointers, epochs, RCU, QSBR, and destruction constraints.

09. CPython's concurrency model

Introduces the two builds, thread-state contracts, public API implications, and replacements for GIL-based assumptions.

10. PyMutex

Explains the concrete mutex API, initialization and address requirements, waiting behavior, diagnostics, and callback hazards.

11. CPython critical sections

Explains suspend-and-resume locking, lexical pairing, nesting and recursion, build contracts, and object/mutex-backed APIs.

12. Stop-the-world

Explains pause scopes, thread-state transitions, rare global updates, and the hazards of locks and arbitrary code during a pause.

13. QSBR in CPython

Explains CPython's reader obligations, sequences, delayed-work helpers, lifecycle and retention costs, and specialized allocator reuse.

14. Choosing a technique

Provides the decision order, important qualifications, combined-protocol example, and final review checklist.

Markdown source