@@ -2294,6 +2294,12 @@ is resumed, and its locks reacquired. This means the critical section API
22942294provides weaker guarantees than traditional locks -- they are useful because
22952295their behavior is similar to the :term:`GIL`.
22962296
2297+ Variants that accept :c:type:`PyMutex` instances rather than objects are also
2298+ available. Use these variants to start a critical section in a situation where
2299+ there is no :c:type:`PyObject` -- for example, when working with a C type that
2300+ does not extend or wrap :c:type:`PyObject` but still needs to call into the C
2301+ API in a manner that might lead to deadlocks.
2302+
22972303The functions and structs used by the macros are exposed for cases
22982304where C macros are not available. They should only be used as in the
22992305given macro expansions. Note that the sizes and contents of the structures may
@@ -2339,6 +2345,23 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
23392345
23402346 .. versionadded:: 3.13
23412347
2348+ .. c:macro:: Py_BEGIN_CRITICAL_SECTION_MUTEX(m)
2349+
2350+ Locks the mutex *m* and begins a critical section.
2351+
2352+ In the free-threaded build, this macro expands to::
2353+
2354+ {
2355+ PyCriticalSection _py_cs;
2356+ PyCriticalSection_BeginMutex(&_py_cs, m)
2357+
2358+ Note that unlike :c:macro:`Py_BEGIN_CRITICAL_SECTION`, there is no cast for
2359+ the second argument - it must be a :c:type:`PyMutex`.
2360+
2361+ On the default build, this macro expands to ``{``.
2362+
2363+ .. versionadded:: 3.15
2364+
23422365.. c:macro:: Py_END_CRITICAL_SECTION()
23432366
23442367 Ends the critical section and releases the per-object lock.
@@ -2368,6 +2391,23 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
23682391
23692392 .. versionadded:: 3.13
23702393
2394+ .. c:macro:: Py_BEGIN_CRITICAL_SECTION2_MUTEX(m1, m2)
2395+
2396+ Locks the mutexes *m1* and *m2* and begins a critical section.
2397+
2398+ In the free-threaded build, this macro expands to::
2399+
2400+ {
2401+ PyCriticalSection2 _py_cs2;
2402+ PyCriticalSection2_BeginMutex(&_py_cs2, m1, m2)
2403+
2404+ Note that unlike :c:macro:`Py_BEGIN_CRITICAL_SECTION2`, there is no cast for
2405+ the second and third arguments - they must be :c:type:`PyMutex` instances.
2406+
2407+ On the default build, this macro expands to ``{``.
2408+
2409+ .. versionadded:: 3.15
2410+
23712411.. c:macro:: Py_END_CRITICAL_SECTION2()
23722412
23732413 Ends the critical section and releases the per-object locks.
0 commit comments