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 140 PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T); 141 142 PyAPI_FUNC(int) _PyInterpreterState_IDInitref(struct _is *); 143 PyAPI_FUNC(void) _PyInterpreterState_IDIncref(struct _is *); 144 PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *); 145 146 147 /* cross-interpreter data registry */ 148 149 /* For now we use a global registry of shareable classes. An 150 alternative would be to add a tp_* slot for a class's 151 crossinterpdatafunc. It would be simpler and more efficient. */ 152 153 struct _xidregitem; 154 155 struct _xidregitem { 156 PyTypeObject *cls; 157 crossinterpdatafunc getdata; 158 struct _xidregitem *next; 159 }; 160 161 /* runtime audit hook state */ 162 163 typedef struct _Py_AuditHookEntry { 164 struct _Py_AuditHookEntry *next; 165 Py_AuditHookFunction hookCFunction; 166 void *userData; 167 } _Py_AuditHookEntry; 168 169 /* GIL state */ 170 171 struct _gilstate_runtime_state { 172 int check_enabled; 173 /* Assuming the current thread holds the GIL, this is the 174 PyThreadState for the current thread. */ 175 _Py_atomic_address tstate_current; 176 PyThreadFrameGetter getframe; 177 /* The single PyInterpreterState used by this process' 178 GILState implementation 179 */ 180 /* TODO: Given interp_main, it may be possible to kill this ref */ 181 PyInterpreterState *autoInterpreterState; 182 Py_tss_t autoTSSkey; 183 }; 184 185 /* hook for PyEval_GetFrame(), requested for Psyco */ 186 #define _PyThreadState_GetFrame _PyRuntime.gilstate.getframe 187 188 /* Issue #26558: Flag to disable PyGILState_Check(). 189 If set to non-zero, PyGILState_Check() always return 1. */ 190 #define _PyGILState_check_enabled _PyRuntime.gilstate.check_enabled 191 192 193 /* Full Python runtime state */ 194 195 typedef struct pyruntimestate { 196 /* Is running Py_PreInitialize()? */ 197 int preinitializing; 198 199 /* Is Python preinitialized? Set to 1 by Py_PreInitialize() */ 200 int preinitialized; 201 202 /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */ 203 int core_initialized; 204 205 /* Is Python fully initialized? Set to 1 by Py_Initialize() */ 206 int initialized; 207 208 /* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize() 209 is called again. */ 210 PyThreadState *finalizing; 211 212 struct pyinterpreters { 213 PyThread_type_lock mutex; 214 PyInterpreterState *head; 215 PyInterpreterState *main; 216 /* _next_interp_id is an auto-numbered sequence of small 217 integers. It gets initialized in _PyInterpreterState_Init(), 218 which is called in Py_Initialize(), and used in 219 PyInterpreterState_New(). A negative interpreter ID 220 indicates an error occurred. The main interpreter will 221 always have an ID of 0. Overflow results in a RuntimeError. 222 If that becomes a problem later then we can adjust, e.g. by 223 using a Python int. */ 224 int64_t next_id; 225 } interpreters; 226 // XXX Remove this field once we have a tp_* slot. 227 struct _xidregistry { 228 PyThread_type_lock mutex; 229 struct _xidregitem *head; 230 } xidregistry; 231 232 unsigned long main_thread; 233 234 #define NEXITFUNCS 32 235 void (*exitfuncs[NEXITFUNCS])(void); 236 int nexitfuncs; 237 238 struct _gc_runtime_state gc; 239 struct _ceval_runtime_state ceval; 240 struct _gilstate_runtime_state gilstate; 241 242 PyPreConfig preconfig; 243 244 Py_OpenCodeHookFunction open_code_hook; 245 void *open_code_userdata; 246 _Py_AuditHookEntry *audit_hook_head; 247 248 // XXX Consolidate globals found via the check-c-globals script. 249 } _PyRuntimeState; 250 251 #define _PyRuntimeState_INIT \ 252 {.preinitialized = 0, .core_initialized = 0, .initialized = 0} 253 /* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */ 254 255 PyAPI_DATA(_PyRuntimeState) _PyRuntime; 256 PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime); 257 PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime); 258 PyAPI_FUNC(void) _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime); 259 260 /* Initialize _PyRuntimeState. 261 Return NULL on success, or return an error message on failure. */ 262 PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void); 263 264 PyAPI_FUNC(void) _PyRuntime_Finalize(void); 265 266 #define _Py_CURRENTLY_FINALIZING(runtime, tstate) \ 267 (runtime->finalizing == tstate) 268 269 270 /* Variable and macro for in-line access to current thread 271 and interpreter state */ 272 273 #define _PyRuntimeState_GetThreadState(runtime) \ 274 ((PyThreadState*)_Py_atomic_load_relaxed(&(runtime)->gilstate.tstate_current)) 275 276 /* Get the current Python thread state. 277 278 Efficient macro reading directly the 'gilstate.tstate_current' atomic 279 variable. The macro is unsafe: it does not check for error and it can 280 return NULL. 281 282 The caller must hold the GIL. 283 284 See also PyThreadState_Get() and PyThreadState_GET(). */ 285 #define _PyThreadState_GET() _PyRuntimeState_GetThreadState(&_PyRuntime) 286 287 /* Redefine PyThreadState_GET() as an alias to _PyThreadState_GET() */ 288 #undef PyThreadState_GET 289 #define PyThreadState_GET() _PyThreadState_GET() 290 291 /* Get the current interpreter state. 292 293 The macro is unsafe: it does not check for error and it can return NULL. 294 295 The caller must hold the GIL. 296 297 See also _PyInterpreterState_Get() 298 and _PyGILState_GetInterpreterStateUnsafe(). */ 299 #define _PyInterpreterState_GET_UNSAFE() (_PyThreadState_GET()->interp) 300 301 302 /* Other */ 303 304 PyAPI_FUNC(void) _PyThreadState_Init( 305 _PyRuntimeState *runtime, 306 PyThreadState *tstate); 307 PyAPI_FUNC(void) _PyThreadState_DeleteExcept( 308 _PyRuntimeState *runtime, 309 PyThreadState *tstate); 310 311 PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap( 312 struct _gilstate_runtime_state *gilstate, 313 PyThreadState *newts); 314 315 PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime); 316 PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime); 317 318 PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime); 319 320 #ifdef __cplusplus 321 } 322 #endif 323 #endif /* !Py_INTERNAL_PYSTATE_H */ 324