1#ifndef Py_INTERNAL_PYSTATE_H
2#define Py_INTERNAL_PYSTATE_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7#ifndef Py_BUILD_CORE
8# error "this header requires Py_BUILD_CORE define"
9#endif
10
11#include "pycore_runtime.h" /* PyRuntimeState */
12
13
14/* Check if the current thread is the main thread.
15 Use _Py_IsMainInterpreter() to check if it's the main interpreter. */
16static inline int
17_Py_IsMainThread(void)
18{
19 unsigned long thread = PyThread_get_thread_ident();
20 return (thread == _PyRuntime.main_thread);
21}
22
23
24static inline int
25_Py_IsMainInterpreter(PyInterpreterState *interp)
26{
27 /* Use directly _PyRuntime rather than tstate->interp->runtime, since
28 this function is used in performance critical code path (ceval) */
29 return (interp == _PyRuntime.interpreters.main);
30}
31
32
33/* Only handle signals on the main thread of the main interpreter. */
34static inline int
35_Py_ThreadCanHandleSignals(PyInterpreterState *interp)
36{
37 return (_Py_IsMainThread() && interp == _PyRuntime.interpreters.main);
38}
39
40
41/* Only execute pending calls on the main thread. */
42static inline int
43_Py_ThreadCanHandlePendingCalls(void)
44{
45 return _Py_IsMainThread();
46}
47
48
49/* Variable and macro for in-line access to current thread
50 and interpreter state */
51
52#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
53PyAPI_FUNC(PyThreadState*) _PyThreadState_GetTSS(void);
54#endif
55
56static inline PyThreadState*
57_PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
58{
59#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
60 return _PyThreadState_GetTSS();
61#else
62 return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->gilstate.tstate_current);
63#endif
64}
65
66/* Get the current Python thread state.
67
68 Efficient macro reading directly the 'gilstate.tstate_current' atomic
69 variable. The macro is unsafe: it does not check for error and it can
70 return NULL.
71
72 The caller must hold the GIL.
73
74 See also PyThreadState_Get() and PyThreadState_GET(). */
75static inline PyThreadState*
76_PyThreadState_GET(void)
77{
78#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
79 return _PyThreadState_GetTSS();
80#else
81 return _PyRuntimeState_GetThreadState(&_PyRuntime);
82#endif
83}
84
85/* Redefine PyThreadState_GET() as an alias to _PyThreadState_GET() */
86#undef PyThreadState_GET
87#define PyThreadState_GET() _PyThreadState_GET()
88
89PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalError_TstateNULL(const char *func);
90
91static inline void
92_Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
93{
94 if (tstate == NULL) {
95 _Py_FatalError_TstateNULL(func);
96 }
97}
98
99// Call Py_FatalError() if tstate is NULL
100#define _Py_EnsureTstateNotNULL(tstate) \
101 _Py_EnsureFuncTstateNotNULL(__func__, tstate)
102
103
104/* Get the current interpreter state.
105
106 The macro is unsafe: it does not check for error and it can return NULL.
107
108 The caller must hold the GIL.
109
110 See also _PyInterpreterState_Get()
111 and _PyGILState_GetInterpreterStateUnsafe(). */
112static inline PyInterpreterState* _PyInterpreterState_GET(void) {
113 PyThreadState *tstate = _PyThreadState_GET();
114#ifdef Py_DEBUG
115 _Py_EnsureTstateNotNULL(tstate);
116#endif
117 return tstate->interp;
118}
119
120
121/* Other */
122
123PyAPI_FUNC(void) _PyThreadState_Init(
124 PyThreadState *tstate);
125PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
126 _PyRuntimeState *runtime,
127 PyThreadState *tstate);
128
129PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
130 struct _gilstate_runtime_state *gilstate,
131 PyThreadState *newts);
132
133PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime);
134
135#ifdef HAVE_FORK
136extern PyStatus _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
137extern PyStatus _PyGILState_Reinit(_PyRuntimeState *runtime);
138extern void _PySignal_AfterFork(void);
139#endif
140
141
142PyAPI_FUNC(int) _PyState_AddModule(
143 PyThreadState *tstate,
144 PyObject* module,
145 struct PyModuleDef* def);
146
147
148PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate);
149
150#ifdef __cplusplus
151}
152#endif
153#endif /* !Py_INTERNAL_PYSTATE_H */
154