gh-111569: Implement Python critical section API (gh-111571)

Critical sections are helpers to replace the global interpreter lock
with finer grained locking.  They provide similar guarantees to the GIL
and avoid the deadlock risk that plain locking involves.  Critical
sections are implicitly ended whenever the GIL would be released.  They
are resumed when the GIL would be acquired.  Nested critical sections
behave as if the sections were interleaved.
This commit is contained in:
Sam Gross 2023-11-08 17:39:29 -05:00 committed by GitHub
parent 0b718e6407
commit 31c90d5838
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 630 additions and 7 deletions

View file

@ -119,7 +119,7 @@ check by comparing the reference count field to the immortality reference count.
{ \
0, \
0, \
0, \
{ 0 }, \
0, \
_Py_IMMORTAL_REFCNT_LOCAL, \
0, \
@ -204,10 +204,14 @@ struct _object {
// Create a shared field from a refcnt and desired flags
#define _Py_REF_SHARED(refcnt, flags) (((refcnt) << _Py_REF_SHARED_SHIFT) + (flags))
// NOTE: In non-free-threaded builds, `struct _PyMutex` is defined in
// pycore_lock.h. See pycore_lock.h for more details.
struct _PyMutex { uint8_t v; };
struct _object {
uintptr_t ob_tid; // thread id (or zero)
uint16_t _padding;
uint8_t ob_mutex; // per-object lock
struct _PyMutex ob_mutex; // per-object lock
uint8_t ob_gc_bits; // gc-related state
uint32_t ob_ref_local; // local reference count
Py_ssize_t ob_ref_shared; // shared (atomic) reference count