bpo-36818: Add PyInterpreterState.runtime field. (gh-13129)

https://bugs.python.org/issue36818
diff --git a/Python/ceval.c b/Python/ceval.c
index 71e6eb8..f9ff4e0 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -217,8 +217,9 @@
 }
 
 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);
@@ -236,7 +237,7 @@
         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
@@ -257,14 +258,15 @@
     if (tstate == NULL) {
         Py_FatalError("PyEval_AcquireThread: NULL new thread state");
     }
+    assert(tstate->interp != NULL);
 
-    _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");
     }
@@ -276,8 +278,9 @@
     if (tstate == NULL) {
         Py_FatalError("PyEval_ReleaseThread: NULL thread state");
     }
+    assert(tstate->interp != NULL);
 
-    _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");
@@ -308,7 +311,7 @@
     }
 
     /* Destroy all threads except the current one */
-    _PyThreadState_DeleteExcept(runtime, current_tstate);
+    _PyThreadState_DeleteExcept(current_tstate);
 }
 
 /* This function is used to signal that async exceptions are waiting to be
@@ -337,17 +340,18 @@
 void
 PyEval_RestoreThread(PyThreadState *tstate)
 {
-    _PyRuntimeState *runtime = &_PyRuntime;
-    struct _ceval_runtime_state *ceval = &runtime->ceval;
-
     if (tstate == NULL) {
         Py_FatalError("PyEval_RestoreThread: NULL tstate");
     }
+    assert(tstate->interp != NULL);
+
+    _PyRuntimeState *runtime = tstate->interp->runtime;
+    struct _ceval_runtime_state *ceval = &runtime->ceval;
     assert(gil_created(&ceval->gil));
 
     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);
@@ -1141,7 +1145,7 @@
                 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");
diff --git a/Python/import.c b/Python/import.c
index ab7db6b..68d1f40 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -541,7 +541,8 @@
     _PyGC_CollectNoFail();
     /* Dump GC stats before it's too late, since it uses the warnings
        machinery. */
-    _PyGC_DumpShutdownStats(&_PyRuntime);
+    _PyRuntimeState *runtime = interp->runtime;
+    _PyGC_DumpShutdownStats(runtime);
 
     /* Now, if there are any modules left alive, clear their globals to
        minimize potential leaks.  All C extension modules actually end
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 10a2881..6590ef8 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -545,7 +545,7 @@
     _PyEval_FiniThreads(&runtime->ceval);
 
     /* Auto-thread-state API */
-    _PyGILState_Init(runtime, interp, tstate);
+    _PyGILState_Init(tstate);
 
     /* Create the GIL */
     PyEval_InitThreads();
@@ -683,7 +683,7 @@
     }
 
     PyObject *sysmod;
-    status = _PySys_Create(runtime, interp, &sysmod);
+    status = _PySys_Create(interp, &sysmod);
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }
@@ -892,8 +892,9 @@
  * non-zero return code.
  */
 static PyStatus
-pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
+pyinit_main(PyInterpreterState *interp)
 {
+    _PyRuntimeState *runtime = interp->runtime;
     if (!runtime->core_initialized) {
         return _PyStatus_ERR("runtime core not initialized");
     }
@@ -919,7 +920,7 @@
         return _PyStatus_ERR("can't initialize time");
     }
 
-    if (_PySys_InitMain(runtime, interp) < 0) {
+    if (_PySys_InitMain(interp) < 0) {
         return _PyStatus_ERR("can't finish initializing sys");
     }
 
@@ -999,7 +1000,7 @@
     _PyRuntimeState *runtime = &_PyRuntime;
     PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
 
-    return pyinit_main(runtime, interp);
+    return pyinit_main(interp);
 }
 
 
@@ -1026,7 +1027,7 @@
     config = &interp->config;
 
     if (config->_init_main) {
-        status = pyinit_main(runtime, interp);
+        status = pyinit_main(interp);
         if (_PyStatus_EXCEPTION(status)) {
             return status;
         }
@@ -1453,7 +1454,7 @@
         }
         Py_INCREF(interp->sysdict);
         PyDict_SetItemString(interp->sysdict, "modules", modules);
