jpayne@69: #ifndef Py_CPYTHON_PYSTATE_H jpayne@69: # error "this header file must not be included directly" jpayne@69: #endif jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: extern "C" { jpayne@69: #endif jpayne@69: jpayne@69: #include "cpython/initconfig.h" jpayne@69: jpayne@69: PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *); jpayne@69: PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int); jpayne@69: jpayne@69: PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *); jpayne@69: jpayne@69: /* State unique per thread */ jpayne@69: jpayne@69: /* Py_tracefunc return -1 when raising an exception, or 0 for success. */ jpayne@69: typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *); jpayne@69: jpayne@69: /* The following values are used for 'what' for tracefunc functions jpayne@69: * jpayne@69: * To add a new kind of trace event, also update "trace_init" in jpayne@69: * Python/sysmodule.c to define the Python level event name jpayne@69: */ jpayne@69: #define PyTrace_CALL 0 jpayne@69: #define PyTrace_EXCEPTION 1 jpayne@69: #define PyTrace_LINE 2 jpayne@69: #define PyTrace_RETURN 3 jpayne@69: #define PyTrace_C_CALL 4 jpayne@69: #define PyTrace_C_EXCEPTION 5 jpayne@69: #define PyTrace_C_RETURN 6 jpayne@69: #define PyTrace_OPCODE 7 jpayne@69: jpayne@69: jpayne@69: typedef struct _err_stackitem { jpayne@69: /* This struct represents an entry on the exception stack, which is a jpayne@69: * per-coroutine state. (Coroutine in the computer science sense, jpayne@69: * including the thread and generators). jpayne@69: * This ensures that the exception state is not impacted by "yields" jpayne@69: * from an except handler. jpayne@69: */ jpayne@69: PyObject *exc_type, *exc_value, *exc_traceback; jpayne@69: jpayne@69: struct _err_stackitem *previous_item; jpayne@69: jpayne@69: } _PyErr_StackItem; jpayne@69: jpayne@69: jpayne@69: // The PyThreadState typedef is in Include/pystate.h. jpayne@69: struct _ts { jpayne@69: /* See Python/ceval.c for comments explaining most fields */ jpayne@69: jpayne@69: struct _ts *prev; jpayne@69: struct _ts *next; jpayne@69: PyInterpreterState *interp; jpayne@69: jpayne@69: struct _frame *frame; jpayne@69: int recursion_depth; jpayne@69: char overflowed; /* The stack has overflowed. Allow 50 more calls jpayne@69: to handle the runtime error. */ jpayne@69: char recursion_critical; /* The current calls must not cause jpayne@69: a stack overflow. */ jpayne@69: int stackcheck_counter; jpayne@69: jpayne@69: /* 'tracing' keeps track of the execution depth when tracing/profiling. jpayne@69: This is to prevent the actual trace/profile code from being recorded in jpayne@69: the trace/profile. */ jpayne@69: int tracing; jpayne@69: int use_tracing; jpayne@69: jpayne@69: Py_tracefunc c_profilefunc; jpayne@69: Py_tracefunc c_tracefunc; jpayne@69: PyObject *c_profileobj; jpayne@69: PyObject *c_traceobj; jpayne@69: jpayne@69: /* The exception currently being raised */ jpayne@69: PyObject *curexc_type; jpayne@69: PyObject *curexc_value; jpayne@69: PyObject *curexc_traceback; jpayne@69: jpayne@69: /* The exception currently being handled, if no coroutines/generators jpayne@69: * are present. Always last element on the stack referred to be exc_info. jpayne@69: */ jpayne@69: _PyErr_StackItem exc_state; jpayne@69: jpayne@69: /* Pointer to the top of the stack of the exceptions currently jpayne@69: * being handled */ jpayne@69: _PyErr_StackItem *exc_info; jpayne@69: jpayne@69: PyObject *dict; /* Stores per-thread state */ jpayne@69: jpayne@69: int gilstate_counter; jpayne@69: jpayne@69: PyObject *async_exc; /* Asynchronous exception to raise */ jpayne@69: unsigned long thread_id; /* Thread id where this tstate was created */ jpayne@69: jpayne@69: int trash_delete_nesting; jpayne@69: PyObject *trash_delete_later; jpayne@69: jpayne@69: /* Called when a thread state is deleted normally, but not when it jpayne@69: * is destroyed after fork(). jpayne@69: * Pain: to prevent rare but fatal shutdown errors (issue 18808), jpayne@69: * Thread.join() must wait for the join'ed thread's tstate to be unlinked jpayne@69: * from the tstate chain. That happens at the end of a thread's life, jpayne@69: * in pystate.c. jpayne@69: * The obvious way doesn't quite work: create a lock which the tstate jpayne@69: * unlinking code releases, and have Thread.join() wait to acquire that jpayne@69: * lock. The problem is that we _are_ at the end of the thread's life: jpayne@69: * if the thread holds the last reference to the lock, decref'ing the jpayne@69: * lock will delete the lock, and that may trigger arbitrary Python code jpayne@69: * if there's a weakref, with a callback, to the lock. But by this time jpayne@69: * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest jpayne@69: * of C code can be allowed to run (in particular it must not be possible to jpayne@69: * release the GIL). jpayne@69: * So instead of holding the lock directly, the tstate holds a weakref to jpayne@69: * the lock: that's the value of on_delete_data below. Decref'ing a jpayne@69: * weakref is harmless. jpayne@69: * on_delete points to _threadmodule.c's static release_sentinel() function. jpayne@69: * After the tstate is unlinked, release_sentinel is called with the jpayne@69: * weakref-to-lock (on_delete_data) argument, and release_sentinel releases jpayne@69: * the indirectly held lock. jpayne@69: */ jpayne@69: void (*on_delete)(void *); jpayne@69: void *on_delete_data; jpayne@69: jpayne@69: int coroutine_origin_tracking_depth; jpayne@69: jpayne@69: PyObject *async_gen_firstiter; jpayne@69: PyObject *async_gen_finalizer; jpayne@69: jpayne@69: PyObject *context; jpayne@69: uint64_t context_ver; jpayne@69: jpayne@69: /* Unique thread state id. */ jpayne@69: uint64_t id; jpayne@69: jpayne@69: /* XXX signal handlers should also be here */ jpayne@69: jpayne@69: }; jpayne@69: jpayne@69: /* Get the current interpreter state. jpayne@69: jpayne@69: Issue a fatal error if there no current Python thread state or no current jpayne@69: interpreter. It cannot return NULL. jpayne@69: jpayne@69: The caller must hold the GIL.*/ jpayne@69: PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void); jpayne@69: jpayne@69: PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*); jpayne@69: PyAPI_FUNC(void) _PyState_ClearModules(void); jpayne@69: PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *); jpayne@69: jpayne@69: /* Similar to PyThreadState_Get(), but don't issue a fatal error jpayne@69: * if it is NULL. */ jpayne@69: PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void); jpayne@69: jpayne@69: /* PyGILState */ jpayne@69: jpayne@69: /* Helper/diagnostic function - return 1 if the current thread jpayne@69: currently holds the GIL, 0 otherwise. jpayne@69: jpayne@69: The function returns 1 if _PyGILState_check_enabled is non-zero. */ jpayne@69: PyAPI_FUNC(int) PyGILState_Check(void); jpayne@69: jpayne@69: /* Get the single PyInterpreterState used by this process' GILState jpayne@69: implementation. jpayne@69: jpayne@69: This function doesn't check for error. Return NULL before _PyGILState_Init() jpayne@69: is called and after _PyGILState_Fini() is called. jpayne@69: jpayne@69: See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */ jpayne@69: PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void); jpayne@69: jpayne@69: /* The implementation of sys._current_frames() Returns a dict mapping jpayne@69: thread id to that thread's current frame. jpayne@69: */ jpayne@69: PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void); jpayne@69: jpayne@69: /* Routines for advanced debuggers, requested by David Beazley. jpayne@69: Don't use unless you know what you are doing! */ jpayne@69: PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void); jpayne@69: PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void); jpayne@69: PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *); jpayne@69: PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *); jpayne@69: PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *); jpayne@69: jpayne@69: typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_); jpayne@69: jpayne@69: /* cross-interpreter data */ jpayne@69: jpayne@69: struct _xid; jpayne@69: jpayne@69: // _PyCrossInterpreterData is similar to Py_buffer as an effectively jpayne@69: // opaque struct that holds data outside the object machinery. This jpayne@69: // is necessary to pass safely between interpreters in the same process. jpayne@69: typedef struct _xid { jpayne@69: // data is the cross-interpreter-safe derivation of a Python object jpayne@69: // (see _PyObject_GetCrossInterpreterData). It will be NULL if the jpayne@69: // new_object func (below) encodes the data. jpayne@69: void *data; jpayne@69: // obj is the Python object from which the data was derived. This jpayne@69: // is non-NULL only if the data remains bound to the object in some jpayne@69: // way, such that the object must be "released" (via a decref) when jpayne@69: // the data is released. In that case the code that sets the field, jpayne@69: // likely a registered "crossinterpdatafunc", is responsible for jpayne@69: // ensuring it owns the reference (i.e. incref). jpayne@69: PyObject *obj; jpayne@69: // interp is the ID of the owning interpreter of the original jpayne@69: // object. It corresponds to the active interpreter when jpayne@69: // _PyObject_GetCrossInterpreterData() was called. This should only jpayne@69: // be set by the cross-interpreter machinery. jpayne@69: // jpayne@69: // We use the ID rather than the PyInterpreterState to avoid issues jpayne@69: // with deleted interpreters. Note that IDs are never re-used, so jpayne@69: // each one will always correspond to a specific interpreter jpayne@69: // (whether still alive or not). jpayne@69: int64_t interp; jpayne@69: // new_object is a function that returns a new object in the current jpayne@69: // interpreter given the data. The resulting object (a new jpayne@69: // reference) will be equivalent to the original object. This field jpayne@69: // is required. jpayne@69: PyObject *(*new_object)(struct _xid *); jpayne@69: // free is called when the data is released. If it is NULL then jpayne@69: // nothing will be done to free the data. For some types this is jpayne@69: // okay (e.g. bytes) and for those types this field should be set jpayne@69: // to NULL. However, for most the data was allocated just for jpayne@69: // cross-interpreter use, so it must be freed when jpayne@69: // _PyCrossInterpreterData_Release is called or the memory will jpayne@69: // leak. In that case, at the very least this field should be set jpayne@69: // to PyMem_RawFree (the default if not explicitly set to NULL). jpayne@69: // The call will happen with the original interpreter activated. jpayne@69: void (*free)(void *); jpayne@69: } _PyCrossInterpreterData; jpayne@69: jpayne@69: PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *); jpayne@69: PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *); jpayne@69: PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *); jpayne@69: jpayne@69: PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *); jpayne@69: jpayne@69: /* cross-interpreter data registry */ jpayne@69: jpayne@69: typedef int (*crossinterpdatafunc)(PyObject *, struct _xid *); jpayne@69: jpayne@69: PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc); jpayne@69: PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *); jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: } jpayne@69: #endif