@@ -2463,12 +2463,20 @@ per-object locks for :term:`free-threaded <free threading>` CPython. They are
2463
2463
intended to replace reliance on the :term:`global interpreter lock`, and are
2464
2464
no-ops in versions of Python with the global interpreter lock.
2465
2465
2466
+ Critical sections are intended to be used for custom types implemented
2467
+ in C-API extensions. They should generally not be used with built-in types like
2468
+ :class:`list` and :class:`dict` because their public C-APIs
2469
+ already use critical sections internally, with the notable
2470
+ exception of :c:func:`PyDict_Next`, which requires critical section
2471
+ to be acquired externally.
2472
+
2466
2473
Critical sections avoid deadlocks by implicitly suspending active critical
2467
- sections and releasing the locks during calls to :c:func:`PyEval_SaveThread`.
2468
- When :c:func:`PyEval_RestoreThread` is called, the most recent critical section
2469
- is resumed, and its locks reacquired. This means the critical section API
2470
- provides weaker guarantees than traditional locks -- they are useful because
2471
- their behavior is similar to the :term:`GIL`.
2474
+ sections, hence, they do not provide exclusive access such as provided by
2475
+ traditional locks like :c:type:`PyMutex`. When a critical section is started,
2476
+ the per-object lock for the object is acquired. If the code executed inside the
2477
+ critical section calls C-API functions then it can suspend the critical section thereby
2478
+ releasing the per-object lock, so other threads can acquire the per-object lock
2479
+ for the same object.
2472
2480
2473
2481
Variants that accept :c:type:`PyMutex` pointers rather than Python objects are also
2474
2482
available. Use these variants to start a critical section in a situation where
0 commit comments