summaryrefslogtreecommitdiffstats
path: root/Python/pystate.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-10-02 20:12:12 (GMT)
committerGitHub <noreply@github.com>2023-10-02 20:12:12 (GMT)
commit1dd9dee45d2591b4e701039d1673282380696849 (patch)
tree14199e2d03ef74ca575d64552676d4797d792d9c /Python/pystate.c
parenta040a32ea2f13f16172394d3e3e3f80f47f25a68 (diff)
downloadcpython-1dd9dee45d2591b4e701039d1673282380696849.zip
cpython-1dd9dee45d2591b4e701039d1673282380696849.tar.gz
cpython-1dd9dee45d2591b4e701039d1673282380696849.tar.bz2
gh-105716: Support Background Threads in Subinterpreters Consistently (gh-109921)
The existence of background threads running on a subinterpreter was preventing interpreters from getting properly destroyed, as well as impacting the ability to run the interpreter again. It also affected how we wait for non-daemon threads to finish. We add PyInterpreterState.threads.main, with some internal C-API functions.
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index 01aa255..fe056bf 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -1091,6 +1091,39 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
#endif
+int
+_PyInterpreterState_SetRunningMain(PyInterpreterState *interp)
+{
+ if (interp->threads.main != NULL) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "interpreter already running");
+ return -1;
+ }
+ PyThreadState *tstate = current_fast_get(&_PyRuntime);
+ _Py_EnsureTstateNotNULL(tstate);
+ if (tstate->interp != interp) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "current tstate has wrong interpreter");
+ return -1;
+ }
+ interp->threads.main = tstate;
+ return 0;
+}
+
+void
+_PyInterpreterState_SetNotRunningMain(PyInterpreterState *interp)
+{
+ assert(interp->threads.main == current_fast_get(&_PyRuntime));
+ interp->threads.main = NULL;
+}
+
+int
+_PyInterpreterState_IsRunningMain(PyInterpreterState *interp)
+{
+ return (interp->threads.main != NULL);
+}
+
+
//----------
// accessors
//----------
@@ -2801,6 +2834,10 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
}
+/*************/
+/* Other API */
+/*************/
+
_PyFrameEvalFunction
_PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
{