-        if (_PySys_InitMain(runtime, interp) < 0) {
+        if (_PySys_InitMain(interp) < 0) {
             return _PyStatus_ERR("can't finish initializing sys");
         }
     }
diff --git a/Python/pystate.c b/Python/pystate.c
index 833e0fb..2b7db0e 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -39,7 +39,6 @@
 
 /* Forward declarations */
 static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
-static void _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate);
 
 
 static PyStatus
@@ -192,6 +191,8 @@
 PyInterpreterState *
 PyInterpreterState_New(void)
 {
+    _PyRuntimeState *runtime = &_PyRuntime;
+
     if (PySys_Audit("cpython.PyInterpreterState_New", NULL) < 0) {
         return NULL;
     }
@@ -202,6 +203,9 @@
     }
 
     memset(interp, 0, sizeof(*interp));
+
+    interp->runtime = runtime;
+
     interp->id_refcount = -1;
     interp->check_interval = 100;
 
@@ -223,7 +227,6 @@
 #endif
 #endif
 
-    _PyRuntimeState *runtime = &_PyRuntime;
     struct pyinterpreters *interpreters = &runtime->interpreters;
 
     HEAD_LOCK(runtime);
@@ -257,9 +260,11 @@
 }
 
 
-static void
-_PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
+void
+PyInterpreterState_Clear(PyInterpreterState *interp)
 {
+    _PyRuntimeState *runtime = interp->runtime;
+
     if (PySys_Audit("cpython.PyInterpreterState_Clear", NULL) < 0) {
         PyErr_Clear();
     }
@@ -297,31 +302,25 @@
     // objects have been cleaned up at the point.
 }
 
-void
-PyInterpreterState_Clear(PyInterpreterState *interp)
-{
-    _PyInterpreterState_Clear(&_PyRuntime, interp);
-}
-
 
 static void
