FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-100227: Add a Granular Lock for _PyRuntime.imports.extensions.dict by ericsnowcurrently · Pull Request #103460 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions Include/internal/pycore_import.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct _import_runtime_state {
used exclusively for when the extensions dict is access/modified
from an arbitrary thread. */
PyThreadState main_tstate;
/* A lock to guard the dict. */
PyThread_type_lock mutex;
/* A dict mapping (filename, name) to PyModuleDef for modules.
Only legacy (single-phase init) extension modules are added
and only if they support multiple initialization (m_size >- 0)
Expand Down
13 changes: 10 additions & 3 deletions Python/import.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -703,21 +706,25 @@ _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) {
name = PKGCONTEXT;
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;
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion Python/pystate.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
static const _PyRuntimeState initial = _PyRuntimeState_INIT(_PyRuntime);
_Py_COMP_DIAG_POP

#define NUMLOCKS 4
#define NUMLOCKS 5

static int
alloc_for_runtime(PyThread_type_lock locks[NUMLOCKS])
Expand Down Expand Up @@ -434,6 +434,7 @@ init_runtime(_PyRuntimeState *runtime,
&runtime->xidregistry.mutex,
&runtime->getargs.mutex,
&runtime->unicode_state.ids.lock,
&runtime->imports.extensions.mutex,
};
for (int i = 0; i < NUMLOCKS; i++) {
assert(locks[i] != NULL);
Expand Down Expand Up @@ -518,6 +519,7 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
&runtime->xidregistry.mutex,
&runtime->getargs.mutex,
&runtime->unicode_state.ids.lock,
&runtime->imports.extensions.mutex,
};
for (int i = 0; i < NUMLOCKS; i++) {
FREE_LOCK(*lockptrs[i]);
Expand Down Expand Up @@ -546,6 +548,7 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
&runtime->xidregistry.mutex,
&runtime->getargs.mutex,
&runtime->unicode_state.ids.lock,
&runtime->imports.extensions.mutex,
};
int reinit_err = 0;
for (int i = 0; i < NUMLOCKS; i++) {
Expand Down

Back | FazBrowse Home | New Git URL