codekingpro/portable-devtools
114k
1/* Thread and interpreter state structures and their interfaces */2 3 4#ifndef Py_PYSTATE_H5#define Py_PYSTATE_H6#ifdef __cplusplus7extern "C" {8#endif9 10/* This limitation is for performance and simplicity. If needed it can be11removed (with effort). */12#define MAX_CO_EXTRA_USERS 25513 14PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);15PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);16PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);17 18#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x0309000019/* New in 3.9 */20/* Get the current interpreter state.21 22 Issue a fatal error if there no current Python thread state or no current23 interpreter. It cannot return NULL.24 25 The caller must hold the GIL. */26PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Get(void);27#endif28 29#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x0308000030/* New in 3.8 */31PyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *);32#endif33 34#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x0307000035/* New in 3.7 */36PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);37#endif38#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x0303000039 40/* State unique per thread */41 42/* New in 3.3 */43PyAPI_FUNC(int) PyState_AddModule(PyObject*, PyModuleDef*);44PyAPI_FUNC(int) PyState_RemoveModule(PyModuleDef*);45#endif46PyAPI_FUNC(PyObject*) PyState_FindModule(PyModuleDef*);47 48PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);49PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);50PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);51 52/* Get the current thread state.53 54 When the current thread state is NULL, this issues a fatal error (so that55 the caller needn't check for NULL).56 57 The caller must hold the GIL.58 59 See also PyThreadState_GetUnchecked() and _PyThreadState_GET(). */60PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);61 62// Alias to PyThreadState_Get()63#define PyThreadState_GET() PyThreadState_Get()64 65PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);66PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);67PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);68 69#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x0309000070/* New in 3.9 */71PyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate);72PyAPI_FUNC(PyFrameObject*) PyThreadState_GetFrame(PyThreadState *tstate);73PyAPI_FUNC(uint64_t) PyThreadState_GetID(PyThreadState *tstate);74#endif75 76typedef77 enum {PyGILState_LOCKED, PyGILState_UNLOCKED}78 PyGILState_STATE;79 80 81/* Ensure that the current thread is ready to call the Python82 C API, regardless of the current state of Python, or of its83 thread lock. This may be called as many times as desired84 by a thread so long as each call is matched with a call to85 PyGILState_Release(). In general, other thread-state APIs may86 be used between _Ensure() and _Release() calls, so long as the87 thread-state is restored to its previous state before the Release().88 For example, normal use of the Py_BEGIN_ALLOW_THREADS/89 Py_END_ALLOW_THREADS macros are acceptable.90 91 The return value is an opaque "handle" to the thread state when92 PyGILState_Ensure() was called, and must be passed to93 PyGILState_Release() to ensure Python is left in the same state. Even94 though recursive calls are allowed, these handles can *not* be shared -95 each unique call to PyGILState_Ensure must save the handle for its96 call to PyGILState_Release.97 98 When the function returns, the current thread will hold the GIL.99 100 Failure is a fatal error.101*/102PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);103 104/* Release any resources previously acquired. After this call, Python's105 state will be the same as it was prior to the corresponding106 PyGILState_Ensure() call (but generally this state will be unknown to107 the caller, hence the use of the GILState API.)108 109 Every call to PyGILState_Ensure must be matched by a call to110 PyGILState_Release on the same thread.111*/112PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);113 114/* Helper/diagnostic function - get the current thread state for115 this thread. May return NULL if no GILState API has been used116 on the current thread. Note that the main thread always has such a117 thread-state, even if no auto-thread-state call has been made118 on the main thread.119*/120PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);121 122 123#ifndef Py_LIMITED_API124# define Py_CPYTHON_PYSTATE_H125# include "cpython/pystate.h"126# undef Py_CPYTHON_PYSTATE_H127#endif128 129#ifdef __cplusplus130}131#endif132#endif /* !Py_PYSTATE_H */133 