diff options
author | Sam Gross <colesbury@gmail.com> | 2023-12-07 14:33:40 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-07 12:33:40 -0700 |
commit | cf6110ba1337cb67e5867d86e7c0e8d923a5bc8d (patch) | |
tree | ab8393161d5ce01479bfb03dce874ab52246d28f /Python/crossinterp.c | |
parent | db460735af7503984d1b7d878069722db44b11e8 (diff) | |
download | cpython-cf6110ba1337cb67e5867d86e7c0e8d923a5bc8d.tar.gz cpython-cf6110ba1337cb67e5867d86e7c0e8d923a5bc8d.zip |
gh-111924: Use PyMutex for Runtime-global Locks. (gh-112207)
This replaces some usages of PyThread_type_lock with PyMutex, which does not require memory allocation to initialize.
This simplifies some of the runtime initialization and is also one step towards avoiding changing the default raw memory allocator during initialize/finalization, which can be non-thread-safe in some circumstances.
Diffstat (limited to 'Python/crossinterp.c')
-rw-r--r-- | Python/crossinterp.c | 29 |
1 files changed, 5 insertions, 24 deletions
diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 21b96ef05ed..f74fee38648 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -456,16 +456,17 @@ _xidregistry_clear(struct _xidregistry *xidregistry) static void _xidregistry_lock(struct _xidregistry *registry) { - if (registry->mutex != NULL) { - PyThread_acquire_lock(registry->mutex, WAIT_LOCK); + if (registry->global) { + PyMutex_Lock(®istry->mutex); } + // else: Within an interpreter we rely on the GIL instead of a separate lock. } static void _xidregistry_unlock(struct _xidregistry *registry) { - if (registry->mutex != NULL) { - PyThread_release_lock(registry->mutex); + if (registry->global) { + PyMutex_Unlock(®istry->mutex); } } @@ -874,19 +875,10 @@ _xidregistry_init(struct _xidregistry *registry) registry->initialized = 1; if (registry->global) { - // We manage the mutex lifecycle in pystate.c. - assert(registry->mutex != NULL); - // Registering the builtins is cheap so we don't bother doing it lazily. assert(registry->head == NULL); _register_builtins_for_crossinterpreter_data(registry); } - else { - // Within an interpreter we rely on the GIL instead of a separate lock. - assert(registry->mutex == NULL); - - // There's nothing else to initialize. - } } static void @@ -898,17 +890,6 @@ _xidregistry_fini(struct _xidregistry *registry) registry->initialized = 0; _xidregistry_clear(registry); - - if (registry->global) { - // We manage the mutex lifecycle in pystate.c. - assert(registry->mutex != NULL); - } - else { - // There's nothing else to finalize. - - // Within an interpreter we rely on the GIL instead of a separate lock. - assert(registry->mutex == NULL); - } } |