summaryrefslogtreecommitdiffstats
path: root/Python/import.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-04-25 03:09:35 (GMT)
committerGitHub <noreply@github.com>2023-04-25 03:09:35 (GMT)
commitd8627999d85cc5b000dbe17180250d919f8510ad (patch)
tree82620a3dd5103dfc93ea8684934a1e0e23f3b3ab /Python/import.c
parentb934f97850b7b2db30fa2b26720d58aac4783149 (diff)
downloadcpython-d8627999d85cc5b000dbe17180250d919f8510ad.zip
cpython-d8627999d85cc5b000dbe17180250d919f8510ad.tar.gz
cpython-d8627999d85cc5b000dbe17180250d919f8510ad.tar.bz2
gh-100227: Add a Granular Lock for _PyRuntime.imports.extensions.dict (gh-103460)
The lock is unnecessary as long as there's a GIL, but completely necessary with a per-interpreter GIL.
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/Python/import.c b/Python/import.c
index df57780..daec64e 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -413,8 +413,11 @@ remove_module(PyThreadState *tstate, PyObject *name)
Py_ssize_t
_PyImport_GetNextModuleIndex(void)
{
+ PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
LAST_MODULE_INDEX++;
- return LAST_MODULE_INDEX;
+ Py_ssize_t index = LAST_MODULE_INDEX;
+ PyThread_release_lock(EXTENSIONS.mutex);
+ return index;
}
static const char *
@@ -703,6 +706,7 @@ _PyImport_ClearModulesByIndex(PyInterpreterState *interp)
const char *
_PyImport_ResolveNameWithPackageContext(const char *name)
{
+ PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
if (PKGCONTEXT != NULL) {
const char *p = strrchr(PKGCONTEXT, '.');
if (p != NULL && strcmp(name, p+1) == 0) {
@@ -710,14 +714,17 @@ _PyImport_ResolveNameWithPackageContext(const char *name)
PKGCONTEXT = NULL;
}
}
+ PyThread_release_lock(EXTENSIONS.mutex);
return name;
}
const char *
_PyImport_SwapPackageContext(const char *newcontext)
{
+ PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
const char *oldcontext = PKGCONTEXT;
PKGCONTEXT = newcontext;
+ PyThread_release_lock(EXTENSIONS.mutex);
return oldcontext;
}
@@ -865,13 +872,13 @@ gets even messier.
static inline void
extensions_lock_acquire(void)
{
- // XXX For now the GIL is sufficient.
+ PyThread_acquire_lock(_PyRuntime.imports.extensions.mutex, WAIT_LOCK);
}
static inline void
extensions_lock_release(void)
{
- // XXX For now the GIL is sufficient.
+ PyThread_release_lock(_PyRuntime.imports.extensions.mutex);
}
/* Magic for extension modules (built-in as well as dynamically