@@ -2458,6 +2458,12 @@ is resumed, and its locks reacquired. This means the critical section API
24582458provides weaker guarantees than traditional locks -- they are useful because
24592459their behavior is similar to the :term:`GIL`.
24602460
2461+ Variants that accept :c:type:`PyMutex` pointers rather than Python objects are also
2462+ available. Use these variants to start a critical section in a situation where
2463+ there is no :c:type:`PyObject` -- for example, when working with a C type that
2464+ does not extend or wrap :c:type:`PyObject` but still needs to call into the C
2465+ API in a manner that might lead to deadlocks.
2466+
24612467The functions and structs used by the macros are exposed for cases
24622468where C macros are not available. They should only be used as in the
24632469given macro expansions. Note that the sizes and contents of the structures may
@@ -2503,6 +2509,23 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
25032509
25042510 .. versionadded:: 3.13
25052511
2512+ .. c:macro:: Py_BEGIN_CRITICAL_SECTION_MUTEX(m)
2513+
2514+ Locks the mutex *m* and begins a critical section.
2515+
2516+ In the free-threaded build, this macro expands to::
2517+
2518+ {
2519+ PyCriticalSection _py_cs;
2520+ PyCriticalSection_BeginMutex(&_py_cs, m)
2521+
2522+ Note that unlike :c:macro:`Py_BEGIN_CRITICAL_SECTION`, there is no cast for
2523+ the argument of the macro - it must be a :c:type:`PyMutex` pointer.
2524+
2525+ On the default build, this macro expands to ``{``.
2526+
2527+ .. versionadded:: 3.14
2528+
25062529.. c:macro:: Py_END_CRITICAL_SECTION()
25072530
25082531 Ends the critical section and releases the per-object lock.
@@ -2532,6 +2555,23 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
25322555
25332556 .. versionadded:: 3.13
25342557
2558+ .. c:macro:: Py_BEGIN_CRITICAL_SECTION2_MUTEX(m1, m2)
2559+
2560+ Locks the mutexes *m1* and *m2* and begins a critical section.
2561+
2562+ In the free-threaded build, this macro expands to::
2563+
2564+ {
2565+ PyCriticalSection2 _py_cs2;
2566+ PyCriticalSection2_BeginMutex(&_py_cs2, m1, m2)
2567+
2568+ Note that unlike :c:macro:`Py_BEGIN_CRITICAL_SECTION2`, there is no cast for
2569+ the arguments of the macro - they must be :c:type:`PyMutex` pointers.
2570+
2571+ On the default build, this macro expands to ``{``.
2572+
2573+ .. versionadded:: 3.14
2574+
25352575.. c:macro:: Py_END_CRITICAL_SECTION2()
25362576
25372577 Ends the critical section and releases the per-object locks.
0 commit comments