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

@ -431,7 +431,7 @@ typedef struct {
visible to other threads before the `dict_final` bit is set.
*/
#define STGINFO_LOCK(stginfo) Py_BEGIN_CRITICAL_SECTION_MUT(&(stginfo)->mutex)
#define STGINFO_LOCK(stginfo) Py_BEGIN_CRITICAL_SECTION_MUTEX(&(stginfo)->mutex)
#define STGINFO_UNLOCK() Py_END_CRITICAL_SECTION()
static inline uint8_t

View file

@ -2418,6 +2418,16 @@ test_critical_sections(PyObject *module, PyObject *Py_UNUSED(args))
Py_BEGIN_CRITICAL_SECTION2(module, module);
Py_END_CRITICAL_SECTION2();
#ifdef Py_GIL_DISABLED
// avoid unused variable compiler warning on GIL-enabled build
PyMutex mut = {0};
Py_BEGIN_CRITICAL_SECTION_MUTEX(&mut);
Py_END_CRITICAL_SECTION();
Py_BEGIN_CRITICAL_SECTION2_MUTEX(&mut, &mut);
Py_END_CRITICAL_SECTION2();
#endif
Py_RETURN_NONE;
}