Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:18:09

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_atomic.h"          /* _Py_atomic_address */
0013 #include "pycore_ceval_state.h"     // struct _ceval_runtime_state
0014 #include "pycore_floatobject.h"     // struct _Py_float_runtime_state
0015 #include "pycore_faulthandler.h"    // struct _faulthandler_runtime_state
0016 #include "pycore_global_objects.h"  // struct _Py_global_objects
0017 #include "pycore_import.h"          // struct _import_runtime_state
0018 #include "pycore_interp.h"          // PyInterpreterState
0019 #include "pycore_object_state.h"    // struct _py_object_runtime_state
0020 #include "pycore_parser.h"          // struct _parser_runtime_state
0021 #include "pycore_pymem.h"           // struct _pymem_allocators
0022 #include "pycore_pyhash.h"          // struct pyhash_runtime_state
0023 #include "pycore_pythread.h"        // struct _pythread_runtime_state
0024 #include "pycore_signal.h"          // struct _signals_runtime_state
0025 #include "pycore_time.h"            // struct _time_runtime_state
0026 #include "pycore_tracemalloc.h"     // struct _tracemalloc_runtime_state
0027 #include "pycore_typeobject.h"      // struct types_runtime_state
0028 #include "pycore_unicodeobject.h"   // struct _Py_unicode_runtime_ids
0029 
0030 struct _getargs_runtime_state {
0031     PyThread_type_lock mutex;
0032     struct _PyArg_Parser *static_parsers;
0033 };
0034 
0035 /* GIL state */
0036 
0037 struct _gilstate_runtime_state {
0038     /* bpo-26558: Flag to disable PyGILState_Check().
0039        If set to non-zero, PyGILState_Check() always return 1. */
0040     int check_enabled;
0041     /* The single PyInterpreterState used by this process'
0042        GILState implementation
0043     */
0044     /* TODO: Given interp_main, it may be possible to kill this ref */
0045     PyInterpreterState *autoInterpreterState;
0046 };
0047 
0048 /* Runtime audit hook state */
0049 
0050 typedef struct _Py_AuditHookEntry {
0051     struct _Py_AuditHookEntry *next;
0052     Py_AuditHookFunction hookCFunction;
0053     void *userData;
0054 } _Py_AuditHookEntry;
0055 
0056 /* Full Python runtime state */
0057 
0058 /* _PyRuntimeState holds the global state for the CPython runtime.
0059    That data is exposed in the internal API as a static variable (_PyRuntime).
0060    */
0061 typedef struct pyruntimestate {
0062     /* Has been initialized to a safe state.
0063 
0064        In order to be effective, this must be set to 0 during or right
0065        after allocation. */
0066     int _initialized;
0067 
0068     /* Is running Py_PreInitialize()? */
0069     int preinitializing;
0070 
0071     /* Is Python preinitialized? Set to 1 by Py_PreInitialize() */
0072     int preinitialized;
0073 
0074     /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */
0075     int core_initialized;
0076 
0077     /* Is Python fully initialized? Set to 1 by Py_Initialize() */
0078     int initialized;
0079 
0080     /* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
0081        is called again.
0082 
0083        Use _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing()
0084        to access it, don't access it directly. */
0085     _Py_atomic_address _finalizing;
0086 
0087     struct pyinterpreters {
0088         PyThread_type_lock mutex;
0089         /* The linked list of interpreters, newest first. */
0090         PyInterpreterState *head;
0091         /* The runtime's initial interpreter, which has a special role
0092            in the operation of the runtime.  It is also often the only
0093            interpreter. */
0094         PyInterpreterState *main;
0095         /* next_id is an auto-numbered sequence of small
0096            integers.  It gets initialized in _PyInterpreterState_Enable(),
0097            which is called in Py_Initialize(), and used in
0098            PyInterpreterState_New().  A negative interpreter ID
0099            indicates an error occurred.  The main interpreter will
0100            always have an ID of 0.  Overflow results in a RuntimeError.
0101            If that becomes a problem later then we can adjust, e.g. by
0102            using a Python int. */
0103         int64_t next_id;
0104     } interpreters;
0105 
0106     unsigned long main_thread;
0107 
0108     /* ---------- IMPORTANT ---------------------------
0109      The fields above this line are declared as early as
0110      possible to facilitate out-of-process observability
0111      tools. */
0112 
0113     // XXX Remove this field once we have a tp_* slot.
0114     struct _xidregistry xidregistry;
0115 
0116     struct _pymem_allocators allocators;
0117     struct _obmalloc_global_state obmalloc;
0118     struct pyhash_runtime_state pyhash_state;
0119     struct _time_runtime_state time;
0120     struct _pythread_runtime_state threads;
0121     struct _signals_runtime_state signals;
0122 
0123     /* Used for the thread state bound to the current thread. */
0124     Py_tss_t autoTSSkey;
0125 
0126     /* Used instead of PyThreadState.trash when there is not current tstate. */
0127     Py_tss_t trashTSSkey;
0128 
0129     PyWideStringList orig_argv;
0130 
0131     struct _parser_runtime_state parser;
0132 
0133     struct _atexit_runtime_state atexit;
0134 
0135     struct _import_runtime_state imports;
0136     struct _ceval_runtime_state ceval;
0137     struct _gilstate_runtime_state gilstate;
0138     struct _getargs_runtime_state getargs;
0139     struct _fileutils_state fileutils;
0140     struct _faulthandler_runtime_state faulthandler;
0141     struct _tracemalloc_runtime_state tracemalloc;
0142 
0143     PyPreConfig preconfig;
0144 
0145     // Audit values must be preserved when Py_Initialize()/Py_Finalize()
0146     // is called multiple times.
0147     Py_OpenCodeHookFunction open_code_hook;
0148     void *open_code_userdata;
0149     struct {
0150         PyThread_type_lock mutex;
0151         _Py_AuditHookEntry *head;
0152     } audit_hooks;
0153 
0154     struct _py_object_runtime_state object_state;
0155     struct _Py_float_runtime_state float_state;
0156     struct _Py_unicode_runtime_state unicode_state;
0157     struct _types_runtime_state types;
0158 
0159     /* All the objects that are shared by the runtime's interpreters. */
0160     struct _Py_static_objects static_objects;
0161     struct _Py_cached_objects cached_objects;
0162 
0163     /* The ID of the OS thread in which we are finalizing.
0164        We use _Py_atomic_address instead of adding a new _Py_atomic_ulong. */
0165     _Py_atomic_address _finalizing_id;
0166     /* The value to use for sys.path[0] in new subinterpreters.
0167        Normally this would be part of the PyConfig struct.  However,
0168        we cannot add it there in 3.12 since that's an ABI change. */
0169     wchar_t *sys_path_0;
0170 
0171     /* The following fields are here to avoid allocation during init.
0172        The data is exposed through _PyRuntimeState pointer fields.
0173        These fields should not be accessed directly outside of init.
0174 
0175        All other _PyRuntimeState pointer fields are populated when
0176        needed and default to NULL.
0177 
0178        For now there are some exceptions to that rule, which require
0179        allocation during init.  These will be addressed on a case-by-case
0180        basis.  Most notably, we don't pre-allocated the several mutex
0181        (PyThread_type_lock) fields, because on Windows we only ever get
0182        a pointer type.
0183        */
0184 
0185     /* PyInterpreterState.interpreters.main */
0186     PyInterpreterState _main_interpreter;
0187 } _PyRuntimeState;
0188 
0189 
0190 /* other API */
0191 
0192 PyAPI_DATA(_PyRuntimeState) _PyRuntime;
0193 
0194 PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime);
0195 PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime);
0196 
0197 #ifdef HAVE_FORK
0198 extern PyStatus _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime);
0199 #endif
0200 
0201 /* Initialize _PyRuntimeState.
0202    Return NULL on success, or return an error message on failure. */
0203 PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void);
0204 
0205 PyAPI_FUNC(void) _PyRuntime_Finalize(void);
0206 
0207 
0208 static inline PyThreadState*
0209 _PyRuntimeState_GetFinalizing(_PyRuntimeState *runtime) {
0210     return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->_finalizing);
0211 }
0212 
0213 static inline unsigned long
0214 _PyRuntimeState_GetFinalizingID(_PyRuntimeState *runtime) {
0215     return (unsigned long)_Py_atomic_load_relaxed(&runtime->_finalizing_id);
0216 }
0217 
0218 static inline void
0219 _PyRuntimeState_SetFinalizing(_PyRuntimeState *runtime, PyThreadState *tstate) {
0220     _Py_atomic_store_relaxed(&runtime->_finalizing, (uintptr_t)tstate);
0221     if (tstate == NULL) {
0222         _Py_atomic_store_relaxed(&runtime->_finalizing_id, 0);
0223     }
0224     else {
0225         // XXX Re-enable this assert once gh-109860 is fixed.
0226         //assert(tstate->thread_id == PyThread_get_thread_ident());
0227         _Py_atomic_store_relaxed(&runtime->_finalizing_id,
0228                                  (uintptr_t)tstate->thread_id);
0229     }
0230 }
0231 
0232 #ifdef __cplusplus
0233 }
0234 #endif
0235 #endif /* !Py_INTERNAL_RUNTIME_H */