pystate.h 6.18 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
    /* '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. */
67
    int tracing;
68
    int use_tracing;
69

70 71 72 73
    Py_tracefunc c_profilefunc;
    Py_tracefunc c_tracefunc;
    PyObject *c_profileobj;
    PyObject *c_traceobj;
74

75 76 77
    PyObject *curexc_type;
    PyObject *curexc_value;
    PyObject *curexc_traceback;
78

79 80 81
    PyObject *exc_type;
    PyObject *exc_value;
    PyObject *exc_traceback;
82

83
    PyObject *dict;  /* Stores per-thread state */
84

85 86 87 88 89 90
    /* 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.
     */
91
    int tick_counter;
92

93
    int gilstate_counter;
94

95 96 97
    PyObject *async_exc; /* Asynchronous exception to raise */
    long thread_id; /* Thread id where this tstate was created */

98
    /* XXX signal handlers should also be here */
99 100 101 102

} PyThreadState;


103 104 105
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
106

107
PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
108 109
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
110 111
PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
112
#ifdef WITH_THREAD
113
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
114
#endif
115

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

121 122 123

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

124
PyAPI_DATA(PyThreadState *) _PyThreadState_Current;
125 126 127 128 129 130 131

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

132
typedef
133 134 135 136 137 138
    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
139 140 141
   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
142 143 144 145 146
   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
147
   PyGILState_Ensure() was called, and must be passed to
148
   PyGILState_Release() to ensure Python is left in the same state. Even
149 150
   though recursive calls are allowed, these handles can *not* be shared -
   each unique call to PyGILState_Ensure must save the handle for its
151 152 153 154 155 156 157 158 159 160
   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
161
   PyGILState_Ensure() call (but generally this state will be unknown to
162 163
   the caller, hence the use of the GILState API.)

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

/* Helper/diagnostic function - get the current thread state for
170 171 172
   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
173 174 175 176
   on the main thread.
*/
PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);

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

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

189 190
typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);

191
/* hook for PyEval_GetFrame(), requested for Psyco */
192
PyAPI_DATA(PyThreadFrameGetter) _PyThreadState_GetFrame;
193

194 195 196 197
#ifdef __cplusplus
}
#endif
#endif /* !Py_PYSTATE_H */