Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:18:09

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_runtime.h"   /* PyRuntimeState */
0012 
0013 
0014 /* Check if the current thread is the main thread.
0015    Use _Py_IsMainInterpreter() to check if it's the main interpreter. */
0016 static inline int
0017 _Py_IsMainThread(void)
0018 {
0019     unsigned long thread = PyThread_get_thread_ident();
0020     return (thread == _PyRuntime.main_thread);
0021 }
0022 
0023 
0024 static inline PyInterpreterState *
0025 _PyInterpreterState_Main(void)
0026 {
0027     return _PyRuntime.interpreters.main;
0028 }
0029 
0030 static inline int
0031 _Py_IsMainInterpreter(PyInterpreterState *interp)
0032 {
0033     return (interp == _PyInterpreterState_Main());
0034 }
0035 
0036 static inline int
0037 _Py_IsMainInterpreterFinalizing(PyInterpreterState *interp)
0038 {
0039     /* bpo-39877: Access _PyRuntime directly rather than using
0040        tstate->interp->runtime to support calls from Python daemon threads.
0041        After Py_Finalize() has been called, tstate can be a dangling pointer:
0042        point to PyThreadState freed memory. */
0043     return (_PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL &&
0044             interp == &_PyRuntime._main_interpreter);
0045 }
0046 
0047 // Export for _xxsubinterpreters module.
0048 PyAPI_FUNC(int) _PyInterpreterState_SetRunningMain(PyInterpreterState *);
0049 PyAPI_FUNC(void) _PyInterpreterState_SetNotRunningMain(PyInterpreterState *);
0050 PyAPI_FUNC(int) _PyInterpreterState_IsRunningMain(PyInterpreterState *);
0051 
0052 
0053 static inline const PyConfig *
0054 _Py_GetMainConfig(void)
0055 {
0056     PyInterpreterState *interp = _PyInterpreterState_Main();
0057     if (interp == NULL) {
0058         return NULL;
0059     }
0060     return _PyInterpreterState_GetConfig(interp);
0061 }
0062 
0063 
0064 /* Only handle signals on the main thread of the main interpreter. */
0065 static inline int
0066 _Py_ThreadCanHandleSignals(PyInterpreterState *interp)
0067 {
0068     return (_Py_IsMainThread() && _Py_IsMainInterpreter(interp));
0069 }
0070 
0071 
0072 /* Variable and static inline functions for in-line access to current thread
0073    and interpreter state */
0074 
0075 #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
0076 extern _Py_thread_local PyThreadState *_Py_tss_tstate;
0077 #endif
0078 PyAPI_DATA(PyThreadState *) _PyThreadState_GetCurrent(void);
0079 
0080 #ifndef NDEBUG
0081 extern int _PyThreadState_CheckConsistency(PyThreadState *tstate);
0082 #endif
0083 
0084 extern int _PyThreadState_MustExit(PyThreadState *tstate);
0085 
0086 /* Get the current Python thread state.
0087 
0088    This function is unsafe: it does not check for error and it can return NULL.
0089 
0090    The caller must hold the GIL.
0091 
0092    See also PyThreadState_Get() and _PyThreadState_UncheckedGet(). */
0093 static inline PyThreadState*
0094 _PyThreadState_GET(void)
0095 {
0096 #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
0097     return _Py_tss_tstate;
0098 #else
0099     return _PyThreadState_GetCurrent();
0100 #endif
0101 }
0102 
0103 
0104 static inline void
0105 _Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
0106 {
0107     if (tstate == NULL) {
0108         _Py_FatalErrorFunc(func,
0109             "the function must be called with the GIL held, "
0110             "after Python initialization and before Python finalization, "
0111             "but the GIL is released (the current Python thread state is NULL)");
0112     }
0113 }
0114 
0115 // Call Py_FatalError() if tstate is NULL
0116 #define _Py_EnsureTstateNotNULL(tstate) \
0117     _Py_EnsureFuncTstateNotNULL(__func__, (tstate))
0118 
0119 
0120 /* Get the current interpreter state.
0121 
0122    The function is unsafe: it does not check for error and it can return NULL.
0123 
0124    The caller must hold the GIL.
0125 
0126    See also _PyInterpreterState_Get()
0127    and _PyGILState_GetInterpreterStateUnsafe(). */
0128 static inline PyInterpreterState* _PyInterpreterState_GET(void) {
0129     PyThreadState *tstate = _PyThreadState_GET();
0130 #ifdef Py_DEBUG
0131     _Py_EnsureTstateNotNULL(tstate);
0132 #endif
0133     return tstate->interp;
0134 }
0135 
0136 
0137 // PyThreadState functions
0138 
0139 PyAPI_FUNC(PyThreadState *) _PyThreadState_New(PyInterpreterState *interp);
0140 PyAPI_FUNC(void) _PyThreadState_Bind(PyThreadState *tstate);
0141 // We keep this around exclusively for stable ABI compatibility.
0142 PyAPI_FUNC(void) _PyThreadState_Init(
0143     PyThreadState *tstate);
0144 PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
0145 
0146 
0147 /* Other */
0148 
0149 PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
0150     _PyRuntimeState *runtime,
0151     PyThreadState *newts);
0152 
0153 PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime);
0154 
0155 #ifdef HAVE_FORK
0156 extern PyStatus _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
0157 extern void _PySignal_AfterFork(void);
0158 #endif
0159 
0160 PyAPI_FUNC(int) _PyCrossInterpreterData_ReleaseAndRawFree(_PyCrossInterpreterData *);
0161 
0162 
0163 PyAPI_FUNC(int) _PyState_AddModule(
0164     PyThreadState *tstate,
0165     PyObject* module,
0166     PyModuleDef* def);
0167 
0168 
0169 PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate);
0170 
0171 #define HEAD_LOCK(runtime) \
0172     PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
0173 #define HEAD_UNLOCK(runtime) \
0174     PyThread_release_lock((runtime)->interpreters.mutex)
0175 
0176 
0177 #ifdef __cplusplus
0178 }
0179 #endif
0180 #endif /* !Py_INTERNAL_PYSTATE_H */