2 /* Thread and interpreter state structures and their interfaces */
11 /* This limitation is for performance and simplicity. If needed it can be
12 removed (with effort). */
13 #define MAX_CO_EXTRA_USERS 255
15 /* State shared between threads */
17 struct _ts; /* Forward */
18 struct _is; /* Forward */
19 struct _frame; /* Forward declaration for PyFrameObject. */
22 typedef struct _is PyInterpreterState;
24 typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
29 struct _ts *tstate_head;
32 PyObject *modules_by_index;
37 PyObject *codec_search_path;
38 PyObject *codec_search_cache;
39 PyObject *codec_error_registry;
40 int codecs_initialized;
41 int fscodec_initialized;
47 PyObject *builtins_copy;
48 PyObject *import_func;
49 /* Initialized to PyEval_EvalFrameDefault(). */
50 _PyFrameEvalFunction eval_frame;
55 /* State unique per thread */
57 #ifndef Py_LIMITED_API
58 /* Py_tracefunc return -1 when raising an exception, or 0 for success. */
59 typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
61 /* The following values are used for 'what' for tracefunc functions: */
62 #define PyTrace_CALL 0
63 #define PyTrace_EXCEPTION 1
64 #define PyTrace_LINE 2
65 #define PyTrace_RETURN 3
66 #define PyTrace_C_CALL 4
67 #define PyTrace_C_EXCEPTION 5
68 #define PyTrace_C_RETURN 6
72 typedef struct _ts PyThreadState;
75 /* See Python/ceval.c for comments explaining most fields */
79 PyInterpreterState *interp;
83 char overflowed; /* The stack has overflowed. Allow 50 more calls
84 to handle the runtime error. */
85 char recursion_critical; /* The current calls must not cause
87 /* 'tracing' keeps track of the execution depth when tracing/profiling.
88 This is to prevent the actual trace/profile code from being recorded in
93 Py_tracefunc c_profilefunc;
94 Py_tracefunc c_tracefunc;
95 PyObject *c_profileobj;
98 PyObject *curexc_type;
99 PyObject *curexc_value;
100 PyObject *curexc_traceback;
104 PyObject *exc_traceback;
106 PyObject *dict; /* Stores per-thread state */
108 int gilstate_counter;
110 PyObject *async_exc; /* Asynchronous exception to raise */
111 long thread_id; /* Thread id where this tstate was created */
113 int trash_delete_nesting;
114 PyObject *trash_delete_later;
116 /* Called when a thread state is deleted normally, but not when it
117 * is destroyed after fork().
118 * Pain: to prevent rare but fatal shutdown errors (issue 18808),
119 * Thread.join() must wait for the join'ed thread's tstate to be unlinked
120 * from the tstate chain. That happens at the end of a thread's life,
122 * The obvious way doesn't quite work: create a lock which the tstate
123 * unlinking code releases, and have Thread.join() wait to acquire that
124 * lock. The problem is that we _are_ at the end of the thread's life:
125 * if the thread holds the last reference to the lock, decref'ing the
126 * lock will delete the lock, and that may trigger arbitrary Python code
127 * if there's a weakref, with a callback, to the lock. But by this time
128 * _PyThreadState_Current is already NULL, so only the simplest of C code
129 * can be allowed to run (in particular it must not be possible to
131 * So instead of holding the lock directly, the tstate holds a weakref to
132 * the lock: that's the value of on_delete_data below. Decref'ing a
133 * weakref is harmless.
134 * on_delete points to _threadmodule.c's static release_sentinel() function.
135 * After the tstate is unlinked, release_sentinel is called with the
136 * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
137 * the indirectly held lock.
139 void (*on_delete)(void *);
140 void *on_delete_data;
142 PyObject *coroutine_wrapper;
143 int in_coroutine_wrapper;
145 Py_ssize_t co_extra_user_count;
146 freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
148 PyObject *async_gen_firstiter;
149 PyObject *async_gen_finalizer;
151 /* XXX signal handlers should also be here */
157 PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
158 PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
159 PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
160 #ifndef Py_LIMITED_API
161 PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
162 #endif /* !Py_LIMITED_API */
163 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
165 PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
166 PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
168 PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
169 #ifndef Py_LIMITED_API
170 PyAPI_FUNC(void) _PyState_ClearModules(void);
173 PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
174 #ifndef Py_LIMITED_API
175 PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
176 PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
177 #endif /* !Py_LIMITED_API */
178 PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
179 PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
180 #ifndef Py_LIMITED_API
181 PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
182 #endif /* !Py_LIMITED_API */
184 PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
185 #ifndef Py_LIMITED_API
186 PyAPI_FUNC(void) _PyGILState_Reinit(void);
187 #endif /* !Py_LIMITED_API */
190 /* Return the current thread state. The global interpreter lock must be held.
191 * When the current thread state is NULL, this issues a fatal error (so that
192 * the caller needn't check for NULL). */
193 PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
195 #ifndef Py_LIMITED_API
196 /* Similar to PyThreadState_Get(), but don't issue a fatal error
198 PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
199 #endif /* !Py_LIMITED_API */
201 PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
202 PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
203 PyAPI_FUNC(int) PyThreadState_SetAsyncExc(long, PyObject *);
206 /* Variable and macro for in-line access to current thread state */
208 /* Assuming the current thread holds the GIL, this is the
209 PyThreadState for the current thread. */
211 PyAPI_DATA(_Py_atomic_address) _PyThreadState_Current;
212 # define PyThreadState_GET() \
213 ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
215 # define PyThreadState_GET() PyThreadState_Get()
219 enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
224 /* Ensure that the current thread is ready to call the Python
225 C API, regardless of the current state of Python, or of its
226 thread lock. This may be called as many times as desired
227 by a thread so long as each call is matched with a call to
228 PyGILState_Release(). In general, other thread-state APIs may
229 be used between _Ensure() and _Release() calls, so long as the
230 thread-state is restored to its previous state before the Release().
231 For example, normal use of the Py_BEGIN_ALLOW_THREADS/
232 Py_END_ALLOW_THREADS macros are acceptable.
234 The return value is an opaque "handle" to the thread state when
235 PyGILState_Ensure() was called, and must be passed to
236 PyGILState_Release() to ensure Python is left in the same state. Even
237 though recursive calls are allowed, these handles can *not* be shared -
238 each unique call to PyGILState_Ensure must save the handle for its
239 call to PyGILState_Release.
241 When the function returns, the current thread will hold the GIL.
243 Failure is a fatal error.
245 PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
247 /* Release any resources previously acquired. After this call, Python's
248 state will be the same as it was prior to the corresponding
249 PyGILState_Ensure() call (but generally this state will be unknown to
250 the caller, hence the use of the GILState API.)
252 Every call to PyGILState_Ensure must be matched by a call to
253 PyGILState_Release on the same thread.
255 PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
257 /* Helper/diagnostic function - get the current thread state for
258 this thread. May return NULL if no GILState API has been used
259 on the current thread. Note that the main thread always has such a
260 thread-state, even if no auto-thread-state call has been made
263 PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
265 #ifndef Py_LIMITED_API
266 /* Issue #26558: Flag to disable PyGILState_Check().
267 If set to non-zero, PyGILState_Check() always return 1. */
268 PyAPI_DATA(int) _PyGILState_check_enabled;
270 /* Helper/diagnostic function - return 1 if the current thread
271 currently holds the GIL, 0 otherwise.
273 The function returns 1 if _PyGILState_check_enabled is non-zero. */
274 PyAPI_FUNC(int) PyGILState_Check(void);
276 /* Unsafe function to get the single PyInterpreterState used by this process'
277 GILState implementation.
279 Return NULL before _PyGILState_Init() is called and after _PyGILState_Fini()
281 PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
284 #endif /* #ifdef WITH_THREAD */
286 /* The implementation of sys._current_frames() Returns a dict mapping
287 thread id to that thread's current frame.
289 #ifndef Py_LIMITED_API
290 PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
293 /* Routines for advanced debuggers, requested by David Beazley.
294 Don't use unless you know what you are doing! */
295 #ifndef Py_LIMITED_API
296 PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
297 PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
298 PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
299 PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
301 typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
304 /* hook for PyEval_GetFrame(), requested for Psyco */
305 #ifndef Py_LIMITED_API
306 PyAPI_DATA(PyThreadFrameGetter) _PyThreadState_GetFrame;
312 #endif /* !Py_PYSTATE_H */