| 1 |
#ifndef Py_INTERNAL_RUNTIME_H |
| 2 |
#define Py_INTERNAL_RUNTIME_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_atexit.h" // struct _atexit_runtime_state |
| 12 |
#include "pycore_ceval_state.h" // struct _ceval_runtime_state |
| 13 |
#include "pycore_crossinterp.h" // struct _xidregistry |
| 14 |
#include "pycore_faulthandler.h" // struct _faulthandler_runtime_state |
| 15 |
#include "pycore_floatobject.h" // struct _Py_float_runtime_state |
| 16 |
#include "pycore_import.h" // struct _import_runtime_state |
| 17 |
#include "pycore_interp.h" // PyInterpreterState |
| 18 |
#include "pycore_object_state.h" // struct _py_object_runtime_state |
| 19 |
#include "pycore_parser.h" // struct _parser_runtime_state |
| 20 |
#include "pycore_pyhash.h" // struct pyhash_runtime_state |
| 21 |
#include "pycore_pymem.h" // struct _pymem_allocators |
| 22 |
#include "pycore_pythread.h" // struct _pythread_runtime_state |
| 23 |
#include "pycore_signal.h" // struct _signals_runtime_state |
| 24 |
#include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state |
| 25 |
#include "pycore_typeobject.h" // struct _types_runtime_state |
| 26 |
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_state |
| 27 |
|
| 28 |
struct _getargs_runtime_state { |
| 29 |
struct _PyArg_Parser *static_parsers; |
| 30 |
}; |
| 31 |
|
| 32 |
/* GIL state */ |
| 33 |
|
| 34 |
struct _gilstate_runtime_state { |
| 35 |
/* bpo-26558: Flag to disable PyGILState_Check(). |
| 36 |
If set to non-zero, PyGILState_Check() always return 1. */ |
| 37 |
int check_enabled; |
| 38 |
/* The single PyInterpreterState used by this process' |
| 39 |
GILState implementation |
| 40 |
*/ |
| 41 |
/* TODO: Given interp_main, it may be possible to kill this ref */ |
| 42 |
PyInterpreterState *autoInterpreterState; |
| 43 |
}; |
| 44 |
|
| 45 |
/* Runtime audit hook state */ |
| 46 |
|
| 47 |
#define _Py_Debug_Cookie "xdebugpy" |
| 48 |
|
| 49 |
#ifdef Py_GIL_DISABLED |
| 50 |
# define _Py_Debug_gilruntimestate_enabled offsetof(struct _gil_runtime_state, enabled) |
| 51 |
# define _Py_Debug_Free_Threaded 1 |
| 52 |
#else |
| 53 |
# define _Py_Debug_gilruntimestate_enabled 0 |
| 54 |
# define _Py_Debug_Free_Threaded 0 |
| 55 |
#endif |
| 56 |
typedef struct _Py_AuditHookEntry { |
| 57 |
struct _Py_AuditHookEntry *next; |
| 58 |
Py_AuditHookFunction hookCFunction; |
| 59 |
void *userData; |
| 60 |
} _Py_AuditHookEntry; |
| 61 |
|
| 62 |
typedef struct _Py_DebugOffsets { |
| 63 |
char cookie[8] _Py_NONSTRING; |
| 64 |
uint64_t version; |
| 65 |
uint64_t free_threaded; |
| 66 |
// Runtime state offset; |
| 67 |
struct _runtime_state { |
| 68 |
uint64_t size; |
| 69 |
uint64_t finalizing; |
| 70 |
uint64_t interpreters_head; |
| 71 |
} runtime_state; |
| 72 |
|
| 73 |
// Interpreter state offset; |
| 74 |
struct _interpreter_state { |
| 75 |
uint64_t size; |
| 76 |
uint64_t id; |
| 77 |
uint64_t next; |
| 78 |
uint64_t threads_head; |
| 79 |
uint64_t gc; |
| 80 |
uint64_t imports_modules; |
| 81 |
uint64_t sysdict; |
| 82 |
uint64_t builtins; |
| 83 |
uint64_t ceval_gil; |
| 84 |
uint64_t gil_runtime_state; |
| 85 |
uint64_t gil_runtime_state_enabled; |
| 86 |
uint64_t gil_runtime_state_locked; |
| 87 |
uint64_t gil_runtime_state_holder; |
| 88 |
} interpreter_state; |
| 89 |
|
| 90 |
// Thread state offset; |
| 91 |
struct _thread_state{ |
| 92 |
uint64_t size; |
| 93 |
uint64_t prev; |
| 94 |
uint64_t next; |
| 95 |
uint64_t interp; |
| 96 |
uint64_t current_frame; |
| 97 |
uint64_t thread_id; |
| 98 |
uint64_t native_thread_id; |
| 99 |
uint64_t datastack_chunk; |
| 100 |
uint64_t status; |
| 101 |
} thread_state; |
| 102 |
|
| 103 |
// InterpreterFrame offset; |
| 104 |
struct _interpreter_frame { |
| 105 |
uint64_t size; |
| 106 |
uint64_t previous; |
| 107 |
uint64_t executable; |
| 108 |
uint64_t instr_ptr; |
| 109 |
uint64_t localsplus; |
| 110 |
uint64_t owner; |
| 111 |
} interpreter_frame; |
| 112 |
|
| 113 |
// Code object offset; |
| 114 |
struct _code_object { |
| 115 |
uint64_t size; |
| 116 |
uint64_t filename; |
| 117 |
uint64_t name; |
| 118 |
uint64_t qualname; |
| 119 |
uint64_t linetable; |
| 120 |
uint64_t firstlineno; |
| 121 |
uint64_t argcount; |
| 122 |
uint64_t localsplusnames; |
| 123 |
uint64_t localspluskinds; |
| 124 |
uint64_t co_code_adaptive; |
| 125 |
} code_object; |
| 126 |
|
| 127 |
// PyObject offset; |
| 128 |
struct _pyobject { |
| 129 |
uint64_t size; |
| 130 |
uint64_t ob_type; |
| 131 |
} pyobject; |
| 132 |
|
| 133 |
// PyTypeObject object offset; |
| 134 |
struct _type_object { |
| 135 |
uint64_t size; |
| 136 |
uint64_t tp_name; |
| 137 |
uint64_t tp_repr; |
| 138 |
uint64_t tp_flags; |
| 139 |
} type_object; |
| 140 |
|
| 141 |
// PyTuple object offset; |
| 142 |
struct _tuple_object { |
| 143 |
uint64_t size; |
| 144 |
uint64_t ob_item; |
| 145 |
uint64_t ob_size; |
| 146 |
} tuple_object; |
| 147 |
|
| 148 |
// PyList object offset; |
| 149 |
struct _list_object { |
| 150 |
uint64_t size; |
| 151 |
uint64_t ob_item; |
| 152 |
uint64_t ob_size; |
| 153 |
} list_object; |
| 154 |
|
| 155 |
// PyDict object offset; |
| 156 |
struct _dict_object { |
| 157 |
uint64_t size; |
| 158 |
uint64_t ma_keys; |
| 159 |
uint64_t ma_values; |
| 160 |
} dict_object; |
| 161 |
|
| 162 |
// PyFloat object offset; |
| 163 |
struct _float_object { |
| 164 |
uint64_t size; |
| 165 |
uint64_t ob_fval; |
| 166 |
} float_object; |
| 167 |
|
| 168 |
// PyLong object offset; |
| 169 |
struct _long_object { |
| 170 |
uint64_t size; |
| 171 |
uint64_t lv_tag; |
| 172 |
uint64_t ob_digit; |
| 173 |
} long_object; |
| 174 |
|
| 175 |
// PyBytes object offset; |
| 176 |
struct _bytes_object { |
| 177 |
uint64_t size; |
| 178 |
uint64_t ob_size; |
| 179 |
uint64_t ob_sval; |
| 180 |
} bytes_object; |
| 181 |
|
| 182 |
// Unicode object offset; |
| 183 |
struct _unicode_object { |
| 184 |
uint64_t size; |
| 185 |
uint64_t state; |
| 186 |
uint64_t length; |
| 187 |
uint64_t asciiobject_size; |
| 188 |
} unicode_object; |
| 189 |
|
| 190 |
// GC runtime state offset; |
| 191 |
struct _gc { |
| 192 |
uint64_t size; |
| 193 |
uint64_t collecting; |
| 194 |
} gc; |
| 195 |
} _Py_DebugOffsets; |
| 196 |
|
| 197 |
/* Reference tracer state */ |
| 198 |
struct _reftracer_runtime_state { |
| 199 |
PyRefTracer tracer_func; |
| 200 |
void* tracer_data; |
| 201 |
}; |
| 202 |
|
| 203 |
/* Full Python runtime state */ |
| 204 |
|
| 205 |
/* _PyRuntimeState holds the global state for the CPython runtime. |
| 206 |
That data is exposed in the internal API as a static variable (_PyRuntime). |
| 207 |
*/ |
| 208 |
typedef struct pyruntimestate { |
| 209 |
/* This field must be first to facilitate locating it by out of process |
| 210 |
* debuggers. Out of process debuggers will use the offsets contained in this |
| 211 |
* field to be able to locate other fields in several interpreter structures |
| 212 |
* in a way that doesn't require them to know the exact layout of those |
| 213 |
* structures. |
| 214 |
* |
| 215 |
* IMPORTANT: |
| 216 |
* This struct is **NOT** backwards compatible between minor version of the |
| 217 |
* interpreter and the members, order of members and size can change between |
| 218 |
* minor versions. This struct is only guaranteed to be stable between patch |
| 219 |
* versions for a given minor version of the interpreter. |
| 220 |
*/ |
| 221 |
_Py_DebugOffsets debug_offsets; |
| 222 |
|
| 223 |
/* Has been initialized to a safe state. |
| 224 |
|
| 225 |
In order to be effective, this must be set to 0 during or right |
| 226 |
after allocation. */ |
| 227 |
int _initialized; |
| 228 |
|
| 229 |
/* Is running Py_PreInitialize()? */ |
| 230 |
int preinitializing; |
| 231 |
|
| 232 |
/* Is Python preinitialized? Set to 1 by Py_PreInitialize() */ |
| 233 |
int preinitialized; |
| 234 |
|
| 235 |
/* Is Python core initialized? Set to 1 by _Py_InitializeCore() */ |
| 236 |
int core_initialized; |
| 237 |
|
| 238 |
/* Is Python fully initialized? Set to 1 by Py_Initialize() */ |
| 239 |
int initialized; |
| 240 |
|
| 241 |
/* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize() |
| 242 |
is called again. |
| 243 |
|
| 244 |
Use _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing() |
| 245 |
to access it, don't access it directly. */ |
| 246 |
PyThreadState *_finalizing; |
| 247 |
/* The ID of the OS thread in which we are finalizing. */ |
| 248 |
unsigned long _finalizing_id; |
| 249 |
|
| 250 |
struct pyinterpreters { |
| 251 |
PyMutex mutex; |
| 252 |
/* The linked list of interpreters, newest first. */ |
| 253 |
PyInterpreterState *head; |
| 254 |
/* The runtime's initial interpreter, which has a special role |
| 255 |
in the operation of the runtime. It is also often the only |
| 256 |
interpreter. */ |
| 257 |
PyInterpreterState *main; |
| 258 |
/* next_id is an auto-numbered sequence of small |
| 259 |
integers. It gets initialized in _PyInterpreterState_Enable(), |
| 260 |
which is called in Py_Initialize(), and used in |
| 261 |
PyInterpreterState_New(). A negative interpreter ID |
| 262 |
indicates an error occurred. The main interpreter will |
| 263 |
always have an ID of 0. Overflow results in a RuntimeError. |
| 264 |
If that becomes a problem later then we can adjust, e.g. by |
| 265 |
using a Python int. */ |
| 266 |
int64_t next_id; |
| 267 |
} interpreters; |
| 268 |
|
| 269 |
/* Platform-specific identifier and PyThreadState, respectively, for the |
| 270 |
main thread in the main interpreter. */ |
| 271 |
unsigned long main_thread; |
| 272 |
PyThreadState *main_tstate; |
| 273 |
|
| 274 |
/* ---------- IMPORTANT --------------------------- |
| 275 |
The fields above this line are declared as early as |
| 276 |
possible to facilitate out-of-process observability |
| 277 |
tools. */ |
| 278 |
|
| 279 |
/* cross-interpreter data and utils */ |
| 280 |
struct _xi_runtime_state xi; |
| 281 |
|
| 282 |
struct _pymem_allocators allocators; |
| 283 |
struct _obmalloc_global_state obmalloc; |
| 284 |
struct pyhash_runtime_state pyhash_state; |
| 285 |
struct _pythread_runtime_state threads; |
| 286 |
struct _signals_runtime_state signals; |
| 287 |
|
| 288 |
/* Used for the thread state bound to the current thread. */ |
| 289 |
Py_tss_t autoTSSkey; |
| 290 |
|
| 291 |
/* Used instead of PyThreadState.trash when there is not current tstate. */ |
| 292 |
Py_tss_t trashTSSkey; |
| 293 |
|
| 294 |
PyWideStringList orig_argv; |
| 295 |
|
| 296 |
struct _parser_runtime_state parser; |
| 297 |
|
| 298 |
struct _atexit_runtime_state atexit; |
| 299 |
|
| 300 |
struct _import_runtime_state imports; |
| 301 |
struct _ceval_runtime_state ceval; |
| 302 |
struct _gilstate_runtime_state gilstate; |
| 303 |
struct _getargs_runtime_state getargs; |
| 304 |
struct _fileutils_state fileutils; |
| 305 |
struct _faulthandler_runtime_state faulthandler; |
| 306 |
struct _tracemalloc_runtime_state tracemalloc; |
| 307 |
struct _reftracer_runtime_state ref_tracer; |
| 308 |
|
| 309 |
// The rwmutex is used to prevent overlapping global and per-interpreter |
| 310 |
// stop-the-world events. Global stop-the-world events lock the mutex |
| 311 |
// exclusively (as a "writer"), while per-interpreter stop-the-world events |
| 312 |
// lock it non-exclusively (as "readers"). |
| 313 |
_PyRWMutex stoptheworld_mutex; |
| 314 |
struct _stoptheworld_state stoptheworld; |
| 315 |
|
| 316 |
PyPreConfig preconfig; |
| 317 |
|
| 318 |
// Audit values must be preserved when Py_Initialize()/Py_Finalize() |
| 319 |
// is called multiple times. |
| 320 |
Py_OpenCodeHookFunction open_code_hook; |
| 321 |
void *open_code_userdata; |
| 322 |
struct { |
| 323 |
PyMutex mutex; |
| 324 |
_Py_AuditHookEntry *head; |
| 325 |
} audit_hooks; |
| 326 |
|
| 327 |
struct _py_object_runtime_state object_state; |
| 328 |
struct _Py_float_runtime_state float_state; |
| 329 |
struct _Py_unicode_runtime_state unicode_state; |
| 330 |
struct _types_runtime_state types; |
| 331 |
|
| 332 |
/* All the objects that are shared by the runtime's interpreters. */ |
| 333 |
struct _Py_cached_objects cached_objects; |
| 334 |
struct _Py_static_objects static_objects; |
| 335 |
|
| 336 |
/* The following fields are here to avoid allocation during init. |
| 337 |
The data is exposed through _PyRuntimeState pointer fields. |
| 338 |
These fields should not be accessed directly outside of init. |
| 339 |
|
| 340 |
All other _PyRuntimeState pointer fields are populated when |
| 341 |
needed and default to NULL. |
| 342 |
|
| 343 |
For now there are some exceptions to that rule, which require |
| 344 |
allocation during init. These will be addressed on a case-by-case |
| 345 |
basis. Most notably, we don't pre-allocated the several mutex |
| 346 |
(PyThread_type_lock) fields, because on Windows we only ever get |
| 347 |
a pointer type. |
| 348 |
*/ |
| 349 |
|
| 350 |
/* _PyRuntimeState.interpreters.main */ |
| 351 |
PyInterpreterState _main_interpreter; |
| 352 |
|
| 353 |
#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) |
| 354 |
// Used in "Python/emscripten_trampoline.c" to choose between type |
| 355 |
// reflection trampoline and EM_JS trampoline. |
| 356 |
bool wasm_type_reflection_available; |
| 357 |
#endif |
| 358 |
|
| 359 |
} _PyRuntimeState; |
| 360 |
|
| 361 |
|
| 362 |
/* other API */ |
| 363 |
|
| 364 |
// Export _PyRuntime for shared extensions which use it in static inline |
| 365 |
// functions for best performance, like _Py_IsMainThread() or _Py_ID(). |
| 366 |
// It's also made accessible for debuggers and profilers. |
| 367 |
PyAPI_DATA(_PyRuntimeState) _PyRuntime; |
| 368 |
|
| 369 |
extern PyStatus _PyRuntimeState_Init(_PyRuntimeState *runtime); |
| 370 |
extern void _PyRuntimeState_Fini(_PyRuntimeState *runtime); |
| 371 |
|
| 372 |
#ifdef HAVE_FORK |
| 373 |
extern PyStatus _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime); |
| 374 |
#endif |
| 375 |
|
| 376 |
/* Initialize _PyRuntimeState. |
| 377 |
Return NULL on success, or return an error message on failure. */ |
| 378 |
extern PyStatus _PyRuntime_Initialize(void); |
| 379 |
|
| 380 |
extern void _PyRuntime_Finalize(void); |
| 381 |
|
| 382 |
|
| 383 |
static inline PyThreadState* |
| 384 |
_PyRuntimeState_GetFinalizing(_PyRuntimeState *runtime) { |
| 385 |
return (PyThreadState*)_Py_atomic_load_ptr_relaxed(&runtime->_finalizing); |
| 386 |
} |
| 387 |
|
| 388 |
static inline unsigned long |
| 389 |
_PyRuntimeState_GetFinalizingID(_PyRuntimeState *runtime) { |
| 390 |
return _Py_atomic_load_ulong_relaxed(&runtime->_finalizing_id); |
| 391 |
} |
| 392 |
|
| 393 |
static inline void |
| 394 |
_PyRuntimeState_SetFinalizing(_PyRuntimeState *runtime, PyThreadState *tstate) { |
| 395 |
_Py_atomic_store_ptr_relaxed(&runtime->_finalizing, tstate); |
| 396 |
if (tstate == NULL) { |
| 397 |
_Py_atomic_store_ulong_relaxed(&runtime->_finalizing_id, 0); |
| 398 |
} |
| 399 |
else { |
| 400 |
// XXX Re-enable this assert once gh-109860 is fixed. |
| 401 |
//assert(tstate->thread_id == PyThread_get_thread_ident()); |
| 402 |
_Py_atomic_store_ulong_relaxed(&runtime->_finalizing_id, |
| 403 |
tstate->thread_id); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
#ifdef __cplusplus |
| 408 |
} |
| 409 |
#endif |
| 410 |
#endif /* !Py_INTERNAL_RUNTIME_H */ |