@@ -84,11 +84,6 @@ there is no :c:type:`PyObject` -- for example, when working with a C type that
8484does not extend or wrap :c:type: `PyObject ` but still needs to call into the C
8585API in a manner that might lead to deadlocks.
8686
87- The functions and structs used by the macros are exposed for cases
88- where C macros are not available. They should only be used as in the
89- given macro expansions. Note that the sizes and contents of the structures may
90- change in future Python versions.
91-
9287.. note ::
9388
9489 Operations that need to lock two objects at once must use
@@ -114,12 +109,15 @@ section API avoids potential deadlocks due to reentrancy and lock ordering
114109by allowing the runtime to temporarily suspend the critical section if the
115110code triggered by the finalizer blocks and calls :c:func: `PyEval_SaveThread `.
116111
112+ .. _critical-section-macros :
113+
117114.. c :macro :: Py_BEGIN_CRITICAL_SECTION(op)
118115
119116 Acquires the per-object lock for the object *op * and begins a
120117 critical section.
121118
122- In the free-threaded build, this macro expands to::
119+ In the free-threaded build, and when building for the
120+ :ref: `Stable ABI <stable-abi >`, this macro expands to::
123121
124122 {
125123 PyCriticalSection _py_cs;
@@ -150,7 +148,8 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
150148
151149 Ends the critical section and releases the per-object lock.
152150
153- In the free-threaded build, this macro expands to::
151+ In the free-threaded build, and when building for the
152+ :ref: `Stable ABI <stable-abi >`, this macro expands to::
154153
155154 PyCriticalSection_End(&_py_cs);
156155 }
@@ -179,7 +178,8 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
179178
180179 Locks the mutexes *m1 * and *m2 * and begins a critical section.
181180
182- In the free-threaded build, this macro expands to::
181+ In the free-threaded build, and when building for the
182+ :ref: `Stable ABI <stable-abi >`, this macro expands to::
183183
184184 {
185185 PyCriticalSection2 _py_cs2;
@@ -196,7 +196,8 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
196196
197197 Ends the critical section and releases the per-object locks.
198198
199- In the free-threaded build, this macro expands to::
199+ In the free-threaded build, and when building for the
200+ :ref: `Stable ABI <stable-abi >`, this macro expands to::
200201
201202 PyCriticalSection2_End(&_py_cs2);
202203 }
@@ -205,6 +206,48 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
205206
206207 .. versionadded :: 3.13
207208
209+ Low-level critical section API
210+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
211+
212+ The following functions and structs are exposed for cases where C macros
213+ are not available.
214+
215+ .. c :function :: void PyCriticalSection_Begin (PyCriticalSection *c, PyObject *op)
216+ void PyCriticalSection_End(PyCriticalSection *c)
217+ void PyCriticalSection2_Begin(PyCriticalSection2 *c, PyObject *a, PyObject *b)
218+ void PyCriticalSection2_End(PyCriticalSection2 *c);
219+
220+ To be used only as in the macro expansions
221+ listed :ref: `earlier in this section <critical-section-macros >`.
222+
223+ In non-:term: `free-threaded <free threading> ` builds of CPython, these
224+ functions do nothing.
225+
226+ .. versionadded :: 3.13
227+
228+ .. c :type :: PyCriticalSection
229+ PyCriticalSection2
230+
231+ To be used only as in the macro expansions
232+ listed :ref: `earlier in this section <critical-section-macros >`.
233+ Note that the contents of the structures are private and their meaning may
234+ change in future Python versions.
235+
236+ .. versionadded :: 3.13
237+
238+ .. c :function :: void PyCriticalSection_BeginMutex (PyCriticalSection *c, PyMutex *m);
239+ void PyCriticalSection2_BeginMutex(PyCriticalSection2 *c, PyMutex *m1, PyMutex *m2);
240+
241+ .. (These need to be in a separate section without a Stable ABI anotation.)
242+
243+ To be used only as in the macro expansions
244+ listed :ref: `earlier in this section <critical-section-macros >`.
245+
246+ In non-:term: `free-threaded <free threading> ` builds of CPython, these
247+ functions do nothing.
248+
249+ .. versionadded :: 3.14
250+
208251
209252Legacy locking APIs
210253-------------------
0 commit comments