From 95996fb56356ec3bb3ba525bbae09f253e1f2771 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 10 Sep 2018 15:31:30 -0600 Subject: [PATCH 01/15] Zero-out PyInterpreterState when initializing. --- Python/pystate.c | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index cdb881364d7a7b..fd1dd5a274bdd2 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -132,28 +132,12 @@ PyInterpreterState_New(void) return NULL; } + memset(interp, 0, sizeof(*interp)); interp->id_refcount = -1; - interp->id_mutex = NULL; - interp->modules = NULL; - interp->modules_by_index = NULL; - interp->sysdict = NULL; - interp->builtins = NULL; - interp->builtins_copy = NULL; - interp->tstate_head = NULL; interp->check_interval = 100; - interp->num_threads = 0; - interp->pythread_stacksize = 0; - interp->codec_search_path = NULL; - interp->codec_search_cache = NULL; - interp->codec_error_registry = NULL; - interp->codecs_initialized = 0; - interp->fscodec_initialized = 0; interp->core_config = _PyCoreConfig_INIT; interp->config = _PyMainInterpreterConfig_INIT; - interp->importlib = NULL; - interp->import_func = NULL; interp->eval_frame = _PyEval_EvalFrameDefault; - interp->co_extra_user_count = 0; #ifdef HAVE_DLOPEN #if HAVE_DECL_RTLD_NOW interp->dlopenflags = RTLD_NOW; @@ -161,13 +145,6 @@ PyInterpreterState_New(void) interp->dlopenflags = RTLD_LAZY; #endif #endif -#ifdef HAVE_FORK - interp->before_forkers = NULL; - interp->after_forkers_parent = NULL; - interp->after_forkers_child = NULL; -#endif - interp->pyexitfunc = NULL; - interp->pyexitmodule = NULL; HEAD_LOCK(); if (_PyRuntime.interpreters.next_id < 0) { From e33324ffb060438be54497ee68f75098fddc5f0d Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 12:48:03 -0600 Subject: [PATCH 02/15] Add _PyRuntimeState.main_thread. --- Include/internal/pycore_pystate.h | 2 ++ Python/ceval.c | 1 + Python/pystate.c | 4 ++++ 3 files changed, 7 insertions(+) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 7796223b59e650..70c9e5135e090d 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -207,6 +207,8 @@ typedef struct pyruntimestate { struct _xidregitem *head; } xidregistry; + unsigned long main_thread; + #define NEXITFUNCS 32 void (*exitfuncs[NEXITFUNCS])(void); int nexitfuncs; diff --git a/Python/ceval.c b/Python/ceval.c index 03456f6d2ab4f6..f0363261e1ac2e 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -243,6 +243,7 @@ PyEval_ReInitThreads(void) if (!gil_created()) return; recreate_gil(); + _PyRuntime.main_thread = PyThread_get_thread_ident(); _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); take_gil(current_tstate); _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident(); diff --git a/Python/pystate.c b/Python/pystate.c index fd1dd5a274bdd2..383618db22269f 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -146,6 +146,10 @@ PyInterpreterState_New(void) #endif #endif + if (_PyRuntime.main_thread == 0) { + _PyRuntime.main_thread = PyThread_get_thread_ident(); + } + HEAD_LOCK(); if (_PyRuntime.interpreters.next_id < 0) { /* overflow or Py_Initialize() not called! */ From e6f788e8ab3d0b4f91983f0f71ead31f82d96fc4 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 4 Dec 2018 13:57:24 -0800 Subject: [PATCH 03/15] Factor out _add_pending_call() and _pop_pending_call(). --- Python/ceval.c | 57 +++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index f0363261e1ac2e..bebec02c90cc2a 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -324,6 +324,34 @@ _PyEval_SignalReceived(void) SIGNAL_PENDING_SIGNALS(); } +static int +_add_pending_call(int (*func)(void *), void *arg) +{ + int i = _PyRuntime.ceval.pending.last; + int j = (i + 1) % NPENDINGCALLS; + if (j == _PyRuntime.ceval.pending.first) { + return -1; /* Queue full */ + } + _PyRuntime.ceval.pending.calls[i].func = func; + _PyRuntime.ceval.pending.calls[i].arg = arg; + _PyRuntime.ceval.pending.last = j; + return 0; +} + +static void +_pop_pending_call(int (**func)(void *), void **arg) +{ + /* pop one item off the queue while holding the lock */ + int i = _PyRuntime.ceval.pending.first; + if (i == _PyRuntime.ceval.pending.last) { + return; /* Queue empty */ + } + + *func = _PyRuntime.ceval.pending.calls[i].func; + *arg = _PyRuntime.ceval.pending.calls[i].arg; + _PyRuntime.ceval.pending.first = (i + 1) % NPENDINGCALLS; +} + /* This implementation is thread-safe. It allows scheduling to be made from any thread, and even from an executing callback. @@ -332,7 +360,6 @@ _PyEval_SignalReceived(void) int Py_AddPendingCall(int (*func)(void *), void *arg) { - int i, j, result=0; PyThread_type_lock lock = _PyRuntime.ceval.pending.lock; /* try a few times for the lock. Since this mechanism is used @@ -347,6 +374,7 @@ Py_AddPendingCall(int (*func)(void *), void *arg) * this function is called before any bytecode evaluation takes place. */ if (lock != NULL) { + int i; for (i = 0; i<100; i++) { if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) break; @@ -355,15 +383,8 @@ Py_AddPendingCall(int (*func)(void *), void *arg) return -1; } - i = _PyRuntime.ceval.pending.last; - j = (i + 1) % NPENDINGCALLS; - if (j == _PyRuntime.ceval.pending.first) { - result = -1; /* Queue full */ - } else { - _PyRuntime.ceval.pending.calls[i].func = func; - _PyRuntime.ceval.pending.calls[i].arg = arg; - _PyRuntime.ceval.pending.last = j; - } + int result = _add_pending_call(func, arg); + /* signal main loop */ SIGNAL_PENDING_CALLS(); if (lock != NULL) @@ -422,24 +443,18 @@ make_pending_calls(void) /* perform a bounded number of calls, in case of recursion */ for (int i=0; i Date: Fri, 13 Jul 2018 16:57:36 -0600 Subject: [PATCH 04/15] Move pending calls from _PyRuntimeState to PyIntepreterState. --- Include/internal/pycore_ceval.h | 10 +-- Include/internal/pycore_pystate.h | 10 +++ Python/ceval.c | 118 +++++++++++++++--------------- Python/pystate.c | 10 +++ 4 files changed, 83 insertions(+), 65 deletions(-) diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index b9f2d7d1758537..208d0f033e9c57 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -11,8 +11,12 @@ extern "C" { #include "pycore_atomic.h" #include "pythread.h" +struct _is; // See PyInterpreterState in cpython/pystate.h. + +PyAPI_FUNC(int) _Py_AddPendingCall(struct _is*, int (*)(void *), void *); +PyAPI_FUNC(int) _Py_MakePendingCalls(struct _is*); + struct _pending_calls { - unsigned long main_thread; PyThread_type_lock lock; /* Request for running pending calls. */ _Py_atomic_int calls_to_do; @@ -39,12 +43,8 @@ struct _ceval_runtime_state { c_tracefunc. This speeds up the if statement in PyEval_EvalFrameEx() after fast_next_opcode. */ int tracing_possible; - /* This single variable consolidates all requests to break out of - the fast path in the eval loop. */ - _Py_atomic_int eval_breaker; /* Request for dropping the GIL */ _Py_atomic_int gil_drop_request; - struct _pending_calls pending; /* Request for checking signals. */ _Py_atomic_int signals_pending; struct _gil_runtime_state gil; diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 70c9e5135e090d..2ee8b208c6fc97 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -11,6 +11,7 @@ extern "C" { #include "pystate.h" #include "pythread.h" +#include "pycore_atomic.h" #include "pycore_ceval.h" #include "pycore_pathconfig.h" #include "pycore_pymem.h" @@ -40,6 +41,15 @@ struct _is { /* Used in Python/sysmodule.c. */ int check_interval; +#ifdef Py_BUILD_CORE + struct _ceval { + /* This single variable consolidates all requests to break out of + the fast path in the eval loop. */ + _Py_atomic_int eval_breaker; + struct _pending_calls pending; + } ceval; +#endif + /* Used in Modules/_threadmodule.c. */ long num_threads; /* Support for runtime thread stack size tuning. diff --git a/Python/ceval.c b/Python/ceval.c index bebec02c90cc2a..ead91c65b09cbc 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -98,16 +98,16 @@ static long dxp[256]; the GIL eventually anyway. */ #define COMPUTE_EVAL_BREAKER() \ _Py_atomic_store_relaxed( \ - &_PyRuntime.ceval.eval_breaker, \ + &PyThreadState_Get()->interp->ceval.eval_breaker, \ GIL_REQUEST | \ _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \ - _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \ - _PyRuntime.ceval.pending.async_exc) + _Py_atomic_load_relaxed(&PyThreadState_Get()->interp->ceval.pending.calls_to_do) | \ + PyThreadState_Get()->interp->ceval.pending.async_exc) #define SET_GIL_DROP_REQUEST() \ do { \ _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \ - _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ + _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.eval_breaker, 1); \ } while (0) #define RESET_GIL_DROP_REQUEST() \ @@ -119,20 +119,20 @@ static long dxp[256]; /* Pending calls are only modified under pending_lock */ #define SIGNAL_PENDING_CALLS() \ do { \ - _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \ - _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ + _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.pending.calls_to_do, 1); \ + _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.eval_breaker, 1); \ } while (0) #define UNSIGNAL_PENDING_CALLS() \ do { \ - _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \ + _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.pending.calls_to_do, 0); \ COMPUTE_EVAL_BREAKER(); \ } while (0) #define SIGNAL_PENDING_SIGNALS() \ do { \ _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \ - _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ + _Py_atomic_store_relaxed(&_PyRuntime.interpreters.main->ceval.eval_breaker, 1); \ } while (0) #define UNSIGNAL_PENDING_SIGNALS() \ @@ -143,13 +143,13 @@ static long dxp[256]; #define SIGNAL_ASYNC_EXC() \ do { \ - _PyRuntime.ceval.pending.async_exc = 1; \ - _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ + PyThreadState_Get()->interp->ceval.pending.async_exc = 1; \ + _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.eval_breaker, 1); \ } while (0) #define UNSIGNAL_ASYNC_EXC() \ do { \ - _PyRuntime.ceval.pending.async_exc = 0; \ + PyThreadState_Get()->interp->ceval.pending.async_exc = 0; \ COMPUTE_EVAL_BREAKER(); \ } while (0) @@ -174,9 +174,6 @@ PyEval_InitThreads(void) PyThread_init_thread(); create_gil(); take_gil(_PyThreadState_GET()); - _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident(); - if (!_PyRuntime.ceval.pending.lock) - _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); } void @@ -244,9 +241,9 @@ PyEval_ReInitThreads(void) return; recreate_gil(); _PyRuntime.main_thread = PyThread_get_thread_ident(); - _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); + // XXX Set for every interpreter. + current_tstate->interp->ceval.pending.lock = PyThread_allocate_lock(); take_gil(current_tstate); - _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident(); /* Destroy all threads except the current one */ _PyThreadState_DeleteExcept(current_tstate); @@ -325,31 +322,38 @@ _PyEval_SignalReceived(void) } static int -_add_pending_call(int (*func)(void *), void *arg) +_add_pending_call(PyInterpreterState *interp, int (*func)(void *), void *arg) { - int i = _PyRuntime.ceval.pending.last; + int i = interp->ceval.pending.last; int j = (i + 1) % NPENDINGCALLS; - if (j == _PyRuntime.ceval.pending.first) { + if (j == interp->ceval.pending.first) { return -1; /* Queue full */ } - _PyRuntime.ceval.pending.calls[i].func = func; - _PyRuntime.ceval.pending.calls[i].arg = arg; - _PyRuntime.ceval.pending.last = j; + interp->ceval.pending.calls[i].func = func; + interp->ceval.pending.calls[i].arg = arg; + interp->ceval.pending.last = j; return 0; } static void -_pop_pending_call(int (**func)(void *), void **arg) +_pop_pending_call(PyInterpreterState *interp, int (**func)(void *), void **arg) { /* pop one item off the queue while holding the lock */ - int i = _PyRuntime.ceval.pending.first; - if (i == _PyRuntime.ceval.pending.last) { + int i = interp->ceval.pending.first; + if (i == interp->ceval.pending.last) { return; /* Queue empty */ } - *func = _PyRuntime.ceval.pending.calls[i].func; - *arg = _PyRuntime.ceval.pending.calls[i].arg; - _PyRuntime.ceval.pending.first = (i + 1) % NPENDINGCALLS; + *func = interp->ceval.pending.calls[i].func; + *arg = interp->ceval.pending.calls[i].arg; + interp->ceval.pending.first = (i + 1) % NPENDINGCALLS; +} + +int +Py_AddPendingCall(int (*func)(void *), void *arg) +{ + PyInterpreterState *interp = _PyRuntime.interpreters.main; + return _Py_AddPendingCall(interp, func, arg); } /* This implementation is thread-safe. It allows @@ -358,10 +362,8 @@ _pop_pending_call(int (**func)(void *), void **arg) */ int -Py_AddPendingCall(int (*func)(void *), void *arg) +_Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg) { - PyThread_type_lock lock = _PyRuntime.ceval.pending.lock; - /* try a few times for the lock. Since this mechanism is used * for signal handling (on the main thread), there is a (slim) * chance that a signal is delivered on the same thread while we @@ -373,6 +375,7 @@ Py_AddPendingCall(int (*func)(void *), void *arg) * We also check for lock being NULL, in the unlikely case that * this function is called before any bytecode evaluation takes place. */ + PyThread_type_lock lock = interp->ceval.pending.lock; if (lock != NULL) { int i; for (i = 0; i<100; i++) { @@ -383,10 +386,10 @@ Py_AddPendingCall(int (*func)(void *), void *arg) return -1; } - int result = _add_pending_call(func, arg); - + int result = _add_pending_call(interp, func, arg); /* signal main loop */ SIGNAL_PENDING_CALLS(); + if (lock != NULL) PyThread_release_lock(lock); return result; @@ -396,9 +399,7 @@ static int handle_signals(void) { /* Only handle signals on main thread. */ - if (_PyRuntime.ceval.pending.main_thread && - PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread) - { + if (PyThread_get_thread_ident() != _PyRuntime.main_thread) { return 0; } @@ -411,17 +412,10 @@ handle_signals(void) } static int -make_pending_calls(void) +make_pending_calls(PyInterpreterState *interp) { static int busy = 0; - /* only service pending calls on main thread */ - if (_PyRuntime.ceval.pending.main_thread && - PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread) - { - return 0; - } - /* don't perform recursive pending calls */ if (busy) { return 0; @@ -432,10 +426,10 @@ make_pending_calls(void) UNSIGNAL_PENDING_CALLS(); int res = 0; - if (!_PyRuntime.ceval.pending.lock) { + if (!interp->ceval.pending.lock) { /* initial allocation of the lock */ - _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); - if (_PyRuntime.ceval.pending.lock == NULL) { + interp->ceval.pending.lock = PyThread_allocate_lock(); + if (interp->ceval.pending.lock == NULL) { res = -1; goto error; } @@ -447,9 +441,9 @@ make_pending_calls(void) void *arg = NULL; /* pop one item off the queue while holding the lock */ - PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK); - _pop_pending_call(&func, &arg); - PyThread_release_lock(_PyRuntime.ceval.pending.lock); + PyThread_acquire_lock(interp->ceval.pending.lock, WAIT_LOCK); + _pop_pending_call(interp, &func, &arg); + PyThread_release_lock(interp->ceval.pending.lock); /* having released the lock, perform the callback */ if (func == NULL) { @@ -470,6 +464,14 @@ make_pending_calls(void) return res; } +int +_Py_MakePendingCalls(PyInterpreterState *interp) +{ + assert(PyGILState_Check()); + + return make_pending_calls(interp); +} + /* Py_MakePendingCalls() is a simple wrapper for the sake of backward-compatibility. */ int @@ -484,12 +486,8 @@ Py_MakePendingCalls(void) return res; } - res = make_pending_calls(); - if (res != 0) { - return res; - } - - return 0; + PyInterpreterState *interp = _PyRuntime.interpreters.main; + return make_pending_calls(interp); } /* The interpreter's recursion limit */ @@ -696,7 +694,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) #define DISPATCH() \ { \ - if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \ + if (!_Py_atomic_load_relaxed(&tstate->interp->ceval.eval_breaker)) { \ FAST_DISPATCH(); \ } \ continue; \ @@ -998,7 +996,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) async I/O handler); see Py_AddPendingCall() and Py_MakePendingCalls() above. */ - if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { + if (_Py_atomic_load_relaxed(&(tstate->interp->ceval.eval_breaker))) { opcode = _Py_OPCODE(*next_instr); if (opcode == SETUP_FINALLY || opcode == SETUP_WITH || @@ -1031,9 +1029,9 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) } } if (_Py_atomic_load_relaxed( - &_PyRuntime.ceval.pending.calls_to_do)) + &(tstate->interp->ceval.pending.calls_to_do))) { - if (make_pending_calls() != 0) { + if (_Py_MakePendingCalls(tstate->interp) != 0) { goto error; } } diff --git a/Python/pystate.c b/Python/pystate.c index 383618db22269f..5f6b23987c509c 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -135,6 +135,13 @@ PyInterpreterState_New(void) memset(interp, 0, sizeof(*interp)); interp->id_refcount = -1; interp->check_interval = 100; + + interp->ceval.pending.lock = PyThread_allocate_lock(); + if (interp->ceval.pending.lock == NULL) { + PyErr_SetString(PyExc_RuntimeError, + "failed to create interpreter ceval pending mutex"); + return NULL; + } interp->core_config = _PyCoreConfig_INIT; interp->config = _PyMainInterpreterConfig_INIT; interp->eval_frame = _PyEval_EvalFrameDefault; @@ -243,6 +250,9 @@ PyInterpreterState_Delete(PyInterpreterState *interp) if (interp->id_mutex != NULL) { PyThread_free_lock(interp->id_mutex); } + if (interp->ceval.pending.lock != NULL) { + PyThread_free_lock(interp->ceval.pending.lock); + } PyMem_RawFree(interp); } From 0c36b4c159b67b2cb5471e49641fb92ab371e480 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 10 Sep 2018 16:41:27 -0600 Subject: [PATCH 05/15] Explicitly pass the interpreter to the stateful ceval macros. --- Include/ceval.h | 2 +- Python/ceval.c | 54 +++++++++++++++++++++++----------------------- Python/ceval_gil.h | 8 +++---- Python/pystate.c | 2 +- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/Include/ceval.h b/Include/ceval.h index 11283c0a570b7e..9c6d420bc23449 100644 --- a/Include/ceval.h +++ b/Include/ceval.h @@ -221,7 +221,7 @@ PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc); #ifndef Py_LIMITED_API PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *); PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *); -PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void); +PyAPI_FUNC(void) _PyEval_SignalAsyncExc(PyInterpreterState *); #endif /* Masks and values used by FORMAT_VALUE opcode. */ diff --git a/Python/ceval.c b/Python/ceval.c index ead91c65b09cbc..7aa13f51729369 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -96,37 +96,37 @@ static long dxp[256]; /* This can set eval_breaker to 0 even though gil_drop_request became 1. We believe this is all right because the eval loop will release the GIL eventually anyway. */ -#define COMPUTE_EVAL_BREAKER() \ +#define COMPUTE_EVAL_BREAKER(interp) \ _Py_atomic_store_relaxed( \ - &PyThreadState_Get()->interp->ceval.eval_breaker, \ + &interp->ceval.eval_breaker, \ GIL_REQUEST | \ _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \ - _Py_atomic_load_relaxed(&PyThreadState_Get()->interp->ceval.pending.calls_to_do) | \ - PyThreadState_Get()->interp->ceval.pending.async_exc) + _Py_atomic_load_relaxed(&interp->ceval.pending.calls_to_do) | \ + interp->ceval.pending.async_exc) -#define SET_GIL_DROP_REQUEST() \ +#define SET_GIL_DROP_REQUEST(interp) \ do { \ _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \ - _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.eval_breaker, 1); \ + _Py_atomic_store_relaxed(&interp->ceval.eval_breaker, 1); \ } while (0) -#define RESET_GIL_DROP_REQUEST() \ +#define RESET_GIL_DROP_REQUEST(interp) \ do { \ _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \ - COMPUTE_EVAL_BREAKER(); \ + COMPUTE_EVAL_BREAKER(interp); \ } while (0) /* Pending calls are only modified under pending_lock */ -#define SIGNAL_PENDING_CALLS() \ +#define SIGNAL_PENDING_CALLS(interp) \ do { \ - _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.pending.calls_to_do, 1); \ - _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.eval_breaker, 1); \ + _Py_atomic_store_relaxed(&interp->ceval.pending.calls_to_do, 1); \ + _Py_atomic_store_relaxed(&interp->ceval.eval_breaker, 1); \ } while (0) -#define UNSIGNAL_PENDING_CALLS() \ +#define UNSIGNAL_PENDING_CALLS(interp) \ do { \ - _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.pending.calls_to_do, 0); \ - COMPUTE_EVAL_BREAKER(); \ + _Py_atomic_store_relaxed(&interp->ceval.pending.calls_to_do, 0); \ + COMPUTE_EVAL_BREAKER(interp); \ } while (0) #define SIGNAL_PENDING_SIGNALS() \ @@ -138,19 +138,19 @@ static long dxp[256]; #define UNSIGNAL_PENDING_SIGNALS() \ do { \ _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \ - COMPUTE_EVAL_BREAKER(); \ + COMPUTE_EVAL_BREAKER(_PyRuntime.interpreters.main); \ } while (0) -#define SIGNAL_ASYNC_EXC() \ +#define SIGNAL_ASYNC_EXC(interp) \ do { \ - PyThreadState_Get()->interp->ceval.pending.async_exc = 1; \ - _Py_atomic_store_relaxed(&PyThreadState_Get()->interp->ceval.eval_breaker, 1); \ + interp->ceval.pending.async_exc = 1; \ + _Py_atomic_store_relaxed(&interp->ceval.eval_breaker, 1); \ } while (0) -#define UNSIGNAL_ASYNC_EXC() \ +#define UNSIGNAL_ASYNC_EXC(interp) \ do { \ - PyThreadState_Get()->interp->ceval.pending.async_exc = 0; \ - COMPUTE_EVAL_BREAKER(); \ + interp->ceval.pending.async_exc = 0; \ + COMPUTE_EVAL_BREAKER(interp); \ } while (0) @@ -253,9 +253,9 @@ PyEval_ReInitThreads(void) raised. */ void -_PyEval_SignalAsyncExc(void) +_PyEval_SignalAsyncExc(PyInterpreterState *interp) { - SIGNAL_ASYNC_EXC(); + SIGNAL_ASYNC_EXC(interp); } PyThreadState * @@ -388,7 +388,7 @@ _Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg) int result = _add_pending_call(interp, func, arg); /* signal main loop */ - SIGNAL_PENDING_CALLS(); + SIGNAL_PENDING_CALLS(interp); if (lock != NULL) PyThread_release_lock(lock); @@ -423,7 +423,7 @@ make_pending_calls(PyInterpreterState *interp) busy = 1; /* unsignal before starting to call callbacks, so that any callback added in-between re-signals */ - UNSIGNAL_PENDING_CALLS(); + UNSIGNAL_PENDING_CALLS(interp); int res = 0; if (!interp->ceval.pending.lock) { @@ -460,7 +460,7 @@ make_pending_calls(PyInterpreterState *interp) error: busy = 0; - SIGNAL_PENDING_CALLS(); + SIGNAL_PENDING_CALLS(interp); /* We're not done yet */ return res; } @@ -1063,7 +1063,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) if (tstate->async_exc != NULL) { PyObject *exc = tstate->async_exc; tstate->async_exc = NULL; - UNSIGNAL_ASYNC_EXC(); + UNSIGNAL_ASYNC_EXC(tstate->interp); PyErr_SetNone(exc); Py_DECREF(exc); goto error; diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h index f2d5fdba015364..d9ad3616fa2427 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -176,7 +176,7 @@ static void drop_gil(PyThreadState *tstate) &_PyRuntime.ceval.gil.last_holder) ) == tstate) { - RESET_GIL_DROP_REQUEST(); + RESET_GIL_DROP_REQUEST(tstate->interp); /* NOTE: if COND_WAIT does not atomically start waiting when releasing the mutex, another thread can run through, take the GIL and drop it again, and reset the condition @@ -213,7 +213,7 @@ static void take_gil(PyThreadState *tstate) if (timed_out && _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil.locked) && _PyRuntime.ceval.gil.switch_number == saved_switchnum) { - SET_GIL_DROP_REQUEST(); + SET_GIL_DROP_REQUEST(tstate->interp); } } _ready: @@ -239,10 +239,10 @@ static void take_gil(PyThreadState *tstate) MUTEX_UNLOCK(_PyRuntime.ceval.gil.switch_mutex); #endif if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)) { - RESET_GIL_DROP_REQUEST(); + RESET_GIL_DROP_REQUEST(tstate->interp); } if (tstate->async_exc != NULL) { - _PyEval_SignalAsyncExc(); + _PyEval_SignalAsyncExc(tstate->interp); } MUTEX_UNLOCK(_PyRuntime.ceval.gil.mutex); diff --git a/Python/pystate.c b/Python/pystate.c index 5f6b23987c509c..0a2d4a3209cb90 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -862,7 +862,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) p->async_exc = exc; HEAD_UNLOCK(); Py_XDECREF(old_exc); - _PyEval_SignalAsyncExc(); + _PyEval_SignalAsyncExc(interp); return 1; } } From 01cb55dcfa4a4a9de59871619fb4b7ad16e655f6 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 13 Jul 2018 16:58:27 -0600 Subject: [PATCH 06/15] Add a note about pending calls during interpreter cleanup. --- Python/pystate.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Python/pystate.c b/Python/pystate.c index 0a2d4a3209cb90..d142ad3b09ed3b 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -189,6 +189,8 @@ void PyInterpreterState_Clear(PyInterpreterState *interp) { PyThreadState *p; + // XXX Also ensure that all pending calls have been made. Disallow + // registration of more pending calls. HEAD_LOCK(); for (p = interp->tstate_head; p != NULL; p = p->next) PyThreadState_Clear(p); @@ -210,6 +212,9 @@ PyInterpreterState_Clear(PyInterpreterState *interp) Py_CLEAR(interp->after_forkers_parent); Py_CLEAR(interp->after_forkers_child); #endif + // XXX Once we have one allocator per interpreter (i.e. + // per-interpreter GC) we must ensure that all of the interpreter's + // objects have been cleaned up at the point. } From 3fd1b0fef76ecd48dc3ba036518cc2f5d9442b66 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 14 Sep 2018 16:35:49 -0700 Subject: [PATCH 07/15] Use the internal function. --- Modules/_testcapimodule.c | 1 + Modules/signalmodule.c | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 350ef771630eab..5b0da783e5e2a4 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2445,6 +2445,7 @@ pending_threadfunc(PyObject *self, PyObject *arg) Py_INCREF(callable); Py_BEGIN_ALLOW_THREADS + /* XXX Use the internal _Py_AddPendingCall(). */ r = Py_AddPendingCall(&_pending_callback, callable); Py_END_ALLOW_THREADS diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 9d49cbd1440097..ebd21c5c980be1 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -19,6 +19,7 @@ #include #endif #endif +#include "internal/pycore_pystate.h" #ifdef HAVE_SIGNAL_H #include @@ -287,8 +288,9 @@ trip_signal(int sig_num) { /* Py_AddPendingCall() isn't signal-safe, but we still use it for this exceptional case. */ - Py_AddPendingCall(report_wakeup_send_error, - (void *)(intptr_t) last_error); + _Py_AddPendingCall(_PyRuntime.interpreters.main, + report_wakeup_send_error, + (void *)(intptr_t) last_error); } } } @@ -305,8 +307,9 @@ trip_signal(int sig_num) { /* Py_AddPendingCall() isn't signal-safe, but we still use it for this exceptional case. */ - Py_AddPendingCall(report_wakeup_write_error, - (void *)(intptr_t)errno); + _Py_AddPendingCall(_PyRuntime.interpreters.main, + report_wakeup_write_error, + (void *)(intptr_t)errno); } } } From 1160c4de54e833b0df7a30f1155476f73f6e5b6e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 11:22:34 -0600 Subject: [PATCH 08/15] Make any remaining pending calls before shutdown. --- Python/pylifecycle.c | 13 +++++++++++++ Python/pystate.c | 2 -- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8d0075a48db6ff..fc7a58449fc857 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1461,6 +1461,19 @@ Py_EndInterpreter(PyThreadState *tstate) wait_for_thread_shutdown(); + if (_Py_atomic_load_relaxed( + &(interp->ceval.pending.calls_to_do))) + { + // XXX Ensure that the interpreter is running in the current thread? + if (_Py_MakePendingCalls(interp) < 0) { + PyObject *exc, *val, *tb; + PyErr_Fetch(&exc, &val, &tb); + PyErr_BadInternalCall(); + _PyErr_ChainExceptions(exc, val, tb); + PyErr_Print(); + } + } + call_py_exitfuncs(interp); if (tstate != interp->tstate_head || tstate->next != NULL) diff --git a/Python/pystate.c b/Python/pystate.c index d142ad3b09ed3b..1a43315e7e1c7b 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -189,8 +189,6 @@ void PyInterpreterState_Clear(PyInterpreterState *interp) { PyThreadState *p; - // XXX Also ensure that all pending calls have been made. Disallow - // registration of more pending calls. HEAD_LOCK(); for (p = interp->tstate_head; p != NULL; p = p->next) PyThreadState_Clear(p); From 54cc32622b431369126739c8126801d6e38ba5c0 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 11:57:29 -0600 Subject: [PATCH 09/15] Prevent adding pending calls if finalizing. --- Include/internal/pycore_pystate.h | 2 ++ Python/ceval.c | 13 ++++++++++++- Python/pylifecycle.c | 11 +++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 2ee8b208c6fc97..b8865ce57f6f79 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -32,6 +32,8 @@ struct _is { int64_t id_refcount; PyThread_type_lock id_mutex; + int finalizing; + PyObject *modules; PyObject *modules_by_index; PyObject *sysdict; diff --git a/Python/ceval.c b/Python/ceval.c index 7aa13f51729369..36834d178db4c6 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -386,10 +386,21 @@ _Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg) return -1; } - int result = _add_pending_call(interp, func, arg); + int result = -1; + if (interp->finalizing) { + PyObject *exc, *val, *tb; + PyErr_Fetch(&exc, &val, &tb); + PyErr_SetString(PyExc_SystemError, "Py_AddPendingCall: cannot add pending calls (interpreter shutting down)"); + PyErr_Print(); + PyErr_Restore(exc, val, tb); + goto done; + } + + result = _add_pending_call(interp, func, arg); /* signal main loop */ SIGNAL_PENDING_CALLS(interp); +done: if (lock != NULL) PyThread_release_lock(lock); return result; diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index fc7a58449fc857..eb0fdddb4b3acf 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1459,8 +1459,19 @@ Py_EndInterpreter(PyThreadState *tstate) if (tstate->frame != NULL) Py_FatalError("Py_EndInterpreter: thread still has a frame"); + // Mark as finalizing. + if (interp->ceval.pending.lock != NULL) { + PyThread_acquire_lock(interp->ceval.pending.lock, 1); + } + interp->finalizing = 1; + if (interp->ceval.pending.lock != NULL) { + PyThread_release_lock(interp->ceval.pending.lock); + } + + // Wrap up existing threads. wait_for_thread_shutdown(); + // Make any pending calls. if (_Py_atomic_load_relaxed( &(interp->ceval.pending.calls_to_do))) { From 117a9cfb7e7bf15b9f0c1e0bbc3fe80a550491b6 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 12:14:04 -0600 Subject: [PATCH 10/15] Add a NEWS entry. --- .../2018-09-15-12-13-46.bpo-33608.avmvVP.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-09-15-12-13-46.bpo-33608.avmvVP.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-09-15-12-13-46.bpo-33608.avmvVP.rst b/Misc/NEWS.d/next/Core and Builtins/2018-09-15-12-13-46.bpo-33608.avmvVP.rst new file mode 100644 index 00000000000000..73a01a1f46bdc1 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-09-15-12-13-46.bpo-33608.avmvVP.rst @@ -0,0 +1,5 @@ +We added a new internal _Py_AddPendingCall() that operates relative to the +provided interpreter. This allows us to use the existing implementation to +ask another interpreter to do work that cannot be done in the current +interpreter, like decref an object the other interpreter owns. The existing +Py_AddPendingCall() only operates relative to the main interpreter. From 4f447cd161b5760cb74128630ceaace0fa6a9930 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 13:16:34 -0600 Subject: [PATCH 11/15] Optionally lock a pending call to a specific thread. --- Include/internal/pycore_ceval.h | 3 ++- Modules/signalmodule.c | 2 ++ Python/ceval.c | 19 ++++++++++++++----- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index 208d0f033e9c57..22815bc55ceabe 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -13,7 +13,7 @@ extern "C" { struct _is; // See PyInterpreterState in cpython/pystate.h. -PyAPI_FUNC(int) _Py_AddPendingCall(struct _is*, int (*)(void *), void *); +PyAPI_FUNC(int) _Py_AddPendingCall(struct _is*, unsigned long, int (*)(void *), void *); PyAPI_FUNC(int) _Py_MakePendingCalls(struct _is*); struct _pending_calls { @@ -26,6 +26,7 @@ struct _pending_calls { int async_exc; #define NPENDINGCALLS 32 struct { + unsigned long thread_id; int (*func)(void *); void *arg; } calls[NPENDINGCALLS]; diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index ebd21c5c980be1..0ce4d8feb7e45e 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -289,6 +289,7 @@ trip_signal(int sig_num) /* Py_AddPendingCall() isn't signal-safe, but we still use it for this exceptional case. */ _Py_AddPendingCall(_PyRuntime.interpreters.main, + main_thread, report_wakeup_send_error, (void *)(intptr_t) last_error); } @@ -308,6 +309,7 @@ trip_signal(int sig_num) /* Py_AddPendingCall() isn't signal-safe, but we still use it for this exceptional case. */ _Py_AddPendingCall(_PyRuntime.interpreters.main, + main_thread, report_wakeup_write_error, (void *)(intptr_t)errno); } diff --git a/Python/ceval.c b/Python/ceval.c index 36834d178db4c6..0912ae8f044d18 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -322,23 +322,24 @@ _PyEval_SignalReceived(void) } static int -_add_pending_call(PyInterpreterState *interp, int (*func)(void *), void *arg) +_add_pending_call(PyInterpreterState *interp, unsigned long thread_id, int (*func)(void *), void *arg) { int i = interp->ceval.pending.last; int j = (i + 1) % NPENDINGCALLS; if (j == interp->ceval.pending.first) { return -1; /* Queue full */ } + interp->ceval.pending.calls[i].thread_id = thread_id; interp->ceval.pending.calls[i].func = func; interp->ceval.pending.calls[i].arg = arg; interp->ceval.pending.last = j; return 0; } +/* pop one item off the queue while holding the lock */ static void _pop_pending_call(PyInterpreterState *interp, int (**func)(void *), void **arg) { - /* pop one item off the queue while holding the lock */ int i = interp->ceval.pending.first; if (i == interp->ceval.pending.last) { return; /* Queue empty */ @@ -347,13 +348,21 @@ _pop_pending_call(PyInterpreterState *interp, int (**func)(void *), void **arg) *func = interp->ceval.pending.calls[i].func; *arg = interp->ceval.pending.calls[i].arg; interp->ceval.pending.first = (i + 1) % NPENDINGCALLS; + + unsigned long thread_id = interp->ceval.pending.calls[i].thread_id; + if (thread_id && PyThread_get_thread_ident() != thread_id) { + // Thread mismatch, so move it to the end of the list + // and start over. + _Py_AddPendingCall(interp, thread_id, *func, *arg); + return; + } } int Py_AddPendingCall(int (*func)(void *), void *arg) { PyInterpreterState *interp = _PyRuntime.interpreters.main; - return _Py_AddPendingCall(interp, func, arg); + return _Py_AddPendingCall(interp, _PyRuntime.main_thread, func, arg); } /* This implementation is thread-safe. It allows @@ -362,7 +371,7 @@ Py_AddPendingCall(int (*func)(void *), void *arg) */ int -_Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg) +_Py_AddPendingCall(PyInterpreterState *interp, unsigned long thread_id, int (*func)(void *), void *arg) { /* try a few times for the lock. Since this mechanism is used * for signal handling (on the main thread), there is a (slim) @@ -396,7 +405,7 @@ _Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg) goto done; } - result = _add_pending_call(interp, func, arg); + result = _add_pending_call(interp, thread_id, func, arg); /* signal main loop */ SIGNAL_PENDING_CALLS(interp); From 5bd5c803efda86b7c3700fc2a97beb0b5e5e083a Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 1 Feb 2019 11:49:28 -0700 Subject: [PATCH 12/15] Move core-only field to end of struct. --- Include/internal/pycore_pystate.h | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index b8865ce57f6f79..17497989dd3f14 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -43,15 +43,6 @@ struct _is { /* Used in Python/sysmodule.c. */ int check_interval; -#ifdef Py_BUILD_CORE - struct _ceval { - /* This single variable consolidates all requests to break out of - the fast path in the eval loop. */ - _Py_atomic_int eval_breaker; - struct _pending_calls pending; - } ceval; -#endif - /* Used in Modules/_threadmodule.c. */ long num_threads; /* Support for runtime thread stack size tuning. @@ -90,6 +81,13 @@ struct _is { PyObject *pyexitmodule; uint64_t tstate_next_unique_id; + + struct _ceval { + /* This single variable consolidates all requests to break out of + the fast path in the eval loop. */ + _Py_atomic_int eval_breaker; + struct _pending_calls pending; + } ceval; }; PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T); From 32d194abb837a052bd25f6f3846993dfdd5d1478 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 1 Feb 2019 12:59:00 -0700 Subject: [PATCH 13/15] Factor out struct _ceval_interpreter_state. --- Include/internal/pycore_ceval.h | 7 +++++++ Include/internal/pycore_pystate.h | 7 +------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index 22815bc55ceabe..5a80f6fee06eaf 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -34,6 +34,13 @@ struct _pending_calls { int last; }; +struct _ceval_interpreter_state { + /* This single variable consolidates all requests to break out of + the fast path in the eval loop. */ + _Py_atomic_int eval_breaker; + struct _pending_calls pending; +}; + #include "pycore_gil.h" struct _ceval_runtime_state { diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 17497989dd3f14..f6c61e7bcbd6d5 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -82,12 +82,7 @@ struct _is { uint64_t tstate_next_unique_id; - struct _ceval { - /* This single variable consolidates all requests to break out of - the fast path in the eval loop. */ - _Py_atomic_int eval_breaker; - struct _pending_calls pending; - } ceval; + struct _ceval_interpreter_state ceval; }; PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T); From 513c65831617ff48588e6f9f3edb47c54be2b7a9 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 18 Feb 2019 15:24:45 -0700 Subject: [PATCH 14/15] Add a TODO. --- Python/pystate.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/pystate.c b/Python/pystate.c index 1a43315e7e1c7b..6fe947d36466e5 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1332,6 +1332,7 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) } // "Release" the data and/or the object. + // XXX Use _Py_AddPendingCall(). _call_in_interpreter(interp, _release_xidata, data); } From 15fd1234c6a61d2a4093a043490dba967a317577 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 23 Feb 2019 15:13:28 -0800 Subject: [PATCH 15/15] Fix PyEval_ReInitThreads(). --- Python/ceval.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index 0912ae8f044d18..996f87a97842cd 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -240,10 +240,11 @@ PyEval_ReInitThreads(void) if (!gil_created()) return; recreate_gil(); - _PyRuntime.main_thread = PyThread_get_thread_ident(); - // XXX Set for every interpreter. - current_tstate->interp->ceval.pending.lock = PyThread_allocate_lock(); + // This will be reset in make_pending_calls() below. + current_tstate->interp->ceval.pending.lock = NULL; + take_gil(current_tstate); + _PyRuntime.main_thread = PyThread_get_thread_ident(); /* Destroy all threads except the current one */ _PyThreadState_DeleteExcept(current_tstate);