Revert "bpo-30860: Consolidate stateful runtime globals." (#3379)

Windows buildbots started failing due to include-related errors.
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 3f405b1..662405b 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -77,30 +77,6 @@
 extern void _PyGILState_Fini(void);
 #endif /* WITH_THREAD */
 
-_PyRuntimeState _PyRuntime = {0, 0};
-
-void
-_PyRuntime_Initialize(void)
-{
-    /* XXX We only initialize once in the process, which aligns with
-       the static initialization of the former globals now found in
-       _PyRuntime.  However, _PyRuntime *should* be initialized with
-       every Py_Initialize() call, but doing so breaks the runtime.
-       This is because the runtime state is not properly finalized
-       currently. */
-    static int initialized = 0;
-    if (initialized)
-        return;
-    initialized = 1;
-    _PyRuntimeState_Init(&_PyRuntime);
-}
-
-void
-_PyRuntime_Finalize(void)
-{
-    _PyRuntimeState_Fini(&_PyRuntime);
-}
-
 /* Global configuration variable declarations are in pydebug.h */
 /* XXX (ncoghlan): move those declarations to pylifecycle.h? */
 int Py_DebugFlag; /* Needed by parser.c */
@@ -124,6 +100,8 @@
 int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */
 #endif
 
+PyThreadState *_Py_Finalizing = NULL;
+
 /* Hack to force loading of object files */
 int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
     PyOS_mystrnicmp; /* Python/pystrcmp.o */
@@ -141,17 +119,19 @@
  *
  * Can be called prior to Py_Initialize.
  */
+int _Py_CoreInitialized = 0;
+int _Py_Initialized = 0;
 
 int
 _Py_IsCoreInitialized(void)
 {
-    return _PyRuntime.core_initialized;
+    return _Py_CoreInitialized;
 }
 
 int
 Py_IsInitialized(void)
 {
-    return _PyRuntime.initialized;
+    return _Py_Initialized;
 }
 
 /* Helper to allow an embedding application to override the normal
@@ -564,16 +544,14 @@
     _PyCoreConfig core_config = _PyCoreConfig_INIT;
     _PyMainInterpreterConfig preinit_config = _PyMainInterpreterConfig_INIT;
 
-    _PyRuntime_Initialize();
-
     if (config != NULL) {
         core_config = *config;
     }
 
-    if (_PyRuntime.initialized) {
+    if (_Py_Initialized) {
         Py_FatalError("Py_InitializeCore: main interpreter already initialized");
     }
-    if (_PyRuntime.core_initialized) {
+    if (_Py_CoreInitialized) {
         Py_FatalError("Py_InitializeCore: runtime core already initialized");
     }
 
@@ -586,14 +564,7 @@
      * threads still hanging around from a previous Py_Initialize/Finalize
      * pair :(
      */
-    _PyRuntime.finalizing = NULL;
-
-    if (_PyMem_SetupAllocators(core_config.allocator) < 0) {
-        fprintf(stderr,
-            "Error in PYTHONMALLOC: unknown allocator \"%s\"!\n",
-            core_config.allocator);
-        exit(1);
-    }
+    _Py_Finalizing = NULL;
 
 #ifdef __ANDROID__
     /* Passing "" to setlocale() on Android requests the C locale rather
@@ -635,7 +606,7 @@
         Py_HashRandomizationFlag = 1;
     }
 
-    _PyInterpreterState_Enable(&_PyRuntime);
+    _PyInterpreterState_Init();
     interp = PyInterpreterState_New();
     if (interp == NULL)
         Py_FatalError("Py_InitializeCore: can't make main interpreter");
@@ -727,7 +698,7 @@
     }
 
     /* Only when we get here is the runtime core fully initialized */
-    _PyRuntime.core_initialized = 1;
+    _Py_CoreInitialized = 1;
 }
 
 /* Read configuration settings from standard locations
@@ -768,10 +739,10 @@
     PyInterpreterState *interp;
     PyThreadState *tstate;
 
-    if (!_PyRuntime.core_initialized) {
+    if (!_Py_CoreInitialized) {
         Py_FatalError("Py_InitializeMainInterpreter: runtime core not initialized");
     }
-    if (_PyRuntime.initialized) {
+    if (_Py_Initialized) {
         Py_FatalError("Py_InitializeMainInterpreter: main interpreter already initialized");
     }
 
@@ -792,7 +763,7 @@
          * This means anything which needs support from extension modules
          * or pure Python code in the standard library won't work.
          */
-        _PyRuntime.initialized = 1;
+        _Py_Initialized = 1;
         return 0;
     }
     /* TODO: Report exceptions rather than fatal errors below here */
@@ -837,7 +808,7 @@
         Py_XDECREF(warnings_module);
     }
 
-    _PyRuntime.initialized = 1;
+    _Py_Initialized = 1;
 
     if (!Py_NoSiteFlag)
         initsite(); /* Module site */
@@ -953,7 +924,7 @@
     PyThreadState *tstate;
     int status = 0;
 
-    if (!_PyRuntime.initialized)
+    if (!_Py_Initialized)
         return status;
 
     wait_for_thread_shutdown();
@@ -975,9 +946,9 @@
 
     /* Remaining threads (e.g. daemon threads) will automatically exit
        after taking the GIL (in PyEval_RestoreThread()). */
-    _PyRuntime.finalizing = tstate;
-    _PyRuntime.initialized = 0;
-    _PyRuntime.core_initialized = 0;
+    _Py_Finalizing = tstate;
+    _Py_Initialized = 0;
+    _Py_CoreInitialized = 0;
 
     /* Flush sys.stdout and sys.stderr */
     if (flush_std_files() < 0) {
@@ -1139,7 +1110,6 @@
 #endif
 
     call_ll_exitfuncs();
-    _PyRuntime_Finalize();
     return status;
 }
 
@@ -1169,7 +1139,7 @@
     PyThreadState *tstate, *save_tstate;
     PyObject *bimod, *sysmod;
 
-    if (!_PyRuntime.initialized)
+    if (!_Py_Initialized)
         Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
 
 #ifdef WITH_THREAD
@@ -1884,19 +1854,20 @@
 #  include "pythread.h"
 #endif
 
+static void (*pyexitfunc)(void) = NULL;
 /* For the atexit module. */
 void _Py_PyAtExit(void (*func)(void))
 {
-    _PyRuntime.pyexitfunc = func;
+    pyexitfunc = func;
 }
 
 static void
 call_py_exitfuncs(void)
 {
-    if (_PyRuntime.pyexitfunc == NULL)
+    if (pyexitfunc == NULL)
         return;
 
-    (*_PyRuntime.pyexitfunc)();
+    (*pyexitfunc)();
     PyErr_Clear();
 }
 
@@ -1929,19 +1900,22 @@
 }
 
 #define NEXITFUNCS 32
+static void (*exitfuncs[NEXITFUNCS])(void);
+static int nexitfuncs = 0;
+
 int Py_AtExit(void (*func)(void))
 {
-    if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
+    if (nexitfuncs >= NEXITFUNCS)
         return -1;
-    _PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
+    exitfuncs[nexitfuncs++] = func;
     return 0;
 }
 
 static void
 call_ll_exitfuncs(void)
 {
-    while (_PyRuntime.nexitfuncs > 0)
-        (*_PyRuntime.exitfuncs[--_PyRuntime.nexitfuncs])();
+    while (nexitfuncs > 0)
+        (*exitfuncs[--nexitfuncs])();
 
     fflush(stdout);
     fflush(stderr);