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