Skip to content

Implement Python Critical Sections from PEP 703 #111569

Description

@colesbury

Feature or enhancement

PEP 703 introduces the concept of "Python Critical Sections", which are an abstraction to help replace the GIL with finer grained locking. The key ideas is to replace the GIL with per-object locks and implicitly release these locks in the same places where the GIL would have been released. The mechanism is:

  1. Keep track of locked mutexes in a per-thread stack
  2. When the thread "detaches" from the interpreter (i.e., _PyThreadState_Detach()), unlock all of the thread's locked mutexes
  3. When the thread re-"attaches" to the interpreter, re-lock the top-most mutex or mutexes.

The "public" 1 API consists of four macros:

Py_BEGIN_CRITICAL_SECTION(object);
Py_END_CRITICAL_SECTION;
Py_BEGIN_CRITICAL_SECTION2(object1, object2);
Py_END_CRITICAL_SECTION2;

These will be no-ops in the default build of CPython.

Note that if you need to operate on two objects at once, then you must use the Py_BEGIN_CRITICAL_SECTION2 macro. Nesting two calls to Py_BEGIN_CRITICAL_SECTION does not guarantee that both objects are locked because the inner calls may suspend the outer critical section.

Linked PRs

Footnotes

  1. At least at the start, even these "public" macros will still be internal-only (i.e., in Include/internal). I expect that we will eventually want to make them public so that C-API extensions can make use of them. ↩

Activity

  1. self-assigned this
    on Oct 31, 2023
  2. added a commit that references this issue on Oct 31, 2023
  3. added a commit that references this issue on Nov 8, 2023
  4. brettcannon commented on Nov 9, 2023

    @brettcannon
    Member

    The latest PR related to this change broke the WebAssembly buildbots by causing them to time out: https://buildbot.python.org/all/#/builders/1046/builds/3456/steps/11/logs/stdio .

  5. added a commit that references this issue on Nov 9, 2023
  6. added a commit that references this issue on Nov 9, 2023
  7. added 2 commits that reference this issue on Feb 11, 2024
  8. added 2 commits that reference this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions