(root)/
Python-3.11.7/
Python/
pystate.c
       1  
       2  /* Thread and interpreter state structures and their interfaces */
       3  
       4  #include "Python.h"
       5  #include "pycore_ceval.h"
       6  #include "pycore_code.h"           // stats
       7  #include "pycore_frame.h"
       8  #include "pycore_initconfig.h"
       9  #include "pycore_object.h"        // _PyType_InitCache()
      10  #include "pycore_pyerrors.h"
      11  #include "pycore_pylifecycle.h"
      12  #include "pycore_pymem.h"         // _PyMem_SetDefaultAllocator()
      13  #include "pycore_pystate.h"       // _PyThreadState_GET()
      14  #include "pycore_runtime_init.h"  // _PyRuntimeState_INIT
      15  #include "pycore_sysmodule.h"
      16  
      17  /* --------------------------------------------------------------------------
      18  CAUTION
      19  
      20  Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file.  A
      21  number of these functions are advertised as safe to call when the GIL isn't
      22  held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
      23  debugging obmalloc functions.  Those aren't thread-safe (they rely on the GIL
      24  to avoid the expense of doing their own locking).
      25  -------------------------------------------------------------------------- */
      26  
      27  #ifdef HAVE_DLOPEN
      28  #ifdef HAVE_DLFCN_H
      29  #include <dlfcn.h>
      30  #endif
      31  #if !HAVE_DECL_RTLD_LAZY
      32  #define RTLD_LAZY 1
      33  #endif
      34  #endif
      35  
      36  #ifdef __cplusplus
      37  extern "C" {
      38  #endif
      39  
      40  #define _PyRuntimeGILState_GetThreadState(gilstate) \
      41      ((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
      42  #define _PyRuntimeGILState_SetThreadState(gilstate, value) \
      43      _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
      44                               (uintptr_t)(value))
      45  
      46  /* Forward declarations */
      47  static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
      48  static void _PyThreadState_Delete(PyThreadState *tstate, int check_current);
      49  
      50  /* Suppress deprecation warning for PyBytesObject.ob_shash */
      51  _Py_COMP_DIAG_PUSH
      52  _Py_COMP_DIAG_IGNORE_DEPR_DECLS
      53  /* We use "initial" if the runtime gets re-used
      54     (e.g. Py_Finalize() followed by Py_Initialize(). */
      55  static const _PyRuntimeState initial = _PyRuntimeState_INIT;
      56  _Py_COMP_DIAG_POP
      57  
      58  static int
      59  alloc_for_runtime(PyThread_type_lock *plock1, PyThread_type_lock *plock2,
      60                    PyThread_type_lock *plock3)
      61  {
      62      /* Force default allocator, since _PyRuntimeState_Fini() must
      63         use the same allocator than this function. */
      64      PyMemAllocatorEx old_alloc;
      65      _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
      66  
      67      PyThread_type_lock lock1 = PyThread_allocate_lock();
      68      if (lock1 == NULL) {
      69          return -1;
      70      }
      71  
      72      PyThread_type_lock lock2 = PyThread_allocate_lock();
      73      if (lock2 == NULL) {
      74          PyThread_free_lock(lock1);
      75          return -1;
      76      }
      77  
      78      PyThread_type_lock lock3 = PyThread_allocate_lock();
      79      if (lock3 == NULL) {
      80          PyThread_free_lock(lock1);
      81          PyThread_free_lock(lock2);
      82          return -1;
      83      }
      84  
      85      PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
      86  
      87      *plock1 = lock1;
      88      *plock2 = lock2;
      89      *plock3 = lock3;
      90      return 0;
      91  }
      92  
      93  static void
      94  init_runtime(_PyRuntimeState *runtime,
      95               void *open_code_hook, void *open_code_userdata,
      96               _Py_AuditHookEntry *audit_hook_head,
      97               Py_ssize_t unicode_next_index,
      98               PyThread_type_lock unicode_ids_mutex,
      99               PyThread_type_lock interpreters_mutex,
     100               PyThread_type_lock xidregistry_mutex)
     101  {
     102      if (runtime->_initialized) {
     103          Py_FatalError("runtime already initialized");
     104      }
     105      assert(!runtime->preinitializing &&
     106             !runtime->preinitialized &&
     107             !runtime->core_initialized &&
     108             !runtime->initialized);
     109  
     110      runtime->open_code_hook = open_code_hook;
     111      runtime->open_code_userdata = open_code_userdata;
     112      runtime->audit_hook_head = audit_hook_head;
     113  
     114      _PyEval_InitRuntimeState(&runtime->ceval);
     115  
     116      PyPreConfig_InitPythonConfig(&runtime->preconfig);
     117  
     118      runtime->interpreters.mutex = interpreters_mutex;
     119  
     120      runtime->xidregistry.mutex = xidregistry_mutex;
     121  
     122      // Set it to the ID of the main thread of the main interpreter.
     123      runtime->main_thread = PyThread_get_thread_ident();
     124  
     125      runtime->unicode_ids.next_index = unicode_next_index;
     126      runtime->unicode_ids.lock = unicode_ids_mutex;
     127  
     128      runtime->_initialized = 1;
     129  }
     130  
     131  PyStatus
     132  _PyRuntimeState_Init(_PyRuntimeState *runtime)
     133  {
     134      /* We preserve the hook across init, because there is
     135         currently no public API to set it between runtime
     136         initialization and interpreter initialization. */
     137      void *open_code_hook = runtime->open_code_hook;
     138      void *open_code_userdata = runtime->open_code_userdata;
     139      _Py_AuditHookEntry *audit_hook_head = runtime->audit_hook_head;
     140      // bpo-42882: Preserve next_index value if Py_Initialize()/Py_Finalize()
     141      // is called multiple times.
     142      Py_ssize_t unicode_next_index = runtime->unicode_ids.next_index;
     143  
     144      PyThread_type_lock lock1, lock2, lock3;
     145      if (alloc_for_runtime(&lock1, &lock2, &lock3) != 0) {
     146          return _PyStatus_NO_MEMORY();
     147      }
     148  
     149      if (runtime->_initialized) {
     150          // Py_Initialize() must be running again.
     151          // Reset to _PyRuntimeState_INIT.
     152          memcpy(runtime, &initial, sizeof(*runtime));
     153      }
     154      init_runtime(runtime, open_code_hook, open_code_userdata, audit_hook_head,
     155                   unicode_next_index, lock1, lock2, lock3);
     156  
     157      return _PyStatus_OK();
     158  }
     159  
     160  void
     161  _PyRuntimeState_Fini(_PyRuntimeState *runtime)
     162  {
     163      /* Force the allocator used by _PyRuntimeState_Init(). */
     164      PyMemAllocatorEx old_alloc;
     165      _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
     166  #define FREE_LOCK(LOCK) \
     167      if (LOCK != NULL) { \
     168          PyThread_free_lock(LOCK); \
     169          LOCK = NULL; \
     170      }
     171  
     172      FREE_LOCK(runtime->interpreters.mutex);
     173      FREE_LOCK(runtime->xidregistry.mutex);
     174      FREE_LOCK(runtime->unicode_ids.lock);
     175  
     176  #undef FREE_LOCK
     177      PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
     178  }
     179  
     180  #ifdef HAVE_FORK
     181  /* This function is called from PyOS_AfterFork_Child to ensure that
     182     newly created child processes do not share locks with the parent. */
     183  PyStatus
     184  _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
     185  {
     186      // This was initially set in _PyRuntimeState_Init().
     187      runtime->main_thread = PyThread_get_thread_ident();
     188  
     189      /* Force default allocator, since _PyRuntimeState_Fini() must
     190         use the same allocator than this function. */
     191      PyMemAllocatorEx old_alloc;
     192      _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
     193  
     194      int reinit_interp = _PyThread_at_fork_reinit(&runtime->interpreters.mutex);
     195      int reinit_xidregistry = _PyThread_at_fork_reinit(&runtime->xidregistry.mutex);
     196      int reinit_unicode_ids = _PyThread_at_fork_reinit(&runtime->unicode_ids.lock);
     197  
     198      PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
     199  
     200      /* bpo-42540: id_mutex is freed by _PyInterpreterState_Delete, which does
     201       * not force the default allocator. */
     202      int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
     203  
     204      if (reinit_interp < 0
     205          || reinit_main_id < 0
     206          || reinit_xidregistry < 0
     207          || reinit_unicode_ids < 0)
     208      {
     209          return _PyStatus_ERR("Failed to reinitialize runtime locks");
     210  
     211      }
     212      return _PyStatus_OK();
     213  }
     214  #endif
     215  
     216  #define HEAD_LOCK(runtime) \
     217      PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
     218  #define HEAD_UNLOCK(runtime) \
     219      PyThread_release_lock((runtime)->interpreters.mutex)
     220  
     221  /* Forward declaration */
     222  static void _PyGILState_NoteThreadState(
     223      struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);
     224  
     225  PyStatus
     226  _PyInterpreterState_Enable(_PyRuntimeState *runtime)
     227  {
     228      struct pyinterpreters *interpreters = &runtime->interpreters;
     229      interpreters->next_id = 0;
     230  
     231      /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
     232         Create a new mutex if needed. */
     233      if (interpreters->mutex == NULL) {
     234          /* Force default allocator, since _PyRuntimeState_Fini() must
     235             use the same allocator than this function. */
     236          PyMemAllocatorEx old_alloc;
     237          _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
     238  
     239          interpreters->mutex = PyThread_allocate_lock();
     240  
     241          PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
     242  
     243          if (interpreters->mutex == NULL) {
     244              return _PyStatus_ERR("Can't initialize threads for interpreter");
     245          }
     246      }
     247  
     248      return _PyStatus_OK();
     249  }
     250  
     251  static PyInterpreterState *
     252  alloc_interpreter(void)
     253  {
     254      return PyMem_RawCalloc(1, sizeof(PyInterpreterState));
     255  }
     256  
     257  static void
     258  free_interpreter(PyInterpreterState *interp)
     259  {
     260      if (!interp->_static) {
     261          PyMem_RawFree(interp);
     262      }
     263  }
     264  
     265  /* Get the interpreter state to a minimal consistent state.
     266     Further init happens in pylifecycle.c before it can be used.
     267     All fields not initialized here are expected to be zeroed out,
     268     e.g. by PyMem_RawCalloc() or memset(), or otherwise pre-initialized.
     269     The runtime state is not manipulated.  Instead it is assumed that
     270     the interpreter is getting added to the runtime.
     271    */
     272  
     273  static void
     274  init_interpreter(PyInterpreterState *interp,
     275                   _PyRuntimeState *runtime, int64_t id,
     276                   PyInterpreterState *next,
     277                   PyThread_type_lock pending_lock)
     278  {
     279      if (interp->_initialized) {
     280          Py_FatalError("interpreter already initialized");
     281      }
     282  
     283      assert(runtime != NULL);
     284      interp->runtime = runtime;
     285  
     286      assert(id > 0 || (id == 0 && interp == runtime->interpreters.main));
     287      interp->id = id;
     288  
     289      assert(runtime->interpreters.head == interp);
     290      assert(next != NULL || (interp == runtime->interpreters.main));
     291      interp->next = next;
     292  
     293      _PyEval_InitState(&interp->ceval, pending_lock);
     294      _PyGC_InitState(&interp->gc);
     295      PyConfig_InitPythonConfig(&interp->config);
     296      _PyType_InitCache(interp);
     297  
     298      interp->_initialized = 1;
     299  }
     300  
     301  PyInterpreterState *
     302  PyInterpreterState_New(void)
     303  {
     304      PyInterpreterState *interp;
     305      PyThreadState *tstate = _PyThreadState_GET();
     306  
     307      /* tstate is NULL when Py_InitializeFromConfig() calls
     308         PyInterpreterState_New() to create the main interpreter. */
     309      if (_PySys_Audit(tstate, "cpython.PyInterpreterState_New", NULL) < 0) {
     310          return NULL;
     311      }
     312  
     313      PyThread_type_lock pending_lock = PyThread_allocate_lock();
     314      if (pending_lock == NULL) {
     315          if (tstate != NULL) {
     316              _PyErr_NoMemory(tstate);
     317          }
     318          return NULL;
     319      }
     320  
     321      /* Don't get runtime from tstate since tstate can be NULL. */
     322      _PyRuntimeState *runtime = &_PyRuntime;
     323      struct pyinterpreters *interpreters = &runtime->interpreters;
     324  
     325      /* We completely serialize creation of multiple interpreters, since
     326         it simplifies things here and blocking concurrent calls isn't a problem.
     327         Regardless, we must fully block subinterpreter creation until
     328         after the main interpreter is created. */
     329      HEAD_LOCK(runtime);
     330  
     331      int64_t id = interpreters->next_id;
     332      interpreters->next_id += 1;
     333  
     334      // Allocate the interpreter and add it to the runtime state.
     335      PyInterpreterState *old_head = interpreters->head;
     336      if (old_head == NULL) {
     337          // We are creating the main interpreter.
     338          assert(interpreters->main == NULL);
     339          assert(id == 0);
     340  
     341          interp = &runtime->_main_interpreter;
     342          assert(interp->id == 0);
     343          assert(interp->next == NULL);
     344          assert(interp->_static);
     345  
     346          interpreters->main = interp;
     347      }
     348      else {
     349          assert(interpreters->main != NULL);
     350          assert(id != 0);
     351  
     352          interp = alloc_interpreter();
     353          if (interp == NULL) {
     354              goto error;
     355          }
     356          // Set to _PyInterpreterState_INIT.
     357          memcpy(interp, &initial._main_interpreter,
     358                 sizeof(*interp));
     359          // We need to adjust any fields that are different from the initial
     360          // interpreter (as defined in _PyInterpreterState_INIT):
     361          interp->_static = false;
     362  
     363          if (id < 0) {
     364              /* overflow or Py_Initialize() not called yet! */
     365              if (tstate != NULL) {
     366                  _PyErr_SetString(tstate, PyExc_RuntimeError,
     367                                   "failed to get an interpreter ID");
     368              }
     369              goto error;
     370          }
     371      }
     372      interpreters->head = interp;
     373  
     374      init_interpreter(interp, runtime, id, old_head, pending_lock);
     375  
     376      HEAD_UNLOCK(runtime);
     377      return interp;
     378  
     379  error:
     380      HEAD_UNLOCK(runtime);
     381  
     382      PyThread_free_lock(pending_lock);
     383      if (interp != NULL) {
     384          free_interpreter(interp);
     385      }
     386      return NULL;
     387  }
     388  
     389  
     390  static void
     391  interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
     392  {
     393      _PyRuntimeState *runtime = interp->runtime;
     394  
     395      if (_PySys_Audit(tstate, "cpython.PyInterpreterState_Clear", NULL) < 0) {
     396          _PyErr_Clear(tstate);
     397      }
     398  
     399      // Clear the current/main thread state last.
     400      HEAD_LOCK(runtime);
     401      PyThreadState *p = interp->threads.head;
     402      HEAD_UNLOCK(runtime);
     403      while (p != NULL) {
     404          // See https://github.com/python/cpython/issues/102126
     405          // Must be called without HEAD_LOCK held as it can deadlock
     406          // if any finalizer tries to acquire that lock.
     407          PyThreadState_Clear(p);
     408          HEAD_LOCK(runtime);
     409          p = p->next;
     410          HEAD_UNLOCK(runtime);
     411      }
     412  
     413      Py_CLEAR(interp->audit_hooks);
     414  
     415      PyConfig_Clear(&interp->config);
     416      Py_CLEAR(interp->codec_search_path);
     417      Py_CLEAR(interp->codec_search_cache);
     418      Py_CLEAR(interp->codec_error_registry);
     419      Py_CLEAR(interp->modules);
     420      Py_CLEAR(interp->modules_by_index);
     421      Py_CLEAR(interp->builtins_copy);
     422      Py_CLEAR(interp->importlib);
     423      Py_CLEAR(interp->import_func);
     424      Py_CLEAR(interp->dict);
     425  #ifdef HAVE_FORK
     426      Py_CLEAR(interp->before_forkers);
     427      Py_CLEAR(interp->after_forkers_parent);
     428      Py_CLEAR(interp->after_forkers_child);
     429  #endif
     430  
     431      _PyAST_Fini(interp);
     432      _PyWarnings_Fini(interp);
     433      _PyAtExit_Fini(interp);
     434  
     435      // All Python types must be destroyed before the last GC collection. Python
     436      // types create a reference cycle to themselves in their in their
     437      // PyTypeObject.tp_mro member (the tuple contains the type).
     438  
     439      /* Last garbage collection on this interpreter */
     440      _PyGC_CollectNoFail(tstate);
     441      _PyGC_Fini(interp);
     442  
     443      /* We don't clear sysdict and builtins until the end of this function.
     444         Because clearing other attributes can execute arbitrary Python code
     445         which requires sysdict and builtins. */
     446      PyDict_Clear(interp->sysdict);
     447      PyDict_Clear(interp->builtins);
     448      Py_CLEAR(interp->sysdict);
     449      Py_CLEAR(interp->builtins);
     450  
     451      // XXX Once we have one allocator per interpreter (i.e.
     452      // per-interpreter GC) we must ensure that all of the interpreter's
     453      // objects have been cleaned up at the point.
     454  }
     455  
     456  
     457  void
     458  PyInterpreterState_Clear(PyInterpreterState *interp)
     459  {
     460      // Use the current Python thread state to call audit hooks and to collect
     461      // garbage. It can be different than the current Python thread state
     462      // of 'interp'.
     463      PyThreadState *current_tstate = _PyThreadState_GET();
     464  
     465      interpreter_clear(interp, current_tstate);
     466  }
     467  
     468  
     469  void
     470  _PyInterpreterState_Clear(PyThreadState *tstate)
     471  {
     472      interpreter_clear(tstate->interp, tstate);
     473  }
     474  
     475  
     476  static void
     477  zapthreads(PyInterpreterState *interp, int check_current)
     478  {
     479      PyThreadState *tstate;
     480      /* No need to lock the mutex here because this should only happen
     481         when the threads are all really dead (XXX famous last words). */
     482      while ((tstate = interp->threads.head) != NULL) {
     483          _PyThreadState_Delete(tstate, check_current);
     484      }
     485  }
     486  
     487  
     488  void
     489  PyInterpreterState_Delete(PyInterpreterState *interp)
     490  {
     491      _PyRuntimeState *runtime = interp->runtime;
     492      struct pyinterpreters *interpreters = &runtime->interpreters;
     493      zapthreads(interp, 0);
     494  
     495      _PyEval_FiniState(&interp->ceval);
     496  
     497      /* Delete current thread. After this, many C API calls become crashy. */
     498      _PyThreadState_Swap(&runtime->gilstate, NULL);
     499  
     500      HEAD_LOCK(runtime);
     501      PyInterpreterState **p;
     502      for (p = &interpreters->head; ; p = &(*p)->next) {
     503          if (*p == NULL) {
     504              Py_FatalError("NULL interpreter");
     505          }
     506          if (*p == interp) {
     507              break;
     508          }
     509      }
     510      if (interp->threads.head != NULL) {
     511          Py_FatalError("remaining threads");
     512      }
     513      *p = interp->next;
     514  
     515      if (interpreters->main == interp) {
     516          interpreters->main = NULL;
     517          if (interpreters->head != NULL) {
     518              Py_FatalError("remaining subinterpreters");
     519          }
     520      }
     521      HEAD_UNLOCK(runtime);
     522  
     523      if (interp->id_mutex != NULL) {
     524          PyThread_free_lock(interp->id_mutex);
     525      }
     526      free_interpreter(interp);
     527  }
     528  
     529  
     530  #ifdef HAVE_FORK
     531  /*
     532   * Delete all interpreter states except the main interpreter.  If there
     533   * is a current interpreter state, it *must* be the main interpreter.
     534   */
     535  PyStatus
     536  _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
     537  {
     538      struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
     539      struct pyinterpreters *interpreters = &runtime->interpreters;
     540  
     541      PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
     542      if (tstate != NULL && tstate->interp != interpreters->main) {
     543          return _PyStatus_ERR("not main interpreter");
     544      }
     545  
     546      HEAD_LOCK(runtime);
     547      PyInterpreterState *interp = interpreters->head;
     548      interpreters->head = NULL;
     549      while (interp != NULL) {
     550          if (interp == interpreters->main) {
     551              interpreters->main->next = NULL;
     552              interpreters->head = interp;
     553              interp = interp->next;
     554              continue;
     555          }
     556  
     557          PyInterpreterState_Clear(interp);  // XXX must activate?
     558          zapthreads(interp, 1);
     559          if (interp->id_mutex != NULL) {
     560              PyThread_free_lock(interp->id_mutex);
     561          }
     562          PyInterpreterState *prev_interp = interp;
     563          interp = interp->next;
     564          free_interpreter(prev_interp);
     565      }
     566      HEAD_UNLOCK(runtime);
     567  
     568      if (interpreters->head == NULL) {
     569          return _PyStatus_ERR("missing main interpreter");
     570      }
     571      _PyThreadState_Swap(gilstate, tstate);
     572      return _PyStatus_OK();
     573  }
     574  #endif
     575  
     576  
     577  PyInterpreterState *
     578  PyInterpreterState_Get(void)
     579  {
     580      PyThreadState *tstate = _PyThreadState_GET();
     581      _Py_EnsureTstateNotNULL(tstate);
     582      PyInterpreterState *interp = tstate->interp;
     583      if (interp == NULL) {
     584          Py_FatalError("no current interpreter");
     585      }
     586      return interp;
     587  }
     588  
     589  
     590  int64_t
     591  PyInterpreterState_GetID(PyInterpreterState *interp)
     592  {
     593      if (interp == NULL) {
     594          PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
     595          return -1;
     596      }
     597      return interp->id;
     598  }
     599  
     600  
     601  static PyInterpreterState *
     602  interp_look_up_id(_PyRuntimeState *runtime, int64_t requested_id)
     603  {
     604      PyInterpreterState *interp = runtime->interpreters.head;
     605      while (interp != NULL) {
     606          int64_t id = PyInterpreterState_GetID(interp);
     607          if (id < 0) {
     608              return NULL;
     609          }
     610          if (requested_id == id) {
     611              return interp;
     612          }
     613          interp = PyInterpreterState_Next(interp);
     614      }
     615      return NULL;
     616  }
     617  
     618  PyInterpreterState *
     619  _PyInterpreterState_LookUpID(int64_t requested_id)
     620  {
     621      PyInterpreterState *interp = NULL;
     622      if (requested_id >= 0) {
     623          _PyRuntimeState *runtime = &_PyRuntime;
     624          HEAD_LOCK(runtime);
     625          interp = interp_look_up_id(runtime, requested_id);
     626          HEAD_UNLOCK(runtime);
     627      }
     628      if (interp == NULL && !PyErr_Occurred()) {
     629          PyErr_Format(PyExc_RuntimeError,
     630                       "unrecognized interpreter ID %lld", requested_id);
     631      }
     632      return interp;
     633  }
     634  
     635  
     636  int
     637  _PyInterpreterState_IDInitref(PyInterpreterState *interp)
     638  {
     639      if (interp->id_mutex != NULL) {
     640          return 0;
     641      }
     642      interp->id_mutex = PyThread_allocate_lock();
     643      if (interp->id_mutex == NULL) {
     644          PyErr_SetString(PyExc_RuntimeError,
     645                          "failed to create init interpreter ID mutex");
     646          return -1;
     647      }
     648      interp->id_refcount = 0;
     649      return 0;
     650  }
     651  
     652  
     653  int
     654  _PyInterpreterState_IDIncref(PyInterpreterState *interp)
     655  {
     656      if (_PyInterpreterState_IDInitref(interp) < 0) {
     657          return -1;
     658      }
     659  
     660      PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
     661      interp->id_refcount += 1;
     662      PyThread_release_lock(interp->id_mutex);
     663      return 0;
     664  }
     665  
     666  
     667  void
     668  _PyInterpreterState_IDDecref(PyInterpreterState *interp)
     669  {
     670      assert(interp->id_mutex != NULL);
     671  
     672      struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
     673      PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
     674      assert(interp->id_refcount != 0);
     675      interp->id_refcount -= 1;
     676      int64_t refcount = interp->id_refcount;
     677      PyThread_release_lock(interp->id_mutex);
     678  
     679      if (refcount == 0 && interp->requires_idref) {
     680          // XXX Using the "head" thread isn't strictly correct.
     681          PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
     682          // XXX Possible GILState issues?
     683          PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate);
     684          Py_EndInterpreter(tstate);
     685          _PyThreadState_Swap(gilstate, save_tstate);
     686      }
     687  }
     688  
     689  int
     690  _PyInterpreterState_RequiresIDRef(PyInterpreterState *interp)
     691  {
     692      return interp->requires_idref;
     693  }
     694  
     695  void
     696  _PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
     697  {
     698      interp->requires_idref = required ? 1 : 0;
     699  }
     700  
     701  PyObject *
     702  _PyInterpreterState_GetMainModule(PyInterpreterState *interp)
     703  {
     704      if (interp->modules == NULL) {
     705          PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
     706          return NULL;
     707      }
     708      return PyMapping_GetItemString(interp->modules, "__main__");
     709  }
     710  
     711  PyObject *
     712  PyInterpreterState_GetDict(PyInterpreterState *interp)
     713  {
     714      if (interp->dict == NULL) {
     715          interp->dict = PyDict_New();
     716          if (interp->dict == NULL) {
     717              PyErr_Clear();
     718          }
     719      }
     720      /* Returning NULL means no per-interpreter dict is available. */
     721      return interp->dict;
     722  }
     723  
     724  /* Minimum size of data stack chunk */
     725  #define DATA_STACK_CHUNK_SIZE (16*1024)
     726  
     727  static _PyStackChunk*
     728  allocate_chunk(int size_in_bytes, _PyStackChunk* previous)
     729  {
     730      assert(size_in_bytes % sizeof(PyObject **) == 0);
     731      _PyStackChunk *res = _PyObject_VirtualAlloc(size_in_bytes);
     732      if (res == NULL) {
     733          return NULL;
     734      }
     735      res->previous = previous;
     736      res->size = size_in_bytes;
     737      res->top = 0;
     738      return res;
     739  }
     740  
     741  static PyThreadState *
     742  alloc_threadstate(void)
     743  {
     744      return PyMem_RawCalloc(1, sizeof(PyThreadState));
     745  }
     746  
     747  static void
     748  free_threadstate(PyThreadState *tstate)
     749  {
     750      if (!tstate->_static) {
     751          PyMem_RawFree(tstate);
     752      }
     753  }
     754  
     755  /* Get the thread state to a minimal consistent state.
     756     Further init happens in pylifecycle.c before it can be used.
     757     All fields not initialized here are expected to be zeroed out,
     758     e.g. by PyMem_RawCalloc() or memset(), or otherwise pre-initialized.
     759     The interpreter state is not manipulated.  Instead it is assumed that
     760     the thread is getting added to the interpreter.
     761    */
     762  
     763  static void
     764  init_threadstate(PyThreadState *tstate,
     765                   PyInterpreterState *interp, uint64_t id,
     766                   PyThreadState *next)
     767  {
     768      if (tstate->_initialized) {
     769          Py_FatalError("thread state already initialized");
     770      }
     771  
     772      assert(interp != NULL);
     773      tstate->interp = interp;
     774  
     775      assert(id > 0);
     776      tstate->id = id;
     777  
     778      assert(interp->threads.head == tstate);
     779      assert((next != NULL && id != 1) || (next == NULL && id == 1));
     780      if (next != NULL) {
     781          assert(next->prev == NULL || next->prev == tstate);
     782          next->prev = tstate;
     783      }
     784      tstate->next = next;
     785      assert(tstate->prev == NULL);
     786  
     787      tstate->thread_id = PyThread_get_thread_ident();
     788  #ifdef PY_HAVE_THREAD_NATIVE_ID
     789      tstate->native_thread_id = PyThread_get_thread_native_id();
     790  #endif
     791  
     792      tstate->recursion_limit = interp->ceval.recursion_limit,
     793      tstate->recursion_remaining = interp->ceval.recursion_limit,
     794  
     795      tstate->exc_info = &tstate->exc_state;
     796  
     797      tstate->cframe = &tstate->root_cframe;
     798      tstate->datastack_chunk = NULL;
     799      tstate->datastack_top = NULL;
     800      tstate->datastack_limit = NULL;
     801  
     802      tstate->_initialized = 1;
     803  }
     804  
     805  static PyThreadState *
     806  new_threadstate(PyInterpreterState *interp)
     807  {
     808      PyThreadState *tstate;
     809      _PyRuntimeState *runtime = interp->runtime;
     810      // We don't need to allocate a thread state for the main interpreter
     811      // (the common case), but doing it later for the other case revealed a
     812      // reentrancy problem (deadlock).  So for now we always allocate before
     813      // taking the interpreters lock.  See GH-96071.
     814      PyThreadState *new_tstate = alloc_threadstate();
     815      int used_newtstate;
     816      if (new_tstate == NULL) {
     817          return NULL;
     818      }
     819      /* We serialize concurrent creation to protect global state. */
     820      HEAD_LOCK(runtime);
     821  
     822      interp->threads.next_unique_id += 1;
     823      uint64_t id = interp->threads.next_unique_id;
     824  
     825      // Allocate the thread state and add it to the interpreter.
     826      PyThreadState *old_head = interp->threads.head;
     827      if (old_head == NULL) {
     828          // It's the interpreter's initial thread state.
     829          assert(id == 1);
     830          used_newtstate = 0;
     831          tstate = &interp->_initial_thread;
     832          assert(tstate->_static);
     833      }
     834      else {
     835          // Every valid interpreter must have at least one thread.
     836          assert(id > 1);
     837          assert(old_head->prev == NULL);
     838          used_newtstate = 1;
     839          tstate = new_tstate;
     840          // Set to _PyThreadState_INIT.
     841          memcpy(tstate,
     842                 &initial._main_interpreter._initial_thread,
     843                 sizeof(*tstate));
     844          // We need to adjust any fields that are different from the initial
     845          // thread (as defined in _PyThreadState_INIT):
     846          tstate->_static = false;
     847      }
     848      interp->threads.head = tstate;
     849  
     850      init_threadstate(tstate, interp, id, old_head);
     851  
     852      HEAD_UNLOCK(runtime);
     853      if (!used_newtstate) {
     854          // Must be called with lock unlocked to avoid re-entrancy deadlock.
     855          PyMem_RawFree(new_tstate);
     856      }
     857      return tstate;
     858  }
     859  
     860  PyThreadState *
     861  PyThreadState_New(PyInterpreterState *interp)
     862  {
     863      PyThreadState *tstate = new_threadstate(interp);
     864      _PyThreadState_SetCurrent(tstate);
     865      return tstate;
     866  }
     867  
     868  PyThreadState *
     869  _PyThreadState_Prealloc(PyInterpreterState *interp)
     870  {
     871      return new_threadstate(interp);
     872  }
     873  
     874  // We keep this around for (accidental) stable ABI compatibility.
     875  // Realisically, no extensions are using it.
     876  void
     877  _PyThreadState_Init(PyThreadState *tstate)
     878  {
     879      Py_FatalError("_PyThreadState_Init() is for internal use only");
     880  }
     881  
     882  void
     883  _PyThreadState_SetCurrent(PyThreadState *tstate)
     884  {
     885      // gh-104690: If Python is being finalized and PyInterpreterState_Delete()
     886      // was called, tstate becomes a dangling pointer.
     887      assert(_PyThreadState_CheckConsistency(tstate));
     888  
     889      _PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
     890  }
     891  
     892  PyObject*
     893  PyState_FindModule(PyModuleDef* module)
     894  {
     895      Py_ssize_t index = module->m_base.m_index;
     896      PyInterpreterState *state = _PyInterpreterState_GET();
     897      PyObject *res;
     898      if (module->m_slots) {
     899          return NULL;
     900      }
     901      if (index == 0)
     902          return NULL;
     903      if (state->modules_by_index == NULL)
     904          return NULL;
     905      if (index >= PyList_GET_SIZE(state->modules_by_index))
     906          return NULL;
     907      res = PyList_GET_ITEM(state->modules_by_index, index);
     908      return res==Py_None ? NULL : res;
     909  }
     910  
     911  int
     912  _PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def)
     913  {
     914      if (!def) {
     915          assert(_PyErr_Occurred(tstate));
     916          return -1;
     917      }
     918      if (def->m_slots) {
     919          _PyErr_SetString(tstate,
     920                           PyExc_SystemError,
     921                           "PyState_AddModule called on module with slots");
     922          return -1;
     923      }
     924  
     925      PyInterpreterState *interp = tstate->interp;
     926      if (!interp->modules_by_index) {
     927          interp->modules_by_index = PyList_New(0);
     928          if (!interp->modules_by_index) {
     929              return -1;
     930          }
     931      }
     932  
     933      while (PyList_GET_SIZE(interp->modules_by_index) <= def->m_base.m_index) {
     934          if (PyList_Append(interp->modules_by_index, Py_None) < 0) {
     935              return -1;
     936          }
     937      }
     938  
     939      Py_INCREF(module);
     940      return PyList_SetItem(interp->modules_by_index,
     941                            def->m_base.m_index, module);
     942  }
     943  
     944  int
     945  PyState_AddModule(PyObject* module, PyModuleDef* def)
     946  {
     947      if (!def) {
     948          Py_FatalError("module definition is NULL");
     949          return -1;
     950      }
     951  
     952      PyThreadState *tstate = _PyThreadState_GET();
     953      PyInterpreterState *interp = tstate->interp;
     954      Py_ssize_t index = def->m_base.m_index;
     955      if (interp->modules_by_index &&
     956          index < PyList_GET_SIZE(interp->modules_by_index) &&
     957          module == PyList_GET_ITEM(interp->modules_by_index, index))
     958      {
     959          _Py_FatalErrorFormat(__func__, "module %p already added", module);
     960          return -1;
     961      }
     962      return _PyState_AddModule(tstate, module, def);
     963  }
     964  
     965  int
     966  PyState_RemoveModule(PyModuleDef* def)
     967  {
     968      PyThreadState *tstate = _PyThreadState_GET();
     969      PyInterpreterState *interp = tstate->interp;
     970  
     971      if (def->m_slots) {
     972          _PyErr_SetString(tstate,
     973                           PyExc_SystemError,
     974                           "PyState_RemoveModule called on module with slots");
     975          return -1;
     976      }
     977  
     978      Py_ssize_t index = def->m_base.m_index;
     979      if (index == 0) {
     980          Py_FatalError("invalid module index");
     981      }
     982      if (interp->modules_by_index == NULL) {
     983          Py_FatalError("Interpreters module-list not accessible.");
     984      }
     985      if (index > PyList_GET_SIZE(interp->modules_by_index)) {
     986          Py_FatalError("Module index out of bounds.");
     987      }
     988  
     989      Py_INCREF(Py_None);
     990      return PyList_SetItem(interp->modules_by_index, index, Py_None);
     991  }
     992  
     993  // Used by finalize_modules()
     994  void
     995  _PyInterpreterState_ClearModules(PyInterpreterState *interp)
     996  {
     997      if (!interp->modules_by_index) {
     998          return;
     999      }
    1000  
    1001      Py_ssize_t i;
    1002      for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); i++) {
    1003          PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i);
    1004          if (PyModule_Check(m)) {
    1005              /* cleanup the saved copy of module dicts */
    1006              PyModuleDef *md = PyModule_GetDef(m);
    1007              if (md) {
    1008                  Py_CLEAR(md->m_base.m_copy);
    1009              }
    1010          }
    1011      }
    1012  
    1013      /* Setting modules_by_index to NULL could be dangerous, so we
    1014         clear the list instead. */
    1015      if (PyList_SetSlice(interp->modules_by_index,
    1016                          0, PyList_GET_SIZE(interp->modules_by_index),
    1017                          NULL)) {
    1018          PyErr_WriteUnraisable(interp->modules_by_index);
    1019      }
    1020  }
    1021  
    1022  void
    1023  PyThreadState_Clear(PyThreadState *tstate)
    1024  {
    1025      int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
    1026  
    1027      if (verbose && tstate->cframe->current_frame != NULL) {
    1028          /* bpo-20526: After the main thread calls
    1029             _PyRuntimeState_SetFinalizing() in Py_FinalizeEx(), threads must
    1030             exit when trying to take the GIL. If a thread exit in the middle of
    1031             _PyEval_EvalFrameDefault(), tstate->frame is not reset to its
    1032             previous value. It is more likely with daemon threads, but it can
    1033             happen with regular threads if threading._shutdown() fails
    1034             (ex: interrupted by CTRL+C). */
    1035          fprintf(stderr,
    1036            "PyThreadState_Clear: warning: thread still has a frame\n");
    1037      }
    1038  
    1039      /* Don't clear tstate->pyframe: it is a borrowed reference */
    1040  
    1041      Py_CLEAR(tstate->dict);
    1042      Py_CLEAR(tstate->async_exc);
    1043  
    1044      Py_CLEAR(tstate->curexc_type);
    1045      Py_CLEAR(tstate->curexc_value);
    1046      Py_CLEAR(tstate->curexc_traceback);
    1047  
    1048      Py_CLEAR(tstate->exc_state.exc_value);
    1049  
    1050      /* The stack of exception states should contain just this thread. */
    1051      if (verbose && tstate->exc_info != &tstate->exc_state) {
    1052          fprintf(stderr,
    1053            "PyThreadState_Clear: warning: thread still has a generator\n");
    1054      }
    1055  
    1056      tstate->c_profilefunc = NULL;
    1057      tstate->c_tracefunc = NULL;
    1058      Py_CLEAR(tstate->c_profileobj);
    1059      Py_CLEAR(tstate->c_traceobj);
    1060  
    1061      Py_CLEAR(tstate->async_gen_firstiter);
    1062      Py_CLEAR(tstate->async_gen_finalizer);
    1063  
    1064      Py_CLEAR(tstate->context);
    1065  
    1066      if (tstate->on_delete != NULL) {
    1067          tstate->on_delete(tstate->on_delete_data);
    1068      }
    1069  }
    1070  
    1071  
    1072  /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
    1073  static void
    1074  tstate_delete_common(PyThreadState *tstate,
    1075                       struct _gilstate_runtime_state *gilstate)
    1076  {
    1077      _Py_EnsureTstateNotNULL(tstate);
    1078      PyInterpreterState *interp = tstate->interp;
    1079      if (interp == NULL) {
    1080          Py_FatalError("NULL interpreter");
    1081      }
    1082      _PyRuntimeState *runtime = interp->runtime;
    1083  
    1084      HEAD_LOCK(runtime);
    1085      if (tstate->prev) {
    1086          tstate->prev->next = tstate->next;
    1087      }
    1088      else {
    1089          interp->threads.head = tstate->next;
    1090      }
    1091      if (tstate->next) {
    1092          tstate->next->prev = tstate->prev;
    1093      }
    1094      HEAD_UNLOCK(runtime);
    1095  
    1096      if (gilstate->autoInterpreterState &&
    1097          PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
    1098      {
    1099          PyThread_tss_set(&gilstate->autoTSSkey, NULL);
    1100      }
    1101      _PyStackChunk *chunk = tstate->datastack_chunk;
    1102      tstate->datastack_chunk = NULL;
    1103      while (chunk != NULL) {
    1104          _PyStackChunk *prev = chunk->previous;
    1105          _PyObject_VirtualFree(chunk, chunk->size);
    1106          chunk = prev;
    1107      }
    1108  }
    1109  
    1110  static void
    1111  _PyThreadState_Delete(PyThreadState *tstate, int check_current)
    1112  {
    1113      struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
    1114      if (check_current) {
    1115          if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
    1116              _Py_FatalErrorFormat(__func__, "tstate %p is still current", tstate);
    1117          }
    1118      }
    1119      tstate_delete_common(tstate, gilstate);
    1120      free_threadstate(tstate);
    1121  }
    1122  
    1123  
    1124  void
    1125  PyThreadState_Delete(PyThreadState *tstate)
    1126  {
    1127      _PyThreadState_Delete(tstate, 1);
    1128  }
    1129  
    1130  
    1131  void
    1132  _PyThreadState_DeleteCurrent(PyThreadState *tstate)
    1133  {
    1134      _Py_EnsureTstateNotNULL(tstate);
    1135      struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
    1136      tstate_delete_common(tstate, gilstate);
    1137      _PyRuntimeGILState_SetThreadState(gilstate, NULL);
    1138      _PyEval_ReleaseLock(tstate);
    1139      free_threadstate(tstate);
    1140  }
    1141  
    1142  void
    1143  PyThreadState_DeleteCurrent(void)
    1144  {
    1145      struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
    1146      PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
    1147      _PyThreadState_DeleteCurrent(tstate);
    1148  }
    1149  
    1150  
    1151  /*
    1152   * Delete all thread states except the one passed as argument.
    1153   * Note that, if there is a current thread state, it *must* be the one
    1154   * passed as argument.  Also, this won't touch any other interpreters
    1155   * than the current one, since we don't know which thread state should
    1156   * be kept in those other interpreters.
    1157   */
    1158  void
    1159  _PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
    1160  {
    1161      PyInterpreterState *interp = tstate->interp;
    1162  
    1163      HEAD_LOCK(runtime);
    1164      /* Remove all thread states, except tstate, from the linked list of
    1165         thread states.  This will allow calling PyThreadState_Clear()
    1166         without holding the lock. */
    1167      PyThreadState *list = interp->threads.head;
    1168      if (list == tstate) {
    1169          list = tstate->next;
    1170      }
    1171      if (tstate->prev) {
    1172          tstate->prev->next = tstate->next;
    1173      }
    1174      if (tstate->next) {
    1175          tstate->next->prev = tstate->prev;
    1176      }
    1177      tstate->prev = tstate->next = NULL;
    1178      interp->threads.head = tstate;
    1179      HEAD_UNLOCK(runtime);
    1180  
    1181      /* Clear and deallocate all stale thread states.  Even if this
    1182         executes Python code, we should be safe since it executes
    1183         in the current thread, not one of the stale threads. */
    1184      PyThreadState *p, *next;
    1185      for (p = list; p; p = next) {
    1186          next = p->next;
    1187          PyThreadState_Clear(p);
    1188          free_threadstate(p);
    1189      }
    1190  }
    1191  
    1192  
    1193  PyThreadState *
    1194  _PyThreadState_UncheckedGet(void)
    1195  {
    1196      return _PyThreadState_GET();
    1197  }
    1198  
    1199  
    1200  PyThreadState *
    1201  PyThreadState_Get(void)
    1202  {
    1203      PyThreadState *tstate = _PyThreadState_GET();
    1204      _Py_EnsureTstateNotNULL(tstate);
    1205      return tstate;
    1206  }
    1207  
    1208  
    1209  PyThreadState *
    1210  _PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
    1211  {
    1212      PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
    1213  
    1214      _PyRuntimeGILState_SetThreadState(gilstate, newts);
    1215      /* It should not be possible for more than one thread state
    1216         to be used for a thread.  Check this the best we can in debug
    1217         builds.
    1218      */
    1219  #if defined(Py_DEBUG)
    1220      if (newts) {
    1221          /* This can be called from PyEval_RestoreThread(). Similar
    1222             to it, we need to ensure errno doesn't change.
    1223          */
    1224          int err = errno;
    1225          PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
    1226          if (check && check->interp == newts->interp && check != newts)
    1227              Py_FatalError("Invalid thread state for this thread");
    1228          errno = err;
    1229      }
    1230  #endif
    1231      return oldts;
    1232  }
    1233  
    1234  PyThreadState *
    1235  PyThreadState_Swap(PyThreadState *newts)
    1236  {
    1237      return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
    1238  }
    1239  
    1240  /* An extension mechanism to store arbitrary additional per-thread state.
    1241     PyThreadState_GetDict() returns a dictionary that can be used to hold such
    1242     state; the caller should pick a unique key and store its state there.  If
    1243     PyThreadState_GetDict() returns NULL, an exception has *not* been raised
    1244     and the caller should assume no per-thread state is available. */
    1245  
    1246  PyObject *
    1247  _PyThreadState_GetDict(PyThreadState *tstate)
    1248  {
    1249      assert(tstate != NULL);
    1250      if (tstate->dict == NULL) {
    1251          tstate->dict = PyDict_New();
    1252          if (tstate->dict == NULL) {
    1253              _PyErr_Clear(tstate);
    1254          }
    1255      }
    1256      return tstate->dict;
    1257  }
    1258  
    1259  
    1260  PyObject *
    1261  PyThreadState_GetDict(void)
    1262  {
    1263      PyThreadState *tstate = _PyThreadState_GET();
    1264      if (tstate == NULL) {
    1265          return NULL;
    1266      }
    1267      return _PyThreadState_GetDict(tstate);
    1268  }
    1269  
    1270  
    1271  PyInterpreterState *
    1272  PyThreadState_GetInterpreter(PyThreadState *tstate)
    1273  {
    1274      assert(tstate != NULL);
    1275      return tstate->interp;
    1276  }
    1277  
    1278  
    1279  PyFrameObject*
    1280  PyThreadState_GetFrame(PyThreadState *tstate)
    1281  {
    1282      assert(tstate != NULL);
    1283      _PyInterpreterFrame *f = tstate->cframe->current_frame;
    1284      while (f && _PyFrame_IsIncomplete(f)) {
    1285          f = f->previous;
    1286      }
    1287      if (f == NULL) {
    1288          return NULL;
    1289      }
    1290      PyFrameObject *frame = _PyFrame_GetFrameObject(f);
    1291      if (frame == NULL) {
    1292          PyErr_Clear();
    1293      }
    1294      Py_XINCREF(frame);
    1295      return frame;
    1296  }
    1297  
    1298  
    1299  uint64_t
    1300  PyThreadState_GetID(PyThreadState *tstate)
    1301  {
    1302      assert(tstate != NULL);
    1303      return tstate->id;
    1304  }
    1305  
    1306  
    1307  /* Asynchronously raise an exception in a thread.
    1308     Requested by Just van Rossum and Alex Martelli.
    1309     To prevent naive misuse, you must write your own extension
    1310     to call this, or use ctypes.  Must be called with the GIL held.
    1311     Returns the number of tstates modified (normally 1, but 0 if `id` didn't
    1312     match any known thread id).  Can be called with exc=NULL to clear an
    1313     existing async exception.  This raises no exceptions. */
    1314  
    1315  int
    1316  PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
    1317  {
    1318      _PyRuntimeState *runtime = &_PyRuntime;
    1319      PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
    1320  
    1321      /* Although the GIL is held, a few C API functions can be called
    1322       * without the GIL held, and in particular some that create and
    1323       * destroy thread and interpreter states.  Those can mutate the
    1324       * list of thread states we're traversing, so to prevent that we lock
    1325       * head_mutex for the duration.
    1326       */
    1327      HEAD_LOCK(runtime);
    1328      for (PyThreadState *tstate = interp->threads.head; tstate != NULL; tstate = tstate->next) {
    1329          if (tstate->thread_id != id) {
    1330              continue;
    1331          }
    1332  
    1333          /* Tricky:  we need to decref the current value
    1334           * (if any) in tstate->async_exc, but that can in turn
    1335           * allow arbitrary Python code to run, including
    1336           * perhaps calls to this function.  To prevent
    1337           * deadlock, we need to release head_mutex before
    1338           * the decref.
    1339           */
    1340          PyObject *old_exc = tstate->async_exc;
    1341          Py_XINCREF(exc);
    1342          tstate->async_exc = exc;
    1343          HEAD_UNLOCK(runtime);
    1344  
    1345          Py_XDECREF(old_exc);
    1346          _PyEval_SignalAsyncExc(tstate->interp);
    1347          return 1;
    1348      }
    1349      HEAD_UNLOCK(runtime);
    1350      return 0;
    1351  }
    1352  
    1353  /* Routines for advanced debuggers, requested by David Beazley.
    1354     Don't use unless you know what you are doing! */
    1355  
    1356  PyInterpreterState *
    1357  PyInterpreterState_Head(void)
    1358  {
    1359      return _PyRuntime.interpreters.head;
    1360  }
    1361  
    1362  PyInterpreterState *
    1363  PyInterpreterState_Main(void)
    1364  {
    1365      return _PyInterpreterState_Main();
    1366  }
    1367  
    1368  PyInterpreterState *
    1369  PyInterpreterState_Next(PyInterpreterState *interp) {
    1370      return interp->next;
    1371  }
    1372  
    1373  PyThreadState *
    1374  PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
    1375      return interp->threads.head;
    1376  }
    1377  
    1378  PyThreadState *
    1379  PyThreadState_Next(PyThreadState *tstate) {
    1380      return tstate->next;
    1381  }
    1382  
    1383  /* The implementation of sys._current_frames().  This is intended to be
    1384     called with the GIL held, as it will be when called via
    1385     sys._current_frames().  It's possible it would work fine even without
    1386     the GIL held, but haven't thought enough about that.
    1387  */
    1388  PyObject *
    1389  _PyThread_CurrentFrames(void)
    1390  {
    1391      PyThreadState *tstate = _PyThreadState_GET();
    1392      if (_PySys_Audit(tstate, "sys._current_frames", NULL) < 0) {
    1393          return NULL;
    1394      }
    1395  
    1396      PyObject *result = PyDict_New();
    1397      if (result == NULL) {
    1398          return NULL;
    1399      }
    1400  
    1401      /* for i in all interpreters:
    1402       *     for t in all of i's thread states:
    1403       *          if t's frame isn't NULL, map t's id to its frame
    1404       * Because these lists can mutate even when the GIL is held, we
    1405       * need to grab head_mutex for the duration.
    1406       */
    1407      _PyRuntimeState *runtime = tstate->interp->runtime;
    1408      HEAD_LOCK(runtime);
    1409      PyInterpreterState *i;
    1410      for (i = runtime->interpreters.head; i != NULL; i = i->next) {
    1411          PyThreadState *t;
    1412          for (t = i->threads.head; t != NULL; t = t->next) {
    1413              _PyInterpreterFrame *frame = t->cframe->current_frame;
    1414              while (frame && _PyFrame_IsIncomplete(frame)) {
    1415                  frame = frame->previous;
    1416              }
    1417              if (frame == NULL) {
    1418                  continue;
    1419              }
    1420              PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
    1421              if (id == NULL) {
    1422                  goto fail;
    1423              }
    1424              PyObject *frameobj = (PyObject *)_PyFrame_GetFrameObject(frame);
    1425              if (frameobj == NULL) {
    1426                  Py_DECREF(id);
    1427                  goto fail;
    1428              }
    1429              int stat = PyDict_SetItem(result, id, frameobj);
    1430              Py_DECREF(id);
    1431              if (stat < 0) {
    1432                  goto fail;
    1433              }
    1434          }
    1435      }
    1436      goto done;
    1437  
    1438  fail:
    1439      Py_CLEAR(result);
    1440  
    1441  done:
    1442      HEAD_UNLOCK(runtime);
    1443      return result;
    1444  }
    1445  
    1446  PyObject *
    1447  _PyThread_CurrentExceptions(void)
    1448  {
    1449      PyThreadState *tstate = _PyThreadState_GET();
    1450  
    1451      _Py_EnsureTstateNotNULL(tstate);
    1452  
    1453      if (_PySys_Audit(tstate, "sys._current_exceptions", NULL) < 0) {
    1454          return NULL;
    1455      }
    1456  
    1457      PyObject *result = PyDict_New();
    1458      if (result == NULL) {
    1459          return NULL;
    1460      }
    1461  
    1462      /* for i in all interpreters:
    1463       *     for t in all of i's thread states:
    1464       *          if t's frame isn't NULL, map t's id to its frame
    1465       * Because these lists can mutate even when the GIL is held, we
    1466       * need to grab head_mutex for the duration.
    1467       */
    1468      _PyRuntimeState *runtime = tstate->interp->runtime;
    1469      HEAD_LOCK(runtime);
    1470      PyInterpreterState *i;
    1471      for (i = runtime->interpreters.head; i != NULL; i = i->next) {
    1472          PyThreadState *t;
    1473          for (t = i->threads.head; t != NULL; t = t->next) {
    1474              _PyErr_StackItem *err_info = _PyErr_GetTopmostException(t);
    1475              if (err_info == NULL) {
    1476                  continue;
    1477              }
    1478              PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
    1479              if (id == NULL) {
    1480                  goto fail;
    1481              }
    1482              PyObject *exc_info = _PyErr_StackItemToExcInfoTuple(err_info);
    1483              if (exc_info == NULL) {
    1484                  Py_DECREF(id);
    1485                  goto fail;
    1486              }
    1487              int stat = PyDict_SetItem(result, id, exc_info);
    1488              Py_DECREF(id);
    1489              Py_DECREF(exc_info);
    1490              if (stat < 0) {
    1491                  goto fail;
    1492              }
    1493          }
    1494      }
    1495      goto done;
    1496  
    1497  fail:
    1498      Py_CLEAR(result);
    1499  
    1500  done:
    1501      HEAD_UNLOCK(runtime);
    1502      return result;
    1503  }
    1504  
    1505  /* Python "auto thread state" API. */
    1506  
    1507  /* Keep this as a static, as it is not reliable!  It can only
    1508     ever be compared to the state for the *current* thread.
    1509     * If not equal, then it doesn't matter that the actual
    1510       value may change immediately after comparison, as it can't
    1511       possibly change to the current thread's state.
    1512     * If equal, then the current thread holds the lock, so the value can't
    1513       change until we yield the lock.
    1514  */
    1515  static int
    1516  PyThreadState_IsCurrent(PyThreadState *tstate)
    1517  {
    1518      /* Must be the tstate for this thread */
    1519      struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
    1520      assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
    1521      return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
    1522  }
    1523  
    1524  /* Internal initialization/finalization functions called by
    1525     Py_Initialize/Py_FinalizeEx
    1526  */
    1527  PyStatus
    1528  _PyGILState_Init(_PyRuntimeState *runtime)
    1529  {
    1530      struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
    1531      if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
    1532          return _PyStatus_NO_MEMORY();
    1533      }
    1534      // PyThreadState_New() calls _PyGILState_NoteThreadState() which does
    1535      // nothing before autoInterpreterState is set.
    1536      assert(gilstate->autoInterpreterState == NULL);
    1537      return _PyStatus_OK();
    1538  }
    1539  
    1540  
    1541  PyStatus
    1542  _PyGILState_SetTstate(PyThreadState *tstate)
    1543  {
    1544      if (!_Py_IsMainInterpreter(tstate->interp)) {
    1545          /* Currently, PyGILState is shared by all interpreters. The main
    1546           * interpreter is responsible to initialize it. */
    1547          return _PyStatus_OK();
    1548      }
    1549  
    1550      /* must init with valid states */
    1551      assert(tstate != NULL);
    1552      assert(tstate->interp != NULL);
    1553  
    1554      struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
    1555  
    1556      gilstate->autoInterpreterState = tstate->interp;
    1557      assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
    1558      assert(tstate->gilstate_counter == 0);
    1559  
    1560      _PyGILState_NoteThreadState(gilstate, tstate);
    1561      return _PyStatus_OK();
    1562  }
    1563  
    1564  PyInterpreterState *
    1565  _PyGILState_GetInterpreterStateUnsafe(void)
    1566  {
    1567      return _PyRuntime.gilstate.autoInterpreterState;
    1568  }
    1569  
    1570  void
    1571  _PyGILState_Fini(PyInterpreterState *interp)
    1572  {
    1573      struct _gilstate_runtime_state *gilstate = &interp->runtime->gilstate;
    1574      PyThread_tss_delete(&gilstate->autoTSSkey);
    1575      gilstate->autoInterpreterState = NULL;
    1576  }
    1577  
    1578  #ifdef HAVE_FORK
    1579  /* Reset the TSS key - called by PyOS_AfterFork_Child().
    1580   * This should not be necessary, but some - buggy - pthread implementations
    1581   * don't reset TSS upon fork(), see issue #10517.
    1582   */
    1583  PyStatus
    1584  _PyGILState_Reinit(_PyRuntimeState *runtime)
    1585  {
    1586      struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
    1587      PyThreadState *tstate = _PyGILState_GetThisThreadState(gilstate);
    1588  
    1589      PyThread_tss_delete(&gilstate->autoTSSkey);
    1590      if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
    1591          return _PyStatus_NO_MEMORY();
    1592      }
    1593  
    1594      /* If the thread had an associated auto thread state, reassociate it with
    1595       * the new key. */
    1596      if (tstate &&
    1597          PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)
    1598      {
    1599          return _PyStatus_ERR("failed to set autoTSSkey");
    1600      }
    1601      return _PyStatus_OK();
    1602  }
    1603  #endif
    1604  
    1605  /* When a thread state is created for a thread by some mechanism other than
    1606     PyGILState_Ensure, it's important that the GILState machinery knows about
    1607     it so it doesn't try to create another thread state for the thread (this is
    1608     a better fix for SF bug #1010677 than the first one attempted).
    1609  */
    1610  static void
    1611  _PyGILState_NoteThreadState(struct _gilstate_runtime_state *gilstate, PyThreadState* tstate)
    1612  {
    1613      /* If autoTSSkey isn't initialized, this must be the very first
    1614         threadstate created in Py_Initialize().  Don't do anything for now
    1615         (we'll be back here when _PyGILState_Init is called). */
    1616      if (!gilstate->autoInterpreterState) {
    1617          return;
    1618      }
    1619  
    1620      /* Stick the thread state for this thread in thread specific storage.
    1621  
    1622         The only situation where you can legitimately have more than one
    1623         thread state for an OS level thread is when there are multiple
    1624         interpreters.
    1625  
    1626         You shouldn't really be using the PyGILState_ APIs anyway (see issues
    1627         #10915 and #15751).
    1628  
    1629         The first thread state created for that given OS level thread will
    1630         "win", which seems reasonable behaviour.
    1631      */
    1632      if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
    1633          if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
    1634              Py_FatalError("Couldn't create autoTSSkey mapping");
    1635          }
    1636      }
    1637  
    1638      /* PyGILState_Release must not try to delete this thread state. */
    1639      tstate->gilstate_counter = 1;
    1640  }
    1641  
    1642  /* The public functions */
    1643  static PyThreadState *
    1644  _PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate)
    1645  {
    1646      if (gilstate->autoInterpreterState == NULL)
    1647          return NULL;
    1648      return (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
    1649  }
    1650  
    1651  PyThreadState *
    1652  PyGILState_GetThisThreadState(void)
    1653  {
    1654      return _PyGILState_GetThisThreadState(&_PyRuntime.gilstate);
    1655  }
    1656  
    1657  int
    1658  PyGILState_Check(void)
    1659  {
    1660      struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
    1661      if (!gilstate->check_enabled) {
    1662          return 1;
    1663      }
    1664  
    1665      if (!PyThread_tss_is_created(&gilstate->autoTSSkey)) {
    1666          return 1;
    1667      }
    1668  
    1669      PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
    1670      if (tstate == NULL) {
    1671          return 0;
    1672      }
    1673  
    1674      return (tstate == _PyGILState_GetThisThreadState(gilstate));
    1675  }
    1676  
    1677  PyGILState_STATE
    1678  PyGILState_Ensure(void)
    1679  {
    1680      _PyRuntimeState *runtime = &_PyRuntime;
    1681      struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
    1682  
    1683      /* Note that we do not auto-init Python here - apart from
    1684         potential races with 2 threads auto-initializing, pep-311
    1685         spells out other issues.  Embedders are expected to have
    1686         called Py_Initialize(). */
    1687  
    1688      /* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been
    1689         called by Py_Initialize() */
    1690      assert(_PyEval_ThreadsInitialized(runtime));
    1691      assert(gilstate->autoInterpreterState);
    1692  
    1693      PyThreadState *tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
    1694      int current;
    1695      if (tcur == NULL) {
    1696          /* Create a new Python thread state for this thread */
    1697          tcur = PyThreadState_New(gilstate->autoInterpreterState);
    1698          if (tcur == NULL) {
    1699              Py_FatalError("Couldn't create thread-state for new thread");
    1700          }
    1701  
    1702          /* This is our thread state!  We'll need to delete it in the
    1703             matching call to PyGILState_Release(). */
    1704          tcur->gilstate_counter = 0;
    1705          current = 0; /* new thread state is never current */
    1706      }
    1707      else {
    1708          current = PyThreadState_IsCurrent(tcur);
    1709      }
    1710  
    1711      if (current == 0) {
    1712          PyEval_RestoreThread(tcur);
    1713      }
    1714  
    1715      /* Update our counter in the thread-state - no need for locks:
    1716         - tcur will remain valid as we hold the GIL.
    1717         - the counter is safe as we are the only thread "allowed"
    1718           to modify this value
    1719      */
    1720      ++tcur->gilstate_counter;
    1721  
    1722      return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
    1723  }
    1724  
    1725  void
    1726  PyGILState_Release(PyGILState_STATE oldstate)
    1727  {
    1728      _PyRuntimeState *runtime = &_PyRuntime;
    1729      PyThreadState *tstate = PyThread_tss_get(&runtime->gilstate.autoTSSkey);
    1730      if (tstate == NULL) {
    1731          Py_FatalError("auto-releasing thread-state, "
    1732                        "but no thread-state for this thread");
    1733      }
    1734  
    1735      /* We must hold the GIL and have our thread state current */
    1736      /* XXX - remove the check - the assert should be fine,
    1737         but while this is very new (April 2003), the extra check
    1738         by release-only users can't hurt.
    1739      */
    1740      if (!PyThreadState_IsCurrent(tstate)) {
    1741          _Py_FatalErrorFormat(__func__,
    1742                               "thread state %p must be current when releasing",
    1743                               tstate);
    1744      }
    1745      assert(PyThreadState_IsCurrent(tstate));
    1746      --tstate->gilstate_counter;
    1747      assert(tstate->gilstate_counter >= 0); /* illegal counter value */
    1748  
    1749      /* If we're going to destroy this thread-state, we must
    1750       * clear it while the GIL is held, as destructors may run.
    1751       */
    1752      if (tstate->gilstate_counter == 0) {
    1753          /* can't have been locked when we created it */
    1754          assert(oldstate == PyGILState_UNLOCKED);
    1755          PyThreadState_Clear(tstate);
    1756          /* Delete the thread-state.  Note this releases the GIL too!
    1757           * It's vital that the GIL be held here, to avoid shutdown
    1758           * races; see bugs 225673 and 1061968 (that nasty bug has a
    1759           * habit of coming back).
    1760           */
    1761          assert(_PyRuntimeGILState_GetThreadState(&runtime->gilstate) == tstate);
    1762          _PyThreadState_DeleteCurrent(tstate);
    1763      }
    1764      /* Release the lock if necessary */
    1765      else if (oldstate == PyGILState_UNLOCKED)
    1766          PyEval_SaveThread();
    1767  }
    1768  
    1769  
    1770  /**************************/
    1771  /* cross-interpreter data */
    1772  /**************************/
    1773  
    1774  /* cross-interpreter data */
    1775  
    1776  crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
    1777  
    1778  /* This is a separate func from _PyCrossInterpreterData_Lookup in order
    1779     to keep the registry code separate. */
    1780  static crossinterpdatafunc
    1781  _lookup_getdata(PyObject *obj)
    1782  {
    1783      crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj);
    1784      if (getdata == NULL && PyErr_Occurred() == 0)
    1785          PyErr_Format(PyExc_ValueError,
    1786                       "%S does not support cross-interpreter data", obj);
    1787      return getdata;
    1788  }
    1789  
    1790  int
    1791  _PyObject_CheckCrossInterpreterData(PyObject *obj)
    1792  {
    1793      crossinterpdatafunc getdata = _lookup_getdata(obj);
    1794      if (getdata == NULL) {
    1795          return -1;
    1796      }
    1797      return 0;
    1798  }
    1799  
    1800  static int
    1801  _check_xidata(PyThreadState *tstate, _PyCrossInterpreterData *data)
    1802  {
    1803      // data->data can be anything, including NULL, so we don't check it.
    1804  
    1805      // data->obj may be NULL, so we don't check it.
    1806  
    1807      if (data->interp < 0) {
    1808          _PyErr_SetString(tstate, PyExc_SystemError, "missing interp");
    1809          return -1;
    1810      }
    1811  
    1812      if (data->new_object == NULL) {
    1813          _PyErr_SetString(tstate, PyExc_SystemError, "missing new_object func");
    1814          return -1;
    1815      }
    1816  
    1817      // data->free may be NULL, so we don't check it.
    1818  
    1819      return 0;
    1820  }
    1821  
    1822  int
    1823  _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
    1824  {
    1825      PyThreadState *tstate = _PyThreadState_GET();
    1826  #ifdef Py_DEBUG
    1827      // The caller must hold the GIL
    1828      _Py_EnsureTstateNotNULL(tstate);
    1829  #endif
    1830      PyInterpreterState *interp = tstate->interp;
    1831  
    1832      // Reset data before re-populating.
    1833      *data = (_PyCrossInterpreterData){0};
    1834      data->free = PyMem_RawFree;  // Set a default that may be overridden.
    1835  
    1836      // Call the "getdata" func for the object.
    1837      Py_INCREF(obj);
    1838      crossinterpdatafunc getdata = _lookup_getdata(obj);
    1839      if (getdata == NULL) {
    1840          Py_DECREF(obj);
    1841          return -1;
    1842      }
    1843      int res = getdata(obj, data);
    1844      Py_DECREF(obj);
    1845      if (res != 0) {
    1846          return -1;
    1847      }
    1848  
    1849      // Fill in the blanks and validate the result.
    1850      data->interp = interp->id;
    1851      if (_check_xidata(tstate, data) != 0) {
    1852          _PyCrossInterpreterData_Release(data);
    1853          return -1;
    1854      }
    1855  
    1856      return 0;
    1857  }
    1858  
    1859  static void
    1860  _release_xidata(void *arg)
    1861  {
    1862      _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
    1863      if (data->free != NULL) {
    1864          data->free(data->data);
    1865      }
    1866      Py_XDECREF(data->obj);
    1867  }
    1868  
    1869  static void
    1870  _call_in_interpreter(struct _gilstate_runtime_state *gilstate,
    1871                       PyInterpreterState *interp,
    1872                       void (*func)(void *), void *arg)
    1873  {
    1874      /* We would use Py_AddPendingCall() if it weren't specific to the
    1875       * main interpreter (see bpo-33608).  In the meantime we take a
    1876       * naive approach.
    1877       */
    1878      PyThreadState *save_tstate = NULL;
    1879      if (interp != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
    1880          // XXX Using the "head" thread isn't strictly correct.
    1881          PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
    1882          // XXX Possible GILState issues?
    1883          save_tstate = _PyThreadState_Swap(gilstate, tstate);
    1884      }
    1885  
    1886      func(arg);
    1887  
    1888      // Switch back.
    1889      if (save_tstate != NULL) {
    1890          _PyThreadState_Swap(gilstate, save_tstate);
    1891      }
    1892  }
    1893  
    1894  void
    1895  _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
    1896  {
    1897      if (data->data == NULL && data->obj == NULL) {
    1898          // Nothing to release!
    1899          return;
    1900      }
    1901  
    1902      // Switch to the original interpreter.
    1903      PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
    1904      if (interp == NULL) {
    1905          // The interpreter was already destroyed.
    1906          if (data->free != NULL) {
    1907              // XXX Someone leaked some memory...
    1908          }
    1909          return;
    1910      }
    1911  
    1912      // "Release" the data and/or the object.
    1913      struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
    1914      _call_in_interpreter(gilstate, interp, _release_xidata, data);
    1915  }
    1916  
    1917  PyObject *
    1918  _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
    1919  {
    1920      return data->new_object(data);
    1921  }
    1922  
    1923  /* registry of {type -> crossinterpdatafunc} */
    1924  
    1925  /* For now we use a global registry of shareable classes.  An
    1926     alternative would be to add a tp_* slot for a class's
    1927     crossinterpdatafunc. It would be simpler and more efficient. */
    1928  
    1929  static int
    1930  _register_xidata(struct _xidregistry *xidregistry, PyTypeObject *cls,
    1931                   crossinterpdatafunc getdata)
    1932  {
    1933      // Note that we effectively replace already registered classes
    1934      // rather than failing.
    1935      struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem));
    1936      if (newhead == NULL)
    1937          return -1;
    1938      newhead->cls = cls;
    1939      newhead->getdata = getdata;
    1940      newhead->next = xidregistry->head;
    1941      xidregistry->head = newhead;
    1942      return 0;
    1943  }
    1944  
    1945  static void _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry);
    1946  
    1947  int
    1948  _PyCrossInterpreterData_RegisterClass(PyTypeObject *cls,
    1949                                         crossinterpdatafunc getdata)
    1950  {
    1951      if (!PyType_Check(cls)) {
    1952          PyErr_Format(PyExc_ValueError, "only classes may be registered");
    1953          return -1;
    1954      }
    1955      if (getdata == NULL) {
    1956          PyErr_Format(PyExc_ValueError, "missing 'getdata' func");
    1957          return -1;
    1958      }
    1959  
    1960      // Make sure the class isn't ever deallocated.
    1961      Py_INCREF((PyObject *)cls);
    1962  
    1963      struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
    1964      PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
    1965      if (xidregistry->head == NULL) {
    1966          _register_builtins_for_crossinterpreter_data(xidregistry);
    1967      }
    1968      int res = _register_xidata(xidregistry, cls, getdata);
    1969      PyThread_release_lock(xidregistry->mutex);
    1970      return res;
    1971  }
    1972  
    1973  /* Cross-interpreter objects are looked up by exact match on the class.
    1974     We can reassess this policy when we move from a global registry to a
    1975     tp_* slot. */
    1976  
    1977  crossinterpdatafunc
    1978  _PyCrossInterpreterData_Lookup(PyObject *obj)
    1979  {
    1980      struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
    1981      PyObject *cls = PyObject_Type(obj);
    1982      crossinterpdatafunc getdata = NULL;
    1983      PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
    1984      struct _xidregitem *cur = xidregistry->head;
    1985      if (cur == NULL) {
    1986          _register_builtins_for_crossinterpreter_data(xidregistry);
    1987          cur = xidregistry->head;
    1988      }
    1989      for(; cur != NULL; cur = cur->next) {
    1990          if (cur->cls == (PyTypeObject *)cls) {
    1991              getdata = cur->getdata;
    1992              break;
    1993          }
    1994      }
    1995      Py_DECREF(cls);
    1996      PyThread_release_lock(xidregistry->mutex);
    1997      return getdata;
    1998  }
    1999  
    2000  /* cross-interpreter data for builtin types */
    2001  
    2002  struct _shared_bytes_data {
    2003      char *bytes;
    2004      Py_ssize_t len;
    2005  };
    2006  
    2007  static PyObject *
    2008  _new_bytes_object(_PyCrossInterpreterData *data)
    2009  {
    2010      struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
    2011      return PyBytes_FromStringAndSize(shared->bytes, shared->len);
    2012  }
    2013  
    2014  static int
    2015  _bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
    2016  {
    2017      struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
    2018      if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
    2019          return -1;
    2020      }
    2021      data->data = (void *)shared;
    2022      Py_INCREF(obj);
    2023      data->obj = obj;  // Will be "released" (decref'ed) when data released.
    2024      data->new_object = _new_bytes_object;
    2025      data->free = PyMem_Free;
    2026      return 0;
    2027  }
    2028  
    2029  struct _shared_str_data {
    2030      int kind;
    2031      const void *buffer;
    2032      Py_ssize_t len;
    2033  };
    2034  
    2035  static PyObject *
    2036  _new_str_object(_PyCrossInterpreterData *data)
    2037  {
    2038      struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
    2039      return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
    2040  }
    2041  
    2042  static int
    2043  _str_shared(PyObject *obj, _PyCrossInterpreterData *data)
    2044  {
    2045      struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
    2046      shared->kind = PyUnicode_KIND(obj);
    2047      shared->buffer = PyUnicode_DATA(obj);
    2048      shared->len = PyUnicode_GET_LENGTH(obj);
    2049      data->data = (void *)shared;
    2050      Py_INCREF(obj);
    2051      data->obj = obj;  // Will be "released" (decref'ed) when data released.
    2052      data->new_object = _new_str_object;
    2053      data->free = PyMem_Free;
    2054      return 0;
    2055  }
    2056  
    2057  static PyObject *
    2058  _new_long_object(_PyCrossInterpreterData *data)
    2059  {
    2060      return PyLong_FromSsize_t((Py_ssize_t)(data->data));
    2061  }
    2062  
    2063  static int
    2064  _long_shared(PyObject *obj, _PyCrossInterpreterData *data)
    2065  {
    2066      /* Note that this means the size of shareable ints is bounded by
    2067       * sys.maxsize.  Hence on 32-bit architectures that is half the
    2068       * size of maximum shareable ints on 64-bit.
    2069       */
    2070      Py_ssize_t value = PyLong_AsSsize_t(obj);
    2071      if (value == -1 && PyErr_Occurred()) {
    2072          if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
    2073              PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
    2074          }
    2075          return -1;
    2076      }
    2077      data->data = (void *)value;
    2078      data->obj = NULL;
    2079      data->new_object = _new_long_object;
    2080      data->free = NULL;
    2081      return 0;
    2082  }
    2083  
    2084  static PyObject *
    2085  _new_none_object(_PyCrossInterpreterData *data)
    2086  {
    2087      // XXX Singleton refcounts are problematic across interpreters...
    2088      Py_INCREF(Py_None);
    2089      return Py_None;
    2090  }
    2091  
    2092  static int
    2093  _none_shared(PyObject *obj, _PyCrossInterpreterData *data)
    2094  {
    2095      data->data = NULL;
    2096      // data->obj remains NULL
    2097      data->new_object = _new_none_object;
    2098      data->free = NULL;  // There is nothing to free.
    2099      return 0;
    2100  }
    2101  
    2102  static void
    2103  _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
    2104  {
    2105      // None
    2106      if (_register_xidata(xidregistry, (PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
    2107          Py_FatalError("could not register None for cross-interpreter sharing");
    2108      }
    2109  
    2110      // int
    2111      if (_register_xidata(xidregistry, &PyLong_Type, _long_shared) != 0) {
    2112          Py_FatalError("could not register int for cross-interpreter sharing");
    2113      }
    2114  
    2115      // bytes
    2116      if (_register_xidata(xidregistry, &PyBytes_Type, _bytes_shared) != 0) {
    2117          Py_FatalError("could not register bytes for cross-interpreter sharing");
    2118      }
    2119  
    2120      // str
    2121      if (_register_xidata(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
    2122          Py_FatalError("could not register str for cross-interpreter sharing");
    2123      }
    2124  }
    2125  
    2126  
    2127  _PyFrameEvalFunction
    2128  _PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
    2129  {
    2130      if (interp->eval_frame == NULL) {
    2131          return _PyEval_EvalFrameDefault;
    2132      }
    2133      return interp->eval_frame;
    2134  }
    2135  
    2136  
    2137  void
    2138  _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp,
    2139                                       _PyFrameEvalFunction eval_frame)
    2140  {
    2141      if (eval_frame == _PyEval_EvalFrameDefault) {
    2142          interp->eval_frame = NULL;
    2143      }
    2144      else {
    2145          interp->eval_frame = eval_frame;
    2146      }
    2147  }
    2148  
    2149  
    2150  const PyConfig*
    2151  _PyInterpreterState_GetConfig(PyInterpreterState *interp)
    2152  {
    2153      return &interp->config;
    2154  }
    2155  
    2156  
    2157  int
    2158  _PyInterpreterState_GetConfigCopy(PyConfig *config)
    2159  {
    2160      PyInterpreterState *interp = PyInterpreterState_Get();
    2161  
    2162      PyStatus status = _PyConfig_Copy(config, &interp->config);
    2163      if (PyStatus_Exception(status)) {
    2164          _PyErr_SetFromPyStatus(status);
    2165          return -1;
    2166      }
    2167      return 0;
    2168  }
    2169  
    2170  
    2171  const PyConfig*
    2172  _Py_GetConfig(void)
    2173  {
    2174      assert(PyGILState_Check());
    2175      PyThreadState *tstate = _PyThreadState_GET();
    2176      return _PyInterpreterState_GetConfig(tstate->interp);
    2177  }
    2178  
    2179  #define MINIMUM_OVERHEAD 1000
    2180  
    2181  static PyObject **
    2182  push_chunk(PyThreadState *tstate, int size)
    2183  {
    2184      int allocate_size = DATA_STACK_CHUNK_SIZE;
    2185      while (allocate_size < (int)sizeof(PyObject*)*(size + MINIMUM_OVERHEAD)) {
    2186          allocate_size *= 2;
    2187      }
    2188      _PyStackChunk *new = allocate_chunk(allocate_size, tstate->datastack_chunk);
    2189      if (new == NULL) {
    2190          return NULL;
    2191      }
    2192      if (tstate->datastack_chunk) {
    2193          tstate->datastack_chunk->top = tstate->datastack_top -
    2194                                         &tstate->datastack_chunk->data[0];
    2195      }
    2196      tstate->datastack_chunk = new;
    2197      tstate->datastack_limit = (PyObject **)(((char *)new) + allocate_size);
    2198      // When new is the "root" chunk (i.e. new->previous == NULL), we can keep
    2199      // _PyThreadState_PopFrame from freeing it later by "skipping" over the
    2200      // first element:
    2201      PyObject **res = &new->data[new->previous == NULL];
    2202      tstate->datastack_top = res + size;
    2203      return res;
    2204  }
    2205  
    2206  _PyInterpreterFrame *
    2207  _PyThreadState_BumpFramePointerSlow(PyThreadState *tstate, size_t size)
    2208  {
    2209      if (_PyThreadState_HasStackSpace(tstate, size)) {
    2210          _PyInterpreterFrame *res = (_PyInterpreterFrame *)tstate->datastack_top;
    2211          tstate->datastack_top += size;
    2212          return res;
    2213      }
    2214      if (size > INT_MAX/2) {
    2215          PyErr_NoMemory();
    2216          return NULL;
    2217      }
    2218      return (_PyInterpreterFrame *)push_chunk(tstate, (int)size);
    2219  }
    2220  
    2221  void
    2222  _PyThreadState_PopFrame(PyThreadState *tstate, _PyInterpreterFrame * frame)
    2223  {
    2224      assert(tstate->datastack_chunk);
    2225      PyObject **base = (PyObject **)frame;
    2226      if (base == &tstate->datastack_chunk->data[0]) {
    2227          _PyStackChunk *chunk = tstate->datastack_chunk;
    2228          _PyStackChunk *previous = chunk->previous;
    2229          // push_chunk ensures that the root chunk is never popped:
    2230          assert(previous);
    2231          tstate->datastack_top = &previous->data[previous->top];
    2232          tstate->datastack_chunk = previous;
    2233          _PyObject_VirtualFree(chunk, chunk->size);
    2234          tstate->datastack_limit = (PyObject **)(((char *)previous) + previous->size);
    2235      }
    2236      else {
    2237          assert(tstate->datastack_top);
    2238          assert(tstate->datastack_top >= base);
    2239          tstate->datastack_top = base;
    2240      }
    2241  }
    2242  
    2243  
    2244  #ifndef NDEBUG
    2245  // Check that a Python thread state valid. In practice, this function is used
    2246  // on a Python debug build to check if 'tstate' is a dangling pointer, if the
    2247  // PyThreadState memory has been freed.
    2248  //
    2249  // Usage:
    2250  //
    2251  //     assert(_PyThreadState_CheckConsistency(tstate));
    2252  int
    2253  _PyThreadState_CheckConsistency(PyThreadState *tstate)
    2254  {
    2255      assert(!_PyMem_IsPtrFreed(tstate));
    2256      assert(!_PyMem_IsPtrFreed(tstate->interp));
    2257      return 1;
    2258  }
    2259  #endif
    2260  
    2261  
    2262  // Check if a Python thread must exit immediately, rather than taking the GIL
    2263  // if Py_Finalize() has been called.
    2264  //
    2265  // When this function is called by a daemon thread after Py_Finalize() has been
    2266  // called, the GIL does no longer exist.
    2267  //
    2268  // tstate can be a dangling pointer (point to freed memory): only tstate value
    2269  // is used, the pointer is not deferenced.
    2270  //
    2271  // tstate must be non-NULL.
    2272  int
    2273  _PyThreadState_MustExit(PyThreadState *tstate)
    2274  {
    2275      /* bpo-39877: Access _PyRuntime directly rather than using
    2276         tstate->interp->runtime to support calls from Python daemon threads.
    2277         After Py_Finalize() has been called, tstate can be a dangling pointer:
    2278         point to PyThreadState freed memory. */
    2279      PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
    2280      return (finalizing != NULL && finalizing != tstate);
    2281  }
    2282  
    2283  
    2284  #ifdef __cplusplus
    2285  }
    2286  #endif