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
0048
0049 struct _stoptheworld_state {
0050 PyMutex mutex;
0051
0052
0053
0054 bool requested;
0055 bool world_stopped;
0056 bool is_global;
0057
0058 PyEvent stop_event;
0059 Py_ssize_t thread_countdown;
0060
0061 PyThreadState *requester;
0062 };
0063
0064 #ifdef Py_GIL_DISABLED
0065
0066
0067 # define NUM_WEAKREF_LIST_LOCKS 127
0068 #endif
0069
0070
0071
0072
0073
0074 typedef struct _rare_events {
0075
0076 uint8_t set_class;
0077
0078 uint8_t set_bases;
0079
0080 uint8_t set_eval_frame_func;
0081
0082 uint8_t builtin_dict;
0083
0084 uint8_t func_modification;
0085 } _rare_events;
0086
0087
0088
0089
0090
0091
0092
0093
0094 struct _is {
0095
0096
0097
0098
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
0119
0120
0121
0122 int _initialized;
0123
0124 int _ready;
0125 int finalizing;
0126
0127 uintptr_t last_restart_version;
0128 struct pythreads {
0129 uint64_t next_unique_id;
0130
0131 PyThreadState *head;
0132
0133 PyThreadState *main;
0134
0135 Py_ssize_t count;
0136
0137
0138
0139
0140 size_t stacksize;
0141 } threads;
0142
0143
0144
0145
0146 struct pyruntimestate *runtime;
0147
0148
0149
0150
0151
0152
0153 PyThreadState* _finalizing;
0154
0155 unsigned long _finalizing_id;
0156
0157 struct _gc_runtime_state gc;
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 PyObject *sysdict;
0173
0174
0175 PyObject *builtins;
0176
0177 struct _import_state imports;
0178
0179
0180 struct _gil_runtime_state _gil;
0181
0182
0183
0184
0185
0186
0187 struct codecs_state codecs;
0188
0189 PyConfig config;
0190 unsigned long feature_flags;
0191
0192 PyObject *dict;
0193
0194 PyObject *sysdict_copy;
0195 PyObject *builtins_copy;
0196
0197 _PyFrameEvalFunction eval_frame;
0198
0199 PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
0200
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
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;
0223 PyMutex weakref_locks[NUM_WEAKREF_LIST_LOCKS];
0224 #endif
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
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
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;
0268 Py_ssize_t sys_tracing_threads;
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
0276 _PyThreadStateImpl _initial_thread;
0277 Py_ssize_t _interactive_src_count;
0278
0279 _PyThreadStateImpl *threads_preallocated;
0280 };
0281
0282
0283
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
0306
0307 _Py_atomic_store_ulong_relaxed(&interp->_finalizing_id,
0308 tstate->thread_id);
0309 }
0310 }
0311
0312
0313
0314
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
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345 PyAPI_FUNC(int) _PyInterpreterState_GetConfigCopy(
0346 struct PyConfig *config);
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365 PyAPI_FUNC(int) _PyInterpreterState_SetConfig(
0366 const struct PyConfig *config);
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379 #define Py_RTFLAGS_USE_MAIN_OBMALLOC (1UL << 5)
0380
0381
0382 #define Py_RTFLAGS_MULTI_INTERP_EXTENSIONS (1UL << 8)
0383
0384
0385 #define Py_RTFLAGS_THREADS (1UL << 10)
0386
0387
0388 #define Py_RTFLAGS_DAEMON_THREADS (1UL << 11)
0389
0390
0391 #define Py_RTFLAGS_FORK (1UL << 15)
0392
0393
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 \
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