aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-05-05 20:27:47 +0200
committerGitHub <noreply@github.com>2020-05-05 20:27:47 +0200
commit7be4e350aadf93c4be5c97b7291d0db2b6bc1dc4 (patch)
tree7285b6051ef96253ce4ef2ac2a34412fae9ea281 /Python/ceval.c
parentbpo-40513: new_interpreter() init GIL earlier (GH-19942) (diff)
downloadcpython-7be4e350aadf93c4be5c97b7291d0db2b6bc1dc4.tar.gz
cpython-7be4e350aadf93c4be5c97b7291d0db2b6bc1dc4.tar.bz2
cpython-7be4e350aadf93c4be5c97b7291d0db2b6bc1dc4.zip
bpo-40513: Per-interpreter GIL (GH-19943)
In the experimental isolated subinterpreters build mode, the GIL is now per-interpreter. Move gil from _PyRuntimeState.ceval to PyInterpreterState.ceval. new_interpreter() always get the config from the main interpreter.
Diffstat (limited to 'Python/ceval.c')
-rw-r--r--Python/ceval.c48
1 files changed, 46 insertions, 2 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index b5854d34464..6435bd05446 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -250,6 +250,21 @@ ensure_tstate_not_null(const char *func, PyThreadState *tstate)
}
+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
+int
+_PyEval_ThreadsInitialized(PyInterpreterState *interp)
+{
+ return gil_created(&interp->ceval.gil);
+}
+
+int
+PyEval_ThreadsInitialized(void)
+{
+ // Fatal error if there is no current interpreter
+ PyInterpreterState *interp = PyInterpreterState_Get();
+ return _PyEval_ThreadsInitialized(interp);
+}
+#else
int
_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
{
@@ -262,18 +277,25 @@ PyEval_ThreadsInitialized(void)
_PyRuntimeState *runtime = &_PyRuntime;
return _PyEval_ThreadsInitialized(runtime);
}
+#endif
PyStatus
_PyEval_InitGIL(PyThreadState *tstate)
{
+#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
if (!_Py_IsMainInterpreter(tstate)) {
/* Currently, the GIL is shared by all interpreters,
and only the main interpreter is responsible to create
and destroy it. */
return _PyStatus_OK();
}
+#endif
+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
+ struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
+#else
struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
+#endif
assert(!gil_created(gil));
PyThread_init_thread();
@@ -288,14 +310,20 @@ _PyEval_InitGIL(PyThreadState *tstate)
void
_PyEval_FiniGIL(PyThreadState *tstate)
{
+#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
if (!_Py_IsMainInterpreter(tstate)) {
/* Currently, the GIL is shared by all interpreters,
and only the main interpreter is responsible to create
and destroy it. */
return;
}
+#endif
+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
+ struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
+#else
struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
+#endif
if (!gil_created(gil)) {
/* First Py_InitializeFromConfig() call: the GIL doesn't exist
yet: do nothing. */
@@ -413,13 +441,18 @@ PyEval_ReleaseThread(PyThreadState *tstate)
void
_PyEval_ReInitThreads(_PyRuntimeState *runtime)
{
+ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
+ ensure_tstate_not_null(__func__, tstate);
+
+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
+ struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
+#else
struct _gil_runtime_state *gil = &runtime->ceval.gil;
+#endif
if (!gil_created(gil)) {
return;
}
recreate_gil(gil);
- PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
- ensure_tstate_not_null(__func__, tstate);
take_gil(tstate);
@@ -457,7 +490,11 @@ PyEval_SaveThread(void)
struct _ceval_runtime_state *ceval = &runtime->ceval;
struct _ceval_state *ceval2 = &tstate->interp->ceval;
+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
+ assert(gil_created(&ceval2->gil));
+#else
assert(gil_created(&ceval->gil));
+#endif
drop_gil(ceval, ceval2, tstate);
return tstate;
}
@@ -716,7 +753,9 @@ void
_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
{
_Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
+#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
_gil_initialize(&ceval->gil);
+#endif
}
int
@@ -731,6 +770,11 @@ _PyEval_InitState(struct _ceval_state *ceval)
if (pending->lock == NULL) {
return -1;
}
+
+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
+ _gil_initialize(&ceval->gil);
+#endif
+
return 0;
}