blob: 94331f35e1bd43343af57d5851d4e9881017581b [file] [log] [blame]
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +01001#ifndef Py_CPYTHON_PYSTATE_H
2# error "this header file must not be included directly"
3#endif
4
5#ifdef __cplusplus
6extern "C" {
7#endif
8
Victor Stinnerf684d832019-03-01 03:44:13 +01009#include "cpython/coreconfig.h"
10
Eric Snowc11183c2019-03-15 16:35:46 -060011PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
12PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
13
Eric Snowbe3b2952019-02-23 11:35:52 -070014PyAPI_FUNC(_PyCoreConfig *) _PyInterpreterState_GetCoreConfig(PyInterpreterState *);
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010015
Eric Snowc11183c2019-03-15 16:35:46 -060016PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *);
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010017
18/* State unique per thread */
19
20/* Py_tracefunc return -1 when raising an exception, or 0 for success. */
21typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
22
23/* The following values are used for 'what' for tracefunc functions
24 *
25 * To add a new kind of trace event, also update "trace_init" in
26 * Python/sysmodule.c to define the Python level event name
27 */
28#define PyTrace_CALL 0
29#define PyTrace_EXCEPTION 1
30#define PyTrace_LINE 2
31#define PyTrace_RETURN 3
32#define PyTrace_C_CALL 4
33#define PyTrace_C_EXCEPTION 5
34#define PyTrace_C_RETURN 6
35#define PyTrace_OPCODE 7
36
37
38typedef struct _err_stackitem {
39 /* This struct represents an entry on the exception stack, which is a
40 * per-coroutine state. (Coroutine in the computer science sense,
41 * including the thread and generators).
42 * This ensures that the exception state is not impacted by "yields"
43 * from an except handler.
44 */
45 PyObject *exc_type, *exc_value, *exc_traceback;
46
47 struct _err_stackitem *previous_item;
48
49} _PyErr_StackItem;
50
51
Eric Snowbe3b2952019-02-23 11:35:52 -070052// The PyThreadState typedef is in Include/pystate.h.
53struct _ts {
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010054 /* See Python/ceval.c for comments explaining most fields */
55
56 struct _ts *prev;
57 struct _ts *next;
58 PyInterpreterState *interp;
59
60 struct _frame *frame;
61 int recursion_depth;
62 char overflowed; /* The stack has overflowed. Allow 50 more calls
63 to handle the runtime error. */
64 char recursion_critical; /* The current calls must not cause
65 a stack overflow. */
66 int stackcheck_counter;
67
68 /* 'tracing' keeps track of the execution depth when tracing/profiling.
69 This is to prevent the actual trace/profile code from being recorded in
70 the trace/profile. */
71 int tracing;
72 int use_tracing;
73
74 Py_tracefunc c_profilefunc;
75 Py_tracefunc c_tracefunc;
76 PyObject *c_profileobj;
77 PyObject *c_traceobj;
78
79 /* The exception currently being raised */
80 PyObject *curexc_type;
81 PyObject *curexc_value;
82 PyObject *curexc_traceback;
83
84 /* The exception currently being handled, if no coroutines/generators
85 * are present. Always last element on the stack referred to be exc_info.
86 */
87 _PyErr_StackItem exc_state;
88
89 /* Pointer to the top of the stack of the exceptions currently
90 * being handled */
91 _PyErr_StackItem *exc_info;
92
93 PyObject *dict; /* Stores per-thread state */
94
95 int gilstate_counter;
96
97 PyObject *async_exc; /* Asynchronous exception to raise */
98 unsigned long thread_id; /* Thread id where this tstate was created */
99
100 int trash_delete_nesting;
101 PyObject *trash_delete_later;
102
103 /* Called when a thread state is deleted normally, but not when it
104 * is destroyed after fork().
105 * Pain: to prevent rare but fatal shutdown errors (issue 18808),
106 * Thread.join() must wait for the join'ed thread's tstate to be unlinked
107 * from the tstate chain. That happens at the end of a thread's life,
108 * in pystate.c.
109 * The obvious way doesn't quite work: create a lock which the tstate
110 * unlinking code releases, and have Thread.join() wait to acquire that
111 * lock. The problem is that we _are_ at the end of the thread's life:
112 * if the thread holds the last reference to the lock, decref'ing the
113 * lock will delete the lock, and that may trigger arbitrary Python code
114 * if there's a weakref, with a callback, to the lock. But by this time
115 * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
116 * of C code can be allowed to run (in particular it must not be possible to
117 * release the GIL).
118 * So instead of holding the lock directly, the tstate holds a weakref to
119 * the lock: that's the value of on_delete_data below. Decref'ing a
120 * weakref is harmless.
121 * on_delete points to _threadmodule.c's static release_sentinel() function.
122 * After the tstate is unlinked, release_sentinel is called with the
123 * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
124 * the indirectly held lock.
125 */
126 void (*on_delete)(void *);
127 void *on_delete_data;
128
129 int coroutine_origin_tracking_depth;
130
131 PyObject *coroutine_wrapper;
132 int in_coroutine_wrapper;
133
134 PyObject *async_gen_firstiter;
135 PyObject *async_gen_finalizer;
136
137 PyObject *context;
138 uint64_t context_ver;
139
140 /* Unique thread state id. */
141 uint64_t id;
142
143 /* XXX signal handlers should also be here */
144
Eric Snowbe3b2952019-02-23 11:35:52 -0700145};
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100146
147/* Get the current interpreter state.
148
149 Issue a fatal error if there no current Python thread state or no current
150 interpreter. It cannot return NULL.
151
152 The caller must hold the GIL.*/
153PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
154
155PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
156PyAPI_FUNC(void) _PyState_ClearModules(void);
157PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100158
159/* Similar to PyThreadState_Get(), but don't issue a fatal error
160 * if it is NULL. */
161PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
162
163/* PyGILState */
164
165/* Helper/diagnostic function - return 1 if the current thread
166 currently holds the GIL, 0 otherwise.
167
168 The function returns 1 if _PyGILState_check_enabled is non-zero. */
169PyAPI_FUNC(int) PyGILState_Check(void);
170
171/* Get the single PyInterpreterState used by this process' GILState
172 implementation.
173
174 This function doesn't check for error. Return NULL before _PyGILState_Init()
175 is called and after _PyGILState_Fini() is called.
176
177 See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */
178PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
179
180/* The implementation of sys._current_frames() Returns a dict mapping
181 thread id to that thread's current frame.
182*/
183PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
184
185/* Routines for advanced debuggers, requested by David Beazley.
186 Don't use unless you know what you are doing! */
187PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
188PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
189PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
190PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
191PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
192
193typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
194
Eric Snowc11183c2019-03-15 16:35:46 -0600195/* cross-interpreter data */
196
197struct _xid;
198
199// _PyCrossInterpreterData is similar to Py_buffer as an effectively
200// opaque struct that holds data outside the object machinery. This
201// is necessary to pass safely between interpreters in the same process.
202typedef struct _xid {
203 // data is the cross-interpreter-safe derivation of a Python object
204 // (see _PyObject_GetCrossInterpreterData). It will be NULL if the
205 // new_object func (below) encodes the data.
206 void *data;
207 // obj is the Python object from which the data was derived. This
208 // is non-NULL only if the data remains bound to the object in some
209 // way, such that the object must be "released" (via a decref) when
210 // the data is released. In that case the code that sets the field,
211 // likely a registered "crossinterpdatafunc", is responsible for
212 // ensuring it owns the reference (i.e. incref).
213 PyObject *obj;
214 // interp is the ID of the owning interpreter of the original
215 // object. It corresponds to the active interpreter when
216 // _PyObject_GetCrossInterpreterData() was called. This should only
217 // be set by the cross-interpreter machinery.
218 //
219 // We use the ID rather than the PyInterpreterState to avoid issues
220 // with deleted interpreters. Note that IDs are never re-used, so
221 // each one will always correspond to a specific interpreter
222 // (whether still alive or not).
223 int64_t interp;
224 // new_object is a function that returns a new object in the current
225 // interpreter given the data. The resulting object (a new
226 // reference) will be equivalent to the original object. This field
227 // is required.
228 PyObject *(*new_object)(struct _xid *);
229 // free is called when the data is released. If it is NULL then
230 // nothing will be done to free the data. For some types this is
231 // okay (e.g. bytes) and for those types this field should be set
232 // to NULL. However, for most the data was allocated just for
233 // cross-interpreter use, so it must be freed when
234 // _PyCrossInterpreterData_Release is called or the memory will
235 // leak. In that case, at the very least this field should be set
236 // to PyMem_RawFree (the default if not explicitly set to NULL).
237 // The call will happen with the original interpreter activated.
238 void (*free)(void *);
239} _PyCrossInterpreterData;
240
241PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
242PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
243PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
244
245PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
246
247/* cross-interpreter data registry */
248
249typedef int (*crossinterpdatafunc)(PyObject *, struct _xid *);
250
251PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
252PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
253
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100254#ifdef __cplusplus
255}
256#endif