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_atomic.h"        // _Py_atomic_address
14 #include "pycore_ast_state.h"     // struct ast_state
15 #include "pycore_code.h"          // struct callable_cache
16 #include "pycore_context.h"       // struct _Py_context_state
17 #include "pycore_dict.h"          // struct _Py_dict_state
18 #include "pycore_exceptions.h"    // struct _Py_exc_state
19 #include "pycore_floatobject.h"   // struct _Py_float_state
20 #include "pycore_genobject.h"     // struct _Py_async_gen_state
21 #include "pycore_gil.h"           // struct _gil_runtime_state
22 #include "pycore_gc.h"            // struct _gc_runtime_state
23 #include "pycore_list.h"          // struct _Py_list_state
24 #include "pycore_tuple.h"         // struct _Py_tuple_state
25 #include "pycore_typeobject.h"    // struct type_cache
26 #include "pycore_unicodeobject.h" // struct _Py_unicode_state
27 #include "pycore_warnings.h"      // struct _warnings_runtime_state
28 
29 struct _pending_calls {
30     PyThread_type_lock lock;
31     /* Request for running pending calls. */
32     _Py_atomic_int calls_to_do;
33     /* Request for looking at the `async_exc` field of the current
34        thread state.
35        Guarded by the GIL. */
36     int async_exc;
37 #define NPENDINGCALLS 32
38     struct {
39         int (*func)(void *);
40         void *arg;
41     } calls[NPENDINGCALLS];
42     int first;
43     int last;
44 };
45 
46 struct _ceval_state {
47     int recursion_limit;
48     /* This single variable consolidates all requests to break out of
49        the fast path in the eval loop. */
50     _Py_atomic_int eval_breaker;
51     /* Request for dropping the GIL */
52     _Py_atomic_int gil_drop_request;
53     struct _pending_calls pending;
54 };
55 
56 
57 // atexit state
58 typedef struct {
59     PyObject *func;
60     PyObject *args;
61     PyObject *kwargs;
62 } atexit_callback;
63 
64 struct atexit_state {
65     atexit_callback **callbacks;
66     int ncallbacks;
67     int callback_len;
68 };
69 
70 
71 /* interpreter state */
72 
73 /* PyInterpreterState holds the global state for one of the runtime's
74    interpreters.  Typically the initial (main) interpreter is the only one.
75 
76    The PyInterpreterState typedef is in Include/pystate.h.
77    */
78 struct _is {
79 
80     PyInterpreterState *next;
81 
82     struct pythreads {
83         uint64_t next_unique_id;
84         /* The linked list of threads, newest first. */
85         PyThreadState *head;
86         /* Used in Modules/_threadmodule.c. */
87         long count;
88         /* Support for runtime thread stack size tuning.
89            A value of 0 means using the platform's default stack size
90            or the size specified by the THREAD_STACK_SIZE macro. */
91         /* Used in Python/thread.c. */
92         size_t stacksize;
93     } threads;
94 
95     /* Reference to the _PyRuntime global variable. This field exists
96        to not have to pass runtime in addition to tstate to a function.
97        Get runtime from tstate: tstate->interp->runtime. */
98     struct pyruntimestate *runtime;
99 
100     int64_t id;
101     int64_t id_refcount;
102     int requires_idref;
103     PyThread_type_lock id_mutex;
104 
105     /* Has been initialized to a safe state.
106 
107        In order to be effective, this must be set to 0 during or right
108        after allocation. */
109     int _initialized;
110     int finalizing;
111 
112     /* Was this interpreter statically allocated? */
113     bool _static;
114 
115     struct _ceval_state ceval;
116     struct _gc_runtime_state gc;
117 
118     // sys.modules dictionary
119     PyObject *modules;
120     PyObject *modules_by_index;
121     // Dictionary of the sys module
122     PyObject *sysdict;
123     // Dictionary of the builtins module
124     PyObject *builtins;
125     // importlib module
126     PyObject *importlib;
127     // override for config->use_frozen_modules (for tests)
128     // (-1: "off", 1: "on", 0: no override)
129     int override_frozen_modules;
130 
131     PyObject *codec_search_path;
132     PyObject *codec_search_cache;
133     PyObject *codec_error_registry;
134     int codecs_initialized;
135 
136     PyConfig config;
137 #ifdef HAVE_DLOPEN
138     int dlopenflags;
139 #endif
140 
141     PyObject *dict;  /* Stores per-interpreter state */
142 
143     PyObject *builtins_copy;
144     PyObject *import_func;
145     // Initialized to _PyEval_EvalFrameDefault().
146     _PyFrameEvalFunction eval_frame;
147 
148     Py_ssize_t co_extra_user_count;
149     freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
150 
151 #ifdef HAVE_FORK
152     PyObject *before_forkers;
153     PyObject *after_forkers_parent;
154     PyObject *after_forkers_child;
155 #endif
156 
157     struct _warnings_runtime_state warnings;
158     struct atexit_state atexit;
159 
160     PyObject *audit_hooks;
161 
162     struct _Py_unicode_state unicode;
163     struct _Py_float_state float_state;
164     /* Using a cache is very effective since typically only a single slice is
165        created and then deleted again. */
166     PySliceObject *slice_cache;
167 
168     struct _Py_tuple_state tuple;
169     struct _Py_list_state list;
170     struct _Py_dict_state dict_state;
171     struct _Py_async_gen_state async_gen;
172     struct _Py_context_state context;
173     struct _Py_exc_state exc_state;
174 
175     struct ast_state ast;
176     struct type_cache type_cache;
177     struct callable_cache callable_cache;
178 
179     int int_max_str_digits;
180 
181     /* The following fields are here to avoid allocation during init.
182        The data is exposed through PyInterpreterState pointer fields.
183        These fields should not be accessed directly outside of init.
184 
185        All other PyInterpreterState pointer fields are populated when
186        needed and default to NULL.
187 
188        For now there are some exceptions to that rule, which require
189        allocation during init.  These will be addressed on a case-by-case
190        basis.  Also see _PyRuntimeState regarding the various mutex fields.
191        */
192 
193     /* the initial PyInterpreterState.threads.head */
194     PyThreadState _initial_thread;
195 };
196 
197 
198 /* other API */
199 
200 extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);
201 extern void _PyInterpreterState_Clear(PyThreadState *tstate);
202 
203 
204 /* cross-interpreter data registry */
205 
206 /* For now we use a global registry of shareable classes.  An
207    alternative would be to add a tp_* slot for a class's
208    crossinterpdatafunc. It would be simpler and more efficient. */
209 
210 struct _xidregitem;
211 
212 struct _xidregitem {
213     PyTypeObject *cls;
214     crossinterpdatafunc getdata;
215     struct _xidregitem *next;
216 };
217 
218 PyAPI_FUNC(PyInterpreterState*) _PyInterpreterState_LookUpID(int64_t);
219 
220 PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
221 PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
222 PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
223 
224 #ifdef __cplusplus
225 }
226 #endif
227 #endif /* !Py_INTERNAL_INTERP_H */
228