(root)/
Python-3.12.0/
Include/
internal/
pycore_pystate.h
       1  #ifndef Py_INTERNAL_PYSTATE_H
       2  #define Py_INTERNAL_PYSTATE_H
       3  #ifdef __cplusplus
       4  extern "C" {
       5  #endif
       6  
       7  #ifndef Py_BUILD_CORE
       8  #  error "this header requires Py_BUILD_CORE define"
       9  #endif
      10  
      11  #include "pycore_runtime.h"   /* PyRuntimeState */
      12  
      13  
      14  /* Check if the current thread is the main thread.
      15     Use _Py_IsMainInterpreter() to check if it's the main interpreter. */
      16  static inline int
      17  _Py_IsMainThread(void)
      18  {
      19      unsigned long thread = PyThread_get_thread_ident();
      20      return (thread == _PyRuntime.main_thread);
      21  }
      22  
      23  
      24  static inline PyInterpreterState *
      25  _PyInterpreterState_Main(void)
      26  {
      27      return _PyRuntime.interpreters.main;
      28  }
      29  
      30  static inline int
      31  _Py_IsMainInterpreter(PyInterpreterState *interp)
      32  {
      33      return (interp == _PyInterpreterState_Main());
      34  }
      35  
      36  static inline int
      37  _Py_IsMainInterpreterFinalizing(PyInterpreterState *interp)
      38  {
      39      return (_PyRuntimeState_GetFinalizing(interp->runtime) != NULL &&
      40              interp == &interp->runtime->_main_interpreter);
      41  }
      42  
      43  
      44  static inline const PyConfig *
      45  _Py_GetMainConfig(void)
      46  {
      47      PyInterpreterState *interp = _PyInterpreterState_Main();
      48      if (interp == NULL) {
      49          return NULL;
      50      }
      51      return _PyInterpreterState_GetConfig(interp);
      52  }
      53  
      54  
      55  /* Only handle signals on the main thread of the main interpreter. */
      56  static inline int
      57  _Py_ThreadCanHandleSignals(PyInterpreterState *interp)
      58  {
      59      return (_Py_IsMainThread() && _Py_IsMainInterpreter(interp));
      60  }
      61  
      62  
      63  /* Variable and static inline functions for in-line access to current thread
      64     and interpreter state */
      65  
      66  #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
      67  extern _Py_thread_local PyThreadState *_Py_tss_tstate;
      68  #endif
      69  PyAPI_DATA(PyThreadState *) _PyThreadState_GetCurrent(void);
      70  
      71  /* Get the current Python thread state.
      72  
      73     This function is unsafe: it does not check for error and it can return NULL.
      74  
      75     The caller must hold the GIL.
      76  
      77     See also PyThreadState_Get() and _PyThreadState_UncheckedGet(). */
      78  static inline PyThreadState*
      79  _PyThreadState_GET(void)
      80  {
      81  #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
      82      return _Py_tss_tstate;
      83  #else
      84      return _PyThreadState_GetCurrent();
      85  #endif
      86  }
      87  
      88  
      89  static inline void
      90  _Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
      91  {
      92      if (tstate == NULL) {
      93          _Py_FatalErrorFunc(func,
      94              "the function must be called with the GIL held, "
      95              "after Python initialization and before Python finalization, "
      96              "but the GIL is released (the current Python thread state is NULL)");
      97      }
      98  }
      99  
     100  // Call Py_FatalError() if tstate is NULL
     101  #define _Py_EnsureTstateNotNULL(tstate) \
     102      _Py_EnsureFuncTstateNotNULL(__func__, (tstate))
     103  
     104  
     105  /* Get the current interpreter state.
     106  
     107     The function is unsafe: it does not check for error and it can return NULL.
     108  
     109     The caller must hold the GIL.
     110  
     111     See also _PyInterpreterState_Get()
     112     and _PyGILState_GetInterpreterStateUnsafe(). */
     113  static inline PyInterpreterState* _PyInterpreterState_GET(void) {
     114      PyThreadState *tstate = _PyThreadState_GET();
     115  #ifdef Py_DEBUG
     116      _Py_EnsureTstateNotNULL(tstate);
     117  #endif
     118      return tstate->interp;
     119  }
     120  
     121  
     122  // PyThreadState functions
     123  
     124  PyAPI_FUNC(PyThreadState *) _PyThreadState_New(PyInterpreterState *interp);
     125  PyAPI_FUNC(void) _PyThreadState_Bind(PyThreadState *tstate);
     126  // We keep this around exclusively for stable ABI compatibility.
     127  PyAPI_FUNC(void) _PyThreadState_Init(
     128      PyThreadState *tstate);
     129  PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
     130  
     131  
     132  /* Other */
     133  
     134  PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
     135      _PyRuntimeState *runtime,
     136      PyThreadState *newts);
     137  
     138  PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime);
     139  
     140  #ifdef HAVE_FORK
     141  extern PyStatus _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
     142  extern void _PySignal_AfterFork(void);
     143  #endif
     144  
     145  
     146  PyAPI_FUNC(int) _PyState_AddModule(
     147      PyThreadState *tstate,
     148      PyObject* module,
     149      PyModuleDef* def);
     150  
     151  
     152  PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate);
     153  
     154  #define HEAD_LOCK(runtime) \
     155      PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
     156  #define HEAD_UNLOCK(runtime) \
     157      PyThread_release_lock((runtime)->interpreters.mutex)
     158  
     159  
     160  #ifdef __cplusplus
     161  }
     162  #endif
     163  #endif /* !Py_INTERNAL_PYSTATE_H */