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_atexit.h" // struct atexit_runtime_state
12 #include "pycore_atomic.h" /* _Py_atomic_address */
13 #include "pycore_ceval_state.h" // struct _ceval_runtime_state
14 #include "pycore_floatobject.h" // struct _Py_float_runtime_state
15 #include "pycore_faulthandler.h" // struct _faulthandler_runtime_state
16 #include "pycore_global_objects.h" // struct _Py_global_objects
17 #include "pycore_import.h" // struct _import_runtime_state
18 #include "pycore_interp.h" // PyInterpreterState
19 #include "pycore_object_state.h" // struct _py_object_runtime_state
20 #include "pycore_parser.h" // struct _parser_runtime_state
21 #include "pycore_pymem.h" // struct _pymem_allocators
22 #include "pycore_pyhash.h" // struct pyhash_runtime_state
23 #include "pycore_pythread.h" // struct _pythread_runtime_state
24 #include "pycore_signal.h" // struct _signals_runtime_state
25 #include "pycore_time.h" // struct _time_runtime_state
26 #include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state
27 #include "pycore_typeobject.h" // struct types_runtime_state
28 #include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids
29
30 struct _getargs_runtime_state {
31 PyThread_type_lock mutex;
32 struct _PyArg_Parser *static_parsers;
33 };
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 pyinterpreters {
88 PyThread_type_lock mutex;
89 /* The linked list of interpreters, newest first. */
90 PyInterpreterState *head;
91 /* The runtime's initial interpreter, which has a special role
92 in the operation of the runtime. It is also often the only
93 interpreter. */
94 PyInterpreterState *main;
95 /* next_id is an auto-numbered sequence of small
96 integers. It gets initialized in _PyInterpreterState_Enable(),
97 which is called in Py_Initialize(), and used in
98 PyInterpreterState_New(). A negative interpreter ID
99 indicates an error occurred. The main interpreter will
100 always have an ID of 0. Overflow results in a RuntimeError.
101 If that becomes a problem later then we can adjust, e.g. by
102 using a Python int. */
103 int64_t next_id;
104 } interpreters;
105
106 unsigned long main_thread;
107
108 /* ---------- IMPORTANT ---------------------------
109 The fields above this line are declared as early as
110 possible to facilitate out-of-process observability
111 tools. */
112
113 // XXX Remove this field once we have a tp_* slot.
114 struct _xidregistry {
115 PyThread_type_lock mutex;
116 struct _xidregitem *head;
117 } xidregistry;
118
119 struct _pymem_allocators allocators;
120 struct _obmalloc_global_state obmalloc;
121 struct pyhash_runtime_state pyhash_state;
122 struct _time_runtime_state time;
123 struct _pythread_runtime_state threads;
124 struct _signals_runtime_state signals;
125
126 /* Used for the thread state bound to the current thread. */
127 Py_tss_t autoTSSkey;
128
129 /* Used instead of PyThreadState.trash when there is not current tstate. */
130 Py_tss_t trashTSSkey;
131
132 PyWideStringList orig_argv;
133
134 struct _parser_runtime_state parser;
135
136 struct _atexit_runtime_state atexit;
137
138 struct _import_runtime_state imports;
139 struct _ceval_runtime_state ceval;
140 struct _gilstate_runtime_state gilstate;
141 struct _getargs_runtime_state getargs;
142 struct _fileutils_state fileutils;
143 struct _faulthandler_runtime_state faulthandler;
144 struct _tracemalloc_runtime_state tracemalloc;
145
146 PyPreConfig preconfig;
147
148 // Audit values must be preserved when Py_Initialize()/Py_Finalize()
149 // is called multiple times.
150 Py_OpenCodeHookFunction open_code_hook;
151 void *open_code_userdata;
152 struct {
153 PyThread_type_lock mutex;
154 _Py_AuditHookEntry *head;
155 } audit_hooks;
156
157 struct _py_object_runtime_state object_state;
158 struct _Py_float_runtime_state float_state;
159 struct _Py_unicode_runtime_state unicode_state;
160 struct _types_runtime_state types;
161
162 /* All the objects that are shared by the runtime's interpreters. */
163 struct _Py_static_objects static_objects;
164
165 /* The following fields are here to avoid allocation during init.
166 The data is exposed through _PyRuntimeState pointer fields.
167 These fields should not be accessed directly outside of init.
168
169 All other _PyRuntimeState pointer fields are populated when
170 needed and default to NULL.
171
172 For now there are some exceptions to that rule, which require
173 allocation during init. These will be addressed on a case-by-case
174 basis. Most notably, we don't pre-allocated the several mutex
175 (PyThread_type_lock) fields, because on Windows we only ever get
176 a pointer type.
177 */
178
179 /* PyInterpreterState.interpreters.main */
180 PyInterpreterState _main_interpreter;
181 } _PyRuntimeState;
182
183
184 /* other API */
185
186 PyAPI_DATA(_PyRuntimeState) _PyRuntime;
187
188 PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime);
189 PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime);
190
191 #ifdef HAVE_FORK
192 extern PyStatus _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime);
193 #endif
194
195 /* Initialize _PyRuntimeState.
196 Return NULL on success, or return an error message on failure. */
197 PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void);
198
199 PyAPI_FUNC(void) _PyRuntime_Finalize(void);
200
201
202 static inline PyThreadState*
203 _PyRuntimeState_GetFinalizing(_PyRuntimeState *runtime) {
204 return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->_finalizing);
205 }
206
207 static inline void
208 _PyRuntimeState_SetFinalizing(_PyRuntimeState *runtime, PyThreadState *tstate) {
209 _Py_atomic_store_relaxed(&runtime->_finalizing, (uintptr_t)tstate);
210 }
211
212 #ifdef __cplusplus
213 }
214 #endif
215 #endif /* !Py_INTERNAL_RUNTIME_H */