From 59b2d34234c76af3e7aac7bd952f8fc69a1aface Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 31 May 2019 12:04:21 -0600 Subject: [PATCH 01/20] ceval -> ceval_r --- Include/internal/pycore_ceval.h | 8 +- Python/ceval.c | 170 ++++++++++++++++---------------- Python/ceval_gil.h | 20 ++-- 3 files changed, 99 insertions(+), 99 deletions(-) diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index 37170ed438f8bb..7c0e0b2aef9479 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -15,16 +15,16 @@ extern "C" { PyAPI_FUNC(void) _Py_FinishPendingCalls(_PyRuntimeState *runtime); PyAPI_FUNC(void) _PyEval_Initialize(struct _ceval_runtime_state *); PyAPI_FUNC(void) _PyEval_FiniThreads( - struct _ceval_runtime_state *ceval); + struct _ceval_runtime_state *); PyAPI_FUNC(void) _PyEval_SignalReceived( - struct _ceval_runtime_state *ceval); + struct _ceval_runtime_state *); PyAPI_FUNC(int) _PyEval_AddPendingCall( PyThreadState *tstate, - struct _ceval_runtime_state *ceval, + struct _ceval_runtime_state *, int (*func)(void *), void *arg); PyAPI_FUNC(void) _PyEval_SignalAsyncExc( - struct _ceval_runtime_state *ceval); + struct _ceval_runtime_state *); PyAPI_FUNC(void) _PyEval_ReInitThreads( _PyRuntimeState *runtime); diff --git a/Python/ceval.c b/Python/ceval.c index f9ff4e09f17e20..54868bb0fe272d 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -101,66 +101,66 @@ static long dxp[256]; #endif #endif -#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request) +#define GIL_REQUEST _Py_atomic_load_relaxed(&(ceval_r)->gil_drop_request) /* 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(ceval) \ +#define COMPUTE_EVAL_BREAKER(ceval_r) \ _Py_atomic_store_relaxed( \ - &(ceval)->eval_breaker, \ + &(ceval_r)->eval_breaker, \ GIL_REQUEST | \ - _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \ - _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \ - (ceval)->pending.async_exc) + _Py_atomic_load_relaxed(&(ceval_r)->signals_pending) | \ + _Py_atomic_load_relaxed(&(ceval_r)->pending.calls_to_do) | \ + (ceval_r)->pending.async_exc) -#define SET_GIL_DROP_REQUEST(ceval) \ +#define SET_GIL_DROP_REQUEST(ceval_r) \ do { \ - _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \ - _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ + _Py_atomic_store_relaxed(&(ceval_r)->gil_drop_request, 1); \ + _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \ } while (0) -#define RESET_GIL_DROP_REQUEST(ceval) \ +#define RESET_GIL_DROP_REQUEST(ceval_r) \ do { \ - _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \ - COMPUTE_EVAL_BREAKER(ceval); \ + _Py_atomic_store_relaxed(&(ceval_r)->gil_drop_request, 0); \ + COMPUTE_EVAL_BREAKER(ceval_r); \ } while (0) /* Pending calls are only modified under pending_lock */ -#define SIGNAL_PENDING_CALLS(ceval) \ +#define SIGNAL_PENDING_CALLS(ceval_r) \ do { \ - _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \ - _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ + _Py_atomic_store_relaxed(&(ceval_r)->pending.calls_to_do, 1); \ + _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \ } while (0) -#define UNSIGNAL_PENDING_CALLS(ceval) \ +#define UNSIGNAL_PENDING_CALLS(ceval_r) \ do { \ - _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \ - COMPUTE_EVAL_BREAKER(ceval); \ + _Py_atomic_store_relaxed(&(ceval_r)->pending.calls_to_do, 0); \ + COMPUTE_EVAL_BREAKER(ceval_r); \ } while (0) -#define SIGNAL_PENDING_SIGNALS(ceval) \ +#define SIGNAL_PENDING_SIGNALS(ceval_r) \ do { \ - _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \ - _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ + _Py_atomic_store_relaxed(&(ceval_r)->signals_pending, 1); \ + _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \ } while (0) -#define UNSIGNAL_PENDING_SIGNALS(ceval) \ +#define UNSIGNAL_PENDING_SIGNALS(ceval_r) \ do { \ - _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \ - COMPUTE_EVAL_BREAKER(ceval); \ + _Py_atomic_store_relaxed(&(ceval_r)->signals_pending, 0); \ + COMPUTE_EVAL_BREAKER(ceval_r); \ } while (0) -#define SIGNAL_ASYNC_EXC(ceval) \ +#define SIGNAL_ASYNC_EXC(ceval_r) \ do { \ - (ceval)->pending.async_exc = 1; \ - _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \ + (ceval_r)->pending.async_exc = 1; \ + _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \ } while (0) -#define UNSIGNAL_ASYNC_EXC(ceval) \ +#define UNSIGNAL_ASYNC_EXC(ceval_r) \ do { \ - (ceval)->pending.async_exc = 0; \ - COMPUTE_EVAL_BREAKER(ceval); \ + (ceval_r)->pending.async_exc = 0; \ + COMPUTE_EVAL_BREAKER(ceval_r); \ } while (0) @@ -180,8 +180,8 @@ void PyEval_InitThreads(void) { _PyRuntimeState *runtime = &_PyRuntime; - struct _ceval_runtime_state *ceval = &runtime->ceval; - struct _gil_runtime_state *gil = &ceval->gil; + struct _ceval_runtime_state *ceval_r = &runtime->ceval; + struct _gil_runtime_state *gil = &ceval_r->gil; if (gil_created(gil)) { return; } @@ -189,9 +189,9 @@ PyEval_InitThreads(void) PyThread_init_thread(); create_gil(gil); PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); - take_gil(ceval, tstate); + take_gil(ceval_r, tstate); - struct _pending_calls *pending = &ceval->pending; + struct _pending_calls *pending = &ceval_r->pending; pending->lock = PyThread_allocate_lock(); if (pending->lock == NULL) { Py_FatalError("Can't initialize threads for pending calls"); @@ -199,9 +199,9 @@ PyEval_InitThreads(void) } void -_PyEval_FiniThreads(struct _ceval_runtime_state *ceval) +_PyEval_FiniThreads(struct _ceval_runtime_state *ceval_r) { - struct _gil_runtime_state *gil = &ceval->gil; + struct _gil_runtime_state *gil = &ceval_r->gil; if (!gil_created(gil)) { return; } @@ -209,7 +209,7 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval) destroy_gil(gil); assert(!gil_created(gil)); - struct _pending_calls *pending = &ceval->pending; + struct _pending_calls *pending = &ceval_r->pending; if (pending->lock != NULL) { PyThread_free_lock(pending->lock); pending->lock = NULL; @@ -231,12 +231,12 @@ void PyEval_AcquireLock(void) { _PyRuntimeState *runtime = &_PyRuntime; - struct _ceval_runtime_state *ceval = &runtime->ceval; + struct _ceval_runtime_state *ceval_r = &runtime->ceval; PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); if (tstate == NULL) { Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); } - take_gil(ceval, tstate); + take_gil(ceval_r, tstate); exit_thread_if_finalizing(tstate); } @@ -261,11 +261,11 @@ PyEval_AcquireThread(PyThreadState *tstate) assert(tstate->interp != NULL); _PyRuntimeState *runtime = tstate->interp->runtime; - struct _ceval_runtime_state *ceval = &runtime->ceval; + struct _ceval_runtime_state *ceval_r = &runtime->ceval; /* Check someone has called PyEval_InitThreads() to create the lock */ - assert(gil_created(&ceval->gil)); - take_gil(ceval, tstate); + assert(gil_created(&ceval_r->gil)); + take_gil(ceval_r, tstate); exit_thread_if_finalizing(tstate); if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) { Py_FatalError("PyEval_AcquireThread: non-NULL old thread state"); @@ -296,15 +296,15 @@ PyEval_ReleaseThread(PyThreadState *tstate) void _PyEval_ReInitThreads(_PyRuntimeState *runtime) { - struct _ceval_runtime_state *ceval = &runtime->ceval; - if (!gil_created(&ceval->gil)) { + struct _ceval_runtime_state *ceval_r = &runtime->ceval; + if (!gil_created(&ceval_r->gil)) { return; } - recreate_gil(&ceval->gil); + recreate_gil(&ceval_r->gil); PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime); - take_gil(ceval, current_tstate); + take_gil(ceval_r, current_tstate); - struct _pending_calls *pending = &ceval->pending; + struct _pending_calls *pending = &ceval_r->pending; pending->lock = PyThread_allocate_lock(); if (pending->lock == NULL) { Py_FatalError("Can't initialize threads for pending calls"); @@ -318,22 +318,22 @@ _PyEval_ReInitThreads(_PyRuntimeState *runtime) raised. */ void -_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval) +_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval_r) { - SIGNAL_ASYNC_EXC(ceval); + SIGNAL_ASYNC_EXC(ceval_r); } PyThreadState * PyEval_SaveThread(void) { _PyRuntimeState *runtime = &_PyRuntime; - struct _ceval_runtime_state *ceval = &runtime->ceval; + struct _ceval_runtime_state *ceval_r = &runtime->ceval; PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); if (tstate == NULL) { Py_FatalError("PyEval_SaveThread: NULL tstate"); } - assert(gil_created(&ceval->gil)); - drop_gil(ceval, tstate); + assert(gil_created(&ceval_r->gil)); + drop_gil(ceval_r, tstate); return tstate; } @@ -346,11 +346,11 @@ PyEval_RestoreThread(PyThreadState *tstate) assert(tstate->interp != NULL); _PyRuntimeState *runtime = tstate->interp->runtime; - struct _ceval_runtime_state *ceval = &runtime->ceval; - assert(gil_created(&ceval->gil)); + struct _ceval_runtime_state *ceval_r = &runtime->ceval; + assert(gil_created(&ceval_r->gil)); int err = errno; - take_gil(ceval, tstate); + take_gil(ceval_r, tstate); exit_thread_if_finalizing(tstate); errno = err; @@ -381,12 +381,12 @@ PyEval_RestoreThread(PyThreadState *tstate) */ void -_PyEval_SignalReceived(struct _ceval_runtime_state *ceval) +_PyEval_SignalReceived(struct _ceval_runtime_state *ceval_r) { /* 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_SIGNALS(ceval); + SIGNAL_PENDING_SIGNALS(ceval_r); } /* Push one item onto the queue while holding the lock. */ @@ -427,10 +427,10 @@ _pop_pending_call(struct _pending_calls *pending, int _PyEval_AddPendingCall(PyThreadState *tstate, - struct _ceval_runtime_state *ceval, + struct _ceval_runtime_state *ceval_r, int (*func)(void *), void *arg) { - struct _pending_calls *pending = &ceval->pending; + struct _pending_calls *pending = &ceval_r->pending; PyThread_acquire_lock(pending->lock, WAIT_LOCK); if (pending->finishing) { @@ -449,7 +449,7 @@ _PyEval_AddPendingCall(PyThreadState *tstate, PyThread_release_lock(pending->lock); /* signal main loop */ - SIGNAL_PENDING_CALLS(ceval); + SIGNAL_PENDING_CALLS(ceval_r); return result; } @@ -479,10 +479,10 @@ handle_signals(_PyRuntimeState *runtime) return 0; } - struct _ceval_runtime_state *ceval = &runtime->ceval; - UNSIGNAL_PENDING_SIGNALS(ceval); + struct _ceval_runtime_state *ceval_r = &runtime->ceval; + UNSIGNAL_PENDING_SIGNALS(ceval_r); if (_PyErr_CheckSignals() < 0) { - SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */ + SIGNAL_PENDING_SIGNALS(ceval_r); /* We're not done yet */ return -1; } return 0; @@ -503,14 +503,14 @@ make_pending_calls(_PyRuntimeState *runtime) return 0; } busy = 1; - struct _ceval_runtime_state *ceval = &runtime->ceval; + struct _ceval_runtime_state *ceval_r = &runtime->ceval; /* unsignal before starting to call callbacks, so that any callback added in-between re-signals */ - UNSIGNAL_PENDING_CALLS(ceval); + UNSIGNAL_PENDING_CALLS(ceval_r); int res = 0; /* perform a bounded number of calls, in case of recursion */ - struct _pending_calls *pending = &ceval->pending; + struct _pending_calls *pending = &ceval_r->pending; for (int i=0; irecursion_limit = Py_DEFAULT_RECURSION_LIMIT; + ceval_r->recursion_limit = Py_DEFAULT_RECURSION_LIMIT; _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; - _gil_initialize(&state->gil); + _gil_initialize(&ceval_r->gil); } int @@ -612,9 +612,9 @@ Py_GetRecursionLimit(void) void Py_SetRecursionLimit(int new_limit) { - struct _ceval_runtime_state *ceval = &_PyRuntime.ceval; - ceval->recursion_limit = new_limit; - _Py_CheckRecursionLimit = ceval->recursion_limit; + struct _ceval_runtime_state *ceval_r = &_PyRuntime.ceval; + ceval_r->recursion_limit = new_limit; + _Py_CheckRecursionLimit = ceval_r->recursion_limit; } /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() @@ -663,7 +663,7 @@ _Py_CheckRecursiveCall(const char *where) static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause); static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **); -#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible) +#define _Py_TracingPossible(ceval_r) ((ceval_r)->tracing_possible) PyObject * @@ -709,8 +709,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) PyObject *retval = NULL; /* Return value */ _PyRuntimeState * const runtime = &_PyRuntime; PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime); - struct _ceval_runtime_state * const ceval = &runtime->ceval; - _Py_atomic_int * const eval_breaker = &ceval->eval_breaker; + struct _ceval_runtime_state * const ceval_r = &runtime->ceval; + _Py_atomic_int * const eval_breaker = &ceval_r->eval_breaker; PyCodeObject *co; /* when tracing we set things up so that @@ -797,7 +797,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) #ifdef LLTRACE #define FAST_DISPATCH() \ { \ - if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \ + if (!lltrace && !_Py_TracingPossible(ceval_r) && !PyDTrace_LINE_ENABLED()) { \ f->f_lasti = INSTR_OFFSET(); \ NEXTOPARG(); \ goto *opcode_targets[opcode]; \ @@ -807,7 +807,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) #else #define FAST_DISPATCH() \ { \ - if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \ + if (!_Py_TracingPossible(ceval_r) && !PyDTrace_LINE_ENABLED()) { \ f->f_lasti = INSTR_OFFSET(); \ NEXTOPARG(); \ goto *opcode_targets[opcode]; \ @@ -1122,27 +1122,27 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) goto fast_next_opcode; } - if (_Py_atomic_load_relaxed(&ceval->signals_pending)) { + if (_Py_atomic_load_relaxed(&ceval_r->signals_pending)) { if (handle_signals(runtime) != 0) { goto error; } } - if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) { + if (_Py_atomic_load_relaxed(&ceval_r->pending.calls_to_do)) { if (make_pending_calls(runtime) != 0) { goto error; } } - if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) { + if (_Py_atomic_load_relaxed(&ceval_r->gil_drop_request)) { /* Give another thread a chance */ if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) { Py_FatalError("ceval: tstate mix-up"); } - drop_gil(ceval, tstate); + drop_gil(ceval_r, tstate); /* Other threads may run now */ - take_gil(ceval, tstate); + take_gil(ceval_r, tstate); /* Check if we should make a quick exit. */ exit_thread_if_finalizing(tstate); @@ -1155,7 +1155,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) if (tstate->async_exc != NULL) { PyObject *exc = tstate->async_exc; tstate->async_exc = NULL; - UNSIGNAL_ASYNC_EXC(ceval); + UNSIGNAL_ASYNC_EXC(ceval_r); _PyErr_SetNone(tstate, exc); Py_DECREF(exc); goto error; @@ -1170,7 +1170,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) /* line-by-line tracing support */ - if (_Py_TracingPossible(ceval) && + if (_Py_TracingPossible(ceval_r) && tstate->c_tracefunc != NULL && !tstate->tracing) { int err; /* see maybe_call_line_trace diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h index 34d48c990c4479..17425a73f5d07a 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -141,9 +141,9 @@ static void recreate_gil(struct _gil_runtime_state *gil) } static void -drop_gil(struct _ceval_runtime_state *ceval, PyThreadState *tstate) +drop_gil(struct _ceval_runtime_state *ceval_r, PyThreadState *tstate) { - struct _gil_runtime_state *gil = &ceval->gil; + struct _gil_runtime_state *gil = &ceval_r->gil; if (!_Py_atomic_load_relaxed(&gil->locked)) { Py_FatalError("drop_gil: GIL is not locked"); } @@ -163,12 +163,12 @@ drop_gil(struct _ceval_runtime_state *ceval, PyThreadState *tstate) MUTEX_UNLOCK(gil->mutex); #ifdef FORCE_SWITCHING - if (_Py_atomic_load_relaxed(&ceval->gil_drop_request) && tstate != NULL) { + if (_Py_atomic_load_relaxed(&ceval_r->gil_drop_request) && tstate != NULL) { MUTEX_LOCK(gil->switch_mutex); /* Not switched yet => wait */ if (((PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) == tstate) { - RESET_GIL_DROP_REQUEST(ceval); + RESET_GIL_DROP_REQUEST(ceval_r); /* 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 @@ -181,13 +181,13 @@ drop_gil(struct _ceval_runtime_state *ceval, PyThreadState *tstate) } static void -take_gil(struct _ceval_runtime_state *ceval, PyThreadState *tstate) +take_gil(struct _ceval_runtime_state *ceval_r, PyThreadState *tstate) { if (tstate == NULL) { Py_FatalError("take_gil: NULL tstate"); } - struct _gil_runtime_state *gil = &ceval->gil; + struct _gil_runtime_state *gil = &ceval_r->gil; int err = errno; MUTEX_LOCK(gil->mutex); @@ -210,7 +210,7 @@ take_gil(struct _ceval_runtime_state *ceval, PyThreadState *tstate) _Py_atomic_load_relaxed(&gil->locked) && gil->switch_number == saved_switchnum) { - SET_GIL_DROP_REQUEST(ceval); + SET_GIL_DROP_REQUEST(ceval_r); } } _ready: @@ -232,11 +232,11 @@ take_gil(struct _ceval_runtime_state *ceval, PyThreadState *tstate) COND_SIGNAL(gil->switch_cond); MUTEX_UNLOCK(gil->switch_mutex); #endif - if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) { - RESET_GIL_DROP_REQUEST(ceval); + if (_Py_atomic_load_relaxed(&ceval_r->gil_drop_request)) { + RESET_GIL_DROP_REQUEST(ceval_r); } if (tstate->async_exc != NULL) { - _PyEval_SignalAsyncExc(ceval); + _PyEval_SignalAsyncExc(ceval_r); } MUTEX_UNLOCK(gil->mutex); From ceceab7c5346710da2ad4454879e00d84799d0d3 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 31 May 2019 13:15:10 -0600 Subject: [PATCH 02/20] _Py_FinishPendingCalls -> _PyEval_FinishPendingCalls --- Include/internal/pycore_ceval.h | 2 +- Python/ceval.c | 2 +- Python/pylifecycle.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index 7c0e0b2aef9479..ff9b33fe99b113 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -12,7 +12,6 @@ extern "C" { #include "pycore_pystate.h" #include "pythread.h" -PyAPI_FUNC(void) _Py_FinishPendingCalls(_PyRuntimeState *runtime); PyAPI_FUNC(void) _PyEval_Initialize(struct _ceval_runtime_state *); PyAPI_FUNC(void) _PyEval_FiniThreads( struct _ceval_runtime_state *); @@ -23,6 +22,7 @@ PyAPI_FUNC(int) _PyEval_AddPendingCall( struct _ceval_runtime_state *, int (*func)(void *), void *arg); +PyAPI_FUNC(void) _PyEval_FinishPendingCalls(_PyRuntimeState *); PyAPI_FUNC(void) _PyEval_SignalAsyncExc( struct _ceval_runtime_state *); PyAPI_FUNC(void) _PyEval_ReInitThreads( diff --git a/Python/ceval.c b/Python/ceval.c index 54868bb0fe272d..3f801c1eed0d17 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -540,7 +540,7 @@ make_pending_calls(_PyRuntimeState *runtime) } void -_Py_FinishPendingCalls(_PyRuntimeState *runtime) +_PyEval_FinishPendingCalls(_PyRuntimeState *runtime) { assert(PyGILState_Check()); diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 6590ef8e9a27a0..a8976ed0c4ab81 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1151,7 +1151,7 @@ Py_FinalizeEx(void) wait_for_thread_shutdown(); // Make any remaining pending calls. - _Py_FinishPendingCalls(runtime); + _PyEval_FinishPendingCalls(runtime); /* Get current thread state and interpreter pointer */ PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); From 3fbc8796cbf4973c3a0eb8541de76950ffb05bfb Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 31 May 2019 13:53:55 -0600 Subject: [PATCH 03/20] Drop the GIL_REQUEST macro. --- Python/ceval.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index 3f801c1eed0d17..9d7e44fab8b4ba 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -101,15 +101,13 @@ static long dxp[256]; #endif #endif -#define GIL_REQUEST _Py_atomic_load_relaxed(&(ceval_r)->gil_drop_request) - /* 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(ceval_r) \ _Py_atomic_store_relaxed( \ &(ceval_r)->eval_breaker, \ - GIL_REQUEST | \ + _Py_atomic_load_relaxed(&(ceval_r)->gil_drop_request) | \ _Py_atomic_load_relaxed(&(ceval_r)->signals_pending) | \ _Py_atomic_load_relaxed(&(ceval_r)->pending.calls_to_do) | \ (ceval_r)->pending.async_exc) From dedcf7f23d9e19544d801624498b3d34cf8158c7 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 13 Jul 2018 16:57:36 -0600 Subject: [PATCH 04/20] Move pending calls from _PyRuntimeState to PyIntepreterState. --- Include/internal/pycore_ceval.h | 6 +- Include/internal/pycore_pystate.h | 11 ++- Modules/signalmodule.c | 8 +- Python/ceval.c | 120 ++++++++++++++++-------------- Python/ceval_gil.h | 14 ++-- Python/pylifecycle.c | 13 ++-- Python/pystate.c | 13 +++- 7 files changed, 112 insertions(+), 73 deletions(-) diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index ff9b33fe99b113..8528a698c3c766 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -20,11 +20,13 @@ PyAPI_FUNC(void) _PyEval_SignalReceived( PyAPI_FUNC(int) _PyEval_AddPendingCall( PyThreadState *tstate, struct _ceval_runtime_state *, + struct _ceval_interpreter_state *, int (*func)(void *), void *arg); -PyAPI_FUNC(void) _PyEval_FinishPendingCalls(_PyRuntimeState *); +PyAPI_FUNC(void) _PyEval_FinishPendingCalls(PyInterpreterState *); PyAPI_FUNC(void) _PyEval_SignalAsyncExc( - struct _ceval_runtime_state *); + struct _ceval_runtime_state *, + struct _ceval_interpreter_state *); PyAPI_FUNC(void) _PyEval_ReInitThreads( _PyRuntimeState *runtime); diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 520a74b8a61fde..e7fe3890a37712 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -15,6 +15,7 @@ extern "C" { #include "sysmodule.h" #include "pycore_gil.h" /* _gil_runtime_state */ +//#include "pycore_atomic.h" #include "pycore_pathconfig.h" #include "pycore_pymem.h" #include "pycore_warnings.h" @@ -53,15 +54,21 @@ struct _ceval_runtime_state { int tracing_possible; /* This single variable consolidates all requests to break out of the fast path in the eval loop. */ + // XXX This can move to _ceval_interpreter_state once all parts + // from COMPUTE_EVAL_BREAKER have moved under PyInterpreterState. _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; }; +struct _ceval_interpreter_state { + struct _pending_calls pending; +}; + + /* interpreter state */ typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int); @@ -89,6 +96,8 @@ struct _is { /* Used in Python/sysmodule.c. */ int check_interval; + struct _ceval_interpreter_state ceval; + /* Used in Modules/_threadmodule.c. */ long num_threads; /* Support for runtime thread stack size tuning. diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 7698984ff3afe1..3f26b9491404b3 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -299,7 +299,9 @@ trip_signal(int sig_num) { /* Py_AddPendingCall() isn't signal-safe, but we still use it for this exceptional case. */ - _PyEval_AddPendingCall(tstate, &runtime->ceval, + _PyEval_AddPendingCall(tstate, + &runtime->ceval, + &tstate->interp->ceval, report_wakeup_send_error, (void *)(intptr_t) last_error); } @@ -318,7 +320,9 @@ trip_signal(int sig_num) { /* Py_AddPendingCall() isn't signal-safe, but we still use it for this exceptional case. */ - _PyEval_AddPendingCall(tstate, &runtime->ceval, + _PyEval_AddPendingCall(tstate, + &runtime->ceval, + &tstate->interp->ceval, report_wakeup_write_error, (void *)(intptr_t)errno); } diff --git a/Python/ceval.c b/Python/ceval.c index 9d7e44fab8b4ba..a1bb8e2d82d459 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -104,13 +104,13 @@ 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(ceval_r) \ +#define COMPUTE_EVAL_BREAKER(ceval_r, ceval_i) \ _Py_atomic_store_relaxed( \ &(ceval_r)->eval_breaker, \ _Py_atomic_load_relaxed(&(ceval_r)->gil_drop_request) | \ _Py_atomic_load_relaxed(&(ceval_r)->signals_pending) | \ - _Py_atomic_load_relaxed(&(ceval_r)->pending.calls_to_do) | \ - (ceval_r)->pending.async_exc) + _Py_atomic_load_relaxed(&(ceval_i)->pending.calls_to_do) | \ + (ceval_i)->pending.async_exc) #define SET_GIL_DROP_REQUEST(ceval_r) \ do { \ @@ -118,23 +118,23 @@ static long dxp[256]; _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \ } while (0) -#define RESET_GIL_DROP_REQUEST(ceval_r) \ +#define RESET_GIL_DROP_REQUEST(ceval_r, ceval_i) \ do { \ _Py_atomic_store_relaxed(&(ceval_r)->gil_drop_request, 0); \ - COMPUTE_EVAL_BREAKER(ceval_r); \ + COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \ } while (0) /* Pending calls are only modified under pending_lock */ -#define SIGNAL_PENDING_CALLS(ceval_r) \ +#define SIGNAL_PENDING_CALLS(ceval_r, ceval_i) \ do { \ - _Py_atomic_store_relaxed(&(ceval_r)->pending.calls_to_do, 1); \ + _Py_atomic_store_relaxed(&(ceval_i)->pending.calls_to_do, 1); \ _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \ } while (0) -#define UNSIGNAL_PENDING_CALLS(ceval_r) \ +#define UNSIGNAL_PENDING_CALLS(ceval_r, ceval_i) \ do { \ - _Py_atomic_store_relaxed(&(ceval_r)->pending.calls_to_do, 0); \ - COMPUTE_EVAL_BREAKER(ceval_r); \ + _Py_atomic_store_relaxed(&(ceval_i)->pending.calls_to_do, 0); \ + COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \ } while (0) #define SIGNAL_PENDING_SIGNALS(ceval_r) \ @@ -143,22 +143,22 @@ static long dxp[256]; _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \ } while (0) -#define UNSIGNAL_PENDING_SIGNALS(ceval_r) \ +#define UNSIGNAL_PENDING_SIGNALS(ceval_r, ceval_i) \ do { \ _Py_atomic_store_relaxed(&(ceval_r)->signals_pending, 0); \ - COMPUTE_EVAL_BREAKER(ceval_r); \ + COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \ } while (0) -#define SIGNAL_ASYNC_EXC(ceval_r) \ +#define SIGNAL_ASYNC_EXC(ceval_r, ceval_i) \ do { \ - (ceval_r)->pending.async_exc = 1; \ + (ceval_i)->pending.async_exc = 1; \ _Py_atomic_store_relaxed(&(ceval_r)->eval_breaker, 1); \ } while (0) -#define UNSIGNAL_ASYNC_EXC(ceval_r) \ +#define UNSIGNAL_ASYNC_EXC(ceval_r, ceval_i) \ do { \ - (ceval_r)->pending.async_exc = 0; \ - COMPUTE_EVAL_BREAKER(ceval_r); \ + (ceval_i)->pending.async_exc = 0; \ + COMPUTE_EVAL_BREAKER(ceval_r, ceval_i); \ } while (0) @@ -189,11 +189,7 @@ PyEval_InitThreads(void) PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); take_gil(ceval_r, tstate); - struct _pending_calls *pending = &ceval_r->pending; - pending->lock = PyThread_allocate_lock(); - if (pending->lock == NULL) { - Py_FatalError("Can't initialize threads for pending calls"); - } + // The pending calls mutex is initialized in PyInterpreterState_New(). } void @@ -207,11 +203,7 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval_r) destroy_gil(gil); assert(!gil_created(gil)); - struct _pending_calls *pending = &ceval_r->pending; - if (pending->lock != NULL) { - PyThread_free_lock(pending->lock); - pending->lock = NULL; - } + // The pending calls mutex is freed in PyInterpreterState_Delete(). } static inline void @@ -220,7 +212,7 @@ exit_thread_if_finalizing(PyThreadState *tstate) _PyRuntimeState *runtime = tstate->interp->runtime; /* _Py_Finalizing is protected by the GIL */ if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) { - drop_gil(&runtime->ceval, tstate); + drop_gil(&runtime->ceval, &tstate->interp->ceval, tstate); PyThread_exit_thread(); } } @@ -247,7 +239,11 @@ PyEval_ReleaseLock(void) We therefore avoid PyThreadState_Get() which dumps a fatal error in debug mode. */ - drop_gil(&runtime->ceval, tstate); + PyInterpreterState *interp = runtime->interpreters.main; + if (tstate != NULL) { + interp = tstate->interp; + } + drop_gil(&runtime->ceval, &interp->ceval, tstate); } void @@ -283,7 +279,7 @@ PyEval_ReleaseThread(PyThreadState *tstate) if (new_tstate != tstate) { Py_FatalError("PyEval_ReleaseThread: wrong thread state"); } - drop_gil(&runtime->ceval, tstate); + drop_gil(&runtime->ceval, &tstate->interp->ceval, tstate); } /* This function is called from PyOS_AfterFork_Child to destroy all threads @@ -302,7 +298,9 @@ _PyEval_ReInitThreads(_PyRuntimeState *runtime) PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime); take_gil(ceval_r, current_tstate); - struct _pending_calls *pending = &ceval_r->pending; + // Only the main interpreter remains, so ignore the rest. + PyInterpreterState *interp = _PyRuntime.interpreters.main; + struct _pending_calls *pending = &interp->ceval.pending; pending->lock = PyThread_allocate_lock(); if (pending->lock == NULL) { Py_FatalError("Can't initialize threads for pending calls"); @@ -316,9 +314,10 @@ _PyEval_ReInitThreads(_PyRuntimeState *runtime) raised. */ void -_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval_r) +_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval_r, + struct _ceval_interpreter_state *ceval_i) { - SIGNAL_ASYNC_EXC(ceval_r); + SIGNAL_ASYNC_EXC(ceval_r, ceval_i); } PyThreadState * @@ -331,7 +330,7 @@ PyEval_SaveThread(void) Py_FatalError("PyEval_SaveThread: NULL tstate"); } assert(gil_created(&ceval_r->gil)); - drop_gil(ceval_r, tstate); + drop_gil(ceval_r, &tstate->interp->ceval, tstate); return tstate; } @@ -426,9 +425,10 @@ _pop_pending_call(struct _pending_calls *pending, int _PyEval_AddPendingCall(PyThreadState *tstate, struct _ceval_runtime_state *ceval_r, + struct _ceval_interpreter_state *ceval_i, int (*func)(void *), void *arg) { - struct _pending_calls *pending = &ceval_r->pending; + struct _pending_calls *pending = &ceval_i->pending; PyThread_acquire_lock(pending->lock, WAIT_LOCK); if (pending->finishing) { @@ -446,17 +446,22 @@ _PyEval_AddPendingCall(PyThreadState *tstate, int result = _push_pending_call(pending, func, arg); PyThread_release_lock(pending->lock); - /* signal main loop */ - SIGNAL_PENDING_CALLS(ceval_r); + /* signal loop */ + SIGNAL_PENDING_CALLS(ceval_r, ceval_i); return result; } +/* Py_AddPendingCall() is a simple wrapper for the sake + of backward-compatibility. */ int Py_AddPendingCall(int (*func)(void *), void *arg) { _PyRuntimeState *runtime = &_PyRuntime; + PyInterpreterState *interp = runtime->interpreters.main; PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); - return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg); + return _PyEval_AddPendingCall(tstate, + &runtime->ceval, &interp->ceval, + func, arg); } static int @@ -478,7 +483,8 @@ handle_signals(_PyRuntimeState *runtime) } struct _ceval_runtime_state *ceval_r = &runtime->ceval; - UNSIGNAL_PENDING_SIGNALS(ceval_r); + struct _ceval_interpreter_state *ceval_i = &interp->ceval; + UNSIGNAL_PENDING_SIGNALS(ceval_r, ceval_i); if (_PyErr_CheckSignals() < 0) { SIGNAL_PENDING_SIGNALS(ceval_r); /* We're not done yet */ return -1; @@ -487,28 +493,25 @@ handle_signals(_PyRuntimeState *runtime) } static int -make_pending_calls(_PyRuntimeState *runtime) +make_pending_calls(PyInterpreterState *interp) { + _PyRuntimeState *runtime = interp->runtime; static int busy = 0; - /* only service pending calls on main thread */ - if (PyThread_get_thread_ident() != runtime->main_thread) { - return 0; - } - /* don't perform recursive pending calls */ if (busy) { return 0; } busy = 1; struct _ceval_runtime_state *ceval_r = &runtime->ceval; + struct _ceval_interpreter_state *ceval_i = &interp->ceval; /* unsignal before starting to call callbacks, so that any callback added in-between re-signals */ - UNSIGNAL_PENDING_CALLS(ceval_r); + UNSIGNAL_PENDING_CALLS(ceval_r, ceval_i); int res = 0; /* perform a bounded number of calls, in case of recursion */ - struct _pending_calls *pending = &ceval_r->pending; + struct _pending_calls *pending = &ceval_i->pending; for (int i=0; iceval.pending; + struct _pending_calls *pending = &interp->ceval.pending; PyThread_acquire_lock(pending->lock, WAIT_LOCK); pending->finishing = 1; @@ -553,7 +555,9 @@ _PyEval_FinishPendingCalls(_PyRuntimeState *runtime) return; } - if (make_pending_calls(runtime) < 0) { + if (make_pending_calls(interp) < 0) { + _PyRuntimeState *runtime = interp->runtime; + PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); PyObject *exc, *val, *tb; _PyErr_Fetch(tstate, &exc, &val, &tb); PyErr_BadInternalCall(); @@ -577,7 +581,8 @@ Py_MakePendingCalls(void) return res; } - res = make_pending_calls(runtime); + PyInterpreterState *interp = _PyRuntime.interpreters.main; + res = make_pending_calls(interp); if (res != 0) { return res; } @@ -708,6 +713,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) _PyRuntimeState * const runtime = &_PyRuntime; PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime); struct _ceval_runtime_state * const ceval_r = &runtime->ceval; + struct _ceval_interpreter_state * const ceval_i = &tstate->interp->ceval; _Py_atomic_int * const eval_breaker = &ceval_r->eval_breaker; PyCodeObject *co; @@ -1125,8 +1131,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) goto error; } } - if (_Py_atomic_load_relaxed(&ceval_r->pending.calls_to_do)) { - if (make_pending_calls(runtime) != 0) { + if (_Py_atomic_load_relaxed(&ceval_i->pending.calls_to_do)) { + if (make_pending_calls(tstate->interp) != 0) { goto error; } } @@ -1136,7 +1142,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) { Py_FatalError("ceval: tstate mix-up"); } - drop_gil(ceval_r, tstate); + drop_gil(ceval_r, ceval_i, tstate); /* Other threads may run now */ @@ -1153,7 +1159,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) if (tstate->async_exc != NULL) { PyObject *exc = tstate->async_exc; tstate->async_exc = NULL; - UNSIGNAL_ASYNC_EXC(ceval_r); + UNSIGNAL_ASYNC_EXC(ceval_r, ceval_i); _PyErr_SetNone(tstate, exc); Py_DECREF(exc); goto error; diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h index 17425a73f5d07a..b62cc660a44f9f 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -141,7 +141,9 @@ static void recreate_gil(struct _gil_runtime_state *gil) } static void -drop_gil(struct _ceval_runtime_state *ceval_r, PyThreadState *tstate) +drop_gil(struct _ceval_runtime_state *ceval_r, + struct _ceval_interpreter_state *ceval_i, + PyThreadState *tstate) { struct _gil_runtime_state *gil = &ceval_r->gil; if (!_Py_atomic_load_relaxed(&gil->locked)) { @@ -168,7 +170,7 @@ drop_gil(struct _ceval_runtime_state *ceval_r, PyThreadState *tstate) /* Not switched yet => wait */ if (((PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) == tstate) { - RESET_GIL_DROP_REQUEST(ceval_r); + RESET_GIL_DROP_REQUEST(ceval_r, ceval_i); /* 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 @@ -181,11 +183,13 @@ drop_gil(struct _ceval_runtime_state *ceval_r, PyThreadState *tstate) } static void -take_gil(struct _ceval_runtime_state *ceval_r, PyThreadState *tstate) +take_gil(struct _ceval_runtime_state *ceval_r, + PyThreadState *tstate) { if (tstate == NULL) { Py_FatalError("take_gil: NULL tstate"); } + struct _ceval_interpreter_state *ceval_i = &tstate->interp->ceval; struct _gil_runtime_state *gil = &ceval_r->gil; int err = errno; @@ -233,10 +237,10 @@ take_gil(struct _ceval_runtime_state *ceval_r, PyThreadState *tstate) MUTEX_UNLOCK(gil->switch_mutex); #endif if (_Py_atomic_load_relaxed(&ceval_r->gil_drop_request)) { - RESET_GIL_DROP_REQUEST(ceval_r); + RESET_GIL_DROP_REQUEST(ceval_r, ceval_i); } if (tstate->async_exc != NULL) { - _PyEval_SignalAsyncExc(ceval_r); + _PyEval_SignalAsyncExc(ceval_r, ceval_i); } MUTEX_UNLOCK(gil->mutex); diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index a8976ed0c4ab81..a405c46d93f28f 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1147,15 +1147,15 @@ Py_FinalizeEx(void) return status; } + /* Get current thread state and interpreter pointer */ + PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); + PyInterpreterState *interp = tstate->interp; + // Wrap up existing "threading"-module-created, non-daemon threads. wait_for_thread_shutdown(); // Make any remaining pending calls. - _PyEval_FinishPendingCalls(runtime); - - /* Get current thread state and interpreter pointer */ - PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); - PyInterpreterState *interp = tstate->interp; + _PyEval_FinishPendingCalls(interp); /* The interpreter is still entirely intact at this point, and the * exit funcs may be relying on that. In particular, if some thread @@ -1580,6 +1580,9 @@ Py_EndInterpreter(PyThreadState *tstate) // Wrap up existing "threading"-module-created, non-daemon threads. wait_for_thread_shutdown(); + // Make any remaining pending calls. + _PyEval_FinishPendingCalls(interp); + call_py_exitfuncs(interp); if (tstate != interp->tstate_head || tstate->next != NULL) diff --git a/Python/pystate.c b/Python/pystate.c index 2b7db0e48debe9..6ff7e233a3f2f9 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -218,6 +218,13 @@ PyInterpreterState_New(void) return NULL; } + 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->eval_frame = _PyEval_EvalFrameDefault; #ifdef HAVE_DLOPEN #if HAVE_DECL_RTLD_NOW @@ -345,6 +352,10 @@ 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); + interp->ceval.pending.lock = NULL; + } PyMem_RawFree(interp); } @@ -1014,7 +1025,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) p->async_exc = exc; HEAD_UNLOCK(runtime); Py_XDECREF(old_exc); - _PyEval_SignalAsyncExc(&runtime->ceval); + _PyEval_SignalAsyncExc(&runtime->ceval, &interp->ceval); return 1; } } From b2c8a7b52715b74fe905a33a2dc4c2d1a038e9c8 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 15 Mar 2019 13:46:25 -0600 Subject: [PATCH 05/20] Release the pending calls lock *after* updating the eval breaker flag. --- Python/ceval.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index a1bb8e2d82d459..10115d1a352fda 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -444,10 +444,11 @@ _PyEval_AddPendingCall(PyThreadState *tstate, return -1; } int result = _push_pending_call(pending, func, arg); - PyThread_release_lock(pending->lock); /* signal loop */ SIGNAL_PENDING_CALLS(ceval_r, ceval_i); + PyThread_release_lock(pending->lock); + return result; } From 05e2808f28ead6ba3c7d931fbd0e8c5ddd06856d Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 14 Sep 2018 16:35:49 -0700 Subject: [PATCH 06/20] Use the internal function. --- Modules/_testcapimodule.c | 1 + Modules/signalmodule.c | 1 + 2 files changed, 2 insertions(+) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index b42f41cc8d8fd7..bf20e81a4ce888 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2677,6 +2677,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 3f26b9491404b3..dff66265fd0cfe 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -21,6 +21,7 @@ #include #endif #endif +#include "internal/pycore_pystate.h" #ifdef HAVE_SIGNAL_H #include From d9d70e8974f5b07da1b184f24d39950f06336af7 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 12:14:04 -0600 Subject: [PATCH 07/20] 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 b02a36b916fe397257690b24c49c0ddbd0557f75 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 15 Sep 2018 13:16:34 -0600 Subject: [PATCH 08/20] Optionally lock a pending call to a specific thread. --- Include/internal/pycore_ceval.h | 1 + Include/internal/pycore_pystate.h | 1 + Modules/signalmodule.c | 2 ++ Python/ceval.c | 24 ++++++++++++++++++++---- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index 8528a698c3c766..d44afdf4fa46a5 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -21,6 +21,7 @@ PyAPI_FUNC(int) _PyEval_AddPendingCall( PyThreadState *tstate, struct _ceval_runtime_state *, struct _ceval_interpreter_state *, + unsigned long thread_id, int (*func)(void *), void *arg); PyAPI_FUNC(void) _PyEval_FinishPendingCalls(PyInterpreterState *); diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index e7fe3890a37712..b2542ad2250e6c 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -37,6 +37,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 dff66265fd0cfe..9f5ff077476a5d 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -303,6 +303,7 @@ trip_signal(int sig_num) _PyEval_AddPendingCall(tstate, &runtime->ceval, &tstate->interp->ceval, + runtime->main_thread, report_wakeup_send_error, (void *)(intptr_t) last_error); } @@ -324,6 +325,7 @@ trip_signal(int sig_num) _PyEval_AddPendingCall(tstate, &runtime->ceval, &tstate->interp->ceval, + runtime->main_thread, report_wakeup_write_error, (void *)(intptr_t)errno); } diff --git a/Python/ceval.c b/Python/ceval.c index 10115d1a352fda..02c1eb9f1e5f8d 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -388,7 +388,7 @@ _PyEval_SignalReceived(struct _ceval_runtime_state *ceval_r) /* Push one item onto the queue while holding the lock. */ static int -_push_pending_call(struct _pending_calls *pending, +_push_pending_call(struct _pending_calls *pending, unsigned long thread_id, int (*func)(void *), void *arg) { int i = pending->last; @@ -396,6 +396,7 @@ _push_pending_call(struct _pending_calls *pending, if (j == pending->first) { return -1; /* Queue full */ } + pending->calls[i].thread_id = thread_id; pending->calls[i].func = func; pending->calls[i].arg = arg; pending->last = j; @@ -404,7 +405,7 @@ _push_pending_call(struct _pending_calls *pending, /* Pop one item off the queue while holding the lock. */ static void -_pop_pending_call(struct _pending_calls *pending, +_pop_pending_call(struct _pending_calls *pending, unsigned long *thread_id, int (**func)(void *), void **arg) { int i = pending->first; @@ -414,6 +415,7 @@ _pop_pending_call(struct _pending_calls *pending, *func = pending->calls[i].func; *arg = pending->calls[i].arg; + *thread_id = pending->calls[i].thread_id; pending->first = (i + 1) % NPENDINGCALLS; } @@ -426,6 +428,7 @@ int _PyEval_AddPendingCall(PyThreadState *tstate, struct _ceval_runtime_state *ceval_r, struct _ceval_interpreter_state *ceval_i, + unsigned long thread_id, int (*func)(void *), void *arg) { struct _pending_calls *pending = &ceval_i->pending; @@ -443,7 +446,7 @@ _PyEval_AddPendingCall(PyThreadState *tstate, _PyErr_Restore(tstate, exc, val, tb); return -1; } - int result = _push_pending_call(pending, func, arg); + int result = _push_pending_call(pending, thread_id, func, arg); /* signal loop */ SIGNAL_PENDING_CALLS(ceval_r, ceval_i); @@ -462,6 +465,7 @@ Py_AddPendingCall(int (*func)(void *), void *arg) PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); return _PyEval_AddPendingCall(tstate, &runtime->ceval, &interp->ceval, + runtime->main_thread, func, arg); } @@ -497,6 +501,7 @@ static int make_pending_calls(PyInterpreterState *interp) { _PyRuntimeState *runtime = interp->runtime; + PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); static int busy = 0; /* don't perform recursive pending calls */ @@ -513,15 +518,26 @@ make_pending_calls(PyInterpreterState *interp) /* perform a bounded number of calls, in case of recursion */ struct _pending_calls *pending = &ceval_i->pending; + unsigned long thread_id = 0; for (int i=0; ilock, WAIT_LOCK); - _pop_pending_call(pending, &func, &arg); + _pop_pending_call(pending, &thread_id, &func, &arg); PyThread_release_lock(pending->lock); + if (thread_id && PyThread_get_thread_ident() != thread_id) { + // Thread mismatch, so move it to the end of the list + // and start over. + _PyEval_AddPendingCall(tstate, + &runtime->ceval, &interp->ceval, + thread_id, + func, arg); + return 0; + } + /* having released the lock, perform the callback */ if (func == NULL) { break; From 28a4583e6863113e9ed5771700c8fb0836610a2c Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 1 Feb 2019 11:49:28 -0700 Subject: [PATCH 09/20] Move core-only field to end of struct. --- Include/internal/pycore_pystate.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index b2542ad2250e6c..742a354d3efbe1 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -97,8 +97,6 @@ struct _is { /* Used in Python/sysmodule.c. */ int check_interval; - struct _ceval_interpreter_state ceval; - /* Used in Modules/_threadmodule.c. */ long num_threads; /* Support for runtime thread stack size tuning. @@ -146,6 +144,7 @@ struct _is { uint64_t tstate_next_unique_id; + struct _ceval_interpreter_state ceval; struct _warnings_runtime_state warnings; PyObject *audit_hooks; From b7150c7e20afcc2172b9c90c0d0e85504731913d Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 18 Feb 2019 15:24:45 -0700 Subject: [PATCH 10/20] Add a TODO. --- Python/pystate.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/pystate.c b/Python/pystate.c index 6ff7e233a3f2f9..e74ce9cbaca112 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1510,6 +1510,7 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) // "Release" the data and/or the object. struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate; + // XXX Use _Py_AddPendingCall(). _call_in_interpreter(gilstate, interp, _release_xidata, data); } From 87e15fe7aeccf3c1b2dd5f51e65e2730966009f9 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 14 Sep 2018 16:37:25 -0700 Subject: [PATCH 11/20] Use _Py_AddPendingCall() for releasing shared data. --- Python/pystate.c | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index e74ce9cbaca112..c808b83c1505e8 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1455,7 +1455,7 @@ _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data) return 0; } -static void +static int _release_xidata(void *arg) { _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg; @@ -1463,36 +1463,14 @@ _release_xidata(void *arg) data->free(data->data); } Py_XDECREF(data->obj); -} - -static void -_call_in_interpreter(struct _gilstate_runtime_state *gilstate, - 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 != _PyRuntimeGILState_GetThreadState(gilstate)->interp) { - // XXX Using the "head" thread isn't strictly correct. - PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); - // XXX Possible GILState issues? - save_tstate = _PyThreadState_Swap(gilstate, tstate); - } - - func(arg); - - // Switch back. - if (save_tstate != NULL) { - _PyThreadState_Swap(gilstate, save_tstate); - } + return 0; } void _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) { + _PyRuntimeState *runtime = &_PyRuntime; + if (data->data == NULL && data->obj == NULL) { // Nothing to release! return; @@ -1509,9 +1487,10 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) } // "Release" the data and/or the object. - struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate; - // XXX Use _Py_AddPendingCall(). - _call_in_interpreter(gilstate, interp, _release_xidata, data); + PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); + _PyEval_AddPendingCall(tstate, + &runtime->ceval, &interp->ceval, + 0, _release_xidata, data); } PyObject * From 07957ba5701b2834d756c4e3a41b62fbcc681a50 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 28 Jan 2019 11:58:49 -0700 Subject: [PATCH 12/20] Check the result of adding the pending call (for releasing XID). --- Python/pystate.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index c808b83c1505e8..e0fc28e785383f 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1476,7 +1476,7 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) return; } - // Switch to the original interpreter. + // Get the original interpreter. PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp); if (interp == NULL) { // The intepreter was already destroyed. @@ -1485,12 +1485,20 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) } return; } + // XXX There's a slight race here... + if (interp->finalizing) { + // XXX Someone leaked some memory... + return; + } // "Release" the data and/or the object. PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); - _PyEval_AddPendingCall(tstate, - &runtime->ceval, &interp->ceval, - 0, _release_xidata, data); + int res = _PyEval_AddPendingCall(tstate, + &runtime->ceval, &interp->ceval, + 0, _release_xidata, data); + if (res != 0) { + // XXX Queue full or couldn't get lock. Try again somehow? + } } PyObject * From 78c49a0952aa0c8ea5a1bb3872845baee00c2b7a Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 28 Jan 2019 17:21:26 -0700 Subject: [PATCH 13/20] Make a copy of data-to-be-deleted. --- Python/pystate.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Python/pystate.c b/Python/pystate.c index e0fc28e785383f..a9f3389a0d833e 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1463,6 +1463,7 @@ _release_xidata(void *arg) data->free(data->data); } Py_XDECREF(data->obj); + PyMem_Free(data); return 0; } @@ -1485,17 +1486,25 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) } return; } - // XXX There's a slight race here... + // XXX There's an ever-so-slight race here... if (interp->finalizing) { // XXX Someone leaked some memory... return; } // "Release" the data and/or the object. + _PyCrossInterpreterData *copied = PyMem_Malloc(sizeof(_PyCrossInterpreterData)); + if (copied == NULL) { + PyErr_SetString(PyExc_MemoryError, + "Not enough memory to preserve cross-interpreter data"); + PyErr_Print(); + return; + } + memcpy(copied, data, sizeof(_PyCrossInterpreterData)); PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); int res = _PyEval_AddPendingCall(tstate, &runtime->ceval, &interp->ceval, - 0, _release_xidata, data); + 0, _release_xidata, copied); if (res != 0) { // XXX Queue full or couldn't get lock. Try again somehow? } From 70ef8f9c4873ea73d27d135ad559388901ece3ad Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 5 Apr 2019 12:36:56 -0600 Subject: [PATCH 14/20] Fix the pending-for-other-thread case. --- Lib/test/test_capi.py | 2 +- Python/ceval.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py index 4dd78bb9a2fd5f..fabc821e5c3ad8 100644 --- a/Lib/test/test_capi.py +++ b/Lib/test/test_capi.py @@ -431,7 +431,7 @@ def pendingcalls_wait(self, l, n, context = None): def test_pendingcalls_threaded(self): #do every callback on a separate thread - n = 32 #total callbacks + n = 32 #total callbacks (see NPENDINGCALLS in pycore_ceval.h) threads = [] class foo(object):pass context = foo() diff --git a/Python/ceval.c b/Python/ceval.c index 02c1eb9f1e5f8d..1c25405f2d2595 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -535,7 +535,7 @@ make_pending_calls(PyInterpreterState *interp) &runtime->ceval, &interp->ceval, thread_id, func, arg); - return 0; + goto error; } /* having released the lock, perform the callback */ From b392d32f62427908d463ef9a6ae47109df3c89a3 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 31 May 2019 22:23:48 -0600 Subject: [PATCH 15/20] Ignore any lingering pending calls. --- Python/pylifecycle.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index a405c46d93f28f..3e6820b659e86a 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1155,7 +1155,8 @@ Py_FinalizeEx(void) wait_for_thread_shutdown(); // Make any remaining pending calls. - _PyEval_FinishPendingCalls(interp); + // XXX For the moment we are going to ignore any lingering pending calls. + //_PyEval_FinishPendingCalls(interp); /* The interpreter is still entirely intact at this point, and the * exit funcs may be relying on that. In particular, if some thread From d8ac4429ff2c2d834bf4bfa8123faed2d6db4824 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 1 Jun 2019 12:12:20 -0600 Subject: [PATCH 16/20] Always use the main interpreter for signals. --- Modules/signalmodule.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 9f5ff077476a5d..1964646da2522d 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -260,6 +260,7 @@ trip_signal(int sig_num) /* Notify ceval.c */ _PyRuntimeState *runtime = &_PyRuntime; PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); + PyInterpreterState *interp = runtime->interpreters.main; _PyEval_SignalReceived(&runtime->ceval); /* And then write to the wakeup fd *after* setting all the globals and @@ -302,7 +303,7 @@ trip_signal(int sig_num) still use it for this exceptional case. */ _PyEval_AddPendingCall(tstate, &runtime->ceval, - &tstate->interp->ceval, + &interp->ceval, runtime->main_thread, report_wakeup_send_error, (void *)(intptr_t) last_error); @@ -324,7 +325,7 @@ trip_signal(int sig_num) still use it for this exceptional case. */ _PyEval_AddPendingCall(tstate, &runtime->ceval, - &tstate->interp->ceval, + &interp->ceval, runtime->main_thread, report_wakeup_write_error, (void *)(intptr_t)errno); From 7c0e3cb53bc6fbc0e42f47fd25d1ffc46483753d Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 1 Jun 2019 13:14:43 -0600 Subject: [PATCH 17/20] Add more information to the TODO in Py_FinalizeEx(). --- Python/pylifecycle.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 3e6820b659e86a..3de5528811ae16 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1155,7 +1155,22 @@ Py_FinalizeEx(void) wait_for_thread_shutdown(); // Make any remaining pending calls. - // XXX For the moment we are going to ignore any lingering pending calls. + /* XXX For the moment we are going to ignore lingering pending calls. + * We've seen sporadic on some of the buildbots during finalization + * with the changes for per-interpreter pending calls (see bpo-33608), + * meaning the previous _PyEval_FinishPendincCalls() call here is + * a trigger, if not responsible. + * + * Ignoring pending calls at this point in the runtime lifecycle + * is okay (for now) for the following reasons: + * + * * pending calls are still not a widely-used feature + * * this only affects runtime finalization, where the process is + * likely to end soon anyway (except for some embdding cases) + * + * See bpo-37127 about resolving the problem. Ultimately the call + * here should be re-enabled. + */ //_PyEval_FinishPendingCalls(interp); /* The interpreter is still entirely intact at this point, and the From b8cfc365a6706edfbd6b6ae3fe52d29e525b3689 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 1 Jun 2019 13:37:38 -0600 Subject: [PATCH 18/20] Drop a superfluous comment. --- Include/internal/pycore_pystate.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 742a354d3efbe1..03c44cce837b9e 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -15,7 +15,6 @@ extern "C" { #include "sysmodule.h" #include "pycore_gil.h" /* _gil_runtime_state */ -//#include "pycore_atomic.h" #include "pycore_pathconfig.h" #include "pycore_pymem.h" #include "pycore_warnings.h" From 652c7bced5701f0ff1c15ff2206df4dfe91ad36b Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 1 Jun 2019 14:26:27 -0600 Subject: [PATCH 19/20] Do not assume that tstate->interp is not NULL. --- Python/ceval.c | 95 ++++++++++++++++++++++++++++++++++++---------- Python/ceval_gil.h | 6 ++- 2 files changed, 79 insertions(+), 22 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index 1c25405f2d2595..39aa8497bfbc9a 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -209,10 +209,18 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval_r) static inline void exit_thread_if_finalizing(PyThreadState *tstate) { - _PyRuntimeState *runtime = tstate->interp->runtime; - /* _Py_Finalizing is protected by the GIL */ + PyInterpreterState *interp = tstate->interp; + // Stop if thread/interpreter inalization already stated. + if (interp == NULL) { + return; + } + _PyRuntimeState *runtime = interp->runtime; + if (runtime == NULL) { + return; + } + // Don't exit if the main thread (i.e. of the main interpreter). if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) { - drop_gil(&runtime->ceval, &tstate->interp->ceval, tstate); + drop_gil(&runtime->ceval, &interp->ceval, tstate); PyThread_exit_thread(); } } @@ -234,14 +242,19 @@ void PyEval_ReleaseLock(void) { _PyRuntimeState *runtime = &_PyRuntime; - PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); /* This function must succeed when the current thread state is NULL. We therefore avoid PyThreadState_Get() which dumps a fatal error in debug mode. */ + PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); + // Fall back to the main interpreter if there is not active Python + // thread. This only affects the eval_breaker. PyInterpreterState *interp = runtime->interpreters.main; if (tstate != NULL) { interp = tstate->interp; + if (interp == NULL) { + Py_FatalError("PyEval_ReleaseLock: NULL interpreter state"); + } } drop_gil(&runtime->ceval, &interp->ceval, tstate); } @@ -252,9 +265,14 @@ PyEval_AcquireThread(PyThreadState *tstate) if (tstate == NULL) { Py_FatalError("PyEval_AcquireThread: NULL new thread state"); } - assert(tstate->interp != NULL); - - _PyRuntimeState *runtime = tstate->interp->runtime; + PyInterpreterState *interp = tstate->interp; + if (interp == NULL) { + Py_FatalError("PyEval_AcquireThread: NULL interpreter state"); + } + _PyRuntimeState *runtime = interp->runtime; + if (runtime == NULL) { + Py_FatalError("PyEval_AcquireThread: NULL runtime state"); + } struct _ceval_runtime_state *ceval_r = &runtime->ceval; /* Check someone has called PyEval_InitThreads() to create the lock */ @@ -272,14 +290,20 @@ PyEval_ReleaseThread(PyThreadState *tstate) if (tstate == NULL) { Py_FatalError("PyEval_ReleaseThread: NULL thread state"); } - assert(tstate->interp != NULL); + PyInterpreterState *interp = tstate->interp; + if (interp == NULL) { + Py_FatalError("PyEval_ReleaseThread: NULL interpreter state"); + } + _PyRuntimeState *runtime = interp->runtime; + if (runtime == NULL) { + Py_FatalError("PyEval_ReleaseThread: NULL runtime state"); + } - _PyRuntimeState *runtime = tstate->interp->runtime; PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL); if (new_tstate != tstate) { Py_FatalError("PyEval_ReleaseThread: wrong thread state"); } - drop_gil(&runtime->ceval, &tstate->interp->ceval, tstate); + drop_gil(&runtime->ceval, &interp->ceval, tstate); } /* This function is called from PyOS_AfterFork_Child to destroy all threads @@ -329,8 +353,13 @@ PyEval_SaveThread(void) if (tstate == NULL) { Py_FatalError("PyEval_SaveThread: NULL tstate"); } + PyInterpreterState *interp = tstate->interp; + if (interp == NULL) { + Py_FatalError("PyEval_SaveThread: NULL interpreter state"); + } + assert(gil_created(&ceval_r->gil)); - drop_gil(ceval_r, &tstate->interp->ceval, tstate); + drop_gil(ceval_r, &interp->ceval, tstate); return tstate; } @@ -340,10 +369,16 @@ PyEval_RestoreThread(PyThreadState *tstate) if (tstate == NULL) { Py_FatalError("PyEval_RestoreThread: NULL tstate"); } - assert(tstate->interp != NULL); - - _PyRuntimeState *runtime = tstate->interp->runtime; + PyInterpreterState *interp = tstate->interp; + if (interp == NULL) { + Py_FatalError("PyEval_RestoreThread: NULL interpreter state"); + } + _PyRuntimeState *runtime = interp->runtime; + if (runtime == NULL) { + Py_FatalError("PyEval_RestoreThread: NULL runtime state"); + } struct _ceval_runtime_state *ceval_r = &runtime->ceval; + assert(gil_created(&ceval_r->gil)); int err = errno; @@ -500,8 +535,20 @@ handle_signals(_PyRuntimeState *runtime) static int make_pending_calls(PyInterpreterState *interp) { + if (interp == NULL) { + Py_FatalError("make_pending_calls: NULL interpreter state"); + } _PyRuntimeState *runtime = interp->runtime; + if (runtime == NULL) { + Py_FatalError("make_pending_calls: NULL runtime state"); + } PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); + if (tstate == NULL) { + Py_FatalError("make_pending_calls: NULL thread state"); + } + if (tstate->interp == NULL || tstate->interp != interp) { + Py_FatalError("make_pending_calls: thread state mismatch"); + } static int busy = 0; /* don't perform recursive pending calls */ @@ -574,12 +621,17 @@ _PyEval_FinishPendingCalls(PyInterpreterState *interp) if (make_pending_calls(interp) < 0) { _PyRuntimeState *runtime = interp->runtime; + if (runtime == NULL) { + runtime = &_PyRuntime; + } PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); - PyObject *exc, *val, *tb; - _PyErr_Fetch(tstate, &exc, &val, &tb); - PyErr_BadInternalCall(); - _PyErr_ChainExceptions(exc, val, tb); - _PyErr_Print(tstate); + if (tstate != NULL) { + PyObject *exc, *val, *tb; + _PyErr_Fetch(tstate, &exc, &val, &tb); + PyErr_BadInternalCall(); + _PyErr_ChainExceptions(exc, val, tb); + _PyErr_Print(tstate); + } } } @@ -729,8 +781,9 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) PyObject *retval = NULL; /* Return value */ _PyRuntimeState * const runtime = &_PyRuntime; PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime); + PyInterpreterState * const interp = tstate->interp; struct _ceval_runtime_state * const ceval_r = &runtime->ceval; - struct _ceval_interpreter_state * const ceval_i = &tstate->interp->ceval; + struct _ceval_interpreter_state * const ceval_i = &interp->ceval; _Py_atomic_int * const eval_breaker = &ceval_r->eval_breaker; PyCodeObject *co; @@ -1149,7 +1202,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) } } if (_Py_atomic_load_relaxed(&ceval_i->pending.calls_to_do)) { - if (make_pending_calls(tstate->interp) != 0) { + if (make_pending_calls(interp) != 0) { goto error; } } diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h index b62cc660a44f9f..b44d0abad36b13 100644 --- a/Python/ceval_gil.h +++ b/Python/ceval_gil.h @@ -189,7 +189,11 @@ take_gil(struct _ceval_runtime_state *ceval_r, if (tstate == NULL) { Py_FatalError("take_gil: NULL tstate"); } - struct _ceval_interpreter_state *ceval_i = &tstate->interp->ceval; + PyInterpreterState *interp = tstate->interp; + if (interp == NULL) { + Py_FatalError("take_gil: NULL interp"); + } + struct _ceval_interpreter_state *ceval_i = &interp->ceval; struct _gil_runtime_state *gil = &ceval_r->gil; int err = errno; From fb0fe21738e1cd6a5c086117790339716c90eaeb Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 1 Jun 2019 14:29:57 -0600 Subject: [PATCH 20/20] _pending_calls -> _ceval_pending_calls --- Include/internal/pycore_pystate.h | 4 ++-- Python/ceval.c | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 03c44cce837b9e..aca5533022e322 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -25,7 +25,7 @@ struct pyruntimestate; /* ceval state */ -struct _pending_calls { +struct _ceval_pending_calls { int finishing; PyThread_type_lock lock; /* Request for running pending calls. */ @@ -65,7 +65,7 @@ struct _ceval_runtime_state { }; struct _ceval_interpreter_state { - struct _pending_calls pending; + struct _ceval_pending_calls pending; }; diff --git a/Python/ceval.c b/Python/ceval.c index 39aa8497bfbc9a..54f12d17ad90a3 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -324,7 +324,7 @@ _PyEval_ReInitThreads(_PyRuntimeState *runtime) // Only the main interpreter remains, so ignore the rest. PyInterpreterState *interp = _PyRuntime.interpreters.main; - struct _pending_calls *pending = &interp->ceval.pending; + struct _ceval_pending_calls *pending = &interp->ceval.pending; pending->lock = PyThread_allocate_lock(); if (pending->lock == NULL) { Py_FatalError("Can't initialize threads for pending calls"); @@ -423,7 +423,7 @@ _PyEval_SignalReceived(struct _ceval_runtime_state *ceval_r) /* Push one item onto the queue while holding the lock. */ static int -_push_pending_call(struct _pending_calls *pending, unsigned long thread_id, +_push_pending_call(struct _ceval_pending_calls *pending, unsigned long thread_id, int (*func)(void *), void *arg) { int i = pending->last; @@ -440,7 +440,7 @@ _push_pending_call(struct _pending_calls *pending, unsigned long thread_id, /* Pop one item off the queue while holding the lock. */ static void -_pop_pending_call(struct _pending_calls *pending, unsigned long *thread_id, +_pop_pending_call(struct _ceval_pending_calls *pending, unsigned long *thread_id, int (**func)(void *), void **arg) { int i = pending->first; @@ -466,7 +466,7 @@ _PyEval_AddPendingCall(PyThreadState *tstate, unsigned long thread_id, int (*func)(void *), void *arg) { - struct _pending_calls *pending = &ceval_i->pending; + struct _ceval_pending_calls *pending = &ceval_i->pending; PyThread_acquire_lock(pending->lock, WAIT_LOCK); if (pending->finishing) { @@ -564,7 +564,7 @@ make_pending_calls(PyInterpreterState *interp) int res = 0; /* perform a bounded number of calls, in case of recursion */ - struct _pending_calls *pending = &ceval_i->pending; + struct _ceval_pending_calls *pending = &ceval_i->pending; unsigned long thread_id = 0; for (int i=0; iceval.pending; + struct _ceval_pending_calls *pending = &interp->ceval.pending; PyThread_acquire_lock(pending->lock, WAIT_LOCK); pending->finishing = 1;