Closed
Description
Feature or enhancement
The critical section API is useful for making C API extensions thread-safe when the GIL is disabled. Compared to plain mutexes, the API makes it easier to avoid deadlocks, especially when interacting with the Python C API, where calls like Py_DECREF()
may invoke destructors that themselves acquire locks.
A more detailed description and motivation is in PEP 703: https://peps.python.org/pep-0703/#python-critical-sections
The underlying implementation is hooked into Python's PyThreadState
management, so it would not be practical to implement this API outside of CPython.