gh-133296: Publicly expose critical section API that accepts PyMutex (gh-135899)

This makes the following APIs public:

* `Py_BEGIN_CRITICAL_SECTION_MUTEX(mutex),`
* `Py_BEGIN_CRITICAL_SECTION2_MUTEX(mutex1, mutex2)`
* `void PyCriticalSection_BeginMutex(PyCriticalSection *c, PyMutex *mutex)`
* `void PyCriticalSection2_BeginMutex(PyCriticalSection2 *c, PyMutex *mutex1, PyMutex *mutex2)`

The macros are identical to the corresponding `Py_BEGIN_CRITICAL_SECTION` and
`Py_BEGIN_CRITICAL_SECTION2` macros (e.g., they include braces), but they
accept a `PyMutex` instead of an object.

The new macros are still paired with the existing END macros
(`Py_END_CRITICAL_SECTION`, `Py_END_CRITICAL_SECTION2`).
This commit is contained in:
Nathan Goldbaum 2025-07-21 15:25:43 -06:00 committed by GitHub
parent f183996eb7
commit 89c220b93c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 96 additions and 15 deletions

View file

@ -2306,6 +2306,12 @@ is resumed, and its locks reacquired. This means the critical section API
provides weaker guarantees than traditional locks -- they are useful because
their behavior is similar to the :term:`GIL`.
Variants that accept :c:type:`PyMutex` pointers rather than Python objects are also
available. Use these variants to start a critical section in a situation where
there is no :c:type:`PyObject` -- for example, when working with a C type that
does not extend or wrap :c:type:`PyObject` but still needs to call into the C
API in a manner that might lead to deadlocks.
The functions and structs used by the macros are exposed for cases
where C macros are not available. They should only be used as in the
given macro expansions. Note that the sizes and contents of the structures may
@ -2351,6 +2357,23 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
.. versionadded:: 3.13
.. c:macro:: Py_BEGIN_CRITICAL_SECTION_MUTEX(m)
Locks the mutex *m* and begins a critical section.
In the free-threaded build, this macro expands to::
{
PyCriticalSection _py_cs;
PyCriticalSection_BeginMutex(&_py_cs, m)
Note that unlike :c:macro:`Py_BEGIN_CRITICAL_SECTION`, there is no cast for
the argument of the macro - it must be a :c:type:`PyMutex` pointer.
On the default build, this macro expands to ``{``.
.. versionadded:: next
.. c:macro:: Py_END_CRITICAL_SECTION()
Ends the critical section and releases the per-object lock.
@ -2380,6 +2403,23 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
.. versionadded:: 3.13
.. c:macro:: Py_BEGIN_CRITICAL_SECTION2_MUTEX(m1, m2)
Locks the mutexes *m1* and *m2* and begins a critical section.
In the free-threaded build, this macro expands to::
{
PyCriticalSection2 _py_cs2;
PyCriticalSection2_BeginMutex(&_py_cs2, m1, m2)
Note that unlike :c:macro:`Py_BEGIN_CRITICAL_SECTION2`, there is no cast for
the arguments of the macro - they must be :c:type:`PyMutex` pointers.
On the default build, this macro expands to ``{``.
.. versionadded:: next
.. c:macro:: Py_END_CRITICAL_SECTION2()
Ends the critical section and releases the per-object locks.