summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-03-09 16:46:21 (GMT)
committerGitHub <noreply@github.com>2023-03-09 16:46:21 (GMT)
commitcf6e7c5e551b3513817d6a77ba88253dc8473298 (patch)
treeca5e5125a1478a2cbc4c65e4467f8fcf7568f34f /Include
parentb45d14b88611fefc6f054226d3e1117082d322c8 (diff)
downloadcpython-cf6e7c5e551b3513817d6a77ba88253dc8473298.zip
cpython-cf6e7c5e551b3513817d6a77ba88253dc8473298.tar.gz
cpython-cf6e7c5e551b3513817d6a77ba88253dc8473298.tar.bz2
gh-100227: Isolate the Import State to Each Interpreter (gh-101941)
Specific changes: * move the import lock to PyInterpreterState * move the "find_and_load" diagnostic state to PyInterpreterState Note that the import lock exists to keep multiple imports of the same module in the same interpreter (but in different threads) from stomping on each other. Independently, we use a distinct global lock to protect globally shared import state, especially related to loaded extension modules. For now we can rely on the GIL as that lock but with a per-interpreter GIL we'll need a new global lock. The remaining state in _PyRuntimeState.imports will (probably) continue being global. https://github.com/python/cpython/issues/100227
Diffstat (limited to 'Include')
-rw-r--r--Include/cpython/import.h4
-rw-r--r--Include/internal/pycore_import.h34
-rw-r--r--Include/internal/pycore_runtime_init.h10
3 files changed, 23 insertions, 25 deletions
diff --git a/Include/cpython/import.h b/Include/cpython/import.h
index a58801b..2bca4ad 100644
--- a/Include/cpython/import.h
+++ b/Include/cpython/import.h
@@ -10,8 +10,8 @@ PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(_Py_Identifier *name);
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
-PyAPI_FUNC(void) _PyImport_AcquireLock(void);
-PyAPI_FUNC(int) _PyImport_ReleaseLock(void);
+PyAPI_FUNC(void) _PyImport_AcquireLock(PyInterpreterState *interp);
+PyAPI_FUNC(int) _PyImport_ReleaseLock(PyInterpreterState *interp);
PyAPI_FUNC(int) _PyImport_FixupBuiltin(
PyObject *mod,
diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h
index b7ffe01..69ed627 100644
--- a/Include/internal/pycore_import.h
+++ b/Include/internal/pycore_import.h
@@ -21,17 +21,6 @@ struct _import_runtime_state {
This is initialized lazily in _PyImport_FixupExtensionObject().
Modules are added there and looked up in _imp.find_extension(). */
PyObject *extensions;
- /* The global import lock. */
- struct {
- PyThread_type_lock mutex;
- unsigned long thread;
- int level;
- } lock;
- struct {
- int import_level;
- _PyTime_t accumulated;
- int header;
- } find_and_load;
/* Package context -- the full module name for package imports */
const char * pkgcontext;
};
@@ -69,6 +58,18 @@ struct _import_state {
int dlopenflags;
#endif
PyObject *import_func;
+ /* The global import lock. */
+ struct {
+ PyThread_type_lock mutex;
+ unsigned long thread;
+ int level;
+ } lock;
+ /* diagnostic info in PyImport_ImportModuleLevelObject() */
+ struct {
+ int import_level;
+ _PyTime_t accumulated;
+ int header;
+ } find_and_load;
};
#ifdef HAVE_DLOPEN
@@ -86,8 +87,15 @@ struct _import_state {
#define IMPORTS_INIT \
{ \
- .override_frozen_modules = 0, \
DLOPENFLAGS_INIT \
+ .lock = { \
+ .mutex = NULL, \
+ .thread = PYTHREAD_INVALID_THREAD_ID, \
+ .level = 0, \
+ }, \
+ .find_and_load = { \
+ .header = 1, \
+ }, \
}
extern void _PyImport_ClearCore(PyInterpreterState *interp);
@@ -138,7 +146,7 @@ extern void _PyImport_FiniExternal(PyInterpreterState *interp);
#ifdef HAVE_FORK
-extern PyStatus _PyImport_ReInitLock(void);
+extern PyStatus _PyImport_ReInitLock(PyInterpreterState *interp);
#endif
diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h
index efc82b4..bdecac9 100644
--- a/Include/internal/pycore_runtime_init.h
+++ b/Include/internal/pycore_runtime_init.h
@@ -40,16 +40,6 @@ extern PyTypeObject _PyExc_MemoryError;
in accordance with the specification. */ \
.autoTSSkey = Py_tss_NEEDS_INIT, \
.parser = _parser_runtime_state_INIT, \
- .imports = { \
- .lock = { \
- .mutex = NULL, \
- .thread = PYTHREAD_INVALID_THREAD_ID, \
- .level = 0, \
- }, \
- .find_and_load = { \
- .header = 1, \
- }, \
- }, \
.ceval = { \
.perf = _PyEval_RUNTIME_PERF_INIT, \
}, \