bpo-39984: Move pending calls to PyInterpreterState (GH-19066)
If Py_AddPendingCall() is called in a subinterpreter, the function is
now scheduled to be called from the subinterpreter, rather than being
called from the main interpreter.
Each subinterpreter now has its own list of scheduled calls.
* Move pending and eval_breaker fields from _PyRuntimeState.ceval
to PyInterpreterState.ceval.
* new_interpreter() now calls _PyEval_InitThreads() to create
pending calls lock.
* Fix Py_AddPendingCall() for subinterpreters. It now calls
_PyThreadState_GET() which works in a subinterpreter if the
caller holds the GIL, and only falls back on
PyGILState_GetThisThreadState() if _PyThreadState_GET()
returns NULL.
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index da2bb37..b7019e3 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -556,7 +556,7 @@
return status;
}
- /* Create the GIL */
+ /* Create the GIL and the pending calls lock */
status = _PyEval_InitThreads(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
@@ -1581,6 +1581,12 @@
goto error;
}
+ /* Create the pending calls lock */
+ status = _PyEval_InitThreads(tstate);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
*tstate_p = tstate;
return _PyStatus_OK();