pycore_pystate.h 4.9 KB

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