Where Online Learning is simpler!
The C and C++ Include Header Files
/usr/include/python3.12/internal/pycore_pystate.h
$ cat -n /usr/include/python3.12/internal/pycore_pystate.h 1 #ifndef Py_INTERNAL_PYSTATE_H 2 #define Py_INTERNAL_PYSTATE_H 3 #ifdef __cplusplus 4 extern "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. */ 16 static inline int 17 _Py_IsMainThread(void) 18 { 19 unsigned long thread = PyThread_get_thread_ident(); 20 return (thread == _PyRuntime.main_thread); 21 } 22 23 24 static inline PyInterpreterState * 25 _PyInterpreterState_Main(void) 26 { 27 return _PyRuntime.interpreters.main; 28 } 29 30 static inline int 31 _Py_IsMainInterpreter(PyInterpreterState *interp) 32 { 33 return (interp == _PyInterpreterState_Main()); 34 } 35 36 static inline int 37 _Py_IsMainInterpreterFinalizing(PyInterpreterState *interp) 38 { 39 /* bpo-39877: Access _PyRuntime directly rather than using 40 tstate->interp->runtime to support calls from Python daemon threads. 41 After Py_Finalize() has been called, tstate can be a dangling pointer: 42 point to PyThreadState freed memory. */ 43 return (_PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL && 44 interp == &_PyRuntime._main_interpreter); 45 } 46 47 // Export for _xxsubinterpreters module. 48 PyAPI_FUNC(int) _PyInterpreterState_SetRunningMain(PyInterpreterState *); 49 PyAPI_FUNC(void) _PyInterpreterState_SetNotRunningMain(PyInterpreterState *); 50 PyAPI_FUNC(int) _PyInterpreterState_IsRunningMain(PyInterpreterState *); 51 52 53 static inline const PyConfig * 54 _Py_GetMainConfig(void) 55 { 56 PyInterpreterState *interp = _PyInterpreterState_Main(); 57 if (interp == NULL) { 58 return NULL; 59 } 60 return _PyInterpreterState_GetConfig(interp); 61 } 62 63 64 /* Only handle signals on the main thread of the main interpreter. */ 65 static inline int 66 _Py_ThreadCanHandleSignals(PyInterpreterState *interp) 67 { 68 return (_Py_IsMainThread() && _Py_IsMainInterpreter(interp)); 69 } 70 71 72 /* Variable and static inline functions for in-line access to current thread 73 and interpreter state */ 74 75 #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE) 76 extern _Py_thread_local PyThreadState *_Py_tss_tstate; 77 #endif 78 PyAPI_DATA(PyThreadState *) _PyThreadState_GetCurrent(void); 79 80 #ifndef NDEBUG 81 extern int _PyThreadState_CheckConsistency(PyThreadState *tstate); 82 #endif 83 84 extern int _PyThreadState_MustExit(PyThreadState *tstate); 85 86 /* Get the current Python thread state. 87 88 This function is unsafe: it does not check for error and it can return NULL. 89 90 The caller must hold the GIL. 91 92 See also PyThreadState_Get() and _PyThreadState_UncheckedGet(). */ 93 static inline PyThreadState* 94 _PyThreadState_GET(void) 95 { 96 #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE) 97 return _Py_tss_tstate; 98 #else 99 return _PyThreadState_GetCurrent(); 100 #endif 101 } 102 103 104 static inline void 105 _Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate) 106 { 107 if (tstate == NULL) { 108 _Py_FatalErrorFunc(func, 109 "the function must be called with the GIL held, " 110 "after Python initialization and before Python finalization, " 111 "but the GIL is released (the current Python thread state is NULL)"); 112 } 113 } 114 115 // Call Py_FatalError() if tstate is NULL 116 #define _Py_EnsureTstateNotNULL(tstate) \ 117 _Py_EnsureFuncTstateNotNULL(__func__, (tstate)) 118 119 120 /* Get the current interpreter state. 121 122 The function is unsafe: it does not check for error and it can return NULL. 123 124 The caller must hold the GIL. 125 126 See also _PyInterpreterState_Get() 127 and _PyGILState_GetInterpreterStateUnsafe(). */ 128 static inline PyInterpreterState* _PyInterpreterState_GET(void) { 129 PyThreadState *tstate = _PyThreadState_GET(); 130 #ifdef Py_DEBUG 131 _Py_EnsureTstateNotNULL(tstate); 132 #endif 133 return tstate->interp; 134 } 135 136 137 // PyThreadState functions 138 139 PyAPI_FUNC(PyThreadState *) _PyThreadState_New(PyInterpreterState *interp); 140 PyAPI_FUNC(void) _PyThreadState_Bind(PyThreadState *tstate); 141 // We keep this around exclusively for stable ABI compatibility. 142 PyAPI_FUNC(void) _PyThreadState_Init( 143 PyThreadState *tstate); 144 PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate); 145 146 147 /* Other */ 148 149 PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap( 150 _PyRuntimeState *runtime, 151 PyThreadState *newts); 152 153 PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime); 154 155 #ifdef HAVE_FORK 156 extern PyStatus _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime); 157 extern void _PySignal_AfterFork(void); 158 #endif 159 160 PyAPI_FUNC(int) _PyCrossInterpreterData_ReleaseAndRawFree(_PyCrossInterpreterData *); 161 162 163 PyAPI_FUNC(int) _PyState_AddModule( 164 PyThreadState *tstate, 165 PyObject* module, 166 PyModuleDef* def); 167 168 169 PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate); 170 171 #define HEAD_LOCK(runtime) \ 172 PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK) 173 #define HEAD_UNLOCK(runtime) \ 174 PyThread_release_lock((runtime)->interpreters.mutex) 175 176 177 #ifdef __cplusplus 178 } 179 #endif 180 #endif /* !Py_INTERNAL_PYSTATE_H */
Contact us
|
About us
|
Term of use
|
Copyright © 2000-2025 MyWebUniversity.com ™