blob: 0f92f67b977939872cbda576cd8e634f372cf16e [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001#ifndef Py_INTERNAL_PYSTATE_H
2#define Py_INTERNAL_PYSTATE_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Victor Stinner5c75f372019-04-17 23:02:26 +02007#ifndef Py_BUILD_CORE
8# error "this header requires Py_BUILD_CORE define"
Victor Stinner50b48572018-11-01 01:51:40 +01009#endif
10
Victor Stinner331a6a52019-05-27 16:39:22 +020011#include "cpython/initconfig.h"
Steve Dowerb82e17e2019-05-23 08:45:22 -070012#include "fileobject.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060013#include "pystate.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060014#include "pythread.h"
Steve Dowerb82e17e2019-05-23 08:45:22 -070015#include "sysmodule.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060016
Victor Stinner09532fe2019-05-10 23:39:09 +020017#include "pycore_gil.h" /* _gil_runtime_state */
Victor Stinnera1c249c2018-11-01 03:15:58 +010018#include "pycore_pathconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010019#include "pycore_pymem.h"
Victor Stinner27e2d1f2018-11-01 00:52:28 +010020#include "pycore_warnings.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060021
22
Victor Stinner09532fe2019-05-10 23:39:09 +020023/* ceval state */
24
Victor Stinnere225beb2019-06-03 18:14:24 +020025struct _pending_calls {
Victor Stinner09532fe2019-05-10 23:39:09 +020026 int finishing;
27 PyThread_type_lock lock;
28 /* Request for running pending calls. */
29 _Py_atomic_int calls_to_do;
30 /* Request for looking at the `async_exc` field of the current
31 thread state.
32 Guarded by the GIL. */
33 int async_exc;
34#define NPENDINGCALLS 32
35 struct {
36 int (*func)(void *);
37 void *arg;
38 } calls[NPENDINGCALLS];
39 int first;
40 int last;
41};
42
43struct _ceval_runtime_state {
44 int recursion_limit;
45 /* Records whether tracing is on for any thread. Counts the number
46 of threads for which tstate->c_tracefunc is non-NULL, so if the
47 value is 0, we know we don't have to check this thread's
48 c_tracefunc. This speeds up the if statement in
49 PyEval_EvalFrameEx() after fast_next_opcode. */
50 int tracing_possible;
51 /* This single variable consolidates all requests to break out of
52 the fast path in the eval loop. */
53 _Py_atomic_int eval_breaker;
54 /* Request for dropping the GIL */
55 _Py_atomic_int gil_drop_request;
Victor Stinnere225beb2019-06-03 18:14:24 +020056 struct _pending_calls pending;
Victor Stinner09532fe2019-05-10 23:39:09 +020057 /* Request for checking signals. */
58 _Py_atomic_int signals_pending;
59 struct _gil_runtime_state gil;
60};
61
Eric Snow7f8bfc92018-01-29 18:23:44 -070062/* interpreter state */
63
Eric Snowbe3b2952019-02-23 11:35:52 -070064typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
Eric Snow7f8bfc92018-01-29 18:23:44 -070065
Eric Snowbe3b2952019-02-23 11:35:52 -070066// The PyInterpreterState typedef is in Include/pystate.h.
67struct _is {
68
69 struct _is *next;
70 struct _ts *tstate_head;
71
72 int64_t id;
73 int64_t id_refcount;
Eric Snowc11183c2019-03-15 16:35:46 -060074 int requires_idref;
Eric Snowbe3b2952019-02-23 11:35:52 -070075 PyThread_type_lock id_mutex;
76
Eric Snow5be45a62019-03-08 22:47:07 -070077 int finalizing;
78
Eric Snowbe3b2952019-02-23 11:35:52 -070079 PyObject *modules;
80 PyObject *modules_by_index;
81 PyObject *sysdict;
82 PyObject *builtins;
83 PyObject *importlib;
84
Eric Snowbe3b2952019-02-23 11:35:52 -070085 /* Used in Modules/_threadmodule.c. */
86 long num_threads;
87 /* Support for runtime thread stack size tuning.
88 A value of 0 means using the platform's default stack size
89 or the size specified by the THREAD_STACK_SIZE macro. */
90 /* Used in Python/thread.c. */
91 size_t pythread_stacksize;
92
93 PyObject *codec_search_path;
94 PyObject *codec_search_cache;
95 PyObject *codec_error_registry;
96 int codecs_initialized;
Victor Stinner709d23d2019-05-02 14:56:30 -040097
98 /* fs_codec.encoding is initialized to NULL.
99 Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */
100 struct {
101 char *encoding; /* Filesystem encoding (encoded to UTF-8) */
102 char *errors; /* Filesystem errors (encoded to UTF-8) */
103 _Py_error_handler error_handler;
104 } fs_codec;
Eric Snowbe3b2952019-02-23 11:35:52 -0700105
Victor Stinner331a6a52019-05-27 16:39:22 +0200106 PyConfig config;
Eric Snowbe3b2952019-02-23 11:35:52 -0700107#ifdef HAVE_DLOPEN
108 int dlopenflags;
109#endif
110
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600111 PyObject *dict; /* Stores per-interpreter state */
112
Eric Snowbe3b2952019-02-23 11:35:52 -0700113 PyObject *builtins_copy;
114 PyObject *import_func;
115 /* Initialized to PyEval_EvalFrameDefault(). */
116 _PyFrameEvalFunction eval_frame;
117
118 Py_ssize_t co_extra_user_count;
119 freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
120
121#ifdef HAVE_FORK
122 PyObject *before_forkers;
123 PyObject *after_forkers_parent;
124 PyObject *after_forkers_child;
125#endif
126 /* AtExit module */
127 void (*pyexitfunc)(PyObject *);
128 PyObject *pyexitmodule;
129
130 uint64_t tstate_next_unique_id;
Eric Snow86ea5812019-05-10 13:29:55 -0400131
132 struct _warnings_runtime_state warnings;
Steve Dowerb82e17e2019-05-23 08:45:22 -0700133
134 PyObject *audit_hooks;
Eric Snowbe3b2952019-02-23 11:35:52 -0700135};
136
137PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T);
138
139PyAPI_FUNC(int) _PyInterpreterState_IDInitref(struct _is *);
140PyAPI_FUNC(void) _PyInterpreterState_IDIncref(struct _is *);
141PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *);
Eric Snow4c6955e2018-02-16 18:53:40 -0700142
Eric Snow7f8bfc92018-01-29 18:23:44 -0700143
Eric Snow7f8bfc92018-01-29 18:23:44 -0700144/* cross-interpreter data registry */
145
146/* For now we use a global registry of shareable classes. An
147 alternative would be to add a tp_* slot for a class's
148 crossinterpdatafunc. It would be simpler and more efficient. */
149
Eric Snow7f8bfc92018-01-29 18:23:44 -0700150struct _xidregitem;
151
152struct _xidregitem {
153 PyTypeObject *cls;
154 crossinterpdatafunc getdata;
155 struct _xidregitem *next;
156};
157
Steve Dowerb82e17e2019-05-23 08:45:22 -0700158/* runtime audit hook state */
159
160typedef struct _Py_AuditHookEntry {
161 struct _Py_AuditHookEntry *next;
162 Py_AuditHookFunction hookCFunction;
163 void *userData;
164} _Py_AuditHookEntry;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700165
Eric Snowbe3b2952019-02-23 11:35:52 -0700166/* GIL state */
167
168struct _gilstate_runtime_state {
169 int check_enabled;
170 /* Assuming the current thread holds the GIL, this is the
171 PyThreadState for the current thread. */
172 _Py_atomic_address tstate_current;
173 PyThreadFrameGetter getframe;
174 /* The single PyInterpreterState used by this process'
175 GILState implementation
176 */
177 /* TODO: Given interp_main, it may be possible to kill this ref */
178 PyInterpreterState *autoInterpreterState;
179 Py_tss_t autoTSSkey;
180};
181
182/* hook for PyEval_GetFrame(), requested for Psyco */
183#define _PyThreadState_GetFrame _PyRuntime.gilstate.getframe
184
185/* Issue #26558: Flag to disable PyGILState_Check().
186 If set to non-zero, PyGILState_Check() always return 1. */
187#define _PyGILState_check_enabled _PyRuntime.gilstate.check_enabled
188
189
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600190/* Full Python runtime state */
191
192typedef struct pyruntimestate {
Victor Stinner331a6a52019-05-27 16:39:22 +0200193 /* Is Python pre-initialized? Set to 1 by Py_PreInitialize() */
Victor Stinnerf29084d2019-03-20 02:20:13 +0100194 int pre_initialized;
195
196 /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600197 int core_initialized;
Victor Stinnerf29084d2019-03-20 02:20:13 +0100198
199 /* Is Python fully initialized? Set to 1 by Py_Initialize() */
200 int initialized;
201
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600202 PyThreadState *finalizing;
203
204 struct pyinterpreters {
205 PyThread_type_lock mutex;
206 PyInterpreterState *head;
207 PyInterpreterState *main;
208 /* _next_interp_id is an auto-numbered sequence of small
209 integers. It gets initialized in _PyInterpreterState_Init(),
210 which is called in Py_Initialize(), and used in
211 PyInterpreterState_New(). A negative interpreter ID
212 indicates an error occurred. The main interpreter will
213 always have an ID of 0. Overflow results in a RuntimeError.
214 If that becomes a problem later then we can adjust, e.g. by
215 using a Python int. */
216 int64_t next_id;
217 } interpreters;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700218 // XXX Remove this field once we have a tp_* slot.
219 struct _xidregistry {
220 PyThread_type_lock mutex;
221 struct _xidregitem *head;
222 } xidregistry;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600223
Eric Snow5be45a62019-03-08 22:47:07 -0700224 unsigned long main_thread;
225
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600226#define NEXITFUNCS 32
227 void (*exitfuncs[NEXITFUNCS])(void);
228 int nexitfuncs;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600229
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600230 struct _gc_runtime_state gc;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600231 struct _ceval_runtime_state ceval;
232 struct _gilstate_runtime_state gilstate;
233
Victor Stinner331a6a52019-05-27 16:39:22 +0200234 PyPreConfig preconfig;
Steve Dowerb82e17e2019-05-23 08:45:22 -0700235
236 Py_OpenCodeHookFunction open_code_hook;
237 void *open_code_userdata;
238 _Py_AuditHookEntry *audit_hook_head;
239
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600240 // XXX Consolidate globals found via the check-c-globals script.
241} _PyRuntimeState;
242
Victor Stinnerf29084d2019-03-20 02:20:13 +0100243#define _PyRuntimeState_INIT \
244 {.pre_initialized = 0, .core_initialized = 0, .initialized = 0}
Victor Stinner33c377e2017-12-05 15:12:41 +0100245/* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800246
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600247PyAPI_DATA(_PyRuntimeState) _PyRuntime;
Victor Stinner331a6a52019-05-27 16:39:22 +0200248PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200249PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime);
250PyAPI_FUNC(void) _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600251
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800252/* Initialize _PyRuntimeState.
253 Return NULL on success, or return an error message on failure. */
Victor Stinner331a6a52019-05-27 16:39:22 +0200254PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800255
Victor Stinnerf5f336a2019-03-19 14:53:58 +0100256PyAPI_FUNC(void) _PyRuntime_Finalize(void);
257
Victor Stinner09532fe2019-05-10 23:39:09 +0200258#define _Py_CURRENTLY_FINALIZING(runtime, tstate) \
259 (runtime->finalizing == tstate)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600260
261
Victor Stinner50b48572018-11-01 01:51:40 +0100262/* Variable and macro for in-line access to current thread
263 and interpreter state */
264
Victor Stinner09532fe2019-05-10 23:39:09 +0200265#define _PyRuntimeState_GetThreadState(runtime) \
266 ((PyThreadState*)_Py_atomic_load_relaxed(&(runtime)->gilstate.tstate_current))
267
Victor Stinner50b48572018-11-01 01:51:40 +0100268/* Get the current Python thread state.
269
270 Efficient macro reading directly the 'gilstate.tstate_current' atomic
271 variable. The macro is unsafe: it does not check for error and it can
272 return NULL.
273
274 The caller must hold the GIL.
275
276 See also PyThreadState_Get() and PyThreadState_GET(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200277#define _PyThreadState_GET() _PyRuntimeState_GetThreadState(&_PyRuntime)
Victor Stinner50b48572018-11-01 01:51:40 +0100278
279/* Redefine PyThreadState_GET() as an alias to _PyThreadState_GET() */
280#undef PyThreadState_GET
281#define PyThreadState_GET() _PyThreadState_GET()
282
283/* Get the current interpreter state.
284
285 The macro is unsafe: it does not check for error and it can return NULL.
286
287 The caller must hold the GIL.
288
289 See also _PyInterpreterState_Get()
290 and _PyGILState_GetInterpreterStateUnsafe(). */
291#define _PyInterpreterState_GET_UNSAFE() (_PyThreadState_GET()->interp)
292
293
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600294/* Other */
295
Victor Stinner0fd2c302019-06-04 03:15:09 +0200296PyAPI_FUNC(void) _PyThreadState_Init(
297 _PyRuntimeState *runtime,
298 PyThreadState *tstate);
299PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
300 _PyRuntimeState *runtime,
301 PyThreadState *tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200302
303PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
304 struct _gilstate_runtime_state *gilstate,
305 PyThreadState *newts);
Victor Stinner8bb32302019-04-24 16:47:40 +0200306
Victor Stinner331a6a52019-05-27 16:39:22 +0200307PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200308PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
309
Victor Stinnerb45d2592019-06-20 00:05:23 +0200310/* Used by _PyImport_Cleanup() */
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200311extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);
312
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200313PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600314
315#ifdef __cplusplus
316}
317#endif
318#endif /* !Py_INTERNAL_PYSTATE_H */