Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:48

0001 #ifndef Py_INTERNAL_INTERP_H
0002 #define Py_INTERNAL_INTERP_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 <stdbool.h>              // bool
0012 
0013 #include "pycore_ast_state.h"     // struct ast_state
0014 #include "pycore_atexit.h"        // struct atexit_state
0015 #include "pycore_ceval_state.h"   // struct _ceval_state
0016 #include "pycore_code.h"          // struct callable_cache
0017 #include "pycore_codecs.h"        // struct codecs_state
0018 #include "pycore_context.h"       // struct _Py_context_state
0019 #include "pycore_crossinterp.h"   // struct _xidregistry
0020 #include "pycore_dict_state.h"    // struct _Py_dict_state
0021 #include "pycore_dtoa.h"          // struct _dtoa_state
0022 #include "pycore_exceptions.h"    // struct _Py_exc_state
0023 #include "pycore_floatobject.h"   // struct _Py_float_state
0024 #include "pycore_function.h"      // FUNC_MAX_WATCHERS
0025 #include "pycore_gc.h"            // struct _gc_runtime_state
0026 #include "pycore_genobject.h"     // struct _Py_async_gen_state
0027 #include "pycore_global_objects.h"// struct _Py_interp_cached_objects
0028 #include "pycore_import.h"        // struct _import_state
0029 #include "pycore_instruments.h"   // _PY_MONITORING_EVENTS
0030 #include "pycore_list.h"          // struct _Py_list_state
0031 #include "pycore_mimalloc.h"      // struct _mimalloc_interp_state
0032 #include "pycore_object_state.h"  // struct _py_object_state
0033 #include "pycore_optimizer.h"     // _PyOptimizerObject
0034 #include "pycore_obmalloc.h"      // struct _obmalloc_state
0035 #include "pycore_qsbr.h"          // struct _qsbr_state
0036 #include "pycore_tstate.h"        // _PyThreadStateImpl
0037 #include "pycore_tuple.h"         // struct _Py_tuple_state
0038 #include "pycore_typeobject.h"    // struct types_state
0039 #include "pycore_unicodeobject.h" // struct _Py_unicode_state
0040 #include "pycore_warnings.h"      // struct _warnings_runtime_state
0041 
0042 
0043 struct _Py_long_state {
0044     int max_str_digits;
0045 };
0046 
0047 // Support for stop-the-world events. This exists in both the PyRuntime struct
0048 // for global pauses and in each PyInterpreterState for per-interpreter pauses.
0049 struct _stoptheworld_state {
0050     PyMutex mutex;       // Serializes stop-the-world attempts.
0051 
0052     // NOTE: The below fields are protected by HEAD_LOCK(runtime), not by the
0053     // above mutex.
0054     bool requested;      // Set when a pause is requested.
0055     bool world_stopped;  // Set when the world is stopped.
0056     bool is_global;      // Set when contained in PyRuntime struct.
0057 
0058     PyEvent stop_event;  // Set when thread_countdown reaches zero.
0059     Py_ssize_t thread_countdown;  // Number of threads that must pause.
0060 
0061     PyThreadState *requester; // Thread that requested the pause (may be NULL).
0062 };
0063 
0064 #ifdef Py_GIL_DISABLED
0065 // This should be prime but otherwise the choice is arbitrary. A larger value
0066 // increases concurrency at the expense of memory.
0067 #  define NUM_WEAKREF_LIST_LOCKS 127
0068 #endif
0069 
0070 /* cross-interpreter data registry */
0071 
0072 /* Tracks some rare events per-interpreter, used by the optimizer to turn on/off
0073    specific optimizations. */
0074 typedef struct _rare_events {
0075     /* Setting an object's class, obj.__class__ = ... */
0076     uint8_t set_class;
0077     /* Setting the bases of a class, cls.__bases__ = ... */
0078     uint8_t set_bases;
0079     /* Setting the PEP 523 frame eval function, _PyInterpreterState_SetFrameEvalFunc() */
0080     uint8_t set_eval_frame_func;
0081     /* Modifying the builtins,  __builtins__.__dict__[var] = ... */
0082     uint8_t builtin_dict;
0083     /* Modifying a function, e.g. func.__defaults__ = ..., etc. */
0084     uint8_t func_modification;
0085 } _rare_events;
0086 
0087 /* interpreter state */
0088 
0089 /* PyInterpreterState holds the global state for one of the runtime's
0090    interpreters.  Typically the initial (main) interpreter is the only one.
0091 
0092    The PyInterpreterState typedef is in Include/pytypedefs.h.
0093    */
0094 struct _is {
0095 
0096     /* This struct contains the eval_breaker,
0097      * which is by far the hottest field in this struct
0098      * and should be placed at the beginning. */
0099     struct _ceval_state ceval;
0100 
0101     PyInterpreterState *next;
0102 
0103     int64_t id;
0104     int64_t id_refcount;
0105     int requires_idref;
0106     PyThread_type_lock id_mutex;
0107 
0108 #define _PyInterpreterState_WHENCE_NOTSET -1
0109 #define _PyInterpreterState_WHENCE_UNKNOWN 0
0110 #define _PyInterpreterState_WHENCE_RUNTIME 1
0111 #define _PyInterpreterState_WHENCE_LEGACY_CAPI 2
0112 #define _PyInterpreterState_WHENCE_CAPI 3
0113 #define _PyInterpreterState_WHENCE_XI 4
0114 #define _PyInterpreterState_WHENCE_STDLIB 5
0115 #define _PyInterpreterState_WHENCE_MAX 5
0116     long _whence;
0117 
0118     /* Has been initialized to a safe state.
0119 
0120        In order to be effective, this must be set to 0 during or right
0121        after allocation. */
0122     int _initialized;
0123     /* Has been fully initialized via pylifecycle.c. */
0124     int _ready;
0125     int finalizing;
0126 
0127     uintptr_t last_restart_version;
0128     struct pythreads {
0129         uint64_t next_unique_id;
0130         /* The linked list of threads, newest first. */
0131         PyThreadState *head;
0132         /* The thread currently executing in the __main__ module, if any. */
0133         PyThreadState *main;
0134         /* Used in Modules/_threadmodule.c. */
0135         Py_ssize_t count;
0136         /* Support for runtime thread stack size tuning.
0137            A value of 0 means using the platform's default stack size
0138            or the size specified by the THREAD_STACK_SIZE macro. */
0139         /* Used in Python/thread.c. */
0140         size_t stacksize;
0141     } threads;
0142 
0143     /* Reference to the _PyRuntime global variable. This field exists
0144        to not have to pass runtime in addition to tstate to a function.
0145        Get runtime from tstate: tstate->interp->runtime. */
0146     struct pyruntimestate *runtime;
0147 
0148     /* Set by Py_EndInterpreter().
0149 
0150        Use _PyInterpreterState_GetFinalizing()
0151        and _PyInterpreterState_SetFinalizing()
0152        to access it, don't access it directly. */
0153     PyThreadState* _finalizing;
0154     /* The ID of the OS thread in which we are finalizing. */
0155     unsigned long _finalizing_id;
0156 
0157     struct _gc_runtime_state gc;
0158 
0159     /* The following fields are here to avoid allocation during init.
0160        The data is exposed through PyInterpreterState pointer fields.
0161        These fields should not be accessed directly outside of init.
0162 
0163        All other PyInterpreterState pointer fields are populated when
0164        needed and default to NULL.
0165 
0166        For now there are some exceptions to that rule, which require
0167        allocation during init.  These will be addressed on a case-by-case
0168        basis.  Also see _PyRuntimeState regarding the various mutex fields.
0169        */
0170 
0171     // Dictionary of the sys module
0172     PyObject *sysdict;
0173 
0174     // Dictionary of the builtins module
0175     PyObject *builtins;
0176 
0177     struct _import_state imports;
0178 
0179     /* The per-interpreter GIL, which might not be used. */
0180     struct _gil_runtime_state _gil;
0181 
0182      /* ---------- IMPORTANT ---------------------------
0183      The fields above this line are declared as early as
0184      possible to facilitate out-of-process observability
0185      tools. */
0186 
0187     struct codecs_state codecs;
0188 
0189     PyConfig config;
0190     unsigned long feature_flags;
0191 
0192     PyObject *dict;  /* Stores per-interpreter state */
0193 
0194     PyObject *sysdict_copy;
0195     PyObject *builtins_copy;
0196     // Initialized to _PyEval_EvalFrameDefault().
0197     _PyFrameEvalFunction eval_frame;
0198 
0199     PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
0200     // One bit is set for each non-NULL entry in func_watchers
0201     uint8_t active_func_watchers;
0202 
0203     Py_ssize_t co_extra_user_count;
0204     freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
0205 
0206     /* cross-interpreter data and utils */
0207     struct _xi_state xi;
0208 
0209 #ifdef HAVE_FORK
0210     PyObject *before_forkers;
0211     PyObject *after_forkers_parent;
0212     PyObject *after_forkers_child;
0213 #endif
0214 
0215     struct _warnings_runtime_state warnings;
0216     struct atexit_state atexit;
0217     struct _stoptheworld_state stoptheworld;
0218     struct _qsbr_shared qsbr;
0219 
0220 #if defined(Py_GIL_DISABLED)
0221     struct _mimalloc_interp_state mimalloc;
0222     struct _brc_state brc;  // biased reference counting state
0223     PyMutex weakref_locks[NUM_WEAKREF_LIST_LOCKS];
0224 #endif
0225 
0226     // Per-interpreter state for the obmalloc allocator.  For the main
0227     // interpreter and for all interpreters that don't have their
0228     // own obmalloc state, this points to the static structure in
0229     // obmalloc.c obmalloc_state_main.  For other interpreters, it is
0230     // heap allocated by _PyMem_init_obmalloc() and freed when the
0231     // interpreter structure is freed.  In the case of a heap allocated
0232     // obmalloc state, it is not safe to hold on to or use memory after
0233     // the interpreter is freed. The obmalloc state corresponding to
0234     // that allocated memory is gone.  See free_obmalloc_arenas() for
0235     // more comments.
0236     struct _obmalloc_state *obmalloc;
0237 
0238     PyObject *audit_hooks;
0239     PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
0240     PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
0241     // One bit is set for each non-NULL entry in code_watchers
0242     uint8_t active_code_watchers;
0243 
0244     struct _py_object_state object_state;
0245     struct _Py_unicode_state unicode;
0246     struct _Py_long_state long_state;
0247     struct _dtoa_state dtoa;
0248     struct _py_func_state func_state;
0249     struct _py_code_state code_state;
0250 
0251     struct _Py_dict_state dict_state;
0252     struct _Py_exc_state exc_state;
0253     struct _Py_mem_interp_free_queue mem_free_queue;
0254 
0255     struct ast_state ast;
0256     struct types_state types;
0257     struct callable_cache callable_cache;
0258     _PyOptimizerObject *optimizer;
0259     _PyExecutorObject *executor_list_head;
0260 
0261     _rare_events rare_events;
0262     PyDict_WatchCallback builtins_dict_watcher;
0263 
0264     _Py_GlobalMonitors monitors;
0265     bool sys_profile_initialized;
0266     bool sys_trace_initialized;
0267     Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */
0268     Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */
0269     PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS];
0270     PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS];
0271 
0272     struct _Py_interp_cached_objects cached_objects;
0273     struct _Py_interp_static_objects static_objects;
0274 
0275     /* the initial PyInterpreterState.threads.head */
0276     _PyThreadStateImpl _initial_thread;
0277     Py_ssize_t _interactive_src_count;
0278     // In 3.14+ this is interp->threads.preallocated.
0279     _PyThreadStateImpl *threads_preallocated;
0280 };
0281 
0282 
0283 /* other API */
0284 
0285 extern void _PyInterpreterState_Clear(PyThreadState *tstate);
0286 
0287 
0288 static inline PyThreadState*
0289 _PyInterpreterState_GetFinalizing(PyInterpreterState *interp) {
0290     return (PyThreadState*)_Py_atomic_load_ptr_relaxed(&interp->_finalizing);
0291 }
0292 
0293 static inline unsigned long
0294 _PyInterpreterState_GetFinalizingID(PyInterpreterState *interp) {
0295     return _Py_atomic_load_ulong_relaxed(&interp->_finalizing_id);
0296 }
0297 
0298 static inline void
0299 _PyInterpreterState_SetFinalizing(PyInterpreterState *interp, PyThreadState *tstate) {
0300     _Py_atomic_store_ptr_relaxed(&interp->_finalizing, tstate);
0301     if (tstate == NULL) {
0302         _Py_atomic_store_ulong_relaxed(&interp->_finalizing_id, 0);
0303     }
0304     else {
0305         // XXX Re-enable this assert once gh-109860 is fixed.
0306         //assert(tstate->thread_id == PyThread_get_thread_ident());
0307         _Py_atomic_store_ulong_relaxed(&interp->_finalizing_id,
0308                                        tstate->thread_id);
0309     }
0310 }
0311 
0312 
0313 
0314 // Exports for the _testinternalcapi module.
0315 PyAPI_FUNC(int64_t) _PyInterpreterState_ObjectToID(PyObject *);
0316 PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(int64_t);
0317 PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpIDObject(PyObject *);
0318 PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
0319 PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
0320 PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
0321 
0322 PyAPI_FUNC(int) _PyInterpreterState_IsReady(PyInterpreterState *interp);
0323 
0324 PyAPI_FUNC(long) _PyInterpreterState_GetWhence(PyInterpreterState *interp);
0325 extern void _PyInterpreterState_SetWhence(
0326     PyInterpreterState *interp,
0327     long whence);
0328 
0329 extern const PyConfig* _PyInterpreterState_GetConfig(PyInterpreterState *interp);
0330 
0331 // Get a copy of the current interpreter configuration.
0332 //
0333 // Return 0 on success. Raise an exception and return -1 on error.
0334 //
0335 // The caller must initialize 'config', using PyConfig_InitPythonConfig()
0336 // for example.
0337 //
0338 // Python must be preinitialized to call this method.
0339 // The caller must hold the GIL.
0340 //
0341 // Once done with the configuration, PyConfig_Clear() must be called to clear
0342 // it.
0343 //
0344 // Export for '_testinternalcapi' shared extension.
0345 PyAPI_FUNC(int) _PyInterpreterState_GetConfigCopy(
0346     struct PyConfig *config);
0347 
0348 // Set the configuration of the current interpreter.
0349 //
0350 // This function should be called during or just after the Python
0351 // initialization.
0352 //
0353 // Update the sys module with the new configuration. If the sys module was
0354 // modified directly after the Python initialization, these changes are lost.
0355 //
0356 // Some configuration like faulthandler or warnoptions can be updated in the
0357 // configuration, but don't reconfigure Python (don't enable/disable
0358 // faulthandler and don't reconfigure warnings filters).
0359 //
0360 // Return 0 on success. Raise an exception and return -1 on error.
0361 //
0362 // The configuration should come from _PyInterpreterState_GetConfigCopy().
0363 //
0364 // Export for '_testinternalcapi' shared extension.
0365 PyAPI_FUNC(int) _PyInterpreterState_SetConfig(
0366     const struct PyConfig *config);
0367 
0368 
0369 /*
0370 Runtime Feature Flags
0371 
0372 Each flag indicate whether or not a specific runtime feature
0373 is available in a given context.  For example, forking the process
0374 might not be allowed in the current interpreter (i.e. os.fork() would fail).
0375 */
0376 
0377 /* Set if the interpreter share obmalloc runtime state
0378    with the main interpreter. */
0379 #define Py_RTFLAGS_USE_MAIN_OBMALLOC (1UL << 5)
0380 
0381 /* Set if import should check a module for subinterpreter support. */
0382 #define Py_RTFLAGS_MULTI_INTERP_EXTENSIONS (1UL << 8)
0383 
0384 /* Set if threads are allowed. */
0385 #define Py_RTFLAGS_THREADS (1UL << 10)
0386 
0387 /* Set if daemon threads are allowed. */
0388 #define Py_RTFLAGS_DAEMON_THREADS (1UL << 11)
0389 
0390 /* Set if os.fork() is allowed. */
0391 #define Py_RTFLAGS_FORK (1UL << 15)
0392 
0393 /* Set if os.exec*() is allowed. */
0394 #define Py_RTFLAGS_EXEC (1UL << 16)
0395 
0396 extern int _PyInterpreterState_HasFeature(PyInterpreterState *interp,
0397                                           unsigned long feature);
0398 
0399 PyAPI_FUNC(PyStatus) _PyInterpreterState_New(
0400     PyThreadState *tstate,
0401     PyInterpreterState **pinterp);
0402 
0403 
0404 #define RARE_EVENT_INTERP_INC(interp, name) \
0405     do { \
0406         /* saturating add */ \
0407         int val = FT_ATOMIC_LOAD_UINT8_RELAXED(interp->rare_events.name); \
0408         if (val < UINT8_MAX) { \
0409             FT_ATOMIC_STORE_UINT8(interp->rare_events.name, val + 1); \
0410         } \
0411         RARE_EVENT_STAT_INC(name); \
0412     } while (0); \
0413 
0414 #define RARE_EVENT_INC(name) \
0415     do { \
0416         PyInterpreterState *interp = PyInterpreterState_Get(); \
0417         RARE_EVENT_INTERP_INC(interp, name); \
0418     } while (0); \
0419 
0420 #ifdef __cplusplus
0421 }
0422 #endif
0423 #endif /* !Py_INTERNAL_INTERP_H */