aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/import.c
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2023-12-07 14:33:40 -0500
committerGitHub <noreply@github.com>2023-12-07 12:33:40 -0700
commitcf6110ba1337cb67e5867d86e7c0e8d923a5bc8d (patch)
treeab8393161d5ce01479bfb03dce874ab52246d28f /Python/import.c
parentdb460735af7503984d1b7d878069722db44b11e8 (diff)
downloadcpython-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/import.c')
-rw-r--r--Python/import.c10
1 files changed, 3 insertions, 7 deletions
diff --git a/Python/import.c b/Python/import.c
index ef81f46a4d6..2dd95d8364a 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -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