Skip to content

Commit 0a72696

Browse files
[3.13] enhance docs for critical sections (GH-137334) (#138168)
enhance docs for critical sections (GH-137334) (cherry picked from commit 5ae8b97) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
1 parent 31eb3ef commit 0a72696

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Doc/c-api/init.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,12 +2396,20 @@ per-object locks for :term:`free-threaded <free threading>` CPython. They are
23962396
intended to replace reliance on the :term:`global interpreter lock`, and are
23972397
no-ops in versions of Python with the global interpreter lock.
23982398
2399+
Critical sections are intended to be used for custom types implemented
2400+
in C-API extensions. They should generally not be used with built-in types like
2401+
:class:`list` and :class:`dict` because their public C-APIs
2402+
already use critical sections internally, with the notable
2403+
exception of :c:func:`PyDict_Next`, which requires critical section
2404+
to be acquired externally.
2405+
23992406
Critical sections avoid deadlocks by implicitly suspending active critical
2400-
sections and releasing the locks during calls to :c:func:`PyEval_SaveThread`.
2401-
When :c:func:`PyEval_RestoreThread` is called, the most recent critical section
2402-
is resumed, and its locks reacquired. This means the critical section API
2403-
provides weaker guarantees than traditional locks -- they are useful because
2404-
their behavior is similar to the :term:`GIL`.
2407+
sections, hence, they do not provide exclusive access such as provided by
2408+
traditional locks like :c:type:`PyMutex`. When a critical section is started,
2409+
the per-object lock for the object is acquired. If the code executed inside the
2410+
critical section calls C-API functions then it can suspend the critical section thereby
2411+
releasing the per-object lock, so other threads can acquire the per-object lock
2412+
for the same object.
24052413
24062414
The functions and structs used by the macros are exposed for cases
24072415
where C macros are not available. They should only be used as in the

0 commit comments

Comments
 (0)