pystate.h 9.2 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 18
#ifdef Py_LIMITED_API
typedef struct _is PyInterpreterState;
#else
19 20
typedef struct _is {

21 22
    struct _is *next;
    struct _ts *tstate_head;
23

24
    PyObject *modules;
25
    PyObject *modules_by_index;
26 27
    PyObject *sysdict;
    PyObject *builtins;
28
    PyObject *importlib;
29

30 31 32
    PyObject *codec_search_path;
    PyObject *codec_search_cache;
    PyObject *codec_error_registry;
33
    int codecs_initialized;
34
    int fscodec_initialized;
35

36 37 38
#ifdef HAVE_DLOPEN
    int dlopenflags;
#endif
39 40 41
#ifdef WITH_TSC
    int tscdump;
#endif
42

43
    PyObject *builtins_copy;
44
} PyInterpreterState;
45
#endif
46 47 48 49 50 51


/* State unique per thread */

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

52
#ifndef Py_LIMITED_API
53 54 55 56 57 58 59 60
/* 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
61 62 63
#define PyTrace_C_CALL 4
#define PyTrace_C_EXCEPTION 5
#define PyTrace_C_RETURN 6
64
#endif
65

66 67 68
#ifdef Py_LIMITED_API
typedef struct _ts PyThreadState;
#else
69
typedef struct _ts {
70
    /* See Python/ceval.c for comments explaining most fields */
71

72
    struct _ts *prev;
73 74
    struct _ts *next;
    PyInterpreterState *interp;
75

76 77
    struct _frame *frame;
    int recursion_depth;
78
    char overflowed; /* The stack has overflowed. Allow 50 more calls
79 80 81
                        to handle the runtime error. */
    char recursion_critical; /* The current calls must not cause
                                a stack overflow. */
82 83 84
    /* '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. */
85
    int tracing;
86
    int use_tracing;
87

88 89 90 91
    Py_tracefunc c_profilefunc;
    Py_tracefunc c_tracefunc;
    PyObject *c_profileobj;
    PyObject *c_traceobj;
92

93 94 95
    PyObject *curexc_type;
    PyObject *curexc_value;
    PyObject *curexc_traceback;
96

97 98 99
    PyObject *exc_type;
    PyObject *exc_value;
    PyObject *exc_traceback;
100

101
    PyObject *dict;  /* Stores per-thread state */
102

103
    int gilstate_counter;
104

105 106 107
    PyObject *async_exc; /* Asynchronous exception to raise */
    long thread_id; /* Thread id where this tstate was created */

108 109 110
    int trash_delete_nesting;
    PyObject *trash_delete_later;

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    /* Called when a thread state is deleted normally, but not when it
     * is destroyed after fork().
     * Pain:  to prevent rare but fatal shutdown errors (issue 18808),
     * Thread.join() must wait for the join'ed thread's tstate to be unlinked
     * from the tstate chain.  That happens at the end of a thread's life,
     * in pystate.c.
     * The obvious way doesn't quite work:  create a lock which the tstate
     * unlinking code releases, and have Thread.join() wait to acquire that
     * lock.  The problem is that we _are_ at the end of the thread's life:
     * if the thread holds the last reference to the lock, decref'ing the
     * lock will delete the lock, and that may trigger arbitrary Python code
     * if there's a weakref, with a callback, to the lock.  But by this time
     * _PyThreadState_Current is already NULL, so only the simplest of C code
     * can be allowed to run (in particular it must not be possible to
     * release the GIL).
     * So instead of holding the lock directly, the tstate holds a weakref to
     * the lock:  that's the value of on_delete_data below.  Decref'ing a
     * weakref is harmless.
     * on_delete points to _threadmodule.c's static release_sentinel() function.
     * After the tstate is unlinked, release_sentinel is called with the
     * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
     * the indirectly held lock.
     */
    void (*on_delete)(void *);
    void *on_delete_data;

137
    PyObject *coroutine_wrapper;
138
    int in_coroutine_wrapper;
139

140
    /* XXX signal handlers should also be here */
141 142

} PyThreadState;
143
#endif
144 145


146 147 148
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
149
PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
150 151 152 153 154
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
/* New in 3.3 */
PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
#endif
155
PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
156 157 158
#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _PyState_ClearModules(void);
#endif
159

160
PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
161 162
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
163 164
PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
165
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
166
#ifdef WITH_THREAD
167
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
168
PyAPI_FUNC(void) _PyGILState_Reinit(void);
169
#endif
170

171 172 173
PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
174
PyAPI_FUNC(int) PyThreadState_SetAsyncExc(long, PyObject *);
175

176 177 178

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

179
/* Assuming the current thread holds the GIL, this is the
180 181 182 183 184 185
   PyThreadState for the current thread.

   Issue #23644: pyatomic.h is incompatible with C++ (yet). Disable
   PyThreadState_GET() optimization: declare it as an alias to
   PyThreadState_Get(), as done for limited API. */
#if !defined(Py_LIMITED_API) && !defined(__cplusplus)
186
PyAPI_DATA(_Py_atomic_address) _PyThreadState_Current;
187
#endif
188

189
#if defined(Py_DEBUG) || defined(Py_LIMITED_API) || defined(__cplusplus)
190 191
#define PyThreadState_GET() PyThreadState_Get()
#else
192 193
#define PyThreadState_GET() \
    ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
194 195
#endif

196
typedef
197 198 199
    enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
        PyGILState_STATE;

200 201
#ifdef WITH_THREAD

202 203 204
/* 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
205 206 207
   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
208 209 210 211 212
   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
213
   PyGILState_Ensure() was called, and must be passed to
214
   PyGILState_Release() to ensure Python is left in the same state. Even
215 216
   though recursive calls are allowed, these handles can *not* be shared -
   each unique call to PyGILState_Ensure must save the handle for its
217 218 219 220 221 222 223 224 225 226
   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
227
   PyGILState_Ensure() call (but generally this state will be unknown to
228 229
   the caller, hence the use of the GILState API.)

230
   Every call to PyGILState_Ensure must be matched by a call to
231 232 233 234 235
   PyGILState_Release on the same thread.
*/
PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);

/* Helper/diagnostic function - get the current thread state for
236
   this thread.  May return NULL if no GILState API has been used
237
   on the current thread.  Note that the main thread always has such a
238
   thread-state, even if no auto-thread-state call has been made
239 240 241 242
   on the main thread.
*/
PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);

243 244 245
/* Helper/diagnostic function - return 1 if the current thread
 * currently holds the GIL, 0 otherwise
 */
246
#ifndef Py_LIMITED_API
247
PyAPI_FUNC(int) PyGILState_Check(void);
248
#endif
249

250 251
#endif   /* #ifdef WITH_THREAD */

252 253 254
/* The implementation of sys._current_frames()  Returns a dict mapping
   thread id to that thread's current frame.
*/
255
#ifndef Py_LIMITED_API
256
PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
257
#endif
258

259 260
/* Routines for advanced debuggers, requested by David Beazley.
   Don't use unless you know what you are doing! */
261
#ifndef Py_LIMITED_API
262 263 264 265
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
266

267
typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
268
#endif
269

270
/* hook for PyEval_GetFrame(), requested for Psyco */
271
#ifndef Py_LIMITED_API
272
PyAPI_DATA(PyThreadFrameGetter) _PyThreadState_GetFrame;
273
#endif
274

275 276 277 278
#ifdef __cplusplus
}
#endif
#endif /* !Py_PYSTATE_H */