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/Include/internal/ceval.h b/Include/internal/ceval.h index cdabb9521d1023..aa0133cb1651f0 100644 --- a/Include/internal/ceval.h +++ b/Include/internal/ceval.h @@ -7,8 +7,12 @@ extern "C" { #include "pyatomic.h" #include "pythread.h" +struct _is; // See PyInterpreterState in pystate.h. + +PyAPI_FUNC(int) _Py_AddPendingCall(struct _is*, unsigned long, int (*)(void *), void *); +PyAPI_FUNC(int) _Py_MakePendingCalls(struct _is*); + struct _pending_calls { - unsigned long main_thread; PyThread_type_lock lock; /* Request for running pending calls. */ _Py_atomic_int calls_to_do; @@ -18,6 +22,7 @@ struct _pending_calls { int async_exc; #define NPENDINGCALLS 32 struct { + unsigned long thread_id; int (*func)(void *); void *arg; } calls[NPENDINGCALLS]; @@ -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/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/Include/pystate.h b/Include/pystate.h index 80ee0d112daade..04085bbe0a1ec2 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). */ @@ -56,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; @@ -65,6 +71,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/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. 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..e869bd0bb35fbd 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 @@ -95,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 { @@ -279,8 +280,10 @@ 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, + main_thread, + report_wakeup_send_error, + (void *)(intptr_t) last_error); } } } @@ -297,8 +300,10 @@ 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, + main_thread, + report_wakeup_write_error, + (void *)(intptr_t)errno); } } } @@ -1278,7 +1283,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 */ @@ -1680,7 +1684,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 f3a74b00a2b64a..1d3b5e59480497 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( \ - &_PyRuntime.ceval.eval_breaker, \ + &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(&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(&_PyRuntime.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(&_PyRuntime.ceval.pending.calls_to_do, 1); \ - _Py_atomic_store_relaxed(&_PyRuntime.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(&_PyRuntime.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 { \ - _PyRuntime.ceval.pending.async_exc = 1; \ - _Py_atomic_store_relaxed(&_PyRuntime.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 { \ - _PyRuntime.ceval.pending.async_exc = 0; \ - COMPUTE_EVAL_BREAKER(); \ + interp->ceval.pending.async_exc = 0; \ + COMPUTE_EVAL_BREAKER(interp); \ } 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,9 @@ PyEval_ReInitThreads(void) if (!gil_created()) return; recreate_gil(); - _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); + _PyRuntime.main_thread = PyThread_get_thread_ident(); + current_tstate->interp->ceval.pending.lock = PyThread_allocate_lock(); take_gil(current_tstate); - _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident(); /* Destroy all threads except the current one */ _PyThreadState_DeleteExcept(current_tstate); @@ -240,9 +237,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 @@ -309,7 +306,16 @@ _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 +Py_AddPendingCall(int (*func)(void *), void *arg) +{ + PyInterpreterState *interp = _PyRuntime.interpreters.main; + return _Py_AddPendingCall(interp, _PyRuntime.main_thread, func, arg); } /* This implementation is thread-safe. It allows @@ -318,10 +324,10 @@ _PyEval_SignalReceived(void) */ int -Py_AddPendingCall(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 = _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,17 +349,30 @@ Py_AddPendingCall(int (*func)(void *), void *arg) return -1; } - i = _PyRuntime.ceval.pending.last; + 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 == _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].thread_id = thread_id; + 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(); + SIGNAL_PENDING_CALLS(interp); + +done: if (lock != NULL) PyThread_release_lock(lock); return result; @@ -361,6 +380,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,26 +394,20 @@ 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) - { - return 0; - } /* don't perform recursive pending calls */ if (busy) return 0; 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(). */ @@ -402,16 +422,23 @@ 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; - } - PyThread_release_lock(_PyRuntime.ceval.pending.lock); + 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); /* having released the lock, perform the callback */ if (func == NULL) break; @@ -426,7 +453,7 @@ Py_MakePendingCalls(void) error: busy = 0; - SIGNAL_PENDING_CALLS(); /* We're not done yet */ + SIGNAL_PENDING_CALLS(interp); /* We're not done yet */ return -1; } @@ -634,7 +661,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 +964,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 +989,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; @@ -994,7 +1021,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/pylifecycle.c b/Python/pylifecycle.c index 379e860b5ba4b4..540d2350d16e88 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1416,8 +1416,31 @@ Py_EndInterpreter(PyThreadState *tstate) if (tstate->frame != NULL) Py_FatalError("Py_EndInterpreter: thread still has a frame"); + // Mark as finalizing. + if (interp->ceval.pending.lock != NULL) { + PyThread_acquire_lock(interp->ceval.pending.lock, 1); + } + interp->finalizing = 1; + if (interp->ceval.pending.lock != NULL) { + PyThread_release_lock(interp->ceval.pending.lock); + } + + // Wrap up existing threads. wait_for_thread_shutdown(); + // Make any pending calls. + if (_Py_atomic_load_relaxed( + &(interp->ceval.pending.calls_to_do))) + { + if (_Py_MakePendingCalls(interp) < 0) { + PyObject *exc, *val, *tb; + PyErr_Fetch(&exc, &val, &tb); + PyErr_BadInternalCall(); + _PyErr_ChainExceptions(exc, val, tb); + PyErr_Print(); + } + } + call_py_exitfuncs(interp); if (tstate != interp->tstate_head || tstate->next != NULL) diff --git a/Python/pystate.c b/Python/pystate.c index 7b3d3d4c903259..cb322b8ac055ff 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) @@ -134,28 +140,19 @@ 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->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->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 +160,10 @@ 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; + + if (_PyRuntime.main_thread == 0) { + _PyRuntime.main_thread = PyThread_get_thread_ident(); + } HEAD_LOCK(); if (_PyRuntime.interpreters.next_id < 0) { @@ -224,6 +218,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. } @@ -264,6 +261,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); } @@ -858,7 +858,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; } } @@ -1272,7 +1272,7 @@ _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data) return 0; } -static void +static int _release_xidata(void *arg) { _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg; @@ -1280,30 +1280,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 @@ -1325,7 +1302,7 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) } // "Release" the data and/or the object. - _call_in_interpreter(interp, _release_xidata, data); + _Py_AddPendingCall(interp, 0, _release_xidata, data); } PyObject *