-zapthreads(_PyRuntimeState *runtime, PyInterpreterState *interp)
+zapthreads(PyInterpreterState *interp)
 {
-    PyThreadState *p;
+    PyThreadState *ts;
     /* No need to lock the mutex here because this should only happen
        when the threads are all really dead (XXX famous last words). */
-    while ((p = interp->tstate_head) != NULL) {
-        _PyThreadState_Delete(runtime, p);
+    while ((ts = interp->tstate_head) != NULL) {
+        PyThreadState_Delete(ts);
     }
 }
 
 
-static void
-_PyInterpreterState_Delete(_PyRuntimeState *runtime,
-                           PyInterpreterState *interp)
+void
+PyInterpreterState_Delete(PyInterpreterState *interp)
 {
+    _PyRuntimeState *runtime = interp->runtime;
     struct pyinterpreters *interpreters = &runtime->interpreters;
-    zapthreads(runtime, interp);
+    zapthreads(interp);
     HEAD_LOCK(runtime);
     PyInterpreterState **p;
     for (p = &interpreters->head; ; p = &(*p)->next) {
@@ -350,13 +349,6 @@
 }
 
 
-void
-PyInterpreterState_Delete(PyInterpreterState *interp)
-{
-    _PyInterpreterState_Delete(&_PyRuntime, interp);
-}
-
-
 /*
  * Delete all interpreter states except the main interpreter.  If there
  * is a current interpreter state, it *must* be the main interpreter.
@@ -383,8 +375,8 @@
             continue;
         }
 
-        _PyInterpreterState_Clear(runtime, interp);  // XXX must activate?
-        zapthreads(runtime, interp);
+        PyInterpreterState_Clear(interp);  // XXX must activate?
+        zapthreads(interp);
         if (interp->id_mutex != NULL) {
             PyThread_free_lock(interp->id_mutex);
         }
@@ -497,7 +489,8 @@
     if (interp->id_mutex == NULL) {
         return;
     }
-    struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
+    _PyRuntimeState *runtime = interp->runtime;
+    struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
     PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
     assert(interp->id_refcount != 0);
     interp->id_refcount -= 1;
@@ -559,7 +552,7 @@
 static PyThreadState *
 new_threadstate(PyInterpreterState *interp, int init)
 {
-    _PyRuntimeState *runtime = &_PyRuntime;
+    _PyRuntimeState *runtime = interp->runtime;
     PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
     if (tstate == NULL) {
         return NULL;
@@ -615,7 +608,7 @@
     tstate->id = ++interp->tstate_next_unique_id;
 
     if (init) {
-        _PyThreadState_Init(runtime, tstate);
+        _PyThreadState_Init(tstate);
     }
 
     HEAD_LOCK(runtime);
@@ -642,8 +635,9 @@
 }
 
 void
-_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
+_PyThreadState_Init(PyThreadState *tstate)
 {
+    _PyRuntimeState *runtime = tstate->interp->runtime;
     _PyGILState_NoteThreadState(&runtime->gilstate, tstate);
 }
 
@@ -808,7 +802,7 @@
 
 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
 static void
-tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
+tstate_delete_common(PyThreadState *tstate)
 {
     if (tstate == NULL) {
         Py_FatalError("PyThreadState_Delete: NULL tstate");
@@ -817,6 +811,7 @@
     if (interp == NULL) {
         Py_FatalError("PyThreadState_Delete: NULL interp");
     }
+    _PyRuntimeState *runtime = interp->runtime;
     HEAD_LOCK(runtime);
     if (tstate->prev)
         tstate->prev->next = tstate->next;
@@ -832,9 +827,10 @@
 }
 
 
-static void
-_PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
+void
+PyThreadState_Delete(PyThreadState *tstate)
 {
+    _PyRuntimeState *runtime = tstate->interp->runtime;
     struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
     if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
         Py_FatalError("PyThreadState_Delete: tstate is still current");
@@ -844,14 +840,7 @@
     {
         PyThread_tss_set(&gilstate->autoTSSkey, NULL);
     }
-    tstate_delete_common(runtime, tstate);
-}
-
-
-void
-PyThreadState_Delete(PyThreadState *tstate)
-{
-    _PyThreadState_Delete(&_PyRuntime, tstate);
+    tstate_delete_common(tstate);
 }
 
 
@@ -863,7 +852,7 @@
     if (tstate == NULL)
         Py_FatalError(
             "PyThreadState_DeleteCurrent: no current tstate");
-    tstate_delete_common(runtime, tstate);
+    tstate_delete_common(tstate);
     if (gilstate->autoInterpreterState &&
         PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
     {
@@ -888,9 +877,10 @@
  * be kept in those other interpreteres.
  */
 void
-_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
+_PyThreadState_DeleteExcept(PyThreadState *tstate)
 {
     PyInterpreterState *interp = tstate->interp;
+    _PyRuntimeState *runtime = interp->runtime;
     PyThreadState *p, *next, *garbage;
     HEAD_LOCK(runtime);
     /* Remove all thread states, except tstate, from the linked list of
@@ -1129,8 +1119,9 @@
 static int
 PyThreadState_IsCurrent(PyThreadState *tstate)
 {
+    _PyRuntimeState *runtime = tstate->interp->runtime;
     /* Must be the tstate for this thread */
-    struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
+    struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
     assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
     return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
 }
@@ -1139,12 +1130,14 @@
    Py_Initialize/Py_FinalizeEx
 */
 void
-_PyGILState_Init(_PyRuntimeState *runtime,
-                 PyInterpreterState *interp, PyThreadState *tstate)
+_PyGILState_Init(PyThreadState *tstate)
 {
     /* must init with valid states */
-    assert(interp != NULL);
     assert(tstate != NULL);
+    PyInterpreterState *interp = tstate->interp;
+    assert(interp != NULL);
+    _PyRuntimeState *runtime = interp->runtime;
+    assert(runtime != NULL);
 
     struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
 
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 12b1bd7..97bff94 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -120,8 +120,9 @@
     if (!ts) {
         return 0;
     }
-    PyInterpreterState *is = ts ? ts->interp : NULL;
-    return _PyRuntime.audit_hook_head
+    PyInterpreterState *is = ts->interp;
+    _PyRuntimeState *runtime = is->runtime;
+    return runtime->audit_hook_head
         || (is && is->audit_hooks)
         || PyDTrace_AUDIT_ENABLED();
 }
@@ -280,8 +281,8 @@
     PySys_Audit("cpython._PySys_ClearAuditHooks", NULL);
     PyErr_Clear();
 
-    _Py_AuditHookEntry *e = _PyRuntime.audit_hook_head, *n;
-    _PyRuntime.audit_hook_head = NULL;
+    _Py_AuditHookEntry *e = runtime->audit_hook_head, *n;
+    runtime->audit_hook_head = NULL;
     while (e) {
         n = e->next;
         PyMem_RawFree(e);
@@ -292,6 +293,7 @@
 int
 PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
 {
+    _PyRuntimeState *runtime = &_PyRuntime;
     /* Invoke existing audit hooks to allow them an opportunity to abort. */
     /* Cannot invoke hooks until we are initialized */
     if (Py_IsInitialized()) {
@@ -305,10 +307,10 @@
         }
     }
 
-    _Py_AuditHookEntry *e = _PyRuntime.audit_hook_head;
+    _Py_AuditHookEntry *e = runtime->audit_hook_head;
     if (!e) {
         e = (_Py_AuditHookEntry*)PyMem_RawMalloc(sizeof(_Py_AuditHookEntry));
-        _PyRuntime.audit_hook_head = e;
+        runtime->audit_hook_head = e;
     } else {
         while (e->next)
             e = e->next;
@@ -2413,8 +2415,9 @@
 };
 
 static PyObject*
-make_flags(_PyRuntimeState *runtime, PyInterpreterState *interp)
+make_flags(PyInterpreterState *interp)
 {
+    _PyRuntimeState *runtime = interp->runtime;
     int pos = 0;
     PyObject *seq;
     const PyPreConfig *preconfig = &runtime->preconfig;
@@ -2633,8 +2636,7 @@
     } while (0)
 
 static PyStatus
-_PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp,
-                PyObject *sysdict)
+_PySys_InitCore(PyInterpreterState *interp, PyObject *sysdict)
 {
     PyObject *version_info;
     int res;
@@ -2728,7 +2730,7 @@
         }
     }
     /* Set flags to their default values (updated by _PySys_InitMain()) */
-    SET_SYS_FROM_STRING("flags", make_flags(runtime, interp));
+    SET_SYS_FROM_STRING("flags", make_flags(interp));
 
 #if defined(MS_WINDOWS)
     /* getwindowsversion */
@@ -2849,7 +2851,7 @@
 
 
 int
-_PySys_InitMain(_PyRuntimeState *runtime, PyInterpreterState *interp)
+_PySys_InitMain(PyInterpreterState *interp)
 {
     PyObject *sysdict = interp->sysdict;
     const PyConfig *config = &interp->config;
@@ -2903,7 +2905,7 @@
 #undef SET_SYS_FROM_WSTR
 
     /* Set flags to their final values */
-    SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags(runtime, interp));
+    SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags(interp));
     /* prevent user from creating new instances */
     FlagsType.tp_init = NULL;
     FlagsType.tp_new = NULL;
@@ -2970,8 +2972,7 @@
 /* Create sys module without all attributes: _PySys_InitMain() should be called
    later to add remaining attributes. */
 PyStatus
-_PySys_Create(_PyRuntimeState *runtime, PyInterpreterState *interp,
-              PyObject **sysmod_p)
+_PySys_Create(PyInterpreterState *interp, PyObject **sysmod_p)
 {
     PyObject *modules = PyDict_New();
     if (modules == NULL) {
@@ -3000,7 +3001,7 @@
         return status;
     }
 
-    status = _PySys_InitCore(runtime, interp, sysdict);
+    status = _PySys_InitCore(interp, sysdict);
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }