bpo-36710: Add PyInterpreterState.runtime field (GH-17270)

Add PyInterpreterState.runtime field: reference to the _PyRuntime
global variable. This field exists to not have to pass runtime in
addition to tstate to a function.  Get runtime from tstate:
tstate->interp->runtime.

Remove "_PyRuntimeState *runtime" parameter from functions already
taking a "PyThreadState *tstate" parameter.

_PyGC_Init() first parameter becomes "PyThreadState *tstate".
This commit is contained in:
Victor Stinner 2019-11-20 02:27:56 +01:00 committed by GitHub
parent eb1cbbff1c
commit 01b1cc12e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 81 additions and 94 deletions

View file

@ -191,7 +191,8 @@ static size_t opcache_global_misses = 0;
int
PyEval_ThreadsInitialized(void)
{
return gil_created(&_PyRuntime.ceval.gil);
_PyRuntimeState *runtime = &_PyRuntime;
return gil_created(&runtime->ceval.gil);
}
void
@ -235,8 +236,9 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
}
static inline void
exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
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);
@ -283,7 +285,7 @@ PyEval_AcquireLock(void)
Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
}
take_gil(ceval, tstate);
exit_thread_if_finalizing(runtime, tstate);
exit_thread_if_finalizing(tstate);
}
void
@ -305,13 +307,13 @@ PyEval_AcquireThread(PyThreadState *tstate)
Py_FatalError("PyEval_AcquireThread: NULL new thread state");
}
_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
struct _ceval_runtime_state *ceval = &runtime->ceval;
/* Check someone has called PyEval_InitThreads() to create the lock */
assert(gil_created(&ceval->gil));
take_gil(ceval, tstate);
exit_thread_if_finalizing(runtime, tstate);
exit_thread_if_finalizing(tstate);
if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
}
@ -324,7 +326,7 @@ PyEval_ReleaseThread(PyThreadState *tstate)
Py_FatalError("PyEval_ReleaseThread: NULL thread state");
}
_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
if (new_tstate != tstate) {
Py_FatalError("PyEval_ReleaseThread: wrong thread state");
@ -384,7 +386,7 @@ PyEval_SaveThread(void)
void
PyEval_RestoreThread(PyThreadState *tstate)
{
_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
struct _ceval_runtime_state *ceval = &runtime->ceval;
if (tstate == NULL) {
@ -394,7 +396,7 @@ PyEval_RestoreThread(PyThreadState *tstate)
int err = errno;
take_gil(ceval, tstate);
exit_thread_if_finalizing(runtime, tstate);
exit_thread_if_finalizing(tstate);
errno = err;
_PyThreadState_Swap(&runtime->gilstate, tstate);
@ -649,7 +651,8 @@ _PyEval_Initialize(struct _ceval_runtime_state *state)
int
Py_GetRecursionLimit(void)
{
return _PyRuntime.ceval.recursion_limit;
struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
return ceval->recursion_limit;
}
void
@ -668,7 +671,7 @@ Py_SetRecursionLimit(int new_limit)
int
_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
{
_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
int recursion_limit = runtime->ceval.recursion_limit;
#ifdef USE_STACKCHECK
@ -1245,7 +1248,7 @@ main_loop:
take_gil(ceval, tstate);
/* Check if we should make a quick exit. */
exit_thread_if_finalizing(runtime, tstate);
exit_thread_if_finalizing(tstate);
if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Py_FatalError("ceval: orphan tstate");
@ -4806,7 +4809,8 @@ _PyEval_GetAsyncGenFinalizer(void)
static PyFrameObject *
_PyEval_GetFrame(PyThreadState *tstate)
{
return _PyRuntime.gilstate.getframe(tstate);
_PyRuntimeState *runtime = tstate->interp->runtime;
return runtime->gilstate.getframe(tstate);
}
PyFrameObject *