pystate.h 6.26 KB
Newer Older
1 2 3 4

/* Thread and interpreter state structures and their interfaces */


5 6 7 8 9 10
#ifndef Py_PYSTATE_H
#define Py_PYSTATE_H
#ifdef __cplusplus
extern "C" {
#endif

11 12
/* State shared between threads */

13 14 15
struct _ts; /* Forward */
struct _is; /* Forward */

16 17
typedef struct _is {

18 19
    struct _is *next;
    struct _ts *tstate_head;
20

21 22 23
    PyObject *modules;
    PyObject *sysdict;
    PyObject *builtins;
24
    PyObject *modules_reloading;
25

26 27 28 29
    PyObject *codec_search_path;
    PyObject *codec_search_cache;
    PyObject *codec_error_registry;

30 31 32
#ifdef HAVE_DLOPEN
    int dlopenflags;
#endif
33 34 35
#ifdef WITH_TSC
    int tscdump;
#endif
36 37 38 39 40 41 42 43

} PyInterpreterState;


/* State unique per thread */

struct _frame; /* Avoid including frameobject.h */

44 45 46 47 48 49 50 51
/* Py_tracefunc return -1 when raising an exception, or 0 for success. */
typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);

/* The following values are used for 'what' for tracefunc functions: */
#define PyTrace_CALL 0
#define PyTrace_EXCEPTION 1
#define PyTrace_LINE 2
#define PyTrace_RETURN 3
52 53 54
#define PyTrace_C_CALL 4
#define PyTrace_C_EXCEPTION 5
#define PyTrace_C_RETURN 6
55

56
typedef struct _ts {
57
    /* See Python/ceval.c for comments explaining most fields */
58

59 60
    struct _ts *next;
    PyInterpreterState *interp;
61

62 63
    struct _frame *frame;
    int recursion_depth;
64 65 66 67
    char overflowed; /* The stack has overflowed. Allow 50 more calls
		        to handle the runtime error. */
    char recursion_critical; /* The current calls must not cause 
				a stack overflow. */
68 69 70
    /* 'tracing' keeps track of the execution depth when tracing/profiling.
       This is to prevent the actual trace/profile code from being recorded in
       the trace/profile. */
71
    int tracing;
72
    int use_tracing;
73

74 75 76 77
    Py_tracefunc c_profilefunc;
    Py_tracefunc c_tracefunc;
    PyObject *c_profileobj;
    PyObject *c_traceobj;
78

79 80 81
    PyObject *curexc_type;
    PyObject *curexc_value;
    PyObject *curexc_traceback;
82

83 84 85
    PyObject *exc_type;
    PyObject *exc_value;
    PyObject *exc_traceback;
86

87
    PyObject *dict;  /* Stores per-thread state */
88

89 90 91 92 93 94
    /* tick_counter is incremented whenever the check_interval ticker
     * reaches zero. The purpose is to give a useful measure of the number
     * of interpreted bytecode instructions in a given thread.  This
     * extremely lightweight statistic collector may be of interest to
     * profilers (like psyco.jit()), although nothing in the core uses it.
     */
95
    int tick_counter;
96

97
    int gilstate_counter;
98

99 100 101
    PyObject *async_exc; /* Asynchronous exception to raise */
    long thread_id; /* Thread id where this tstate was created */

102
    /* XXX signal handlers should also be here */
103 104 105 106

} PyThreadState;


107 108 109
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
110

111 112 113
PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
114
#ifdef WITH_THREAD
115
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
116
#endif
117

118 119 120
PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
121
PyAPI_FUNC(int) PyThreadState_SetAsyncExc(long, PyObject *);
122

123 124 125

/* Variable and macro for in-line access to current thread state */

126
PyAPI_DATA(PyThreadState *) _PyThreadState_Current;
127 128 129 130 131 132 133

#ifdef Py_DEBUG
#define PyThreadState_GET() PyThreadState_Get()
#else
#define PyThreadState_GET() (_PyThreadState_Current)
#endif

134
typedef
135 136 137 138 139 140
    enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
        PyGILState_STATE;

/* Ensure that the current thread is ready to call the Python
   C API, regardless of the current state of Python, or of its
   thread lock.  This may be called as many times as desired
141 142 143
   by a thread so long as each call is matched with a call to
   PyGILState_Release().  In general, other thread-state APIs may
   be used between _Ensure() and _Release() calls, so long as the
144 145 146 147 148
   thread-state is restored to its previous state before the Release().
   For example, normal use of the Py_BEGIN_ALLOW_THREADS/
   Py_END_ALLOW_THREADS macros are acceptable.

   The return value is an opaque "handle" to the thread state when
149
   PyGILState_Ensure() was called, and must be passed to
150
   PyGILState_Release() to ensure Python is left in the same state. Even
151 152
   though recursive calls are allowed, these handles can *not* be shared -
   each unique call to PyGILState_Ensure must save the handle for its
153 154 155 156 157 158 159 160 161 162
   call to PyGILState_Release.

   When the function returns, the current thread will hold the GIL.

   Failure is a fatal error.
*/
PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);

/* Release any resources previously acquired.  After this call, Python's
   state will be the same as it was prior to the corresponding
163
   PyGILState_Ensure() call (but generally this state will be unknown to
164 165
   the caller, hence the use of the GILState API.)

166
   Every call to PyGILState_Ensure must be matched by a call to
167 168 169 170 171
   PyGILState_Release on the same thread.
*/
PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);

/* Helper/diagnostic function - get the current thread state for
172 173 174
   this thread.  May return NULL if no GILState API has been used
   on the current thread.  Note the main thread always has such a
   thread-state, even if no auto-thread-state call has been made
175 176 177 178
   on the main thread.
*/
PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);

179 180 181 182 183
/* The implementation of sys._current_frames()  Returns a dict mapping
   thread id to that thread's current frame.
*/
PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);

184 185
/* Routines for advanced debuggers, requested by David Beazley.
   Don't use unless you know what you are doing! */
186 187 188 189
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
190

191 192
typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);

193
/* hook for PyEval_GetFrame(), requested for Psyco */
194
PyAPI_DATA(PyThreadFrameGetter) _PyThreadState_GetFrame;
195

196 197 198 199
#ifdef __cplusplus
}
#endif
#endif /* !Py_PYSTATE_H */