Implement Python Critical Sections from PEP 703 #111569
Labels
3.13
new features, bugs and security fixes
topic-free-threaded
type-feature
A feature request or enhancement
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:
_PyThreadState_Detach()), unlock all of the thread's locked mutexesThe "public" 1 API consists of four macros:
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_SECTION2macro. Nesting two calls toPy_BEGIN_CRITICAL_SECTIONdoes not guarantee that both objects are locked because the inner calls may suspend the outer critical section.Linked PRs
Footnotes
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. ↩The text was updated successfully, but these errors were encountered: