From 3b8cb643e1e304275c3e67407862801c674e1926 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 10 Sep 2018 15:31:30 -0600 Subject: [PATCH 01/18] 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 7b3d3d4c903259..129083a7597fe7 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -134,28 +134,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; @@ -163,13 +147,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 26e09660c10b223398442274b752f9223cd26cd3 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 13 Jul 2018 16:57:36 -0600 Subject: [PATCH 02/18] Move pending calls from _PyRuntimeState to PyIntepreterState. --- Include/internal/ceval.h | 9 +++-- Include/pystate.h | 13 ++++++ Python/ceval.c | 87 +++++++++++++++++++++++----------------- Python/pystate.c | 12 ++++++ 4 files changed, 80 insertions(+), 41 deletions(-) diff --git a/Include/internal/ceval.h b/Include/internal/ceval.h index cdabb9521d1023..f4a975dad8e950 100644 --- a/Include/internal/ceval.h +++ b/Include/internal/ceval.h @@ -7,6 +7,11 @@ extern "C" { #include "pyatomic.h" #include "pythread.h" +struct _is; // See PyInterpreterState in 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; @@ -35,12 +40,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; struct _gil_runtime_state gil; }; diff --git a/Include/pystate.h b/Include/pystate.h index 80ee0d112daade..d74cc1a5d8b54a 100644 --- a/Include/pystate.h +++ b/Include/pystate.h @@ -9,6 +9,10 @@ extern "C" { #include "pythread.h" #include "coreconfig.h" +#ifdef Py_BUILD_CORE +#include "pyatomic.h" +#include "internal/ceval.h" +#endif /* This limitation is for performance and simplicity. If needed it can be removed (with effort). */ @@ -65,6 +69,15 @@ typedef 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 f3a74b00a2b64a..39cbb00b083c0b 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -96,15 +96,15 @@ 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.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() \ @@ -116,25 +116,25 @@ 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_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) @@ -158,9 +158,6 @@ PyEval_InitThreads(void) return; 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 @@ -228,9 +225,11 @@ PyEval_ReInitThreads(void) if (!gil_created()) return; recreate_gil(); - _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(); + // XXX Set for every interpreter. + current_tstate->interp->ceval.pending.main_thread = PyThread_get_thread_ident(); /* Destroy all threads except the current one */ _PyThreadState_DeleteExcept(current_tstate); @@ -312,16 +311,23 @@ _PyEval_SignalReceived(void) SIGNAL_PENDING_CALLS(); } +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 scheduling to be made from any thread, and even from an executing callback. */ int -Py_AddPendingCall(int (*func)(void *), void *arg) +_Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg) { int i, j, result=0; - PyThread_type_lock lock = _PyRuntime.ceval.pending.lock; + PyThread_type_lock lock = interp->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) @@ -343,14 +349,14 @@ Py_AddPendingCall(int (*func)(void *), void *arg) return -1; } - i = _PyRuntime.ceval.pending.last; + i = interp->ceval.pending.last; j = (i + 1) % NPENDINGCALLS; - if (j == _PyRuntime.ceval.pending.first) { + if (j == interp->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; + interp->ceval.pending.calls[i].func = func; + interp->ceval.pending.calls[i].arg = arg; + interp->ceval.pending.last = j; } /* signal main loop */ SIGNAL_PENDING_CALLS(); @@ -361,6 +367,13 @@ Py_AddPendingCall(int (*func)(void *), void *arg) int Py_MakePendingCalls(void) +{ + PyInterpreterState *interp = _PyRuntime.interpreters.main; + return _Py_MakePendingCalls(interp); +} + +int +_Py_MakePendingCalls(PyInterpreterState *interp) { static int busy = 0; int i; @@ -368,16 +381,16 @@ Py_MakePendingCalls(void) assert(PyGILState_Check()); - 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) return -1; } /* only service pending calls on main thread */ - if (_PyRuntime.ceval.pending.main_thread && - PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread) + if (interp->ceval.pending.main_thread && + PyThread_get_thread_ident() != interp->ceval.pending.main_thread) { return 0; } @@ -402,16 +415,16 @@ Py_MakePendingCalls(void) void *arg = NULL; /* pop one item off the queue while holding the lock */ - PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK); - j = _PyRuntime.ceval.pending.first; - if (j == _PyRuntime.ceval.pending.last) { + PyThread_acquire_lock(interp->ceval.pending.lock, WAIT_LOCK); + j = interp->ceval.pending.first; + if (j == interp->ceval.pending.last) { func = NULL; /* Queue empty */ } else { - func = _PyRuntime.ceval.pending.calls[j].func; - arg = _PyRuntime.ceval.pending.calls[j].arg; - _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS; + func = interp->ceval.pending.calls[j].func; + arg = interp->ceval.pending.calls[j].arg; + interp->ceval.pending.first = (j + 1) % NPENDINGCALLS; } - PyThread_release_lock(_PyRuntime.ceval.pending.lock); + PyThread_release_lock(interp->ceval.pending.lock); /* having released the lock, perform the callback */ if (func == NULL) break; @@ -634,7 +647,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; \ @@ -937,7 +950,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 || @@ -962,7 +975,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) goto fast_next_opcode; } if (_Py_atomic_load_relaxed( - &_PyRuntime.ceval.pending.calls_to_do)) + &(tstate->interp->ceval.pending.calls_to_do))) { if (Py_MakePendingCalls() < 0) goto error; diff --git a/Python/pystate.c b/Python/pystate.c index 129083a7597fe7..09eadc601ea24b 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -137,6 +137,15 @@ PyInterpreterState_New(void) memset(interp, 0, sizeof(*interp)); interp->id_refcount = -1; interp->check_interval = 100; + + // XXX Is this the right thread? + interp->ceval.pending.main_thread = PyThread_get_thread_ident(); + 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; @@ -241,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 9ebc65b7815802e2476a8ed22ad4b5c67df6a6b5 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 10 Sep 2018 16:41:27 -0600 Subject: [PATCH 03/18] Explicitly pass the interpreter to the stateful ceval macros. --- Include/ceval.h | 2 +- Python/ceval.c | 56 ++++++++++++++++++++++++---------------------- Python/ceval_gil.h | 8 +++---- Python/pystate.c | 2 +- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Include/ceval.h b/Include/ceval.h index bce8a0beed85e1..a284f6d2cbb39d 100644 --- a/Include/ceval.h +++ b/Include/ceval.h @@ -219,7 +219,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 39cbb00b083c0b..cdfe1de8504ac0 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -94,48 +94,48 @@ 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(&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_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) @@ -239,9 +239,9 @@ PyEval_ReInitThreads(void) raised, therefore it is also useful in non-threaded builds. */ void -_PyEval_SignalAsyncExc(void) +_PyEval_SignalAsyncExc(PyInterpreterState *interp) { - SIGNAL_ASYNC_EXC(); + SIGNAL_ASYNC_EXC(interp); } /* Functions save_thread and restore_thread are always defined so @@ -308,7 +308,9 @@ _PyEval_SignalReceived(void) /* bpo-30703: Function called when the C signal handler of Python gets a signal. We cannot queue a callback using Py_AddPendingCall() since that function is not async-signal-safe. */ - SIGNAL_PENDING_CALLS(); + /* Note that we explicitly use the main interpreter. Signals + are always handled by the main interpreter. */ + SIGNAL_PENDING_CALLS(_PyRuntime.interpreters.main); } int @@ -359,7 +361,7 @@ _Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg) interp->ceval.pending.last = j; } /* signal main loop */ - SIGNAL_PENDING_CALLS(); + SIGNAL_PENDING_CALLS(interp); if (lock != NULL) PyThread_release_lock(lock); return result; @@ -400,7 +402,7 @@ _Py_MakePendingCalls(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); /* Python signal handler doesn't really queue a callback: it only signals that a signal was received, see _PyEval_SignalReceived(). */ @@ -439,7 +441,7 @@ _Py_MakePendingCalls(PyInterpreterState *interp) error: busy = 0; - SIGNAL_PENDING_CALLS(); /* We're not done yet */ + SIGNAL_PENDING_CALLS(interp); /* We're not done yet */ return -1; } @@ -1007,7 +1009,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 ef5189068e0c9b..c14ebd0fd1c0d7 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -175,7 +175,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 @@ -212,7 +212,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: @@ -238,10 +238,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 09eadc601ea24b..ff2e1abf7fe1ec 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -847,7 +847,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 c8ec310f299cc3973005a6e4f58ce2cf444c50b5 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 13 Jul 2018 16:58:27 -0600 Subject: [PATCH 04/18] 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 ff2e1abf7fe1ec..6567692c89b00c 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 43669601a31b7dae9f2d8bbd319eec9310beb678 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 12 Sep 2018 12:24:53 -0600 Subject: [PATCH 05/18] main -> active --- Include/internal/ceval.h | 2 +- Python/ceval.c | 6 +++--- Python/pystate.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Include/internal/ceval.h b/Include/internal/ceval.h index f4a975dad8e950..202caad8874850 100644 --- a/Include/internal/ceval.h +++ b/Include/internal/ceval.h @@ -13,7 +13,7 @@ PyAPI_FUNC(int) _Py_AddPendingCall(struct _is*, int (*)(void *), void *); PyAPI_FUNC(int) _Py_MakePendingCalls(struct _is*); struct _pending_calls { - unsigned long main_thread; + unsigned long active_thread; PyThread_type_lock lock; /* Request for running pending calls. */ _Py_atomic_int calls_to_do; diff --git a/Python/ceval.c b/Python/ceval.c index cdfe1de8504ac0..17fe43380a5de9 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -229,7 +229,7 @@ PyEval_ReInitThreads(void) current_tstate->interp->ceval.pending.lock = PyThread_allocate_lock(); take_gil(current_tstate); // XXX Set for every interpreter. - current_tstate->interp->ceval.pending.main_thread = PyThread_get_thread_ident(); + current_tstate->interp->ceval.pending.active_thread = PyThread_get_thread_ident(); /* Destroy all threads except the current one */ _PyThreadState_DeleteExcept(current_tstate); @@ -391,8 +391,8 @@ _Py_MakePendingCalls(PyInterpreterState *interp) } /* only service pending calls on main thread */ - if (interp->ceval.pending.main_thread && - PyThread_get_thread_ident() != interp->ceval.pending.main_thread) + if (interp->ceval.pending.active_thread && + PyThread_get_thread_ident() != interp->ceval.pending.active_thread) { return 0; } diff --git a/Python/pystate.c b/Python/pystate.c index 6567692c89b00c..f4181bcad896d7 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -139,7 +139,7 @@ PyInterpreterState_New(void) interp->check_interval = 100; // XXX Is this the right thread? - interp->ceval.pending.main_thread = PyThread_get_thread_ident(); + interp->ceval.pending.active_thread = PyThread_get_thread_ident(); interp->ceval.pending.lock = PyThread_allocate_lock(); if (interp->ceval.pending.lock == NULL) { PyErr_SetString(PyExc_RuntimeError, From 10a46ce02e9209ba54327b15ff8dd16c86cfc5da Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 12 Sep 2018 12:30:32 -0600 Subject: [PATCH 06/18] Move "active_thread" up to PyInterpreterState.ceval. --- Include/internal/ceval.h | 1 - Include/pystate.h | 2 ++ Python/ceval.c | 8 +++----- Python/pystate.c | 3 +-- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Include/internal/ceval.h b/Include/internal/ceval.h index 202caad8874850..f806a5ca067cbe 100644 --- a/Include/internal/ceval.h +++ b/Include/internal/ceval.h @@ -13,7 +13,6 @@ PyAPI_FUNC(int) _Py_AddPendingCall(struct _is*, int (*)(void *), void *); PyAPI_FUNC(int) _Py_MakePendingCalls(struct _is*); struct _pending_calls { - unsigned long active_thread; PyThread_type_lock lock; /* Request for running pending calls. */ _Py_atomic_int calls_to_do; diff --git a/Include/pystate.h b/Include/pystate.h index d74cc1a5d8b54a..c96cbf5379e8ff 100644 --- a/Include/pystate.h +++ b/Include/pystate.h @@ -75,6 +75,8 @@ typedef struct _is { the fast path in the eval loop. */ _Py_atomic_int eval_breaker; struct _pending_calls pending; + // XXX Is "active" the right word? + unsigned long active_thread; } ceval; #endif diff --git a/Python/ceval.c b/Python/ceval.c index 17fe43380a5de9..841dc37821bffb 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -225,11 +225,9 @@ PyEval_ReInitThreads(void) if (!gil_created()) return; recreate_gil(); - // XXX Set for every interpreter. current_tstate->interp->ceval.pending.lock = PyThread_allocate_lock(); take_gil(current_tstate); - // XXX Set for every interpreter. - current_tstate->interp->ceval.pending.active_thread = PyThread_get_thread_ident(); + current_tstate->interp->ceval.active_thread = PyThread_get_thread_ident(); /* Destroy all threads except the current one */ _PyThreadState_DeleteExcept(current_tstate); @@ -391,8 +389,8 @@ _Py_MakePendingCalls(PyInterpreterState *interp) } /* only service pending calls on main thread */ - if (interp->ceval.pending.active_thread && - PyThread_get_thread_ident() != interp->ceval.pending.active_thread) + if (interp->ceval.active_thread && + PyThread_get_thread_ident() != interp->ceval.active_thread) { return 0; } diff --git a/Python/pystate.c b/Python/pystate.c index f4181bcad896d7..cc41559a89f0f6 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -138,8 +138,7 @@ PyInterpreterState_New(void) interp->id_refcount = -1; interp->check_interval = 100; - // XXX Is this the right thread? - interp->ceval.pending.active_thread = PyThread_get_thread_ident(); + interp->ceval.active_thread = PyThread_get_thread_ident(); interp->ceval.pending.lock = PyThread_allocate_lock(); if (interp->ceval.pending.lock == NULL) { PyErr_SetString(PyExc_RuntimeError, From 47fb2543629c6865de9c4dc2252dd743f9a7466f Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 14 Sep 2018 15:12:58 -0700 Subject: [PATCH 07/18] Add a flag to track if an interpreter is active. --- Include/pystate.h | 1 + Python/ceval.c | 1 + Python/pystate.c | 1 + 3 files changed, 3 insertions(+) diff --git a/Include/pystate.h b/Include/pystate.h index c96cbf5379e8ff..46e59a229167a6 100644 --- a/Include/pystate.h +++ b/Include/pystate.h @@ -76,6 +76,7 @@ typedef struct _is { _Py_atomic_int eval_breaker; struct _pending_calls pending; // XXX Is "active" the right word? + int active; unsigned long active_thread; } ceval; #endif diff --git a/Python/ceval.c b/Python/ceval.c index 841dc37821bffb..222026adcf25a7 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -227,6 +227,7 @@ PyEval_ReInitThreads(void) recreate_gil(); current_tstate->interp->ceval.pending.lock = PyThread_allocate_lock(); take_gil(current_tstate); + current_tstate->interp->ceval.active = 1; current_tstate->interp->ceval.active_thread = PyThread_get_thread_ident(); /* Destroy all threads except the current one */ diff --git a/Python/pystate.c b/Python/pystate.c index cc41559a89f0f6..5fcd626155a06e 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -138,6 +138,7 @@ PyInterpreterState_New(void) interp->id_refcount = -1; interp->check_interval = 100; + interp->ceval.active = 1; interp->ceval.active_thread = PyThread_get_thread_ident(); interp->ceval.pending.lock = PyThread_allocate_lock(); if (interp->ceval.pending.lock == NULL) { From 08102dd731f04646b9665326f2a66300387e5753 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 14 Sep 2018 15:59:24 -0700 Subject: [PATCH 08/18] Tie interpreter activity to the GIL. --- Python/ceval.c | 4 +--- Python/ceval_gil.h | 7 +++++++ Python/pystate.c | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index 222026adcf25a7..5df33f7f3ee437 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -227,8 +227,6 @@ PyEval_ReInitThreads(void) recreate_gil(); current_tstate->interp->ceval.pending.lock = PyThread_allocate_lock(); take_gil(current_tstate); - current_tstate->interp->ceval.active = 1; - current_tstate->interp->ceval.active_thread = PyThread_get_thread_ident(); /* Destroy all threads except the current one */ _PyThreadState_DeleteExcept(current_tstate); @@ -390,7 +388,7 @@ _Py_MakePendingCalls(PyInterpreterState *interp) } /* only service pending calls on main thread */ - if (interp->ceval.active_thread && + if (!interp->ceval.active || PyThread_get_thread_ident() != interp->ceval.active_thread) { return 0; diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h index c14ebd0fd1c0d7..a685cb4a0cf809 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -160,6 +160,9 @@ static void drop_gil(PyThreadState *tstate) } MUTEX_LOCK(_PyRuntime.ceval.gil.mutex); + /* Mark the interpreter as inactive. */ + tstate->interp->ceval.active = 0; + tstate->interp->ceval.active_thread = 0; _Py_ANNOTATE_RWLOCK_RELEASED(&_PyRuntime.ceval.gil.locked, /*is_write=*/1); _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil.locked, 0); COND_SIGNAL(_PyRuntime.ceval.gil.cond); @@ -244,6 +247,10 @@ static void take_gil(PyThreadState *tstate) _PyEval_SignalAsyncExc(tstate->interp); } + /* Mark the interpreter as active. */ + tstate->interp->ceval.active = 1; + tstate->interp->ceval.active_thread = PyThread_get_thread_ident(); + MUTEX_UNLOCK(_PyRuntime.ceval.gil.mutex); errno = err; } diff --git a/Python/pystate.c b/Python/pystate.c index 5fcd626155a06e..d0fa9157466082 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -138,8 +138,8 @@ PyInterpreterState_New(void) interp->id_refcount = -1; interp->check_interval = 100; - interp->ceval.active = 1; - interp->ceval.active_thread = PyThread_get_thread_ident(); + //interp->ceval.active = 1; + //interp->ceval.active_thread = PyThread_get_thread_ident(); interp->ceval.pending.lock = PyThread_allocate_lock(); if (interp->ceval.pending.lock == NULL) { PyErr_SetString(PyExc_RuntimeError, From d84f5f41e1de8efe299a75156bcaa8461984a9e1 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 14 Sep 2018 16:19:41 -0700 Subject: [PATCH 09/18] Do not bother if no tstate provided. --- Python/ceval_gil.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h index a685cb4a0cf809..bc519be737d916 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -160,9 +160,11 @@ static void drop_gil(PyThreadState *tstate) } MUTEX_LOCK(_PyRuntime.ceval.gil.mutex); - /* Mark the interpreter as inactive. */ - tstate->interp->ceval.active = 0; - tstate->interp->ceval.active_thread = 0; + if (tstate != NULL) { + /* Mark the interpreter as inactive. */ + tstate->interp->ceval.active = 0; + tstate->interp->ceval.active_thread = 0; + } _Py_ANNOTATE_RWLOCK_RELEASED(&_PyRuntime.ceval.gil.locked, /*is_write=*/1); _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil.locked, 0); COND_SIGNAL(_PyRuntime.ceval.gil.cond); From 5f068992d844debc638bc235b4dc9bcedc441cc0 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 14 Sep 2018 16:35:49 -0700 Subject: [PATCH 10/18] 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 add642f223afdc..1bda7212da4e34 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2441,6 +2441,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 d1209485827331..444ec40c0d9bc6 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -17,6 +17,7 @@ #include #endif #endif +#include "internal/pystate.h" #ifdef HAVE_SIGNAL_H #include @@ -279,8 +280,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); } } } @@ -297,8 +299,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 93f27066be7a3b1a82994739084f0a62d78e0cde Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 14 Sep 2018 16:37:25 -0700 Subject: [PATCH 11/18] Use _Py_AddPendingCall() for releasing shared data. --- Python/pystate.c | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index d0fa9157466082..ff3d629cb9f65a 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1266,7 +1266,7 @@ _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data) return 0; } -static void +static int _release_xidata(void *arg) { _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg; @@ -1274,30 +1274,7 @@ _release_xidata(void *arg) data->free(data->data); } Py_XDECREF(data->obj); -} - -static void -_call_in_interpreter(PyInterpreterState *interp, - void (*func)(void *), void *arg) -{ - /* We would use Py_AddPendingCall() if it weren't specific to the - * main interpreter (see bpo-33608). In the meantime we take a - * naive approach. - */ - PyThreadState *save_tstate = NULL; - if (interp != _PyInterpreterState_Get()) { - // XXX Using the "head" thread isn't strictly correct. - PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); - // XXX Possible GILState issues? - save_tstate = PyThreadState_Swap(tstate); - } - - func(arg); - - // Switch back. - if (save_tstate != NULL) { - PyThreadState_Swap(save_tstate); - } + return 0; } void @@ -1319,7 +1296,7 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) } // "Release" the data and/or the object. - _call_in_interpreter(interp, _release_xidata, data); + _Py_AddPendingCall(interp, _release_xidata, data); } PyObject * From 6db2288d65fbae2d570337a8785375ed824c106e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 11:16:05 -0600 Subject: [PATCH 12/18] Drop an outdated TODO comment. --- Include/pystate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/pystate.h b/Include/pystate.h index 46e59a229167a6..ed19fc43f464b1 100644 --- a/Include/pystate.h +++ b/Include/pystate.h @@ -75,7 +75,7 @@ typedef struct _is { the fast path in the eval loop. */ _Py_atomic_int eval_breaker; struct _pending_calls pending; - // XXX Is "active" the right word? + int active; unsigned long active_thread; } ceval; From 1ceba050231c1eac0f8ca7bc024829e6a2ea3da8 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 11:22:34 -0600 Subject: [PATCH 13/18] Make any remaining pending calls before shutdown. --- Python/pylifecycle.c | 15 +++++++++++++++ Python/pystate.c | 2 -- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 379e860b5ba4b4..7afa9dba4df614 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1417,6 +1417,21 @@ Py_EndInterpreter(PyThreadState *tstate) Py_FatalError("Py_EndInterpreter: thread still has a frame"); wait_for_thread_shutdown(); + // XXX Forcibly mark current thread as active? + assert(interp->ceval.active); + assert(interp->ceval.active_thread == PyThread_get_thread_ident()); + + if (_Py_atomic_load_relaxed( + &(interp->ceval.pending.calls_to_do))) + { + 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); diff --git a/Python/pystate.c b/Python/pystate.c index ff3d629cb9f65a..09a21232713f17 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 331728d47ab5a9edf2832535235a16b3731f4d0c Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 11:35:59 -0600 Subject: [PATCH 14/18] Forcibly mark the interpreter as active. --- Python/pylifecycle.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 7afa9dba4df614..a8dc475b0dca94 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1417,9 +1417,8 @@ Py_EndInterpreter(PyThreadState *tstate) Py_FatalError("Py_EndInterpreter: thread still has a frame"); wait_for_thread_shutdown(); - // XXX Forcibly mark current thread as active? - assert(interp->ceval.active); - assert(interp->ceval.active_thread == PyThread_get_thread_ident()); + interp->ceval.active = 1; + interp->ceval.active_thread = PyThread_get_thread_ident(); if (_Py_atomic_load_relaxed( &(interp->ceval.pending.calls_to_do))) From 3faea72e1e1d3dc9b575905d07613228f871ec06 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 11:57:29 -0600 Subject: [PATCH 15/18] Prevent adding pending calls if finalizing. --- Include/pystate.h | 2 ++ Python/ceval.c | 12 ++++++++++++ Python/pylifecycle.c | 11 +++++++++++ 3 files changed, 25 insertions(+) diff --git a/Include/pystate.h b/Include/pystate.h index ed19fc43f464b1..8d901198a68166 100644 --- a/Include/pystate.h +++ b/Include/pystate.h @@ -60,6 +60,8 @@ typedef 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 5df33f7f3ee437..0131b4e0167f28 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -348,6 +348,16 @@ _Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg) return -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); + result = -1; + goto done; + } + i = interp->ceval.pending.last; j = (i + 1) % NPENDINGCALLS; if (j == interp->ceval.pending.first) { @@ -359,6 +369,8 @@ _Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *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 a8dc475b0dca94..5b2c0f399a0526 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1416,10 +1416,21 @@ 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(); interp->ceval.active = 1; interp->ceval.active_thread = PyThread_get_thread_ident(); + // Make any pending calls. if (_Py_atomic_load_relaxed( &(interp->ceval.pending.calls_to_do))) { From 9d7c14fe699188cdf9d69de2d5f7118de043c4c2 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 12:14:04 -0600 Subject: [PATCH 16/18] 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 dd5f656f1442f740079a8d2853d8514a691ff0d1 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 12:48:03 -0600 Subject: [PATCH 17/18] Add _PyRuntimeState.main_thread. --- Include/internal/pystate.h | 4 ++++ Modules/signalmodule.c | 4 +--- Python/ceval.c | 1 + Python/pystate.c | 12 ++++++++++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Include/internal/pystate.h b/Include/internal/pystate.h index c93dda28954ae8..d8cd3d14a75825 100644 --- a/Include/internal/pystate.h +++ b/Include/internal/pystate.h @@ -188,6 +188,8 @@ typedef struct pyruntimestate { struct _xidregitem *head; } xidregistry; + unsigned long main_thread; + #define NEXITFUNCS 32 void (*exitfuncs[NEXITFUNCS])(void); int nexitfuncs; @@ -211,6 +213,8 @@ PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *); Return NULL on success, or return an error message on failure. */ PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void); +PyAPI_FUNC(unsigned long) _PyRuntimeState_GetMainThreadID(_PyRuntimeState*); + #define _Py_CURRENTLY_FINALIZING(tstate) \ (_PyRuntime.finalizing == tstate) diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 444ec40c0d9bc6..2e6bfff5a7b300 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -96,7 +96,7 @@ class sigset_t_converter(CConverter): #include /* For pid_t */ #include "pythread.h" -static unsigned long main_thread; +#define main_thread _PyRuntimeState_GetMainThreadID(&_PyRuntime) static pid_t main_pid; static volatile struct { @@ -1281,7 +1281,6 @@ PyInit__signal(void) PyObject *m, *d, *x; int i; - main_thread = PyThread_get_thread_ident(); main_pid = getpid(); /* Create the module and add the functions */ @@ -1683,7 +1682,6 @@ _PySignal_AfterFork(void) * in both processes if they came in just before the fork() but before * the interpreter had an opportunity to call the handlers. issue9535. */ _clear_pending_signals(); - main_thread = PyThread_get_thread_ident(); main_pid = getpid(); } diff --git a/Python/ceval.c b/Python/ceval.c index 0131b4e0167f28..66eaf2c2220643 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -225,6 +225,7 @@ PyEval_ReInitThreads(void) if (!gil_created()) return; recreate_gil(); + _PyRuntime.main_thread = PyThread_get_thread_ident(); current_tstate->interp->ceval.pending.lock = PyThread_allocate_lock(); take_gil(current_tstate); diff --git a/Python/pystate.c b/Python/pystate.c index 09a21232713f17..3c8b9c7aecbe76 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -93,6 +93,12 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime) PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); } +unsigned long +_PyRuntimeState_GetMainThreadID(_PyRuntimeState *runtime) +{ + return runtime->main_thread; +} + #define HEAD_LOCK() PyThread_acquire_lock(_PyRuntime.interpreters.mutex, \ WAIT_LOCK) #define HEAD_UNLOCK() PyThread_release_lock(_PyRuntime.interpreters.mutex) @@ -138,8 +144,6 @@ PyInterpreterState_New(void) interp->id_refcount = -1; interp->check_interval = 100; - //interp->ceval.active = 1; - //interp->ceval.active_thread = PyThread_get_thread_ident(); interp->ceval.pending.lock = PyThread_allocate_lock(); if (interp->ceval.pending.lock == NULL) { PyErr_SetString(PyExc_RuntimeError, @@ -157,6 +161,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 178e2b8d397176edb728b2460d3a4d796210e7ef Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 13:16:34 -0600 Subject: [PATCH 18/18] Optionally lock a pending call to a specific thread. --- Include/internal/ceval.h | 3 ++- Include/pystate.h | 3 --- Modules/signalmodule.c | 2 ++ Python/ceval.c | 18 ++++++++++-------- Python/ceval_gil.h | 9 --------- Python/pylifecycle.c | 2 -- Python/pystate.c | 2 +- 7 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Include/internal/ceval.h b/Include/internal/ceval.h index f806a5ca067cbe..aa0133cb1651f0 100644 --- a/Include/internal/ceval.h +++ b/Include/internal/ceval.h @@ -9,7 +9,7 @@ extern "C" { struct _is; // See PyInterpreterState in 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 { @@ -22,6 +22,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/Include/pystate.h b/Include/pystate.h index 8d901198a68166..04085bbe0a1ec2 100644 --- a/Include/pystate.h +++ b/Include/pystate.h @@ -77,9 +77,6 @@ typedef struct _is { the fast path in the eval loop. */ _Py_atomic_int eval_breaker; struct _pending_calls pending; - - int active; - unsigned long active_thread; } ceval; #endif diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 2e6bfff5a7b300..e869bd0bb35fbd 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -281,6 +281,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); } @@ -300,6 +301,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 66eaf2c2220643..1d3b5e59480497 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -315,7 +315,7 @@ 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 @@ -324,7 +324,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) { int i, j, result=0; PyThread_type_lock lock = interp->ceval.pending.lock; @@ -364,6 +364,7 @@ _Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg) if (j == interp->ceval.pending.first) { result = -1; /* Queue full */ } else { + 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; @@ -400,12 +401,6 @@ _Py_MakePendingCalls(PyInterpreterState *interp) return -1; } - /* only service pending calls on main thread */ - if (!interp->ceval.active || - PyThread_get_thread_ident() != interp->ceval.active_thread) - { - return 0; - } /* don't perform recursive pending calls */ if (busy) return 0; @@ -432,8 +427,15 @@ _Py_MakePendingCalls(PyInterpreterState *interp) if (j == interp->ceval.pending.last) { func = NULL; /* Queue empty */ } else { + unsigned long thread_id = interp->ceval.pending.calls[j].thread_id; func = interp->ceval.pending.calls[j].func; arg = interp->ceval.pending.calls[j].arg; + 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); + func = NULL; + } interp->ceval.pending.first = (j + 1) % NPENDINGCALLS; } PyThread_release_lock(interp->ceval.pending.lock); diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h index bc519be737d916..c14ebd0fd1c0d7 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -160,11 +160,6 @@ static void drop_gil(PyThreadState *tstate) } MUTEX_LOCK(_PyRuntime.ceval.gil.mutex); - if (tstate != NULL) { - /* Mark the interpreter as inactive. */ - tstate->interp->ceval.active = 0; - tstate->interp->ceval.active_thread = 0; - } _Py_ANNOTATE_RWLOCK_RELEASED(&_PyRuntime.ceval.gil.locked, /*is_write=*/1); _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil.locked, 0); COND_SIGNAL(_PyRuntime.ceval.gil.cond); @@ -249,10 +244,6 @@ static void take_gil(PyThreadState *tstate) _PyEval_SignalAsyncExc(tstate->interp); } - /* Mark the interpreter as active. */ - tstate->interp->ceval.active = 1; - tstate->interp->ceval.active_thread = PyThread_get_thread_ident(); - MUTEX_UNLOCK(_PyRuntime.ceval.gil.mutex); errno = err; } diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 5b2c0f399a0526..540d2350d16e88 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1427,8 +1427,6 @@ Py_EndInterpreter(PyThreadState *tstate) // Wrap up existing threads. wait_for_thread_shutdown(); - interp->ceval.active = 1; - interp->ceval.active_thread = PyThread_get_thread_ident(); // Make any pending calls. if (_Py_atomic_load_relaxed( diff --git a/Python/pystate.c b/Python/pystate.c index 3c8b9c7aecbe76..cb322b8ac055ff 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1302,7 +1302,7 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) } // "Release" the data and/or the object. - _Py_AddPendingCall(interp, _release_xidata, data); + _Py_AddPendingCall(interp, 0, _release_xidata, data); } PyObject *