Back to home page

EIC code displayed by LXR

 
 

    


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 /* GIL state */
0033 
0034 struct _gilstate_runtime_state {
0035     /* bpo-26558: Flag to disable PyGILState_Check().
0036        If set to non-zero, PyGILState_Check() always return 1. */
0037     int check_enabled;
0038     /* The single PyInterpreterState used by this process'
0039        GILState implementation
0040     */
0041     /* TODO: Given interp_main, it may be possible to kill this ref */
0042     PyInterpreterState *autoInterpreterState;
0043 };
0044 
0045 /* Runtime audit hook state */
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     // Runtime state offset;
0067     struct _runtime_state {
0068         uint64_t size;
0069         uint64_t finalizing;
0070         uint64_t interpreters_head;
0071     } runtime_state;
0072 
0073     // Interpreter state offset;
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     // Thread state offset;
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     // InterpreterFrame offset;
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     // Code object offset;
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     // PyObject offset;
0128     struct _pyobject {
0129         uint64_t size;
0130         uint64_t ob_type;
0131     } pyobject;
0132 
0133     // PyTypeObject object offset;
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     // PyTuple object offset;
0142     struct _tuple_object {
0143         uint64_t size;
0144         uint64_t ob_item;
0145         uint64_t ob_size;
0146     } tuple_object;
0147 
0148     // PyList object offset;
0149     struct _list_object {
0150         uint64_t size;
0151         uint64_t ob_item;
0152         uint64_t ob_size;
0153     } list_object;
0154 
0155     // PyDict object offset;
0156     struct _dict_object {
0157         uint64_t size;
0158         uint64_t ma_keys;
0159         uint64_t ma_values;
0160     } dict_object;
0161 
0162     // PyFloat object offset;
0163     struct _float_object {
0164         uint64_t size;
0165         uint64_t ob_fval;
0166     } float_object;
0167 
0168     // PyLong object offset;
0169     struct _long_object {
0170         uint64_t size;
0171         uint64_t lv_tag;
0172         uint64_t ob_digit;
0173     } long_object;
0174 
0175     // PyBytes object offset;
0176     struct _bytes_object {
0177         uint64_t size;
0178         uint64_t ob_size;
0179         uint64_t ob_sval;
0180     } bytes_object;
0181 
0182     // Unicode object offset;
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     // GC runtime state offset;
0191     struct _gc {
0192         uint64_t size;
0193         uint64_t collecting;
0194     } gc;
0195 } _Py_DebugOffsets;
0196 
0197 /* Reference tracer state */
0198 struct _reftracer_runtime_state {
0199     PyRefTracer tracer_func;
0200     void* tracer_data;
0201 };
0202 
0203 /* Full Python runtime state */
0204 
0205 /* _PyRuntimeState holds the global state for the CPython runtime.
0206    That data is exposed in the internal API as a static variable (_PyRuntime).
0207    */
0208 typedef struct pyruntimestate {
0209     /* This field must be first to facilitate locating it by out of process
0210      * debuggers. Out of process debuggers will use the offsets contained in this
0211      * field to be able to locate other fields in several interpreter structures
0212      * in a way that doesn't require them to know the exact layout of those
0213      * structures.
0214      *
0215      * IMPORTANT:
0216      * This struct is **NOT** backwards compatible between minor version of the
0217      * interpreter and the members, order of members and size can change between
0218      * minor versions. This struct is only guaranteed to be stable between patch
0219      * versions for a given minor version of the interpreter.
0220      */
0221     _Py_DebugOffsets debug_offsets;
0222 
0223     /* Has been initialized to a safe state.
0224 
0225        In order to be effective, this must be set to 0 during or right
0226        after allocation. */
0227     int _initialized;
0228 
0229     /* Is running Py_PreInitialize()? */
0230     int preinitializing;
0231 
0232     /* Is Python preinitialized? Set to 1 by Py_PreInitialize() */
0233     int preinitialized;
0234 
0235     /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */
0236     int core_initialized;
0237 
0238     /* Is Python fully initialized? Set to 1 by Py_Initialize() */
0239     int initialized;
0240 
0241     /* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
0242        is called again.
0243 
0244        Use _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing()
0245        to access it, don't access it directly. */
0246     PyThreadState *_finalizing;
0247     /* The ID of the OS thread in which we are finalizing. */
0248     unsigned long _finalizing_id;
0249 
0250     struct pyinterpreters {
0251         PyMutex mutex;
0252         /* The linked list of interpreters, newest first. */
0253         PyInterpreterState *head;
0254         /* The runtime's initial interpreter, which has a special role
0255            in the operation of the runtime.  It is also often the only
0256            interpreter. */
0257         PyInterpreterState *main;
0258         /* next_id is an auto-numbered sequence of small
0259            integers.  It gets initialized in _PyInterpreterState_Enable(),
0260            which is called in Py_Initialize(), and used in
0261            PyInterpreterState_New().  A negative interpreter ID
0262            indicates an error occurred.  The main interpreter will
0263            always have an ID of 0.  Overflow results in a RuntimeError.
0264            If that becomes a problem later then we can adjust, e.g. by
0265            using a Python int. */
0266         int64_t next_id;
0267     } interpreters;
0268 
0269     /* Platform-specific identifier and PyThreadState, respectively, for the
0270        main thread in the main interpreter. */
0271     unsigned long main_thread;
0272     PyThreadState *main_tstate;
0273 
0274     /* ---------- IMPORTANT ---------------------------
0275      The fields above this line are declared as early as
0276      possible to facilitate out-of-process observability
0277      tools. */
0278 
0279     /* cross-interpreter data and utils */
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     /* Used for the thread state bound to the current thread. */
0289     Py_tss_t autoTSSkey;
0290 
0291     /* Used instead of PyThreadState.trash when there is not current tstate. */
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     // The rwmutex is used to prevent overlapping global and per-interpreter
0310     // stop-the-world events. Global stop-the-world events lock the mutex
0311     // exclusively (as a "writer"), while per-interpreter stop-the-world events
0312     // lock it non-exclusively (as "readers").
0313     _PyRWMutex stoptheworld_mutex;
0314     struct _stoptheworld_state stoptheworld;
0315 
0316     PyPreConfig preconfig;
0317 
0318     // Audit values must be preserved when Py_Initialize()/Py_Finalize()
0319     // is called multiple times.
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     /* All the objects that are shared by the runtime's interpreters. */
0333     struct _Py_cached_objects cached_objects;
0334     struct _Py_static_objects static_objects;
0335 
0336     /* The following fields are here to avoid allocation during init.
0337        The data is exposed through _PyRuntimeState pointer fields.
0338        These fields should not be accessed directly outside of init.
0339 
0340        All other _PyRuntimeState pointer fields are populated when
0341        needed and default to NULL.
0342 
0343        For now there are some exceptions to that rule, which require
0344        allocation during init.  These will be addressed on a case-by-case
0345        basis.  Most notably, we don't pre-allocated the several mutex
0346        (PyThread_type_lock) fields, because on Windows we only ever get
0347        a pointer type.
0348        */
0349 
0350     /* _PyRuntimeState.interpreters.main */
0351     PyInterpreterState _main_interpreter;
0352 
0353 #if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
0354     // Used in "Python/emscripten_trampoline.c" to choose between type
0355     // reflection trampoline and EM_JS trampoline.
0356     bool wasm_type_reflection_available;
0357 #endif
0358 
0359 } _PyRuntimeState;
0360 
0361 
0362 /* other API */
0363 
0364 // Export _PyRuntime for shared extensions which use it in static inline
0365 // functions for best performance, like _Py_IsMainThread() or _Py_ID().
0366 // It's also made accessible for debuggers and profilers.
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 /* Initialize _PyRuntimeState.
0377    Return NULL on success, or return an error message on failure. */
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         // XXX Re-enable this assert once gh-109860 is fixed.
0401         //assert(tstate->thread_id == PyThread_get_thread_ident());
0402         _Py_atomic_store_ulong_relaxed(&runtime->_finalizing_id,
0403                                        tstate->thread_id);
0404     }
0405 }
0406 
0407 #ifdef __cplusplus
0408 }
0409 #endif
0410 #endif /* !Py_INTERNAL_RUNTIME_H */