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 "cpython/initconfig.h" 12 #include "fileobject.h" 13 #include "pystate.h" 14 #include "pythread.h" 15 #include "sysmodule.h" 16 17 #include "pycore_gil.h" /* _gil_runtime_state */ 18 #include "pycore_pathconfig.h" 19 #include "pycore_pymem.h" 20 #include "pycore_warnings.h" 21 22 23 /* ceval state */ 24 25 struct _pending_calls { 26 int finishing; 27 PyThread_type_lock lock; 28 /* Request for running pending calls. */ 29 _Py_atomic_int calls_to_do; 30 /* Request for looking at the `async_exc` field of the current 31 thread state. 32 Guarded by the GIL. */ 33 int async_exc; 34 #define NPENDINGCALLS 32 35 struct { 36 int (*func)(void *); 37 void *arg; 38 } calls[NPENDINGCALLS]; 39 int first; 40 int last; 41 }; 42 43 struct _ceval_runtime_state { 44 int recursion_limit; 45 /* Records whether tracing is on for any thread. Counts the number 46 of threads for which tstate->c_tracefunc is non-NULL, so if the 47 value is 0, we know we don't have to check this thread's 48 c_tracefunc. This speeds up the if statement in 49 PyEval_EvalFrameEx() after fast_next_opcode. */ 50 int tracing_possible; 51 /* This single variable consolidates all requests to break out of 52 the fast path in the eval loop. */ 53 _Py_atomic_int eval_breaker; 54 /* Request for dropping the GIL */ 55 _Py_atomic_int gil_drop_request; 56 struct _pending_calls pending; 57 /* Request for checking signals. */ 58 _Py_atomic_int signals_pending; 59 struct _gil_runtime_state gil; 60 }; 61 62 /* interpreter state */ 63 64 typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int); 65 66 // The PyInterpreterState typedef is in Include/pystate.h. 67 struct _is { 68 69 struct _is *next; 70 struct _ts *tstate_head; 71 72 int64_t id; 73 int64_t id_refcount; 74 int requires_idref; 75 PyThread_type_lock id_mutex; 76 77 int finalizing; 78 79 PyObject *modules; 80 PyObject *modules_by_index; 81 PyObject *sysdict; 82 PyObject *builtins; 83 PyObject *importlib; 84 85 /* Used in Python/sysmodule.c. */ 86 int check_interval; 87 88 /* Used in Modules/_threadmodule.c. */ 89 long num_threads; 90 /* Support for runtime thread stack size tuning. 91 A value of 0 means using the platform's default stack size 92 or the size specified by the THREAD_STACK_SIZE macro. */ 93 /* Used in Python/thread.c. */ 94 size_t pythread_stacksize; 95 96 PyObject *codec_search_path; 97 PyObject *codec_search_cache; 98 PyObject *codec_error_registry; 99 int codecs_initialized; 100 101 /* fs_codec.encoding is initialized to NULL. 102 Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */ 103 struct { 104 char *encoding; /* Filesystem encoding (encoded to UTF-8) */ 105 char *errors; /* Filesystem errors (encoded to UTF-8) */ 106 _Py_error_handler error_handler; 107 } fs_codec; 108 109 PyConfig config; 110 #ifdef HAVE_DLOPEN 111 int dlopenflags; 112 #endif 113 114 PyObject *dict; /* Stores per-interpreter state */ 115 116 PyObject *builtins_copy; 117 PyObject *import_func; 118 /* Initialized to PyEval_EvalFrameDefault(). */ 119 _PyFrameEvalFunction eval_frame; 120 121 Py_ssize_t co_extra_user_count; 122 freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS]; 123 124 #ifdef HAVE_FORK 125 PyObject *before_forkers; 126 PyObject *after_forkers_parent; 127 PyObject *after_forkers_child; 128 #endif 129 /* AtExit module */ 130 void (*pyexitfunc)(PyObject *); 131 PyObject *pyexitmodule; 132 133 uint64_t tstate_next_unique_id; 134 135 struct _warnings_runtime_state warnings; 136 137 PyObject *audit_hooks; 138 139 int int_max_str_digits; 140 }; 141 142 PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T); 143 144 PyAPI_FUNC(int) _PyInterpreterState_IDInitref(struct _is *); 145 PyAPI_FUNC(void) _PyInterpreterState_IDIncref(struct _is *); 146 PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *); 147 148 149 /* cross-interpreter data registry */ 150 151 /* For now we use a global registry of shareable classes. An 152 alternative would be to add a tp_* slot for a class's 153 crossinterpdatafunc. It would be simpler and more efficient. */ 154 155 struct _xidregitem; 156 157 struct _xidregitem { 158 PyTypeObject *cls; 159 crossinterpdatafunc getdata; 160 struct _xidregitem *next; 161 }; 162 163 /* runtime audit hook state */ 164 165 typedef struct _Py_AuditHookEntry { 166 struct _Py_AuditHookEntry *next; 167 Py_AuditHookFunction hookCFunction; 168 void *userData; 169 } _Py_AuditHookEntry; 170 171 /* GIL state */ 172 173 struct _gilstate_runtime_state { 174 int check_enabled; 175 /* Assuming the current thread holds the GIL, this is the 176 PyThreadState for the current thread. */ 177 _Py_atomic_address tstate_current; 178 PyThreadFrameGetter getframe; 179 /* The single PyInterpreterState used by this process' 180 GILState implementation 181 */ 182 /* TODO: Given interp_main, it may be possible to kill this ref */ 183 PyInterpreterState *autoInterpreterState; 184 Py_tss_t autoTSSkey; 185 }; 186 187 /* hook for PyEval_GetFrame(), requested for Psyco */ 188 #define _PyThreadState_GetFrame _PyRuntime.gilstate.getframe 189 190 /* Issue #26558: Flag to disable PyGILState_Check(). 191 If set to non-zero, PyGILState_Check() always return 1. */ 192 #define _PyGILState_check_enabled _PyRuntime.gilstate.check_enabled 193 194 195 /* Full Python runtime state */ 196 197 typedef struct pyruntimestate { 198 /* Is running Py_PreInitialize()? */ 199 int preinitializing; 200 201 /* Is Python preinitialized? Set to 1 by Py_PreInitialize() */ 202 int preinitialized; 203 204 /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */ 205 int core_initialized; 206 207 /* Is Python fully initialized? Set to 1 by Py_Initialize() */ 208 int initialized; 209 210 /* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize() 211 is called again. */ 212 PyThreadState *finalizing; 213 214 struct pyinterpreters { 215 PyThread_type_lock mutex; 216 PyInterpreterState *head; 217 PyInterpreterState *main; 218 /* _next_interp_id is an auto-numbered sequence of small 219 integers. It gets initialized in _PyInterpreterState_Init(), 220 which is called in Py_Initialize(), and used in 221 PyInterpreterState_New(). A negative interpreter ID 222 indicates an error occurred. The main interpreter will 223 always have an ID of 0. Overflow results in a RuntimeError. 224 If that becomes a problem later then we can adjust, e.g. by 225 using a Python int. */ 226 int64_t next_id; 227 } interpreters; 228 // XXX Remove this field once we have a tp_* slot. 229 struct _xidregistry { 230 PyThread_type_lock mutex; 231 struct _xidregitem *head; 232 } xidregistry; 233 234 unsigned long main_thread; 235 236 #define NEXITFUNCS 32 237 void (*exitfuncs[NEXITFUNCS])(void); 238 int nexitfuncs; 239 240 struct _gc_runtime_state gc; 241 struct _ceval_runtime_state ceval; 242 struct _gilstate_runtime_state gilstate; 243 244 PyPreConfig preconfig; 245 246 Py_OpenCodeHookFunction open_code_hook; 247 void *open_code_userdata; 248 _Py_AuditHookEntry *audit_hook_head; 249 250 // XXX Consolidate globals found via the check-c-globals script. 251 } _PyRuntimeState; 252 253 #define _PyRuntimeState_INIT \ 254 {.preinitialized = 0, .core_initialized = 0, .initialized = 0} 255 /* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */ 256 257 PyAPI_DATA(_PyRuntimeState) _PyRuntime; 258 PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime); 259 PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime); 260 PyAPI_FUNC(void) _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime); 261 262 /* Initialize _PyRuntimeState. 263 Return NULL on success, or return an error message on failure. */ 264 PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void); 265 266 PyAPI_FUNC(void) _PyRuntime_Finalize(void); 267 268 #define _Py_CURRENTLY_FINALIZING(runtime, tstate) \ 269 (runtime->finalizing == tstate) 270 271 272 /* Variable and macro for in-line access to current thread 273 and interpreter state */ 274 275 #define _PyRuntimeState_GetThreadState(runtime) \ 276 ((PyThreadState*)_Py_atomic_load_relaxed(&(runtime)->gilstate.tstate_current)) 277 278 /* Get the current Python thread state. 279 280 Efficient macro reading directly the 'gilstate.tstate_current' atomic 281 variable. The macro is unsafe: it does not check for error and it can 282 return NULL. 283 284 The caller must hold the GIL. 285 286 See also PyThreadState_Get() and PyThreadState_GET(). */ 287 #define _PyThreadState_GET() _PyRuntimeState_GetThreadState(&_PyRuntime) 288 289 /* Redefine PyThreadState_GET() as an alias to _PyThreadState_GET() */ 290 #undef PyThreadState_GET 291 #define PyThreadState_GET() _PyThreadState_GET() 292 293 /* Get the current interpreter state. 294 295 The macro is unsafe: it does not check for error and it can return NULL. 296 297 The caller must hold the GIL. 298 299 See also _PyInterpreterState_Get() 300 and _PyGILState_GetInterpreterStateUnsafe(). */ 301 #define _PyInterpreterState_GET_UNSAFE() (_PyThreadState_GET()->interp) 302 303 304 /* Other */ 305 306 PyAPI_FUNC(void) _PyThreadState_Init( 307 _PyRuntimeState *runtime, 308 PyThreadState *tstate); 309 PyAPI_FUNC(void) _PyThreadState_DeleteExcept( 310 _PyRuntimeState *runtime, 311 PyThreadState *tstate); 312 313 PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap( 314 struct _gilstate_runtime_state *gilstate, 315 PyThreadState *newts); 316 317 PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime); 318 PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime); 319 320 PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime); 321 322 323 PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate); 324 325 #ifdef __cplusplus 326 } 327 #endif 328 #endif /* !Py_INTERNAL_PYSTATE_H */ 329