1 #ifndef Py_INTERNAL_INTERP_H
2 #define Py_INTERNAL_INTERP_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 <stdbool.h>
12
13 #include "pycore_ast_state.h" // struct ast_state
14 #include "pycore_atexit.h" // struct atexit_state
15 #include "pycore_atomic.h" // _Py_atomic_address
16 #include "pycore_ceval_state.h" // struct _ceval_state
17 #include "pycore_code.h" // struct callable_cache
18 #include "pycore_context.h" // struct _Py_context_state
19 #include "pycore_dict_state.h" // struct _Py_dict_state
20 #include "pycore_dtoa.h" // struct _dtoa_state
21 #include "pycore_exceptions.h" // struct _Py_exc_state
22 #include "pycore_floatobject.h" // struct _Py_float_state
23 #include "pycore_function.h" // FUNC_MAX_WATCHERS
24 #include "pycore_genobject.h" // struct _Py_async_gen_state
25 #include "pycore_gc.h" // struct _gc_runtime_state
26 #include "pycore_global_objects.h" // struct _Py_interp_static_objects
27 #include "pycore_import.h" // struct _import_state
28 #include "pycore_instruments.h" // _PY_MONITORING_EVENTS
29 #include "pycore_list.h" // struct _Py_list_state
30 #include "pycore_object_state.h" // struct _py_object_state
31 #include "pycore_obmalloc.h" // struct obmalloc_state
32 #include "pycore_tuple.h" // struct _Py_tuple_state
33 #include "pycore_typeobject.h" // struct type_cache
34 #include "pycore_unicodeobject.h" // struct _Py_unicode_state
35 #include "pycore_warnings.h" // struct _warnings_runtime_state
36
37
38 struct _Py_long_state {
39 int max_str_digits;
40 };
41
42 /* interpreter state */
43
44 /* PyInterpreterState holds the global state for one of the runtime's
45 interpreters. Typically the initial (main) interpreter is the only one.
46
47 The PyInterpreterState typedef is in Include/pytypedefs.h.
48 */
49 struct _is {
50
51 PyInterpreterState *next;
52
53 int64_t id;
54 int64_t id_refcount;
55 int requires_idref;
56 PyThread_type_lock id_mutex;
57
58 /* Has been initialized to a safe state.
59
60 In order to be effective, this must be set to 0 during or right
61 after allocation. */
62 int _initialized;
63 int finalizing;
64
65 uint64_t monitoring_version;
66 uint64_t last_restart_version;
67 struct pythreads {
68 uint64_t next_unique_id;
69 /* The linked list of threads, newest first. */
70 PyThreadState *head;
71 /* Used in Modules/_threadmodule.c. */
72 long count;
73 /* Support for runtime thread stack size tuning.
74 A value of 0 means using the platform's default stack size
75 or the size specified by the THREAD_STACK_SIZE macro. */
76 /* Used in Python/thread.c. */
77 size_t stacksize;
78 } threads;
79
80 /* Reference to the _PyRuntime global variable. This field exists
81 to not have to pass runtime in addition to tstate to a function.
82 Get runtime from tstate: tstate->interp->runtime. */
83 struct pyruntimestate *runtime;
84
85 /* Set by Py_EndInterpreter().
86
87 Use _PyInterpreterState_GetFinalizing()
88 and _PyInterpreterState_SetFinalizing()
89 to access it, don't access it directly. */
90 _Py_atomic_address _finalizing;
91
92 struct _gc_runtime_state gc;
93
94 /* The following fields are here to avoid allocation during init.
95 The data is exposed through PyInterpreterState pointer fields.
96 These fields should not be accessed directly outside of init.
97
98 All other PyInterpreterState pointer fields are populated when
99 needed and default to NULL.
100
101 For now there are some exceptions to that rule, which require
102 allocation during init. These will be addressed on a case-by-case
103 basis. Also see _PyRuntimeState regarding the various mutex fields.
104 */
105
106 // Dictionary of the sys module
107 PyObject *sysdict;
108
109 // Dictionary of the builtins module
110 PyObject *builtins;
111
112 struct _ceval_state ceval;
113
114 struct _import_state imports;
115
116 /* The per-interpreter GIL, which might not be used. */
117 struct _gil_runtime_state _gil;
118
119 /* ---------- IMPORTANT ---------------------------
120 The fields above this line are declared as early as
121 possible to facilitate out-of-process observability
122 tools. */
123
124 PyObject *codec_search_path;
125 PyObject *codec_search_cache;
126 PyObject *codec_error_registry;
127 int codecs_initialized;
128
129 PyConfig config;
130 unsigned long feature_flags;
131
132 PyObject *dict; /* Stores per-interpreter state */
133
134 PyObject *sysdict_copy;
135 PyObject *builtins_copy;
136 // Initialized to _PyEval_EvalFrameDefault().
137 _PyFrameEvalFunction eval_frame;
138
139 PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
140 // One bit is set for each non-NULL entry in func_watchers
141 uint8_t active_func_watchers;
142
143 Py_ssize_t co_extra_user_count;
144 freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
145
146 #ifdef HAVE_FORK
147 PyObject *before_forkers;
148 PyObject *after_forkers_parent;
149 PyObject *after_forkers_child;
150 #endif
151
152 struct _warnings_runtime_state warnings;
153 struct atexit_state atexit;
154
155 struct _obmalloc_state obmalloc;
156
157 PyObject *audit_hooks;
158 PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
159 PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
160 // One bit is set for each non-NULL entry in code_watchers
161 uint8_t active_code_watchers;
162
163 struct _py_object_state object_state;
164 struct _Py_unicode_state unicode;
165 struct _Py_float_state float_state;
166 struct _Py_long_state long_state;
167 struct _dtoa_state dtoa;
168 struct _py_func_state func_state;
169 /* Using a cache is very effective since typically only a single slice is
170 created and then deleted again. */
171 PySliceObject *slice_cache;
172
173 struct _Py_tuple_state tuple;
174 struct _Py_list_state list;
175 struct _Py_dict_state dict_state;
176 struct _Py_async_gen_state async_gen;
177 struct _Py_context_state context;
178 struct _Py_exc_state exc_state;
179
180 struct ast_state ast;
181 struct types_state types;
182 struct callable_cache callable_cache;
183 PyCodeObject *interpreter_trampoline;
184
185 _Py_GlobalMonitors monitors;
186 bool f_opcode_trace_set;
187 bool sys_profile_initialized;
188 bool sys_trace_initialized;
189 Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */
190 Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */
191 PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS];
192 PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS];
193
194 struct _Py_interp_cached_objects cached_objects;
195 struct _Py_interp_static_objects static_objects;
196
197 /* the initial PyInterpreterState.threads.head */
198 PyThreadState _initial_thread;
199 };
200
201
202 /* other API */
203
204 extern void _PyInterpreterState_Clear(PyThreadState *tstate);
205
206
207 static inline PyThreadState*
208 _PyInterpreterState_GetFinalizing(PyInterpreterState *interp) {
209 return (PyThreadState*)_Py_atomic_load_relaxed(&interp->_finalizing);
210 }
211
212 static inline void
213 _PyInterpreterState_SetFinalizing(PyInterpreterState *interp, PyThreadState *tstate) {
214 _Py_atomic_store_relaxed(&interp->_finalizing, (uintptr_t)tstate);
215 }
216
217
218 /* cross-interpreter data registry */
219
220 /* For now we use a global registry of shareable classes. An
221 alternative would be to add a tp_* slot for a class's
222 crossinterpdatafunc. It would be simpler and more efficient. */
223
224 struct _xidregitem;
225
226 struct _xidregitem {
227 struct _xidregitem *prev;
228 struct _xidregitem *next;
229 PyObject *cls; // weakref to a PyTypeObject
230 crossinterpdatafunc getdata;
231 };
232
233 PyAPI_FUNC(PyInterpreterState*) _PyInterpreterState_LookUpID(int64_t);
234
235 PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
236 PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
237 PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
238
239 #ifdef __cplusplus
240 }
241 #endif
242 #endif /* !Py_INTERNAL_INTERP_H */