diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-11-28 02:01:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-28 02:01:05 (GMT) |
commit | 0122b4d7c92855e97912cf827dd81d836725c9a4 (patch) | |
tree | 4a1e3355d8bf5e9d2b9f36745518f56f590df021 /Python | |
parent | 82ae5a609d9a2c0ff2384527a18ff1caf7410052 (diff) | |
download | cpython-0122b4d7c92855e97912cf827dd81d836725c9a4.zip cpython-0122b4d7c92855e97912cf827dd81d836725c9a4.tar.gz cpython-0122b4d7c92855e97912cf827dd81d836725c9a4.tar.bz2 |
[3.12] gh-105716: Support Background Threads in Subinterpreters Consistently (gh-109921) (gh-110707)
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.
(cherry-picked from commit 1dd9dee45d2591b4e701039d1673282380696849)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pystate.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Python/pystate.c b/Python/pystate.c index 0430454..534e77f 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1049,6 +1049,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 //---------- @@ -2757,6 +2790,10 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry) } +/*************/ +/* Other API */ +/*************/ + _PyFrameEvalFunction _PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp) { |