1997-05-05 17:56:21 -03:00
|
|
|
|
|
|
|
/* Thread and interpreter state structures and their interfaces */
|
|
|
|
|
|
|
|
|
2000-07-08 20:37:28 -03:00
|
|
|
#ifndef Py_PYSTATE_H
|
|
|
|
#define Py_PYSTATE_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
/* State shared between threads */
|
|
|
|
|
1997-08-01 23:56:48 -03:00
|
|
|
struct _ts; /* Forward */
|
|
|
|
struct _is; /* Forward */
|
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
typedef struct _is {
|
|
|
|
|
2000-07-08 20:37:28 -03:00
|
|
|
struct _is *next;
|
|
|
|
struct _ts *tstate_head;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2000-07-08 20:37:28 -03:00
|
|
|
PyObject *modules;
|
|
|
|
PyObject *sysdict;
|
|
|
|
PyObject *builtins;
|
2007-03-12 13:11:39 -03:00
|
|
|
PyObject *modules_reloading;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2003-03-18 20:35:36 -04:00
|
|
|
PyObject *codec_search_path;
|
|
|
|
PyObject *codec_search_cache;
|
|
|
|
PyObject *codec_error_registry;
|
|
|
|
|
2001-07-18 13:17:16 -03:00
|
|
|
#ifdef HAVE_DLOPEN
|
|
|
|
int dlopenflags;
|
|
|
|
#endif
|
2004-06-08 05:17:44 -03:00
|
|
|
#ifdef WITH_TSC
|
|
|
|
int tscdump;
|
|
|
|
#endif
|
1997-05-05 17:56:21 -03:00
|
|
|
|
|
|
|
} PyInterpreterState;
|
|
|
|
|
|
|
|
|
|
|
|
/* State unique per thread */
|
|
|
|
|
|
|
|
struct _frame; /* Avoid including frameobject.h */
|
|
|
|
|
2001-06-27 16:18:03 -03:00
|
|
|
/* 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
|
2004-03-24 17:57:10 -04:00
|
|
|
#define PyTrace_C_CALL 4
|
|
|
|
#define PyTrace_C_EXCEPTION 5
|
|
|
|
#define PyTrace_C_RETURN 6
|
2001-06-27 16:18:03 -03:00
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
typedef struct _ts {
|
2005-06-25 04:07:35 -03:00
|
|
|
/* See Python/ceval.c for comments explaining most fields */
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2000-07-08 20:37:28 -03:00
|
|
|
struct _ts *next;
|
|
|
|
PyInterpreterState *interp;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2000-07-08 20:37:28 -03:00
|
|
|
struct _frame *frame;
|
|
|
|
int recursion_depth;
|
2005-06-25 04:07:35 -03:00
|
|
|
/* '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. */
|
2000-07-08 20:37:28 -03:00
|
|
|
int tracing;
|
2001-07-03 20:39:52 -03:00
|
|
|
int use_tracing;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2001-06-27 16:18:03 -03:00
|
|
|
Py_tracefunc c_profilefunc;
|
|
|
|
Py_tracefunc c_tracefunc;
|
|
|
|
PyObject *c_profileobj;
|
|
|
|
PyObject *c_traceobj;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2000-07-08 20:37:28 -03:00
|
|
|
PyObject *curexc_type;
|
|
|
|
PyObject *curexc_value;
|
|
|
|
PyObject *curexc_traceback;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2000-07-08 20:37:28 -03:00
|
|
|
PyObject *exc_type;
|
|
|
|
PyObject *exc_value;
|
|
|
|
PyObject *exc_traceback;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2005-06-25 04:07:35 -03:00
|
|
|
PyObject *dict; /* Stores per-thread state */
|
1998-04-13 17:24:05 -03:00
|
|
|
|
2004-03-28 22:24:26 -04:00
|
|
|
/* 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.
|
|
|
|
*/
|
2002-11-08 08:53:11 -04:00
|
|
|
int tick_counter;
|
2004-03-28 22:24:26 -04:00
|
|
|
|
2003-04-19 12:41:53 -03:00
|
|
|
int gilstate_counter;
|
2002-11-08 08:53:11 -04:00
|
|
|
|
2003-06-28 18:53:52 -03:00
|
|
|
PyObject *async_exc; /* Asynchronous exception to raise */
|
|
|
|
long thread_id; /* Thread id where this tstate was created */
|
|
|
|
|
2000-07-08 20:37:28 -03:00
|
|
|
/* XXX signal handlers should also be here */
|
1997-05-05 17:56:21 -03:00
|
|
|
|
|
|
|
} PyThreadState;
|
|
|
|
|
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
|
|
|
|
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
|
|
|
|
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
|
2010-03-03 19:20:25 -04:00
|
|
|
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
|
|
|
|
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
|
|
|
|
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
|
2001-01-22 21:46:06 -04:00
|
|
|
#ifdef WITH_THREAD
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
|
2011-04-27 14:20:48 -03:00
|
|
|
PyAPI_FUNC(void) _PyGILState_Reinit(void);
|
2001-01-22 21:46:06 -04:00
|
|
|
#endif
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
|
|
|
|
PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
|
|
|
|
PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
|
2003-06-28 18:53:52 -03:00
|
|
|
PyAPI_FUNC(int) PyThreadState_SetAsyncExc(long, PyObject *);
|
1997-05-05 17:56:21 -03:00
|
|
|
|
1998-12-21 14:28:10 -04:00
|
|
|
|
|
|
|
/* Variable and macro for in-line access to current thread state */
|
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_DATA(PyThreadState *) _PyThreadState_Current;
|
1998-12-21 14:28:10 -04:00
|
|
|
|
|
|
|
#ifdef Py_DEBUG
|
|
|
|
#define PyThreadState_GET() PyThreadState_Get()
|
|
|
|
#else
|
|
|
|
#define PyThreadState_GET() (_PyThreadState_Current)
|
|
|
|
#endif
|
|
|
|
|
2004-03-28 22:24:26 -04:00
|
|
|
typedef
|
2003-04-19 12:41:53 -03:00
|
|
|
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
|
2004-03-28 22:24:26 -04:00
|
|
|
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
|
2003-04-19 12:41:53 -03:00
|
|
|
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
|
2004-03-13 16:45:47 -04:00
|
|
|
PyGILState_Ensure() was called, and must be passed to
|
2003-04-19 12:41:53 -03:00
|
|
|
PyGILState_Release() to ensure Python is left in the same state. Even
|
2004-03-28 22:24:26 -04:00
|
|
|
though recursive calls are allowed, these handles can *not* be shared -
|
|
|
|
each unique call to PyGILState_Ensure must save the handle for its
|
2003-04-19 12:41:53 -03:00
|
|
|
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
|
2004-03-28 22:24:26 -04:00
|
|
|
PyGILState_Ensure() call (but generally this state will be unknown to
|
2003-04-19 12:41:53 -03:00
|
|
|
the caller, hence the use of the GILState API.)
|
|
|
|
|
2004-03-28 22:24:26 -04:00
|
|
|
Every call to PyGILState_Ensure must be matched by a call to
|
2003-04-19 12:41:53 -03:00
|
|
|
PyGILState_Release on the same thread.
|
|
|
|
*/
|
|
|
|
PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
|
|
|
|
|
|
|
|
/* Helper/diagnostic function - get the current thread state for
|
2004-03-28 22:24:26 -04:00
|
|
|
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
|
2003-04-19 12:41:53 -03:00
|
|
|
on the main thread.
|
|
|
|
*/
|
|
|
|
PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
|
|
|
|
|
2006-07-10 18:08:24 -03:00
|
|
|
/* The implementation of sys._current_frames() Returns a dict mapping
|
|
|
|
thread id to that thread's current frame.
|
|
|
|
*/
|
|
|
|
PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
|
|
|
|
|
2001-07-19 09:19:27 -03:00
|
|
|
/* Routines for advanced debuggers, requested by David Beazley.
|
|
|
|
Don't use unless you know what you are doing! */
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
|
|
|
|
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
|
|
|
|
PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
|
|
|
|
PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
|
2001-07-19 09:19:27 -03:00
|
|
|
|
2003-02-19 11:53:17 -04:00
|
|
|
typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
|
|
|
|
|
2002-11-08 08:53:11 -04:00
|
|
|
/* hook for PyEval_GetFrame(), requested for Psyco */
|
2003-02-19 11:53:17 -04:00
|
|
|
PyAPI_DATA(PyThreadFrameGetter) _PyThreadState_GetFrame;
|
2002-11-08 08:53:11 -04:00
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_PYSTATE_H */
|