pystate.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef Py_INTERNAL_PYSTATE_H
  2. #define Py_INTERNAL_PYSTATE_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "pystate.h"
  7. #include "pyatomic.h"
  8. #include "pythread.h"
  9. #include "internal/mem.h"
  10. #include "internal/ceval.h"
  11. #include "internal/warnings.h"
  12. /* GIL state */
  13. struct _gilstate_runtime_state {
  14. int check_enabled;
  15. /* Assuming the current thread holds the GIL, this is the
  16. PyThreadState for the current thread. */
  17. _Py_atomic_address tstate_current;
  18. PyThreadFrameGetter getframe;
  19. /* The single PyInterpreterState used by this process'
  20. GILState implementation
  21. */
  22. /* TODO: Given interp_main, it may be possible to kill this ref */
  23. PyInterpreterState *autoInterpreterState;
  24. Py_tss_t autoTSSkey;
  25. };
  26. /* hook for PyEval_GetFrame(), requested for Psyco */
  27. #define _PyThreadState_GetFrame _PyRuntime.gilstate.getframe
  28. /* Issue #26558: Flag to disable PyGILState_Check().
  29. If set to non-zero, PyGILState_Check() always return 1. */
  30. #define _PyGILState_check_enabled _PyRuntime.gilstate.check_enabled
  31. typedef struct {
  32. /* Full path to the Python program */
  33. wchar_t *program_full_path;
  34. wchar_t *prefix;
  35. #ifdef MS_WINDOWS
  36. wchar_t *dll_path;
  37. #else
  38. wchar_t *exec_prefix;
  39. #endif
  40. /* Set by Py_SetPath(), or computed by _PyPathConfig_Init() */
  41. wchar_t *module_search_path;
  42. /* Python program name */
  43. wchar_t *program_name;
  44. /* Set by Py_SetPythonHome() or PYTHONHOME environment variable */
  45. wchar_t *home;
  46. } _PyPathConfig;
  47. #define _PyPathConfig_INIT {.module_search_path = NULL}
  48. /* Note: _PyPathConfig_INIT sets other fields to 0/NULL */
  49. PyAPI_DATA(_PyPathConfig) _Py_path_config;
  50. PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate(
  51. _PyPathConfig *config,
  52. const _PyCoreConfig *core_config);
  53. PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);
  54. /* interpreter state */
  55. PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(PY_INT64_T);
  56. PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
  57. PyAPI_FUNC(void) _PyInterpreterState_IDIncref(PyInterpreterState *);
  58. PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
  59. /* Full Python runtime state */
  60. typedef struct pyruntimestate {
  61. int initialized;
  62. int core_initialized;
  63. PyThreadState *finalizing;
  64. struct pyinterpreters {
  65. PyThread_type_lock mutex;
  66. PyInterpreterState *head;
  67. PyInterpreterState *main;
  68. /* _next_interp_id is an auto-numbered sequence of small
  69. integers. It gets initialized in _PyInterpreterState_Init(),
  70. which is called in Py_Initialize(), and used in
  71. PyInterpreterState_New(). A negative interpreter ID
  72. indicates an error occurred. The main interpreter will
  73. always have an ID of 0. Overflow results in a RuntimeError.
  74. If that becomes a problem later then we can adjust, e.g. by
  75. using a Python int. */
  76. int64_t next_id;
  77. } interpreters;
  78. #define NEXITFUNCS 32
  79. void (*exitfuncs[NEXITFUNCS])(void);
  80. int nexitfuncs;
  81. struct _gc_runtime_state gc;
  82. struct _warnings_runtime_state warnings;
  83. struct _ceval_runtime_state ceval;
  84. struct _gilstate_runtime_state gilstate;
  85. // XXX Consolidate globals found via the check-c-globals script.
  86. } _PyRuntimeState;
  87. #define _PyRuntimeState_INIT {.initialized = 0, .core_initialized = 0}
  88. /* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */
  89. PyAPI_DATA(_PyRuntimeState) _PyRuntime;
  90. PyAPI_FUNC(_PyInitError) _PyRuntimeState_Init(_PyRuntimeState *);
  91. PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *);
  92. /* Initialize _PyRuntimeState.
  93. Return NULL on success, or return an error message on failure. */
  94. PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void);
  95. PyAPI_FUNC(void) _PyRuntime_Finalize(void);
  96. #define _Py_CURRENTLY_FINALIZING(tstate) \
  97. (_PyRuntime.finalizing == tstate)
  98. /* Other */
  99. PyAPI_FUNC(_PyInitError) _PyInterpreterState_Enable(_PyRuntimeState *);
  100. #ifdef __cplusplus
  101. }
  102. #endif
  103. #endif /* !Py_INTERNAL_PYSTATE_H */