File indexing completed on 2025-11-19 09:50:50
0001 #ifndef Py_INTERNAL_PYSTATE_H
0002 #define Py_INTERNAL_PYSTATE_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006
0007 #ifndef Py_BUILD_CORE
0008 # error "this header requires Py_BUILD_CORE define"
0009 #endif
0010
0011 #include "pycore_freelist.h" // _PyFreeListState
0012 #include "pycore_runtime.h" // _PyRuntime
0013 #include "pycore_tstate.h" // _PyThreadStateImpl
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 #define _Py_THREAD_DETACHED 0
0043 #define _Py_THREAD_ATTACHED 1
0044 #define _Py_THREAD_SUSPENDED 2
0045
0046
0047
0048
0049 static inline int
0050 _Py_IsMainThread(void)
0051 {
0052 unsigned long thread = PyThread_get_thread_ident();
0053 return (thread == _PyRuntime.main_thread);
0054 }
0055
0056
0057 static inline PyInterpreterState *
0058 _PyInterpreterState_Main(void)
0059 {
0060 return _PyRuntime.interpreters.main;
0061 }
0062
0063 static inline int
0064 _Py_IsMainInterpreter(PyInterpreterState *interp)
0065 {
0066 return (interp == _PyInterpreterState_Main());
0067 }
0068
0069 static inline int
0070 _Py_IsMainInterpreterFinalizing(PyInterpreterState *interp)
0071 {
0072
0073
0074
0075
0076 return (_PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL &&
0077 interp == &_PyRuntime._main_interpreter);
0078 }
0079
0080
0081 PyAPI_FUNC(PyObject *) _PyInterpreterState_GetIDObject(PyInterpreterState *);
0082
0083
0084 PyAPI_FUNC(int) _PyInterpreterState_SetRunningMain(PyInterpreterState *);
0085 PyAPI_FUNC(void) _PyInterpreterState_SetNotRunningMain(PyInterpreterState *);
0086 PyAPI_FUNC(int) _PyInterpreterState_IsRunningMain(PyInterpreterState *);
0087 PyAPI_FUNC(int) _PyInterpreterState_FailIfRunningMain(PyInterpreterState *);
0088
0089 extern int _PyThreadState_IsRunningMain(PyThreadState *);
0090 extern void _PyInterpreterState_ReinitRunningMain(PyThreadState *);
0091
0092
0093 static inline const PyConfig *
0094 _Py_GetMainConfig(void)
0095 {
0096 PyInterpreterState *interp = _PyInterpreterState_Main();
0097 if (interp == NULL) {
0098 return NULL;
0099 }
0100 return _PyInterpreterState_GetConfig(interp);
0101 }
0102
0103
0104
0105 static inline int
0106 _Py_ThreadCanHandleSignals(PyInterpreterState *interp)
0107 {
0108 return (_Py_IsMainThread() && _Py_IsMainInterpreter(interp));
0109 }
0110
0111
0112
0113
0114
0115 #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
0116 extern _Py_thread_local PyThreadState *_Py_tss_tstate;
0117 #endif
0118
0119 #ifndef NDEBUG
0120 extern int _PyThreadState_CheckConsistency(PyThreadState *tstate);
0121 #endif
0122
0123 int _PyThreadState_MustExit(PyThreadState *tstate);
0124
0125
0126
0127 PyAPI_FUNC(PyThreadState *) _PyThreadState_GetCurrent(void);
0128
0129
0130
0131
0132
0133
0134
0135
0136 static inline PyThreadState*
0137 _PyThreadState_GET(void)
0138 {
0139 #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
0140 return _Py_tss_tstate;
0141 #else
0142 return _PyThreadState_GetCurrent();
0143 #endif
0144 }
0145
0146
0147
0148
0149
0150
0151
0152
0153 extern void _PyThreadState_Attach(PyThreadState *tstate);
0154
0155
0156
0157
0158
0159 extern void _PyThreadState_Detach(PyThreadState *tstate);
0160
0161
0162
0163
0164
0165
0166 extern void _PyThreadState_Suspend(PyThreadState *tstate);
0167
0168
0169
0170
0171
0172
0173
0174
0175 extern void _PyEval_StopTheWorldAll(_PyRuntimeState *runtime);
0176 extern void _PyEval_StartTheWorldAll(_PyRuntimeState *runtime);
0177
0178
0179
0180
0181 extern void _PyEval_StopTheWorld(PyInterpreterState *interp);
0182 extern void _PyEval_StartTheWorld(PyInterpreterState *interp);
0183
0184
0185 static inline void
0186 _Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
0187 {
0188 if (tstate == NULL) {
0189 _Py_FatalErrorFunc(func,
0190 "the function must be called with the GIL held, "
0191 "after Python initialization and before Python finalization, "
0192 "but the GIL is released (the current Python thread state is NULL)");
0193 }
0194 }
0195
0196
0197 #define _Py_EnsureTstateNotNULL(tstate) \
0198 _Py_EnsureFuncTstateNotNULL(__func__, (tstate))
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209 static inline PyInterpreterState* _PyInterpreterState_GET(void) {
0210 PyThreadState *tstate = _PyThreadState_GET();
0211 #ifdef Py_DEBUG
0212 _Py_EnsureTstateNotNULL(tstate);
0213 #endif
0214 return tstate->interp;
0215 }
0216
0217
0218
0219
0220
0221 PyAPI_FUNC(PyThreadState *) _PyThreadState_New(
0222 PyInterpreterState *interp,
0223 int whence);
0224 extern void _PyThreadState_Bind(PyThreadState *tstate);
0225 PyAPI_FUNC(PyThreadState *) _PyThreadState_NewBound(
0226 PyInterpreterState *interp,
0227 int whence);
0228 extern PyThreadState * _PyThreadState_RemoveExcept(PyThreadState *tstate);
0229 extern void _PyThreadState_DeleteList(PyThreadState *list);
0230 extern void _PyThreadState_ClearMimallocHeaps(PyThreadState *tstate);
0231
0232
0233 PyAPI_FUNC(PyObject*) _PyThreadState_GetDict(PyThreadState *tstate);
0234
0235
0236
0237
0238 extern PyObject* _PyThread_CurrentExceptions(void);
0239
0240
0241
0242
0243 extern PyThreadState * _PyThreadState_Swap(
0244 _PyRuntimeState *runtime,
0245 PyThreadState *newts);
0246
0247 extern PyStatus _PyInterpreterState_Enable(_PyRuntimeState *runtime);
0248
0249 #ifdef HAVE_FORK
0250 extern PyStatus _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
0251 extern void _PySignal_AfterFork(void);
0252 #endif
0253
0254
0255 PyAPI_FUNC(int) _PyState_AddModule(
0256 PyThreadState *tstate,
0257 PyObject* module,
0258 PyModuleDef* def);
0259
0260
0261 extern int _PyOS_InterruptOccurred(PyThreadState *tstate);
0262
0263 #define HEAD_LOCK(runtime) \
0264 PyMutex_LockFlags(&(runtime)->interpreters.mutex, _Py_LOCK_DONT_DETACH)
0265 #define HEAD_UNLOCK(runtime) \
0266 PyMutex_Unlock(&(runtime)->interpreters.mutex)
0267
0268
0269
0270
0271 PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
0272
0273
0274
0275
0276
0277
0278
0279
0280 extern PyInterpreterState* _PyGILState_GetInterpreterStateUnsafe(void);
0281
0282 static inline struct _Py_object_freelists* _Py_object_freelists_GET(void)
0283 {
0284 PyThreadState *tstate = _PyThreadState_GET();
0285 #ifdef Py_DEBUG
0286 _Py_EnsureTstateNotNULL(tstate);
0287 #endif
0288
0289 #ifdef Py_GIL_DISABLED
0290 return &((_PyThreadStateImpl*)tstate)->freelists;
0291 #else
0292 return &tstate->interp->object_state.freelists;
0293 #endif
0294 }
0295
0296 #ifdef __cplusplus
0297 }
0298 #endif
0299 #endif