@@ -243,6 +243,112 @@ depend on your extension, but some common patterns include:
243243 `thread-local storage <https://en.cppreference.com/w/c/language/storage_duration >`_.
244244
245245
246+ Critical Sections
247+ =================
248+
249+ .. _critical-sections :
250+
251+ In the free-threaded build, CPython provides a mechanism called "critical
252+ sections" to protect data that would otherwise be protected by the GIL.
253+ While extension authors may not interact with the internal critical section
254+ implementation directly, understanding their behavior is crucial when using
255+ certain C API functions or managing shared state in the free-threaded build.
256+
257+ What Are Critical Sections?
258+ ...........................
259+
260+ Conceptually, critical sections act as a deadlock avoidance layer built on
261+ top of simple mutexes. Each thread maintains a stack of active critical
262+ sections. When a thread needs to acquire a lock associated with a critical
263+ section (e.g., implicitly when calling a thread-safe C API function like
264+ :c:func: `PyDict_SetItem `, or explicitly using macros), it attempts to acquire
265+ the underlying mutex.
266+
267+ Using Critical Sections
268+ .......................
269+
270+ The primary APIs for using critical sections are:
271+
272+ * :c:macro: `Py_BEGIN_CRITICAL_SECTION ` and :c:macro: `Py_END_CRITICAL_SECTION ` -
273+ For locking a single object
274+
275+ * :c:macro: `Py_BEGIN_CRITICAL_SECTION2 ` and :c:macro: `Py_END_CRITICAL_SECTION2 `
276+ - For locking two objects simultaneously
277+
278+ These macros are no-ops in non-free-threaded builds, so they can be safely
279+ added to code that needs to support both build types.
280+
281+ How Critical Sections Work
282+ ..........................
283+
284+ Unlike traditional locks, critical sections do not guarantee exclusive access
285+ throughout their entire duration. If a thread would block while holding a
286+ critical section (e.g., by acquiring another lock or performing I/O), the
287+ critical section is temporarily suspended—all locks are released—and then
288+ resumed when the blocking operation completes.
289+
290+ This behavior is similar to what happens with the GIL when a thread makes a
291+ blocking call. The key differences are:
292+
293+ * Critical sections operate on a per-object basis rather than globally
294+
295+ * Critical sections follow a stack discipline within each thread
296+
297+ * Critical sections automatically release and reacquire locks around potential
298+ blocking operations
299+
300+ Deadlock Avoidance
301+ ..................
302+
303+ Critical sections help avoid deadlocks in two ways:
304+
305+ 1. If a thread tries to acquire a lock that's already held by another thread,
306+ it first suspends all of its active critical sections, temporarily releasing
307+ their locks
308+
309+ 2. When the blocking operation completes, only the top-most critical section is
310+ reacquired first
311+
312+ This means you cannot rely on nested critical sections to lock multiple objects
313+ at once, as the inner critical section may suspend the outer ones. Instead, use
314+ :c:macro: `Py_BEGIN_CRITICAL_SECTION2 ` to lock two objects simultaneously.
315+
316+ Important Considerations
317+ ........................
318+
319+ * Critical sections may temporarily release their locks, allowing other threads
320+ to modify the protected data. Be careful about making assumptions about the
321+ state of the data after operations that might block.
322+
323+ * Because locks can be temporarily released (suspended), entering a critical
324+ section does not guarantee exclusive access to the protected resource
325+ throughout the section's duration. If code within a critical section calls
326+ another function that blocks (e.g., acquires another lock, performs blocking
327+ I/O), all locks held by the thread via critical sections will be released.
328+ This is similar to how the GIL can be released during blocking calls.
329+
330+ * Only the lock(s) associated with the most recently entered (top-most)
331+ critical section are guaranteed to be held at any given time. Locks for
332+ outer, nested critical sections might have been suspended.
333+
334+ * You can lock at most two objects simultaneously with these APIs. If you need
335+ to lock more objects, you'll need to restructure your code.
336+
337+ * While critical sections will not deadlock if you attempt to lock the same
338+ object twice, they are less efficient than purpose-built reentrant locks for
339+ this use case.
340+
341+ * When using :c:macro: `Py_BEGIN_CRITICAL_SECTION2 `, the order of the objects
342+ doesn't affect correctness (the implementation handles deadlock avoidance),
343+ but it's good practice to always lock objects in a consistent order.
344+
345+ * Remember that the critical section macros are primarily for protecting access
346+ to *Python objects * that might be involved in internal CPython operations
347+ susceptible to the deadlock scenarios described above. For protecting purely
348+ internal extension state, standard mutexes or other synchronization
349+ primitives might be more appropriate.
350+
351+
246352Building Extensions for the Free-Threaded Build
247353===============================================
248354
0 commit comments