bpo-40513: Per-interpreter GIL (GH-19943) · python/cpython@7be4e35
@@ -250,6 +250,21 @@ ensure_tstate_not_null(const char *func, PyThreadState *tstate)
250250}
251251252252253+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
254+int
255+_PyEval_ThreadsInitialized(PyInterpreterState *interp)
256+{
257+return gil_created(&interp->ceval.gil);
258+}
259+260+int
261+PyEval_ThreadsInitialized(void)
262+{
263+// Fatal error if there is no current interpreter
264+PyInterpreterState *interp = PyInterpreterState_Get();
265+return _PyEval_ThreadsInitialized(interp);
266+}
267+#else
253268int
254269_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
255270{
@@ -262,18 +277,25 @@ PyEval_ThreadsInitialized(void)
262277_PyRuntimeState *runtime = &_PyRuntime;
263278return _PyEval_ThreadsInitialized(runtime);
264279}
280+#endif
265281266282PyStatus
267283_PyEval_InitGIL(PyThreadState *tstate)
268284{
285+#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
269286if (!_Py_IsMainInterpreter(tstate)) {
270287/* Currently, the GIL is shared by all interpreters,
271288 and only the main interpreter is responsible to create
272289 and destroy it. */
273290return _PyStatus_OK();
274291 }
292+#endif
275293294+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
295+struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
296+#else
276297struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
298+#endif
277299assert(!gil_created(gil));
278300279301PyThread_init_thread();
@@ -288,14 +310,20 @@ _PyEval_InitGIL(PyThreadState *tstate)
288310void
289311_PyEval_FiniGIL(PyThreadState *tstate)
290312{
313+#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
291314if (!_Py_IsMainInterpreter(tstate)) {
292315/* Currently, the GIL is shared by all interpreters,
293316 and only the main interpreter is responsible to create
294317 and destroy it. */
295318return;
296319 }
320+#endif
297321322+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
323+struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
324+#else
298325struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
326+#endif
299327if (!gil_created(gil)) {
300328/* First Py_InitializeFromConfig() call: the GIL doesn't exist
301329 yet: do nothing. */
@@ -413,13 +441,18 @@ PyEval_ReleaseThread(PyThreadState *tstate)
413441void
414442_PyEval_ReInitThreads(_PyRuntimeState *runtime)
415443{
444+PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
445+ensure_tstate_not_null(__func__, tstate);
446+447+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
448+struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
449+#else
416450struct _gil_runtime_state *gil = &runtime->ceval.gil;
451+#endif
417452if (!gil_created(gil)) {
418453return;
419454 }
420455recreate_gil(gil);
421-PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
422-ensure_tstate_not_null(__func__, tstate);
423456424457take_gil(tstate);
425458@@ -457,7 +490,11 @@ PyEval_SaveThread(void)
457490458491struct _ceval_runtime_state *ceval = &runtime->ceval;
459492struct _ceval_state *ceval2 = &tstate->interp->ceval;
493+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
494+assert(gil_created(&ceval2->gil));
495+#else
460496assert(gil_created(&ceval->gil));
497+#endif
461498drop_gil(ceval, ceval2, tstate);
462499return tstate;
463500}
716753_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
717754{
718755_Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
756+#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
719757_gil_initialize(&ceval->gil);
758+#endif
720759}
721760722761int
@@ -731,6 +770,11 @@ _PyEval_InitState(struct _ceval_state *ceval)
731770if (pending->lock == NULL) {
732771return -1;
733772 }
773+774+#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
775+_gil_initialize(&ceval->gil);
776+#endif
777+734778return 0;
735779}
736780