Debian Codegraph @

Include/internal/pycore_interp_structs.h

raw ◀ ▶
1 Debian patch touch this file
1 /* This file contains the struct definitions for interpreter state
2 * and other necessary structs */
3
4 #ifndef Py_INTERNAL_INTERP_STRUCTS_H
5 #define Py_INTERNAL_INTERP_STRUCTS_H
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9
10 #include "pycore_ast_state.h" // struct ast_state
11 #include "pycore_llist.h" // struct llist_node
12 #include "pycore_opcode_utils.h" // NUM_COMMON_CONSTANTS
13 #include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR
14 #include "pycore_structs.h" // PyHamtObject
15 #include "pycore_tstate.h" // _PyThreadStateImpl
16 #include "pycore_typedefs.h" // _PyRuntimeState
17
18
19 #define CODE_MAX_WATCHERS 8
20 #define CONTEXT_MAX_WATCHERS 8
21 #define FUNC_MAX_WATCHERS 8
22 #define TYPE_MAX_WATCHERS 8
23
24
25 #ifdef Py_GIL_DISABLED
26 // This should be prime but otherwise the choice is arbitrary. A larger value
27 // increases concurrency at the expense of memory.
28 # define NUM_WEAKREF_LIST_LOCKS 127
29 #endif
30
31 typedef int (*_Py_pending_call_func)(void *);
32
33 struct _pending_call {
34 _Py_pending_call_func func;
35 void *arg;
36 int flags;
37 };
38
39 #define PENDINGCALLSARRAYSIZE 300
40
41 struct _pending_calls {
42 PyThreadState *handling_thread;
43 PyMutex mutex;
44 /* Request for running pending calls. */
45 int32_t npending;
46 /* The maximum allowed number of pending calls.
47 If the queue fills up to this point then _PyEval_AddPendingCall()
48 will return _Py_ADD_PENDING_FULL. */
49 int32_t max;
50 /* We don't want a flood of pending calls to interrupt any one thread
51 for too long, so we keep a limit on the number handled per pass.
52 A value of 0 means there is no limit (other than the maximum
53 size of the list of pending calls). */
54 int32_t maxloop;
55 struct _pending_call calls[PENDINGCALLSARRAYSIZE];
56 int first;
57 int next;
58 };
59
60 typedef enum {
61 PERF_STATUS_FAILED = -1, // Perf trampoline is in an invalid state
62 PERF_STATUS_NO_INIT = 0, // Perf trampoline is not initialized
63 PERF_STATUS_OK = 1, // Perf trampoline is ready to be executed
64 } perf_status_t;
65
66 #ifdef PY_HAVE_PERF_TRAMPOLINE
67 struct code_arena_st;
68
69 struct trampoline_api_st {
70 void* (*init_state)(void);
71 void (*write_state)(void* state, const void *code_addr,
72 unsigned int code_size, PyCodeObject* code);
73 int (*free_state)(void* state);
74 void *state;
75 Py_ssize_t code_padding;
76 };
77 #endif
78
79
80 struct _ceval_runtime_state {
81 struct {
82 #ifdef PY_HAVE_PERF_TRAMPOLINE
83 perf_status_t status;
84 int perf_trampoline_type;
85 Py_ssize_t extra_code_index;
86 struct code_arena_st *code_arena;
87 struct trampoline_api_st trampoline_api;
88 FILE *map_file;
89 Py_ssize_t persist_after_fork;
90 _PyFrameEvalFunction prev_eval_frame;
91 Py_ssize_t trampoline_refcount;
92 int code_watcher_id;
93 #else
94 int _not_used;
95 #endif
96 } perf;
97 /* Pending calls to be made only on the main thread. */
98 // The signal machinery falls back on this
99 // so it must be especially stable and efficient.
100 // For example, we use a preallocated array
101 // for the list of pending calls.
102 struct _pending_calls pending_mainthread;
103 PyMutex unused_sys_trace_profile_mutex; // kept for ABI compatibility
104 };
105
106
107 struct _ceval_state {
108 /* This variable holds the global instrumentation version. When a thread is
109 running, this value is overlaid onto PyThreadState.eval_breaker so that
110 changes in the instrumentation version will trigger the eval breaker. */
111 uintptr_t instrumentation_version;
112 int recursion_limit;
113 struct _gil_runtime_state *gil;
114 int own_gil;
115 struct _pending_calls pending;
116 };
117
118
119 //###############
120 // runtime atexit
121
122 typedef void (*atexit_callbackfunc)(void);
123
124 struct _atexit_runtime_state {
125 PyMutex mutex;
126 #define NEXITFUNCS 32
127 atexit_callbackfunc callbacks[NEXITFUNCS];
128 int ncallbacks;
129 };
130
131
132 //###################
133 // interpreter atexit
134
135 typedef void (*atexit_datacallbackfunc)(void *);
136
137 typedef struct atexit_callback {
138 atexit_datacallbackfunc func;
139 void *data;
140 struct atexit_callback *next;
141 } atexit_callback;
142
143 struct atexit_state {
144 #ifdef Py_GIL_DISABLED
145 PyMutex ll_callbacks_lock;
146 #endif
147 atexit_callback *ll_callbacks;
148
149 // XXX The rest of the state could be moved to the atexit module state
150 // and a low-level callback added for it during module exec.
151 // For the moment we leave it here.
152
153 // List containing tuples with callback information.
154 // e.g. [(func, args, kwargs), ...]
155 PyObject *callbacks;
156 };
157
158
159 /****** Garbage collector **********/
160
161 /* GC information is stored BEFORE the object structure. */
162 typedef struct {
163 // Tagged pointer to next object in the list.
164 // 0 means the object is not tracked
•165 _Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, uintptr_t) _gc_next;
166
167 // Tagged pointer to previous object in the list.
168 // Lowest two bits are used for flags documented later.
•169 // Those bits are made available by the struct's minimum alignment.
170 uintptr_t _gc_prev;
171 } PyGC_Head;
172
173 #define _PyGC_Head_UNUSED PyGC_Head
174
175 struct gc_generation {
176 PyGC_Head head;
177 int threshold; /* collection threshold */
178 int count; /* count of allocations or collections of younger
179 generations */
180 };
181
182 struct gc_collection_stats {
183 /* number of collected objects */
184 Py_ssize_t collected;
185 /* total number of uncollectable objects (put into gc.garbage) */
186 Py_ssize_t uncollectable;
187 };
188
189 /* Running stats per generation */
190 struct gc_generation_stats {
191 /* total number of collections */
192 Py_ssize_t collections;
193 /* total number of collected objects */
194 Py_ssize_t collected;
195 /* total number of uncollectable objects (put into gc.garbage) */
196 Py_ssize_t uncollectable;
197 };
198
199 /* If we change this, we need to change the default value in the
200 signature of gc.collect. */
201 #define NUM_GENERATIONS 3
202
203 struct _gc_runtime_state {
204 /* List of objects that still need to be cleaned up, singly linked
205 * via their gc headers' gc_prev pointers. */
206 PyObject *trash_delete_later;
207 /* Current call-stack depth of tp_dealloc calls. */
208 int trash_delete_nesting;
209
210 /* Is automatic collection enabled? */
211 int enabled;
212 int debug;
213 /* linked lists of container objects */
214 #ifndef Py_GIL_DISABLED
215 struct gc_generation generations[NUM_GENERATIONS];
216 #else
217 struct gc_generation young;
218 struct gc_generation old[2];
219 #endif
220
221 /* a permanent generation which won't be collected */
222 struct gc_generation permanent_generation;
223 struct gc_generation_stats generation_stats[NUM_GENERATIONS];
224 /* true if we are currently running the collector */
225 int collecting;
226 /* list of uncollectable objects */
227 PyObject *garbage;
228 /* a list of callbacks to be invoked when collection is performed */
229 PyObject *callbacks;
230
231 /* The number of live objects. */
232 Py_ssize_t heap_size;
233
234 /* dummy members to preserve other offsets */
235 Py_ssize_t dummy1; /* was work_to_do */
236 int dummy2; /* was visited_space */
237 int dummy3; /* was phase */
238
239 /* This is the number of objects that survived the last full
240 collection. It approximates the number of long lived objects
241 tracked by the GC.
242
243 (by "full collection", we mean a collection of the oldest
244 generation). */
245 Py_ssize_t long_lived_total;
246 /* This is the number of objects that survived all "non-full"
247 collections, and are awaiting to undergo a full collection for
248 the first time. */
249 Py_ssize_t long_lived_pending;
250
251 #ifdef Py_GIL_DISABLED
252 /* True if gc.freeze() has been used. */
253 int freeze_active;
254
255 /* Memory usage of the process (RSS + swap) after last GC. */
256 Py_ssize_t last_mem;
257
258 /* This accumulates the new object count whenever collection is deferred
259 due to the RSS increase condition not being meet. Reset on collection. */
260 Py_ssize_t deferred_count;
261
262 /* Mutex held for gc_should_collect_mem_usage(). */
263 PyMutex mutex;
264 #else
265 PyGC_Head *generation0;
266 #endif
267 };
268
269 #ifndef Py_GIL_DISABLED
270 #define GC_GENERATION_INIT \
271 .generations = { \
272 { .threshold = 2000, }, \
273 { .threshold = 10, }, \
274 { .threshold = 10, }, \
275 }, \
276 .heap_size = 0,
277 #else
278 #define GC_GENERATION_INIT \
279 .young = { .threshold = 2000, }, \
280 .old = { \
281 { .threshold = 10, }, \
282 { .threshold = 10, }, \
283 },
284 #endif
285
286 #include "pycore_gil.h" // struct _gil_runtime_state
287
288 /**** Import ********/
289
290 struct _import_runtime_state {
291 /* The builtin modules (defined in config.c). */
292 struct _inittab *inittab;
293 /* The most recent value assigned to a PyModuleDef.m_base.m_index.
294 This is incremented each time PyModuleDef_Init() is called,
295 which is just about every time an extension module is imported.
296 See PyInterpreterState.modules_by_index for more info. */
297 Py_ssize_t last_module_index;
298 struct {
299 /* A lock to guard the cache. */
300 PyMutex mutex;
301 /* The actual cache of (filename, name, PyModuleDef) for modules.
302 Only legacy (single-phase init) extension modules are added
303 and only if they support multiple initialization (m_size >= 0)
304 or are imported in the main interpreter.
305 This is initialized lazily in fix_up_extension() in import.c.
306 Modules are added there and looked up in _imp.find_extension(). */
307 struct _Py_hashtable_t *hashtable;
308 } extensions;
309 /* Package context -- the full module name for package imports */
310 const char * pkgcontext;
311 };
312
313 struct _import_state {
314 /* cached sys.modules dictionary */
315 PyObject *modules;
316 /* This is the list of module objects for all legacy (single-phase init)
317 extension modules ever loaded in this process (i.e. imported
318 in this interpreter or in any other). Py_None stands in for
319 modules that haven't actually been imported in this interpreter.
320
321 A module's index (PyModuleDef.m_base.m_index) is used to look up
322 the corresponding module object for this interpreter, if any.
323 (See PyState_FindModule().) When any extension module
324 is initialized during import, its moduledef gets initialized by
325 PyModuleDef_Init(), and the first time that happens for each
326 PyModuleDef, its index gets set to the current value of
327 a global counter (see _PyRuntimeState.imports.last_module_index).
328 The entry for that index in this interpreter remains unset until
329 the module is actually imported here. (Py_None is used as
330 a placeholder.) Note that multi-phase init modules always get
331 an index for which there will never be a module set.
332
333 This is initialized lazily in PyState_AddModule(), which is also
334 where modules get added. */
335 PyObject *modules_by_index;
336 /* importlib module._bootstrap */
337 PyObject *importlib;
338 /* override for config->use_frozen_modules (for tests)
339 (-1: "off", 1: "on", 0: no override) */
340 int override_frozen_modules;
341 int override_multi_interp_extensions_check;
342 #ifdef HAVE_DLOPEN
343 int dlopenflags;
344 #endif
345 PyObject *import_func;
346 /* The global import lock. */
347 _PyRecursiveMutex lock;
348 /* diagnostic info in PyImport_ImportModuleLevelObject() */
349 struct {
350 int import_level;
351 PyTime_t accumulated;
352 int header;
353 } find_and_load;
354 };
355
356
357
358 /********** Interpreter state **************/
359
360 #include "pycore_object_state.h" // struct _py_object_state
361 #include "pycore_crossinterp.h" // _PyXI_state_t
362
363
364 struct _Py_long_state {
365 int max_str_digits;
366 };
367
368 struct codecs_state {
369 // A list of callable objects used to search for codecs.
370 PyObject *search_path;
371
372 // A dict mapping codec names to codecs returned from a callable in
373 // search_path.
374 PyObject *search_cache;
375
376 // A dict mapping error handling strategies to functions to implement them.
377 PyObject *error_registry;
378
379 #ifdef Py_GIL_DISABLED
380 // Used to safely delete a specific item from search_path.
381 PyMutex search_path_mutex;
382 #endif
383
384 // Whether or not the rest of the state is initialized.
385 int initialized;
386 };
387
388 // Support for stop-the-world events. This exists in both the PyRuntime struct
389 // for global pauses and in each PyInterpreterState for per-interpreter pauses.
390 struct _stoptheworld_state {
391 PyMutex mutex; // Serializes stop-the-world attempts.
392
393 // NOTE: The below fields are protected by HEAD_LOCK(runtime), not by the
394 // above mutex.
395 bool requested; // Set when a pause is requested.
396 bool world_stopped; // Set when the world is stopped.
397 bool is_global; // Set when contained in PyRuntime struct.
398
399 PyEvent stop_event; // Set when thread_countdown reaches zero.
400 Py_ssize_t thread_countdown; // Number of threads that must pause.
401
402 PyThreadState *requester; // Thread that requested the pause (may be NULL).
403 };
404
405 /* Tracks some rare events per-interpreter, used by the optimizer to turn on/off
406 specific optimizations. */
407 typedef struct _rare_events {
408 /* Setting an object's class, obj.__class__ = ... */
409 uint8_t set_class;
410 /* Setting the bases of a class, cls.__bases__ = ... */
411 uint8_t set_bases;
412 /* Setting the PEP 523 frame eval function, _PyInterpreterState_SetFrameEvalFunc() */
413 uint8_t set_eval_frame_func;
414 /* Modifying the builtins, __builtins__.__dict__[var] = ... */
415 uint8_t builtin_dict;
416 /* Modifying a function, e.g. func.__defaults__ = ..., etc. */
417 uint8_t func_modification;
418 } _rare_events;
419
420 struct
421 Bigint {
422 struct Bigint *next;
423 int k, maxwds, sign, wds;
424 uint32_t x[1];
425 };
426
427 #if defined(Py_USING_MEMORY_DEBUGGER) || _PY_SHORT_FLOAT_REPR == 0
428
429 struct _dtoa_state {
430 int _not_used;
431 };
432
433 #else // !Py_USING_MEMORY_DEBUGGER && _PY_SHORT_FLOAT_REPR != 0
434
435 /* The size of the Bigint freelist */
436 #define Bigint_Kmax 7
437
438 /* The size of the cached powers of 5 array */
439 #define Bigint_Pow5size 8
440
441 #ifndef PRIVATE_MEM
442 #define PRIVATE_MEM 2304
443 #endif
444 #define Bigint_PREALLOC_SIZE \
445 ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
446
447 struct _dtoa_state {
448 // p5s is an array of powers of 5 of the form:
449 // 5**(2**(i+2)) for 0 <= i < Bigint_Pow5size
450 struct Bigint *p5s[Bigint_Pow5size];
451 // XXX This should be freed during runtime fini.
452 struct Bigint *freelist[Bigint_Kmax+1];
453 double preallocated[Bigint_PREALLOC_SIZE];
454 double *preallocated_next;
455 };
456
457 #endif // !Py_USING_MEMORY_DEBUGGER
458
459 struct _py_code_state {
460 PyMutex mutex;
461 // Interned constants from code objects. Used by the free-threaded build.
462 struct _Py_hashtable_t *constants;
463 };
464
465 #define FUNC_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */
466
467 struct _func_version_cache_item {
468 PyFunctionObject *func;
469 PyObject *code;
470 };
471
472 struct _py_func_state {
473 #ifdef Py_GIL_DISABLED
474 // Protects next_version
475 PyMutex mutex;
476 #endif
477
478 uint32_t next_version;
479 // Borrowed references to function and code objects whose
480 // func_version % FUNC_VERSION_CACHE_SIZE
481 // once was equal to the index in the table.
482 // They are cleared when the function or code object is deallocated.
483 struct _func_version_cache_item func_version_cache[FUNC_VERSION_CACHE_SIZE];
484 };
485
486 #include "pycore_dict_state.h" // struct _Py_dict_state
487 #include "pycore_exceptions.h" // struct _Py_exc_state
488
489
490 /****** type state *********/
491
492 /* For now we hard-code this to a value for which we are confident
493 all the static builtin types will fit (for all builds). */
494 #define _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES 200
495 #define _Py_MAX_MANAGED_STATIC_EXT_TYPES 10
496 #define _Py_MAX_MANAGED_STATIC_TYPES \
497 (_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES + _Py_MAX_MANAGED_STATIC_EXT_TYPES)
498
499 struct _types_runtime_state {
500 /* Used to set PyTypeObject.tp_version_tag for core static types. */
501 // bpo-42745: next_version_tag remains shared by all interpreters
502 // because of static types.
503 unsigned int next_version_tag;
504
505 struct {
506 struct {
507 PyTypeObject *type;
508 int64_t interp_count;
509 } types[_Py_MAX_MANAGED_STATIC_TYPES];
510 } managed_static;
511 };
512
513
514 // Type attribute lookup cache: speed up attribute and method lookups,
515 // see _PyType_Lookup().
516 struct type_cache_entry {
517 unsigned int version; // initialized from type->tp_version_tag
518 #ifdef Py_GIL_DISABLED
519 _PySeqLock sequence;
520 #endif
521 PyObject *name; // reference to exactly a str or None
522 PyObject *value; // borrowed reference or NULL
523 };
524
525 #define MCACHE_SIZE_EXP 12
526
527 struct type_cache {
528 struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP];
529 };
530
531 typedef struct {
532 PyTypeObject *type;
533 int isbuiltin;
534 int readying;
535 int ready;
536 // XXX tp_dict can probably be statically allocated,
537 // instead of dynamically and stored on the interpreter.
538 PyObject *tp_dict;
539 PyObject *tp_subclasses;
540 /* We never clean up weakrefs for static builtin types since
541 they will effectively never get triggered. However, there
542 are also some diagnostic uses for the list of weakrefs,
543 so we still keep it. */
544 PyObject *tp_weaklist;
545 } managed_static_type_state;
546
547 #define TYPE_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */
548
549 struct types_state {
550 /* Used to set PyTypeObject.tp_version_tag.
551 It starts at _Py_MAX_GLOBAL_TYPE_VERSION_TAG + 1,
552 where all those lower numbers are used for core static types. */
553 unsigned int next_version_tag;
554
555 struct type_cache type_cache;
556
557 /* Every static builtin type is initialized for each interpreter
558 during its own initialization, including for the main interpreter
559 during global runtime initialization. This is done by calling
560 _PyStaticType_InitBuiltin().
561
562 The first time a static builtin type is initialized, all the
563 normal PyType_Ready() stuff happens. The only difference from
564 normal is that there are three PyTypeObject fields holding
565 objects which are stored here (on PyInterpreterState) rather
566 than in the corresponding PyTypeObject fields. Those are:
567 tp_dict (cls.__dict__), tp_subclasses (cls.__subclasses__),
568 and tp_weaklist.
569
570 When a subinterpreter is initialized, each static builtin type
571 is still initialized, but only the interpreter-specific portion,
572 namely those three objects.
573
574 Those objects are stored in the PyInterpreterState.types.builtins
575 array, at the index corresponding to each specific static builtin
576 type. That index (a size_t value) is stored in the tp_subclasses
577 field. For static builtin types, we re-purposed the now-unused
578 tp_subclasses to avoid adding another field to PyTypeObject.
579 In all other cases tp_subclasses holds a dict like before.
580 (The field was previously defined as PyObject*, but is now void*
581 to reflect its dual use.)
582
583 The index for each static builtin type isn't statically assigned.
584 Instead it is calculated the first time a type is initialized
585 (by the main interpreter). The index matches the order in which
586 the type was initialized relative to the others. The actual
587 value comes from the current value of num_builtins_initialized,
588 as each type is initialized for the main interpreter.
589
590 num_builtins_initialized is incremented once for each static
591 builtin type. Once initialization is over for a subinterpreter,
592 the value will be the same as for all other interpreters. */
593 struct {
594 size_t num_initialized;
595 managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES];
596 } builtins;
597 /* We apply a similar strategy for managed extension modules. */
598 struct {
599 size_t num_initialized;
600 size_t next_index;
601 managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_EXT_TYPES];
602 } for_extensions;
603 PyMutex mutex;
604
605 // Borrowed references to type objects whose
606 // tp_version_tag % TYPE_VERSION_CACHE_SIZE
607 // once was equal to the index in the table.
608 // They are cleared when the type object is deallocated.
609 PyTypeObject *type_version_cache[TYPE_VERSION_CACHE_SIZE];
610 };
611
612 struct _warnings_runtime_state {
613 /* Both 'filters' and 'onceregistry' can be set in warnings.py;
614 get_warnings_attr() will reset these variables accordingly. */
615 PyObject *filters; /* List */
616 PyObject *once_registry; /* Dict */
617 PyObject *default_action; /* String */
618 _PyRecursiveMutex lock;
619 long filters_version;
620 PyObject *context;
621 };
622
623 struct _Py_mem_interp_free_queue {
624 int has_work; // true if the queue is not empty
625 PyMutex mutex; // protects the queue
626 struct llist_node head; // queue of _mem_work_chunk items
627 };
628
629
630 /****** Unicode state *********/
631
632 typedef enum {
633 _Py_ERROR_UNKNOWN=0,
634 _Py_ERROR_STRICT,
635 _Py_ERROR_SURROGATEESCAPE,
636 _Py_ERROR_REPLACE,
637 _Py_ERROR_IGNORE,
638 _Py_ERROR_BACKSLASHREPLACE,
639 _Py_ERROR_SURROGATEPASS,
640 _Py_ERROR_XMLCHARREFREPLACE,
641 _Py_ERROR_OTHER
642 } _Py_error_handler;
643
644 struct _Py_unicode_runtime_ids {
645 PyMutex mutex;
646 // next_index value must be preserved when Py_Initialize()/Py_Finalize()
647 // is called multiple times: see _PyUnicode_FromId() implementation.
648 Py_ssize_t next_index;
649 };
650
651 struct _Py_unicode_runtime_state {
652 struct _Py_unicode_runtime_ids ids;
653 };
654
655 /* fs_codec.encoding is initialized to NULL.
656 Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */
657 struct _Py_unicode_fs_codec {
658 char *encoding; // Filesystem encoding (encoded to UTF-8)
659 int utf8; // encoding=="utf-8"?
660 char *errors; // Filesystem errors (encoded to UTF-8)
661 _Py_error_handler error_handler;
662 };
663
664 struct _Py_unicode_ids {
665 Py_ssize_t size;
666 PyObject **array;
667 };
668
669 #include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
670
671 struct _Py_unicode_state {
672 struct _Py_unicode_fs_codec fs_codec;
673
674 _PyUnicode_Name_CAPI *ucnhash_capi;
675
676 // Unicode identifiers (_Py_Identifier): see _PyUnicode_FromId()
677 struct _Py_unicode_ids ids;
678 };
679
680 // Borrowed references to common callables:
681 struct callable_cache {
682 PyObject *isinstance;
683 PyObject *len;
684 PyObject *list_append;
685 PyObject *object__getattribute__;
686 };
687
688 /* Length of array of slotdef pointers used to store slots with the
689 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
690 the same __name__, for any __name__. Since that's a static property, it is
691 appropriate to declare fixed-size arrays for this. */
692 #define MAX_EQUIV 10
693
694 typedef struct wrapperbase pytype_slotdef;
695
696
697 struct _Py_interp_cached_objects {
698 #ifdef Py_GIL_DISABLED
699 PyMutex interned_mutex;
700 #endif
701 PyObject *interned_strings;
702
703 /* object.__reduce__ */
704 PyObject *objreduce;
705 PyObject *type_slots_pname;
706 pytype_slotdef *type_slots_ptrs[MAX_EQUIV];
707
708 /* TypeVar and related types */
709 PyTypeObject *generic_type;
710 PyTypeObject *typevar_type;
711 PyTypeObject *typevartuple_type;
712 PyTypeObject *paramspec_type;
713 PyTypeObject *paramspecargs_type;
714 PyTypeObject *paramspeckwargs_type;
715 PyTypeObject *constevaluator_type;
716 };
717
718 struct _Py_interp_static_objects {
719 struct {
720 int _not_used;
721 // hamt_empty is here instead of global because of its weakreflist.
722 _PyGC_Head_UNUSED _hamt_empty_gc_not_used;
723 PyHamtObject hamt_empty;
724 PyBaseExceptionObject last_resort_memory_error;
725 } singletons;
726 };
727
728 #include "pycore_instruments.h" // PY_MONITORING_TOOL_IDS
729
730
731 #ifdef Py_GIL_DISABLED
732
733 // A min-heap of indices
734 typedef struct _PyIndexHeap {
735 int32_t *values;
736
737 // Number of items stored in values
738 Py_ssize_t size;
739
740 // Maximum number of items that can be stored in values
741 Py_ssize_t capacity;
742 } _PyIndexHeap;
743
744 // An unbounded pool of indices. Indices are allocated starting from 0. They
745 // may be released back to the pool once they are no longer in use.
746 typedef struct _PyIndexPool {
747 PyMutex mutex;
748
749 // Min heap of indices available for allocation
750 _PyIndexHeap free_indices;
751
752 // Next index to allocate if no free indices are available
753 int32_t next_index;
754
755 // Generation counter incremented on thread creation/destruction
756 // Used for TLBC cache invalidation in remote debugging
757 uint32_t tlbc_generation;
758 } _PyIndexPool;
759
760 typedef union _Py_unique_id_entry {
761 // Points to the next free type id, when part of the freelist
762 union _Py_unique_id_entry *next;
763
764 // Stores the object when the id is assigned
765 PyObject *obj;
766 } _Py_unique_id_entry;
767
768 struct _Py_unique_id_pool {
769 PyMutex mutex;
770
771 // combined table of object with allocated unique ids and unallocated ids.
772 _Py_unique_id_entry *table;
773
774 // Next entry to allocate inside 'table' or NULL
775 _Py_unique_id_entry *freelist;
776
777 // size of 'table'
778 Py_ssize_t size;
779 };
780
781 #endif
782
783
784 /* PyInterpreterState holds the global state for one of the runtime's
785 interpreters. Typically the initial (main) interpreter is the only one.
786
787 The PyInterpreterState typedef is in Include/pytypedefs.h.
788 */
789 struct _is {
790
791 /* This struct contains the eval_breaker,
792 * which is by far the hottest field in this struct
793 * and should be placed at the beginning. */
794 struct _ceval_state ceval;
795
796 // unused, kept for ABI compatibility
797 void *_malloced;
798
799 PyInterpreterState *next;
800
801 int64_t id;
802 Py_ssize_t id_refcount;
803 int requires_idref;
804
805 long _whence;
806
807 /* Has been initialized to a safe state.
808
809 In order to be effective, this must be set to 0 during or right
810 after allocation. */
811 int _initialized;
812 /* Has been fully initialized via pylifecycle.c. */
813 int _ready;
814 int finalizing;
815
816 uintptr_t last_restart_version;
817 struct pythreads {
818 uint64_t next_unique_id;
819 /* The linked list of threads, newest first. */
820 PyThreadState *head;
821 _PyThreadStateImpl *preallocated;
822 /* The thread currently executing in the __main__ module, if any. */
823 PyThreadState *main;
824 /* Used in Modules/_threadmodule.c. */
825 Py_ssize_t count;
826 /* Support for runtime thread stack size tuning.
827 A value of 0 means using the platform's default stack size
828 or the size specified by the THREAD_STACK_SIZE macro. */
829 /* Used in Python/thread.c. */
830 size_t stacksize;
831 } threads;
832
833 /* Reference to the _PyRuntime global variable. This field exists
834 to not have to pass runtime in addition to tstate to a function.
835 Get runtime from tstate: tstate->interp->runtime. */
836 _PyRuntimeState *runtime;
837
838 /* Set by Py_EndInterpreter().
839
840 Use _PyInterpreterState_GetFinalizing()
841 and _PyInterpreterState_SetFinalizing()
842 to access it, don't access it directly. */
843 PyThreadState* _finalizing;
844 /* The ID of the OS thread in which we are finalizing. */
845 unsigned long _finalizing_id;
846
847 struct _gc_runtime_state gc;
848
849 /* The following fields are here to avoid allocation during init.
850 The data is exposed through PyInterpreterState pointer fields.
851 These fields should not be accessed directly outside of init.
852
853 All other PyInterpreterState pointer fields are populated when
854 needed and default to NULL.
855
856 For now there are some exceptions to that rule, which require
857 allocation during init. These will be addressed on a case-by-case
858 basis. Also see _PyRuntimeState regarding the various mutex fields.
859 */
860
861 // Dictionary of the sys module
862 PyObject *sysdict;
863
864 // Dictionary of the builtins module
865 PyObject *builtins;
866
867 struct _import_state imports;
868
869 /* The per-interpreter GIL, which might not be used. */
870 struct _gil_runtime_state _gil;
871
872 uint64_t _code_object_generation;
873
874 /* ---------- IMPORTANT ---------------------------
875 The fields above this line are declared as early as
876 possible to facilitate out-of-process observability
877 tools. */
878
879 struct codecs_state codecs;
880
881 PyConfig config;
882 unsigned long feature_flags;
883
884 PyObject *dict; /* Stores per-interpreter state */
885
886 PyObject *sysdict_copy;
887 PyObject *builtins_copy;
888 // Initialized to _PyEval_EvalFrameDefault().
889 _PyFrameEvalFunction eval_frame;
890
891 PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
892 // One bit is set for each non-NULL entry in func_watchers
893 uint8_t active_func_watchers;
894
895 Py_ssize_t co_extra_user_count;
896 freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
897
898 /* cross-interpreter data and utils */
899 _PyXI_state_t xi;
900
901 #ifdef HAVE_FORK
902 PyObject *before_forkers;
903 PyObject *after_forkers_parent;
904 PyObject *after_forkers_child;
905 #endif
906
907 struct _warnings_runtime_state warnings;
908 struct atexit_state atexit;
909 struct _stoptheworld_state stoptheworld;
910 struct _qsbr_shared qsbr;
911
912 #if defined(Py_GIL_DISABLED)
913 struct _mimalloc_interp_state mimalloc;
914 struct _brc_state brc; // biased reference counting state
915 struct _Py_unique_id_pool unique_ids; // object ids for per-thread refcounts
916 PyMutex weakref_locks[NUM_WEAKREF_LIST_LOCKS];
917 _PyIndexPool tlbc_indices;
918 #endif
919 // Per-interpreter list of tasks, any lingering tasks from thread
920 // states gets added here and removed from the corresponding
921 // thread state's list.
922 struct llist_node asyncio_tasks_head;
923 // `asyncio_tasks_lock` is used when tasks are moved
924 // from thread's list to interpreter's list.
925 PyMutex asyncio_tasks_lock;
926
927 // Per-interpreter state for the obmalloc allocator. For the main
928 // interpreter and for all interpreters that don't have their
929 // own obmalloc state, this points to the static structure in
930 // obmalloc.c obmalloc_state_main. For other interpreters, it is
931 // heap allocated by _PyMem_init_obmalloc() and freed when the
932 // interpreter structure is freed. In the case of a heap allocated
933 // obmalloc state, it is not safe to hold on to or use memory after
934 // the interpreter is freed. The obmalloc state corresponding to
935 // that allocated memory is gone. See free_obmalloc_arenas() for
936 // more comments.
937 struct _obmalloc_state *obmalloc;
938
939 PyObject *audit_hooks;
940 PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
941 PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
942 PyContext_WatchCallback context_watchers[CONTEXT_MAX_WATCHERS];
943 // One bit is set for each non-NULL entry in code_watchers
944 uint8_t active_code_watchers;
945 uint8_t active_context_watchers;
946
947 struct _py_object_state object_state;
948 struct _Py_unicode_state unicode;
949 struct _Py_long_state long_state;
950 struct _dtoa_state dtoa;
951 struct _py_func_state func_state;
952 struct _py_code_state code_state;
953
954 struct _Py_dict_state dict_state;
955 struct _Py_exc_state exc_state;
956 struct _Py_mem_interp_free_queue mem_free_queue;
957
958 struct ast_state ast;
959 struct types_state types;
960 struct callable_cache callable_cache;
961 PyObject *common_consts[NUM_COMMON_CONSTANTS];
962 bool jit;
963 struct _PyExecutorObject *executor_list_head;
964 struct _PyExecutorObject *executor_deletion_list_head;
965 int executor_deletion_list_remaining_capacity;
966 size_t trace_run_counter;
967 _rare_events rare_events;
968 PyDict_WatchCallback builtins_dict_watcher;
969
970 _Py_GlobalMonitors monitors;
971 _PyOnceFlag sys_profile_once_flag;
972 _PyOnceFlag sys_trace_once_flag;
973 Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */
974 Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */
975 PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS];
976 PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS];
977 uintptr_t monitoring_tool_versions[PY_MONITORING_TOOL_IDS];
978
979 struct _Py_interp_cached_objects cached_objects;
980 struct _Py_interp_static_objects static_objects;
981
982 Py_ssize_t _interactive_src_count;
983
984 #if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)
985 uint64_t next_stackref;
986 _Py_hashtable_t *open_stackrefs_table;
987 # ifdef Py_STACKREF_CLOSE_DEBUG
988 _Py_hashtable_t *closed_stackrefs_table;
989 # endif
990 #endif
991
992 /* the initial PyInterpreterState.threads.head */
993 _PyThreadStateImpl _initial_thread;
994 // _initial_thread should be the last field of PyInterpreterState.
995 // See https://github.com/python/cpython/issues/127117.
996 };
997
998
999 #ifdef __cplusplus
1000 }
1001 #endif
1002 #endif /* Py_INTERNAL_INTERP_STRUCTS_H */