| 1 | #ifndef Py_PYSTATE_H |
| 2 | #define Py_PYSTATE_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
| 7 | /* State shared between threads */ |
| 8 | |
| 9 | struct _ts; /* Forward */ |
| 10 | struct _is; /* Forward */ |
| 11 | |
| 12 | typedef struct _is { |
| 13 | |
| 14 | struct _is *next; |
| 15 | struct _ts *tstate_head; |
| 16 | |
| 17 | PyObject *modules; |
| 18 | PyObject *sysdict; |
| 19 | PyObject *builtins; |
| 20 | |
| 21 | PyObject *codec_search_path; |
| 22 | PyObject *codec_search_cache; |
| 23 | PyObject *codec_error_registry; |
| 24 | |
| 25 | #ifdef HAVE_DLOPEN |
| 26 | int dlopenflags; |
| 27 | #endif |
| 28 | #ifdef WITH_TSC |
| 29 | int tscdump; |
| 30 | #endif |
| 31 | |
| 32 | } PyInterpreterState; |
| 33 | |
| 34 | |
| 35 | /* State unique per thread */ |
| 36 | |
| 37 | struct _frame; /* Avoid including frameobject.h */ |
| 38 | |
| 39 | /* Py_tracefunc return -1 when raising an exception, or 0 for success. */ |
| 40 | typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *); |
| 41 | |
| 42 | /* The following values are used for 'what' for tracefunc functions: */ |
| 43 | #define PyTrace_CALL 0 |
| 44 | #define PyTrace_EXCEPTION 1 |
| 45 | #define PyTrace_LINE 2 |
| 46 | #define PyTrace_RETURN 3 |
| 47 | #define PyTrace_C_CALL 4 |
| 48 | #define PyTrace_C_EXCEPTION 5 |
| 49 | #define PyTrace_C_RETURN 6 |
| 50 | |
| 51 | typedef struct _ts { |
| 52 | |
| 53 | struct _ts *next; |
| 54 | PyInterpreterState *interp; |
| 55 | |
| 56 | struct _frame *frame; |
| 57 | int recursion_depth; |
| 58 | int tracing; |
| 59 | int use_tracing; |
| 60 | |
| 61 | Py_tracefunc c_profilefunc; |
| 62 | Py_tracefunc c_tracefunc; |
| 63 | PyObject *c_profileobj; |
| 64 | PyObject *c_traceobj; |
| 65 | |
| 66 | PyObject *curexc_type; |
| 67 | PyObject *curexc_value; |
| 68 | PyObject *curexc_traceback; |
| 69 | |
| 70 | PyObject *exc_type; |
| 71 | PyObject *exc_value; |
| 72 | PyObject *exc_traceback; |
| 73 | |
| 74 | PyObject *dict; |
| 75 | |
| 76 | /* tick_counter is incremented whenever the check_interval ticker |
| 77 | * reaches zero. The purpose is to give a useful measure of the number |
| 78 | * of interpreted bytecode instructions in a given thread. This |
| 79 | * extremely lightweight statistic collector may be of interest to |
| 80 | * profilers (like psyco.jit()), although nothing in the core uses it. |
| 81 | */ |
| 82 | int tick_counter; |
| 83 | |
| 84 | int gilstate_counter; |
| 85 | |
| 86 | PyObject *async_exc; /* Asynchronous exception to raise */ |
| 87 | long thread_id; /* Thread id where this tstate was created */ |
| 88 | |
| 89 | /* XXX signal handlers should also be here */ |
| 90 | |
| 91 | } PyThreadState; |
| 92 | |
| 93 | |
| 94 | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void); |
| 95 | PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *); |
| 96 | PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *); |
| 97 | |
| 98 | PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *); |
| 99 | PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *); |
| 100 | PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *); |
| 101 | #ifdef WITH_THREAD |
| 102 | PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void); |
| 103 | #endif |
| 104 | |
| 105 | PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void); |
| 106 | PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *); |
| 107 | PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void); |
| 108 | PyAPI_FUNC(int) PyThreadState_SetAsyncExc(long, PyObject *); |
| 109 | |
| 110 | |
| 111 | /* Variable and macro for in-line access to current thread state */ |
| 112 | |
| 113 | PyAPI_DATA(PyThreadState *) _PyThreadState_Current; |
| 114 | |
| 115 | #ifdef Py_DEBUG |
| 116 | #define PyThreadState_GET() PyThreadState_Get() |
| 117 | #else |
| 118 | #define PyThreadState_GET() (_PyThreadState_Current) |
| 119 | #endif |
| 120 | |
| 121 | typedef |
| 122 | enum {PyGILState_LOCKED, PyGILState_UNLOCKED} |
| 123 | PyGILState_STATE; |
| 124 | |
| 125 | /* Ensure that the current thread is ready to call the Python |
| 126 | C API, regardless of the current state of Python, or of its |
| 127 | thread lock. This may be called as many times as desired |
| 128 | by a thread so long as each call is matched with a call to |
| 129 | PyGILState_Release(). In general, other thread-state APIs may |
| 130 | be used between _Ensure() and _Release() calls, so long as the |
| 131 | thread-state is restored to its previous state before the Release(). |
| 132 | For example, normal use of the Py_BEGIN_ALLOW_THREADS/ |
| 133 | Py_END_ALLOW_THREADS macros are acceptable. |
| 134 | |
| 135 | The return value is an opaque "handle" to the thread state when |
| 136 | PyGILState_Ensure() was called, and must be passed to |
| 137 | PyGILState_Release() to ensure Python is left in the same state. Even |
| 138 | though recursive calls are allowed, these handles can *not* be shared - |
| 139 | each unique call to PyGILState_Ensure must save the handle for its |
| 140 | call to PyGILState_Release. |
| 141 | |
| 142 | When the function returns, the current thread will hold the GIL. |
| 143 | |
| 144 | Failure is a fatal error. |
| 145 | */ |
| 146 | PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void); |
| 147 | |
| 148 | /* Release any resources previously acquired. After this call, Python's |
| 149 | state will be the same as it was prior to the corresponding |
| 150 | PyGILState_Ensure() call (but generally this state will be unknown to |
| 151 | the caller, hence the use of the GILState API.) |
| 152 | |
| 153 | Every call to PyGILState_Ensure must be matched by a call to |
| 154 | PyGILState_Release on the same thread. |
| 155 | */ |
| 156 | PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE); |
| 157 | |
| 158 | /* Helper/diagnostic function - get the current thread state for |
| 159 | this thread. May return NULL if no GILState API has been used |
| 160 | on the current thread. Note the main thread always has such a |
| 161 | thread-state, even if no auto-thread-state call has been made |
| 162 | on the main thread. |
| 163 | */ |
| 164 | PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void); |
| 165 | |
| 166 | /* Routines for advanced debuggers, requested by David Beazley. |
| 167 | Don't use unless you know what you are doing! */ |
| 168 | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void); |
| 169 | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *); |
| 170 | PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *); |
| 171 | PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *); |
| 172 | |
| 173 | typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_); |
| 174 | |
| 175 | /* hook for PyEval_GetFrame(), requested for Psyco */ |
| 176 | PyAPI_DATA(PyThreadFrameGetter) _PyThreadState_GetFrame; |
| 177 | |
| 178 | #ifdef __cplusplus |
| 179 | } |
| 180 | #endif |
| 181 | #endif /* !Py_PYSTATE_H */ |