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.
This commit is contained in:
Sam Gross 2023-12-07 14:33:40 -05:00 committed by GitHub
parent db460735af
commit cf6110ba13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 97 additions and 241 deletions

View file

@ -418,11 +418,7 @@ remove_module(PyThreadState *tstate, PyObject *name)
Py_ssize_t
_PyImport_GetNextModuleIndex(void)
{
PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
LAST_MODULE_INDEX++;
Py_ssize_t index = LAST_MODULE_INDEX;
PyThread_release_lock(EXTENSIONS.mutex);
return index;
return _Py_atomic_add_ssize(&LAST_MODULE_INDEX, 1) + 1;
}
static const char *
@ -882,13 +878,13 @@ gets even messier.
static inline void
extensions_lock_acquire(void)
{
PyThread_acquire_lock(_PyRuntime.imports.extensions.mutex, WAIT_LOCK);
PyMutex_Lock(&_PyRuntime.imports.extensions.mutex);
}
static inline void
extensions_lock_release(void)
{
PyThread_release_lock(_PyRuntime.imports.extensions.mutex);
PyMutex_Unlock(&_PyRuntime.imports.extensions.mutex);
}
/* Magic for extension modules (built-in as well as dynamically