1 /* Thread and interpreter state structures and their interfaces */
2 
3 
4 #ifndef Py_PYSTATE_H
5 #define Py_PYSTATE_H
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #include "pythread.h"
11 
12 /* This limitation is for performance and simplicity. If needed it can be
13 removed (with effort). */
14 #define MAX_CO_EXTRA_USERS 255
15 
16 /* Forward declarations for PyFrameObject, PyThreadState
17    and PyInterpreterState */
18 struct _frame;
19 struct _ts;
20 struct _is;
21 
22 /* struct _ts is defined in cpython/pystate.h */
23 typedef struct _ts PyThreadState;
24 /* struct _is is defined in internal/pycore_pystate.h */
25 typedef struct _is PyInterpreterState;
26 
27 PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
28 PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
29 PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
30 
31 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
32 /* New in 3.8 */
33 PyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *);
34 #endif
35 
36 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
37 /* New in 3.7 */
38 PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);
39 #endif
40 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
41 
42 /* State unique per thread */
43 
44 /* New in 3.3 */
45 PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
46 PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
47 #endif
48 PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
49 
50 PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
51 PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
52 PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
53 PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
54 
55 /* Get the current thread state.
56 
57    When the current thread state is NULL, this issues a fatal error (so that
58    the caller needn't check for NULL).
59 
60    The caller must hold the GIL.
61 
62    See also PyThreadState_GET() and _PyThreadState_GET(). */
63 PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
64 
65 /* Get the current Python thread state.
66 
67    Macro using PyThreadState_Get() or _PyThreadState_GET() depending if
68    pycore_pystate.h is included or not (this header redefines the macro).
69 
70    If PyThreadState_Get() is used, issue a fatal error if the current thread
71    state is NULL.
72 
73    See also PyThreadState_Get() and _PyThreadState_GET(). */
74 #define PyThreadState_GET() PyThreadState_Get()
75 
76 PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
77 PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
78 PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
79 
80 typedef
81     enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
82         PyGILState_STATE;
83 
84 
85 /* Ensure that the current thread is ready to call the Python
86    C API, regardless of the current state of Python, or of its
87    thread lock.  This may be called as many times as desired
88    by a thread so long as each call is matched with a call to
89    PyGILState_Release().  In general, other thread-state APIs may
90    be used between _Ensure() and _Release() calls, so long as the
91    thread-state is restored to its previous state before the Release().
92    For example, normal use of the Py_BEGIN_ALLOW_THREADS/
93    Py_END_ALLOW_THREADS macros are acceptable.
94 
95    The return value is an opaque "handle" to the thread state when
96    PyGILState_Ensure() was called, and must be passed to
97    PyGILState_Release() to ensure Python is left in the same state. Even
98    though recursive calls are allowed, these handles can *not* be shared -
99    each unique call to PyGILState_Ensure must save the handle for its
100    call to PyGILState_Release.
101 
102    When the function returns, the current thread will hold the GIL.
103 
104    Failure is a fatal error.
105 */
106 PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
107 
108 /* Release any resources previously acquired.  After this call, Python's
109    state will be the same as it was prior to the corresponding
110    PyGILState_Ensure() call (but generally this state will be unknown to
111    the caller, hence the use of the GILState API.)
112 
113    Every call to PyGILState_Ensure must be matched by a call to
114    PyGILState_Release on the same thread.
115 */
116 PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
117 
118 /* Helper/diagnostic function - get the current thread state for
119    this thread.  May return NULL if no GILState API has been used
120    on the current thread.  Note that the main thread always has such a
121    thread-state, even if no auto-thread-state call has been made
122    on the main thread.
123 */
124 PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
125 
126 
127 #ifndef Py_LIMITED_API
128 #  define Py_CPYTHON_PYSTATE_H
129 #  include  "cpython/pystate.h"
130 #  undef Py_CPYTHON_PYSTATE_H
131 #endif
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 #endif /* !Py_PYSTATE_H */
137