Skip to content

Make Py_BEGIN_CRITICAL_SECTION() and Py_END_CRITICAL_SECTION() public in the non-limited C API #119344

Closed
@colesbury

Description

@colesbury

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.

C-API WG Issue

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions