pycore_runtime.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #ifndef Py_INTERNAL_RUNTIME_H
  2. #define Py_INTERNAL_RUNTIME_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_atexit.h" // struct atexit_runtime_state
  10. #include "pycore_atomic.h" /* _Py_atomic_address */
  11. #include "pycore_ceval_state.h" // struct _ceval_runtime_state
  12. #include "pycore_floatobject.h" // struct _Py_float_runtime_state
  13. #include "pycore_faulthandler.h" // struct _faulthandler_runtime_state
  14. #include "pycore_global_objects.h" // struct _Py_global_objects
  15. #include "pycore_import.h" // struct _import_runtime_state
  16. #include "pycore_interp.h" // PyInterpreterState
  17. #include "pycore_object_state.h" // struct _py_object_runtime_state
  18. #include "pycore_parser.h" // struct _parser_runtime_state
  19. #include "pycore_pymem.h" // struct _pymem_allocators
  20. #include "pycore_pyhash.h" // struct pyhash_runtime_state
  21. #include "pycore_pythread.h" // struct _pythread_runtime_state
  22. #include "pycore_signal.h" // struct _signals_runtime_state
  23. #include "pycore_time.h" // struct _time_runtime_state
  24. #include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state
  25. #include "pycore_typeobject.h" // struct types_runtime_state
  26. #include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids
  27. struct _getargs_runtime_state {
  28. PyThread_type_lock mutex;
  29. struct _PyArg_Parser *static_parsers;
  30. };
  31. /* GIL state */
  32. struct _gilstate_runtime_state {
  33. /* bpo-26558: Flag to disable PyGILState_Check().
  34. If set to non-zero, PyGILState_Check() always return 1. */
  35. int check_enabled;
  36. /* The single PyInterpreterState used by this process'
  37. GILState implementation
  38. */
  39. /* TODO: Given interp_main, it may be possible to kill this ref */
  40. PyInterpreterState *autoInterpreterState;
  41. };
  42. /* Runtime audit hook state */
  43. typedef struct _Py_AuditHookEntry {
  44. struct _Py_AuditHookEntry *next;
  45. Py_AuditHookFunction hookCFunction;
  46. void *userData;
  47. } _Py_AuditHookEntry;
  48. /* Full Python runtime state */
  49. /* _PyRuntimeState holds the global state for the CPython runtime.
  50. That data is exposed in the internal API as a static variable (_PyRuntime).
  51. */
  52. typedef struct pyruntimestate {
  53. /* Has been initialized to a safe state.
  54. In order to be effective, this must be set to 0 during or right
  55. after allocation. */
  56. int _initialized;
  57. /* Is running Py_PreInitialize()? */
  58. int preinitializing;
  59. /* Is Python preinitialized? Set to 1 by Py_PreInitialize() */
  60. int preinitialized;
  61. /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */
  62. int core_initialized;
  63. /* Is Python fully initialized? Set to 1 by Py_Initialize() */
  64. int initialized;
  65. /* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
  66. is called again.
  67. Use _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing()
  68. to access it, don't access it directly. */
  69. _Py_atomic_address _finalizing;
  70. struct pyinterpreters {
  71. PyThread_type_lock mutex;
  72. /* The linked list of interpreters, newest first. */
  73. PyInterpreterState *head;
  74. /* The runtime's initial interpreter, which has a special role
  75. in the operation of the runtime. It is also often the only
  76. interpreter. */
  77. PyInterpreterState *main;
  78. /* next_id is an auto-numbered sequence of small
  79. integers. It gets initialized in _PyInterpreterState_Enable(),
  80. which is called in Py_Initialize(), and used in
  81. PyInterpreterState_New(). A negative interpreter ID
  82. indicates an error occurred. The main interpreter will
  83. always have an ID of 0. Overflow results in a RuntimeError.
  84. If that becomes a problem later then we can adjust, e.g. by
  85. using a Python int. */
  86. int64_t next_id;
  87. } interpreters;
  88. unsigned long main_thread;
  89. /* ---------- IMPORTANT ---------------------------
  90. The fields above this line are declared as early as
  91. possible to facilitate out-of-process observability
  92. tools. */
  93. // XXX Remove this field once we have a tp_* slot.
  94. struct _xidregistry xidregistry;
  95. struct _pymem_allocators allocators;
  96. struct _obmalloc_global_state obmalloc;
  97. struct pyhash_runtime_state pyhash_state;
  98. struct _time_runtime_state time;
  99. struct _pythread_runtime_state threads;
  100. struct _signals_runtime_state signals;
  101. /* Used for the thread state bound to the current thread. */
  102. Py_tss_t autoTSSkey;
  103. /* Used instead of PyThreadState.trash when there is not current tstate. */
  104. Py_tss_t trashTSSkey;
  105. PyWideStringList orig_argv;
  106. struct _parser_runtime_state parser;
  107. struct _atexit_runtime_state atexit;
  108. struct _import_runtime_state imports;
  109. struct _ceval_runtime_state ceval;
  110. struct _gilstate_runtime_state gilstate;
  111. struct _getargs_runtime_state getargs;
  112. struct _fileutils_state fileutils;
  113. struct _faulthandler_runtime_state faulthandler;
  114. struct _tracemalloc_runtime_state tracemalloc;
  115. PyPreConfig preconfig;
  116. // Audit values must be preserved when Py_Initialize()/Py_Finalize()
  117. // is called multiple times.
  118. Py_OpenCodeHookFunction open_code_hook;
  119. void *open_code_userdata;
  120. struct {
  121. PyThread_type_lock mutex;
  122. _Py_AuditHookEntry *head;
  123. } audit_hooks;
  124. struct _py_object_runtime_state object_state;
  125. struct _Py_float_runtime_state float_state;
  126. struct _Py_unicode_runtime_state unicode_state;
  127. struct _types_runtime_state types;
  128. /* All the objects that are shared by the runtime's interpreters. */
  129. struct _Py_static_objects static_objects;
  130. struct _Py_cached_objects cached_objects;
  131. /* The ID of the OS thread in which we are finalizing.
  132. We use _Py_atomic_address instead of adding a new _Py_atomic_ulong. */
  133. _Py_atomic_address _finalizing_id;
  134. /* The value to use for sys.path[0] in new subinterpreters.
  135. Normally this would be part of the PyConfig struct. However,
  136. we cannot add it there in 3.12 since that's an ABI change. */
  137. wchar_t *sys_path_0;
  138. /* The following fields are here to avoid allocation during init.
  139. The data is exposed through _PyRuntimeState pointer fields.
  140. These fields should not be accessed directly outside of init.
  141. All other _PyRuntimeState pointer fields are populated when
  142. needed and default to NULL.
  143. For now there are some exceptions to that rule, which require
  144. allocation during init. These will be addressed on a case-by-case
  145. basis. Most notably, we don't pre-allocated the several mutex
  146. (PyThread_type_lock) fields, because on Windows we only ever get
  147. a pointer type.
  148. */
  149. /* PyInterpreterState.interpreters.main */
  150. PyInterpreterState _main_interpreter;
  151. } _PyRuntimeState;
  152. /* other API */
  153. PyAPI_DATA(_PyRuntimeState) _PyRuntime;
  154. PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime);
  155. PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime);
  156. #ifdef HAVE_FORK
  157. extern PyStatus _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime);
  158. #endif
  159. /* Initialize _PyRuntimeState.
  160. Return NULL on success, or return an error message on failure. */
  161. PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void);
  162. PyAPI_FUNC(void) _PyRuntime_Finalize(void);
  163. static inline PyThreadState*
  164. _PyRuntimeState_GetFinalizing(_PyRuntimeState *runtime) {
  165. return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->_finalizing);
  166. }
  167. static inline unsigned long
  168. _PyRuntimeState_GetFinalizingID(_PyRuntimeState *runtime) {
  169. return (unsigned long)_Py_atomic_load_relaxed(&runtime->_finalizing_id);
  170. }
  171. static inline void
  172. _PyRuntimeState_SetFinalizing(_PyRuntimeState *runtime, PyThreadState *tstate) {
  173. _Py_atomic_store_relaxed(&runtime->_finalizing, (uintptr_t)tstate);
  174. if (tstate == NULL) {
  175. _Py_atomic_store_relaxed(&runtime->_finalizing_id, 0);
  176. }
  177. else {
  178. // XXX Re-enable this assert once gh-109860 is fixed.
  179. //assert(tstate->thread_id == PyThread_get_thread_ident());
  180. _Py_atomic_store_relaxed(&runtime->_finalizing_id,
  181. (uintptr_t)tstate->thread_id);
  182. }
  183. }
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #endif /* !Py_INTERNAL_RUNTIME_H */