File indexing completed on 2025-11-19 09:50:50
0001 #ifndef Py_INTERNAL_RUNTIME_H
0002 #define Py_INTERNAL_RUNTIME_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_atexit.h" // struct _atexit_runtime_state
0012 #include "pycore_ceval_state.h" // struct _ceval_runtime_state
0013 #include "pycore_crossinterp.h" // struct _xidregistry
0014 #include "pycore_faulthandler.h" // struct _faulthandler_runtime_state
0015 #include "pycore_floatobject.h" // struct _Py_float_runtime_state
0016 #include "pycore_import.h" // struct _import_runtime_state
0017 #include "pycore_interp.h" // PyInterpreterState
0018 #include "pycore_object_state.h" // struct _py_object_runtime_state
0019 #include "pycore_parser.h" // struct _parser_runtime_state
0020 #include "pycore_pyhash.h" // struct pyhash_runtime_state
0021 #include "pycore_pymem.h" // struct _pymem_allocators
0022 #include "pycore_pythread.h" // struct _pythread_runtime_state
0023 #include "pycore_signal.h" // struct _signals_runtime_state
0024 #include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state
0025 #include "pycore_typeobject.h" // struct _types_runtime_state
0026 #include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_state
0027
0028 struct _getargs_runtime_state {
0029 struct _PyArg_Parser *static_parsers;
0030 };
0031
0032
0033
0034 struct _gilstate_runtime_state {
0035
0036
0037 int check_enabled;
0038
0039
0040
0041
0042 PyInterpreterState *autoInterpreterState;
0043 };
0044
0045
0046
0047 #define _Py_Debug_Cookie "xdebugpy"
0048
0049 #ifdef Py_GIL_DISABLED
0050 # define _Py_Debug_gilruntimestate_enabled offsetof(struct _gil_runtime_state, enabled)
0051 # define _Py_Debug_Free_Threaded 1
0052 #else
0053 # define _Py_Debug_gilruntimestate_enabled 0
0054 # define _Py_Debug_Free_Threaded 0
0055 #endif
0056 typedef struct _Py_AuditHookEntry {
0057 struct _Py_AuditHookEntry *next;
0058 Py_AuditHookFunction hookCFunction;
0059 void *userData;
0060 } _Py_AuditHookEntry;
0061
0062 typedef struct _Py_DebugOffsets {
0063 char cookie[8] _Py_NONSTRING;
0064 uint64_t version;
0065 uint64_t free_threaded;
0066
0067 struct _runtime_state {
0068 uint64_t size;
0069 uint64_t finalizing;
0070 uint64_t interpreters_head;
0071 } runtime_state;
0072
0073
0074 struct _interpreter_state {
0075 uint64_t size;
0076 uint64_t id;
0077 uint64_t next;
0078 uint64_t threads_head;
0079 uint64_t gc;
0080 uint64_t imports_modules;
0081 uint64_t sysdict;
0082 uint64_t builtins;
0083 uint64_t ceval_gil;
0084 uint64_t gil_runtime_state;
0085 uint64_t gil_runtime_state_enabled;
0086 uint64_t gil_runtime_state_locked;
0087 uint64_t gil_runtime_state_holder;
0088 } interpreter_state;
0089
0090
0091 struct _thread_state{
0092 uint64_t size;
0093 uint64_t prev;
0094 uint64_t next;
0095 uint64_t interp;
0096 uint64_t current_frame;
0097 uint64_t thread_id;
0098 uint64_t native_thread_id;
0099 uint64_t datastack_chunk;
0100 uint64_t status;
0101 } thread_state;
0102
0103
0104 struct _interpreter_frame {
0105 uint64_t size;
0106 uint64_t previous;
0107 uint64_t executable;
0108 uint64_t instr_ptr;
0109 uint64_t localsplus;
0110 uint64_t owner;
0111 } interpreter_frame;
0112
0113
0114 struct _code_object {
0115 uint64_t size;
0116 uint64_t filename;
0117 uint64_t name;
0118 uint64_t qualname;
0119 uint64_t linetable;
0120 uint64_t firstlineno;
0121 uint64_t argcount;
0122 uint64_t localsplusnames;
0123 uint64_t localspluskinds;
0124 uint64_t co_code_adaptive;
0125 } code_object;
0126
0127
0128 struct _pyobject {
0129 uint64_t size;
0130 uint64_t ob_type;
0131 } pyobject;
0132
0133
0134 struct _type_object {
0135 uint64_t size;
0136 uint64_t tp_name;
0137 uint64_t tp_repr;
0138 uint64_t tp_flags;
0139 } type_object;
0140
0141
0142 struct _tuple_object {
0143 uint64_t size;
0144 uint64_t ob_item;
0145 uint64_t ob_size;
0146 } tuple_object;
0147
0148
0149 struct _list_object {
0150 uint64_t size;
0151 uint64_t ob_item;
0152 uint64_t ob_size;
0153 } list_object;
0154
0155
0156 struct _dict_object {
0157 uint64_t size;
0158 uint64_t ma_keys;
0159 uint64_t ma_values;
0160 } dict_object;
0161
0162
0163 struct _float_object {
0164 uint64_t size;
0165 uint64_t ob_fval;
0166 } float_object;
0167
0168
0169 struct _long_object {
0170 uint64_t size;
0171 uint64_t lv_tag;
0172 uint64_t ob_digit;
0173 } long_object;
0174
0175
0176 struct _bytes_object {
0177 uint64_t size;
0178 uint64_t ob_size;
0179 uint64_t ob_sval;
0180 } bytes_object;
0181
0182
0183 struct _unicode_object {
0184 uint64_t size;
0185 uint64_t state;
0186 uint64_t length;
0187 uint64_t asciiobject_size;
0188 } unicode_object;
0189
0190
0191 struct _gc {
0192 uint64_t size;
0193 uint64_t collecting;
0194 } gc;
0195 } _Py_DebugOffsets;
0196
0197
0198 struct _reftracer_runtime_state {
0199 PyRefTracer tracer_func;
0200 void* tracer_data;
0201 };
0202
0203
0204
0205
0206
0207
0208 typedef struct pyruntimestate {
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221 _Py_DebugOffsets debug_offsets;
0222
0223
0224
0225
0226
0227 int _initialized;
0228
0229
0230 int preinitializing;
0231
0232
0233 int preinitialized;
0234
0235
0236 int core_initialized;
0237
0238
0239 int initialized;
0240
0241
0242
0243
0244
0245
0246 PyThreadState *_finalizing;
0247
0248 unsigned long _finalizing_id;
0249
0250 struct pyinterpreters {
0251 PyMutex mutex;
0252
0253 PyInterpreterState *head;
0254
0255
0256
0257 PyInterpreterState *main;
0258
0259
0260
0261
0262
0263
0264
0265
0266 int64_t next_id;
0267 } interpreters;
0268
0269
0270
0271 unsigned long main_thread;
0272 PyThreadState *main_tstate;
0273
0274
0275
0276
0277
0278
0279
0280 struct _xi_runtime_state xi;
0281
0282 struct _pymem_allocators allocators;
0283 struct _obmalloc_global_state obmalloc;
0284 struct pyhash_runtime_state pyhash_state;
0285 struct _pythread_runtime_state threads;
0286 struct _signals_runtime_state signals;
0287
0288
0289 Py_tss_t autoTSSkey;
0290
0291
0292 Py_tss_t trashTSSkey;
0293
0294 PyWideStringList orig_argv;
0295
0296 struct _parser_runtime_state parser;
0297
0298 struct _atexit_runtime_state atexit;
0299
0300 struct _import_runtime_state imports;
0301 struct _ceval_runtime_state ceval;
0302 struct _gilstate_runtime_state gilstate;
0303 struct _getargs_runtime_state getargs;
0304 struct _fileutils_state fileutils;
0305 struct _faulthandler_runtime_state faulthandler;
0306 struct _tracemalloc_runtime_state tracemalloc;
0307 struct _reftracer_runtime_state ref_tracer;
0308
0309
0310
0311
0312
0313 _PyRWMutex stoptheworld_mutex;
0314 struct _stoptheworld_state stoptheworld;
0315
0316 PyPreConfig preconfig;
0317
0318
0319
0320 Py_OpenCodeHookFunction open_code_hook;
0321 void *open_code_userdata;
0322 struct {
0323 PyMutex mutex;
0324 _Py_AuditHookEntry *head;
0325 } audit_hooks;
0326
0327 struct _py_object_runtime_state object_state;
0328 struct _Py_float_runtime_state float_state;
0329 struct _Py_unicode_runtime_state unicode_state;
0330 struct _types_runtime_state types;
0331
0332
0333 struct _Py_cached_objects cached_objects;
0334 struct _Py_static_objects static_objects;
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351 PyInterpreterState _main_interpreter;
0352
0353 #if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
0354
0355
0356 bool wasm_type_reflection_available;
0357 #endif
0358
0359 } _PyRuntimeState;
0360
0361
0362
0363
0364
0365
0366
0367 PyAPI_DATA(_PyRuntimeState) _PyRuntime;
0368
0369 extern PyStatus _PyRuntimeState_Init(_PyRuntimeState *runtime);
0370 extern void _PyRuntimeState_Fini(_PyRuntimeState *runtime);
0371
0372 #ifdef HAVE_FORK
0373 extern PyStatus _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime);
0374 #endif
0375
0376
0377
0378 extern PyStatus _PyRuntime_Initialize(void);
0379
0380 extern void _PyRuntime_Finalize(void);
0381
0382
0383 static inline PyThreadState*
0384 _PyRuntimeState_GetFinalizing(_PyRuntimeState *runtime) {
0385 return (PyThreadState*)_Py_atomic_load_ptr_relaxed(&runtime->_finalizing);
0386 }
0387
0388 static inline unsigned long
0389 _PyRuntimeState_GetFinalizingID(_PyRuntimeState *runtime) {
0390 return _Py_atomic_load_ulong_relaxed(&runtime->_finalizing_id);
0391 }
0392
0393 static inline void
0394 _PyRuntimeState_SetFinalizing(_PyRuntimeState *runtime, PyThreadState *tstate) {
0395 _Py_atomic_store_ptr_relaxed(&runtime->_finalizing, tstate);
0396 if (tstate == NULL) {
0397 _Py_atomic_store_ulong_relaxed(&runtime->_finalizing_id, 0);
0398 }
0399 else {
0400
0401
0402 _Py_atomic_store_ulong_relaxed(&runtime->_finalizing_id,
0403 tstate->thread_id);
0404 }
0405 }
0406
0407 #ifdef __cplusplus
0408 }
0409 #endif
0410 #endif