Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:45

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>
0012 
0013 #include "pycore_ast_state.h"     // struct ast_state
0014 #include "pycore_atexit.h"        // struct atexit_state
0015 #include "pycore_atomic.h"        // _Py_atomic_address
0016 #include "pycore_ceval_state.h"   // struct _ceval_state
0017 #include "pycore_code.h"          // struct callable_cache
0018 #include "pycore_context.h"       // struct _Py_context_state
0019 #include "pycore_dict_state.h"    // struct _Py_dict_state
0020 #include "pycore_dtoa.h"          // struct _dtoa_state
0021 #include "pycore_exceptions.h"    // struct _Py_exc_state
0022 #include "pycore_floatobject.h"   // struct _Py_float_state
0023 #include "pycore_function.h"      // FUNC_MAX_WATCHERS
0024 #include "pycore_genobject.h"     // struct _Py_async_gen_state
0025 #include "pycore_gc.h"            // struct _gc_runtime_state
0026 #include "pycore_global_objects.h"  // struct _Py_interp_static_objects
0027 #include "pycore_import.h"        // struct _import_state
0028 #include "pycore_instruments.h"   // _PY_MONITORING_EVENTS
0029 #include "pycore_list.h"          // struct _Py_list_state
0030 #include "pycore_object_state.h"   // struct _py_object_state
0031 #include "pycore_obmalloc.h"      // struct obmalloc_state
0032 #include "pycore_tuple.h"         // struct _Py_tuple_state
0033 #include "pycore_typeobject.h"    // struct type_cache
0034 #include "pycore_unicodeobject.h" // struct _Py_unicode_state
0035 #include "pycore_warnings.h"      // struct _warnings_runtime_state
0036 
0037 
0038 struct _Py_long_state {
0039     int max_str_digits;
0040 };
0041 
0042 
0043 /* cross-interpreter data registry */
0044 
0045 /* For now we use a global registry of shareable classes.  An
0046    alternative would be to add a tp_* slot for a class's
0047    crossinterpdatafunc. It would be simpler and more efficient. */
0048 
0049 struct _xidregitem;
0050 
0051 struct _xidregitem {
0052     struct _xidregitem *prev;
0053     struct _xidregitem *next;
0054     /* This can be a dangling pointer, but only if weakref is set. */
0055     PyTypeObject *cls;
0056     /* This is NULL for builtin types. */
0057     PyObject *weakref;
0058     size_t refcount;
0059     crossinterpdatafunc getdata;
0060 };
0061 
0062 struct _xidregistry {
0063     PyThread_type_lock mutex;
0064     struct _xidregitem *head;
0065 };
0066 
0067 
0068 /* interpreter state */
0069 
0070 /* PyInterpreterState holds the global state for one of the runtime's
0071    interpreters.  Typically the initial (main) interpreter is the only one.
0072 
0073    The PyInterpreterState typedef is in Include/pytypedefs.h.
0074    */
0075 struct _is {
0076 
0077     PyInterpreterState *next;
0078 
0079     int64_t id;
0080     int64_t id_refcount;
0081     int requires_idref;
0082     PyThread_type_lock id_mutex;
0083 
0084     /* Has been initialized to a safe state.
0085 
0086        In order to be effective, this must be set to 0 during or right
0087        after allocation. */
0088     int _initialized;
0089     int finalizing;
0090 
0091     uint64_t monitoring_version;
0092     uint64_t last_restart_version;
0093     struct pythreads {
0094         uint64_t next_unique_id;
0095         /* The linked list of threads, newest first. */
0096         PyThreadState *head;
0097         /* Used in Modules/_threadmodule.c. */
0098         long count;
0099         /* Support for runtime thread stack size tuning.
0100            A value of 0 means using the platform's default stack size
0101            or the size specified by the THREAD_STACK_SIZE macro. */
0102         /* Used in Python/thread.c. */
0103         size_t stacksize;
0104     } threads;
0105 
0106     /* Reference to the _PyRuntime global variable. This field exists
0107        to not have to pass runtime in addition to tstate to a function.
0108        Get runtime from tstate: tstate->interp->runtime. */
0109     struct pyruntimestate *runtime;
0110 
0111     /* Set by Py_EndInterpreter().
0112 
0113        Use _PyInterpreterState_GetFinalizing()
0114        and _PyInterpreterState_SetFinalizing()
0115        to access it, don't access it directly. */
0116     _Py_atomic_address _finalizing;
0117 
0118     struct _gc_runtime_state gc;
0119 
0120     /* The following fields are here to avoid allocation during init.
0121        The data is exposed through PyInterpreterState pointer fields.
0122        These fields should not be accessed directly outside of init.
0123 
0124        All other PyInterpreterState pointer fields are populated when
0125        needed and default to NULL.
0126 
0127        For now there are some exceptions to that rule, which require
0128        allocation during init.  These will be addressed on a case-by-case
0129        basis.  Also see _PyRuntimeState regarding the various mutex fields.
0130        */
0131 
0132     // Dictionary of the sys module
0133     PyObject *sysdict;
0134 
0135     // Dictionary of the builtins module
0136     PyObject *builtins;
0137 
0138     struct _ceval_state ceval;
0139 
0140     struct _import_state imports;
0141 
0142     /* The per-interpreter GIL, which might not be used. */
0143     struct _gil_runtime_state _gil;
0144 
0145      /* ---------- IMPORTANT ---------------------------
0146      The fields above this line are declared as early as
0147      possible to facilitate out-of-process observability
0148      tools. */
0149 
0150     PyObject *codec_search_path;
0151     PyObject *codec_search_cache;
0152     PyObject *codec_error_registry;
0153     int codecs_initialized;
0154 
0155     PyConfig config;
0156     unsigned long feature_flags;
0157 
0158     PyObject *dict;  /* Stores per-interpreter state */
0159 
0160     PyObject *sysdict_copy;
0161     PyObject *builtins_copy;
0162     // Initialized to _PyEval_EvalFrameDefault().
0163     _PyFrameEvalFunction eval_frame;
0164 
0165     PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
0166     // One bit is set for each non-NULL entry in func_watchers
0167     uint8_t active_func_watchers;
0168 
0169     Py_ssize_t co_extra_user_count;
0170     freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
0171 
0172 #ifdef HAVE_FORK
0173     PyObject *before_forkers;
0174     PyObject *after_forkers_parent;
0175     PyObject *after_forkers_child;
0176 #endif
0177 
0178     struct _warnings_runtime_state warnings;
0179     struct atexit_state atexit;
0180 
0181     struct _obmalloc_state obmalloc;
0182 
0183     PyObject *audit_hooks;
0184     PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
0185     PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
0186     // One bit is set for each non-NULL entry in code_watchers
0187     uint8_t active_code_watchers;
0188 
0189     struct _py_object_state object_state;
0190     struct _Py_unicode_state unicode;
0191     struct _Py_float_state float_state;
0192     struct _Py_long_state long_state;
0193     struct _dtoa_state dtoa;
0194     struct _py_func_state func_state;
0195     /* Using a cache is very effective since typically only a single slice is
0196        created and then deleted again. */
0197     PySliceObject *slice_cache;
0198 
0199     struct _Py_tuple_state tuple;
0200     struct _Py_list_state list;
0201     struct _Py_dict_state dict_state;
0202     struct _Py_async_gen_state async_gen;
0203     struct _Py_context_state context;
0204     struct _Py_exc_state exc_state;
0205 
0206     struct ast_state ast;
0207     struct types_state types;
0208     struct callable_cache callable_cache;
0209     PyCodeObject *interpreter_trampoline;
0210 
0211     _Py_GlobalMonitors monitors;
0212     bool f_opcode_trace_set;
0213     bool sys_profile_initialized;
0214     bool sys_trace_initialized;
0215     Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */
0216     Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */
0217     PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS];
0218     PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS];
0219 
0220     struct _Py_interp_cached_objects cached_objects;
0221     struct _Py_interp_static_objects static_objects;
0222 
0223     // XXX Remove this field once we have a tp_* slot.
0224     struct _xidregistry xidregistry;
0225     /* The thread currently executing in the __main__ module, if any. */
0226     PyThreadState *threads_main;
0227     /* The ID of the OS thread in which we are finalizing.
0228        We use _Py_atomic_address instead of adding a new _Py_atomic_ulong. */
0229     _Py_atomic_address _finalizing_id;
0230 
0231    /* the initial PyInterpreterState.threads.head */
0232     PyThreadState _initial_thread;
0233 };
0234 
0235 
0236 /* other API */
0237 
0238 extern void _PyInterpreterState_Clear(PyThreadState *tstate);
0239 
0240 
0241 static inline PyThreadState*
0242 _PyInterpreterState_GetFinalizing(PyInterpreterState *interp) {
0243     return (PyThreadState*)_Py_atomic_load_relaxed(&interp->_finalizing);
0244 }
0245 
0246 static inline unsigned long
0247 _PyInterpreterState_GetFinalizingID(PyInterpreterState *interp) {
0248     return (unsigned long)_Py_atomic_load_relaxed(&interp->_finalizing_id);
0249 }
0250 
0251 static inline void
0252 _PyInterpreterState_SetFinalizing(PyInterpreterState *interp, PyThreadState *tstate) {
0253     _Py_atomic_store_relaxed(&interp->_finalizing, (uintptr_t)tstate);
0254     if (tstate == NULL) {
0255         _Py_atomic_store_relaxed(&interp->_finalizing_id, 0);
0256     }
0257     else {
0258         // XXX Re-enable this assert once gh-109860 is fixed.
0259         //assert(tstate->thread_id == PyThread_get_thread_ident());
0260         _Py_atomic_store_relaxed(&interp->_finalizing_id,
0261                                  (uintptr_t)tstate->thread_id);
0262     }
0263 }
0264 
0265 
0266 PyAPI_FUNC(PyInterpreterState*) _PyInterpreterState_LookUpID(int64_t);
0267 
0268 PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
0269 PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
0270 PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
0271 
0272 #ifdef __cplusplus
0273 }
0274 #endif
0275 #endif /* !Py_INTERNAL_INTERP_H */