blob: 8508da0554bfaea44372ad301c96184e4439747d [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001
2/* Thread and interpreter state structures and their interfaces */
3
4
Fred Drake5eb6d4e2000-07-08 23:37:28 +00005#ifndef Py_PYSTATE_H
6#define Py_PYSTATE_H
7#ifdef __cplusplus
8extern "C" {
9#endif
10
Guido van Rossuma027efa1997-05-05 20:56:21 +000011/* State shared between threads */
12
Guido van Rossum29e46a91997-08-02 02:56:48 +000013struct _ts; /* Forward */
14struct _is; /* Forward */
15
Guido van Rossuma027efa1997-05-05 20:56:21 +000016typedef struct _is {
17
Fred Drake5eb6d4e2000-07-08 23:37:28 +000018 struct _is *next;
19 struct _ts *tstate_head;
Guido van Rossum29e46a91997-08-02 02:56:48 +000020
Fred Drake5eb6d4e2000-07-08 23:37:28 +000021 PyObject *modules;
Martin v. Löwis1a214512008-06-11 05:26:20 +000022 PyObject *modules_by_index;
Fred Drake5eb6d4e2000-07-08 23:37:28 +000023 PyObject *sysdict;
24 PyObject *builtins;
Guido van Rossumd8faa362007-04-27 19:54:29 +000025 PyObject *modules_reloading;
Guido van Rossuma027efa1997-05-05 20:56:21 +000026
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000027 PyObject *codec_search_path;
28 PyObject *codec_search_cache;
29 PyObject *codec_error_registry;
30
Martin v. Löwisf0473d52001-07-18 16:17:16 +000031#ifdef HAVE_DLOPEN
32 int dlopenflags;
33#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000034#ifdef WITH_TSC
35 int tscdump;
36#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +000037
38} PyInterpreterState;
39
40
41/* State unique per thread */
42
43struct _frame; /* Avoid including frameobject.h */
44
Fred Drake55fb6e02001-06-27 19:18:03 +000045/* Py_tracefunc return -1 when raising an exception, or 0 for success. */
46typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
47
48/* The following values are used for 'what' for tracefunc functions: */
49#define PyTrace_CALL 0
50#define PyTrace_EXCEPTION 1
51#define PyTrace_LINE 2
52#define PyTrace_RETURN 3
Nicholas Bastinc69ebe82004-03-24 21:57:10 +000053#define PyTrace_C_CALL 4
54#define PyTrace_C_EXCEPTION 5
55#define PyTrace_C_RETURN 6
Fred Drake55fb6e02001-06-27 19:18:03 +000056
Guido van Rossuma027efa1997-05-05 20:56:21 +000057typedef struct _ts {
Brett Cannon55fa66d2005-06-25 07:07:35 +000058 /* See Python/ceval.c for comments explaining most fields */
Guido van Rossuma027efa1997-05-05 20:56:21 +000059
Fred Drake5eb6d4e2000-07-08 23:37:28 +000060 struct _ts *next;
61 PyInterpreterState *interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +000062
Fred Drake5eb6d4e2000-07-08 23:37:28 +000063 struct _frame *frame;
64 int recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +000065 char overflowed; /* The stack has overflowed. Allow 50 more calls
66 to handle the runtime error. */
67 char recursion_critical; /* The current calls must not cause
68 a stack overflow. */
Brett Cannon55fa66d2005-06-25 07:07:35 +000069 /* 'tracing' keeps track of the execution depth when tracing/profiling.
70 This is to prevent the actual trace/profile code from being recorded in
71 the trace/profile. */
Fred Drake5eb6d4e2000-07-08 23:37:28 +000072 int tracing;
Fred Drake9e3ad782001-07-03 23:39:52 +000073 int use_tracing;
Guido van Rossuma027efa1997-05-05 20:56:21 +000074
Fred Drake55fb6e02001-06-27 19:18:03 +000075 Py_tracefunc c_profilefunc;
76 Py_tracefunc c_tracefunc;
77 PyObject *c_profileobj;
78 PyObject *c_traceobj;
Guido van Rossuma027efa1997-05-05 20:56:21 +000079
Fred Drake5eb6d4e2000-07-08 23:37:28 +000080 PyObject *curexc_type;
81 PyObject *curexc_value;
82 PyObject *curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000083
Fred Drake5eb6d4e2000-07-08 23:37:28 +000084 PyObject *exc_type;
85 PyObject *exc_value;
86 PyObject *exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000087
Brett Cannon55fa66d2005-06-25 07:07:35 +000088 PyObject *dict; /* Stores per-thread state */
Guido van Rossumee0a63b1998-04-13 20:24:05 +000089
Tim Peters174175b2004-03-29 02:24:26 +000090 /* tick_counter is incremented whenever the check_interval ticker
91 * reaches zero. The purpose is to give a useful measure of the number
92 * of interpreted bytecode instructions in a given thread. This
93 * extremely lightweight statistic collector may be of interest to
94 * profilers (like psyco.jit()), although nothing in the core uses it.
95 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +000096 int tick_counter;
Tim Peters174175b2004-03-29 02:24:26 +000097
Mark Hammond8d98d2c2003-04-19 15:41:53 +000098 int gilstate_counter;
Michael W. Hudson019a78e2002-11-08 12:53:11 +000099
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000100 PyObject *async_exc; /* Asynchronous exception to raise */
101 long thread_id; /* Thread id where this tstate was created */
102
Fred Drake5eb6d4e2000-07-08 23:37:28 +0000103 /* XXX signal handlers should also be here */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000104
105} PyThreadState;
106
107
Mark Hammond91a681d2002-08-12 07:21:58 +0000108PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
109PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
110PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000111PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
112PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000113
Mark Hammond91a681d2002-08-12 07:21:58 +0000114PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
115PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
116PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
Guido van Rossum29757862001-01-23 01:46:06 +0000117#ifdef WITH_THREAD
Mark Hammond91a681d2002-08-12 07:21:58 +0000118PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
Guido van Rossum29757862001-01-23 01:46:06 +0000119#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000120
Mark Hammond91a681d2002-08-12 07:21:58 +0000121PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
122PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
123PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000124PyAPI_FUNC(int) PyThreadState_SetAsyncExc(long, PyObject *);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000125
Guido van Rossum275ea671998-12-21 18:28:10 +0000126
127/* Variable and macro for in-line access to current thread state */
128
Mark Hammond91a681d2002-08-12 07:21:58 +0000129PyAPI_DATA(PyThreadState *) _PyThreadState_Current;
Guido van Rossum275ea671998-12-21 18:28:10 +0000130
131#ifdef Py_DEBUG
132#define PyThreadState_GET() PyThreadState_Get()
133#else
134#define PyThreadState_GET() (_PyThreadState_Current)
135#endif
136
Tim Peters174175b2004-03-29 02:24:26 +0000137typedef
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000138 enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
139 PyGILState_STATE;
140
141/* Ensure that the current thread is ready to call the Python
142 C API, regardless of the current state of Python, or of its
143 thread lock. This may be called as many times as desired
Tim Peters174175b2004-03-29 02:24:26 +0000144 by a thread so long as each call is matched with a call to
145 PyGILState_Release(). In general, other thread-state APIs may
146 be used between _Ensure() and _Release() calls, so long as the
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000147 thread-state is restored to its previous state before the Release().
148 For example, normal use of the Py_BEGIN_ALLOW_THREADS/
149 Py_END_ALLOW_THREADS macros are acceptable.
150
151 The return value is an opaque "handle" to the thread state when
Raymond Hettinger4eec95a2004-03-13 20:45:47 +0000152 PyGILState_Ensure() was called, and must be passed to
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000153 PyGILState_Release() to ensure Python is left in the same state. Even
Tim Peters174175b2004-03-29 02:24:26 +0000154 though recursive calls are allowed, these handles can *not* be shared -
155 each unique call to PyGILState_Ensure must save the handle for its
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000156 call to PyGILState_Release.
157
158 When the function returns, the current thread will hold the GIL.
159
160 Failure is a fatal error.
161*/
162PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
163
164/* Release any resources previously acquired. After this call, Python's
165 state will be the same as it was prior to the corresponding
Tim Peters174175b2004-03-29 02:24:26 +0000166 PyGILState_Ensure() call (but generally this state will be unknown to
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000167 the caller, hence the use of the GILState API.)
168
Tim Peters174175b2004-03-29 02:24:26 +0000169 Every call to PyGILState_Ensure must be matched by a call to
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000170 PyGILState_Release on the same thread.
171*/
172PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
173
174/* Helper/diagnostic function - get the current thread state for
Tim Peters174175b2004-03-29 02:24:26 +0000175 this thread. May return NULL if no GILState API has been used
176 on the current thread. Note the main thread always has such a
177 thread-state, even if no auto-thread-state call has been made
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000178 on the main thread.
179*/
180PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
181
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000182/* The implementation of sys._current_frames() Returns a dict mapping
183 thread id to that thread's current frame.
184*/
185PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
186
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000187/* Routines for advanced debuggers, requested by David Beazley.
188 Don't use unless you know what you are doing! */
Mark Hammond91a681d2002-08-12 07:21:58 +0000189PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
190PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
191PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
192PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000193
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000194typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
195
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000196/* hook for PyEval_GetFrame(), requested for Psyco */
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000197PyAPI_DATA(PyThreadFrameGetter) _PyThreadState_GetFrame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000198
Guido van Rossuma027efa1997-05-05 20:56:21 +0000199#ifdef __cplusplus
200}
201#endif
202#endif /* !Py_PYSTATE_H */