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