pycore_interp.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef Py_INTERNAL_INTERP_H
  2. #define Py_INTERNAL_INTERP_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. #include <stdbool.h>
  10. #include "pycore_ast_state.h" // struct ast_state
  11. #include "pycore_atexit.h" // struct atexit_state
  12. #include "pycore_atomic.h" // _Py_atomic_address
  13. #include "pycore_ceval_state.h" // struct _ceval_state
  14. #include "pycore_code.h" // struct callable_cache
  15. #include "pycore_context.h" // struct _Py_context_state
  16. #include "pycore_dict_state.h" // struct _Py_dict_state
  17. #include "pycore_dtoa.h" // struct _dtoa_state
  18. #include "pycore_exceptions.h" // struct _Py_exc_state
  19. #include "pycore_floatobject.h" // struct _Py_float_state
  20. #include "pycore_function.h" // FUNC_MAX_WATCHERS
  21. #include "pycore_genobject.h" // struct _Py_async_gen_state
  22. #include "pycore_gc.h" // struct _gc_runtime_state
  23. #include "pycore_global_objects.h" // struct _Py_interp_static_objects
  24. #include "pycore_import.h" // struct _import_state
  25. #include "pycore_instruments.h" // _PY_MONITORING_EVENTS
  26. #include "pycore_list.h" // struct _Py_list_state
  27. #include "pycore_object_state.h" // struct _py_object_state
  28. #include "pycore_obmalloc.h" // struct obmalloc_state
  29. #include "pycore_tuple.h" // struct _Py_tuple_state
  30. #include "pycore_typeobject.h" // struct type_cache
  31. #include "pycore_unicodeobject.h" // struct _Py_unicode_state
  32. #include "pycore_warnings.h" // struct _warnings_runtime_state
  33. struct _Py_long_state {
  34. int max_str_digits;
  35. };
  36. /* cross-interpreter data registry */
  37. /* For now we use a global registry of shareable classes. An
  38. alternative would be to add a tp_* slot for a class's
  39. crossinterpdatafunc. It would be simpler and more efficient. */
  40. struct _xidregitem;
  41. struct _xidregitem {
  42. struct _xidregitem *prev;
  43. struct _xidregitem *next;
  44. /* This can be a dangling pointer, but only if weakref is set. */
  45. PyTypeObject *cls;
  46. /* This is NULL for builtin types. */
  47. PyObject *weakref;
  48. size_t refcount;
  49. crossinterpdatafunc getdata;
  50. };
  51. struct _xidregistry {
  52. PyThread_type_lock mutex;
  53. struct _xidregitem *head;
  54. };
  55. /* interpreter state */
  56. /* PyInterpreterState holds the global state for one of the runtime's
  57. interpreters. Typically the initial (main) interpreter is the only one.
  58. The PyInterpreterState typedef is in Include/pytypedefs.h.
  59. */
  60. struct _is {
  61. PyInterpreterState *next;
  62. int64_t id;
  63. int64_t id_refcount;
  64. int requires_idref;
  65. PyThread_type_lock id_mutex;
  66. /* Has been initialized to a safe state.
  67. In order to be effective, this must be set to 0 during or right
  68. after allocation. */
  69. int _initialized;
  70. int finalizing;
  71. uint64_t monitoring_version;
  72. uint64_t last_restart_version;
  73. struct pythreads {
  74. uint64_t next_unique_id;
  75. /* The linked list of threads, newest first. */
  76. PyThreadState *head;
  77. /* Used in Modules/_threadmodule.c. */
  78. long count;
  79. /* Support for runtime thread stack size tuning.
  80. A value of 0 means using the platform's default stack size
  81. or the size specified by the THREAD_STACK_SIZE macro. */
  82. /* Used in Python/thread.c. */
  83. size_t stacksize;
  84. } threads;
  85. /* Reference to the _PyRuntime global variable. This field exists
  86. to not have to pass runtime in addition to tstate to a function.
  87. Get runtime from tstate: tstate->interp->runtime. */
  88. struct pyruntimestate *runtime;
  89. /* Set by Py_EndInterpreter().
  90. Use _PyInterpreterState_GetFinalizing()
  91. and _PyInterpreterState_SetFinalizing()
  92. to access it, don't access it directly. */
  93. _Py_atomic_address _finalizing;
  94. struct _gc_runtime_state gc;
  95. /* The following fields are here to avoid allocation during init.
  96. The data is exposed through PyInterpreterState pointer fields.
  97. These fields should not be accessed directly outside of init.
  98. All other PyInterpreterState pointer fields are populated when
  99. needed and default to NULL.
  100. For now there are some exceptions to that rule, which require
  101. allocation during init. These will be addressed on a case-by-case
  102. basis. Also see _PyRuntimeState regarding the various mutex fields.
  103. */
  104. // Dictionary of the sys module
  105. PyObject *sysdict;
  106. // Dictionary of the builtins module
  107. PyObject *builtins;
  108. struct _ceval_state ceval;
  109. struct _import_state imports;
  110. /* The per-interpreter GIL, which might not be used. */
  111. struct _gil_runtime_state _gil;
  112. /* ---------- IMPORTANT ---------------------------
  113. The fields above this line are declared as early as
  114. possible to facilitate out-of-process observability
  115. tools. */
  116. PyObject *codec_search_path;
  117. PyObject *codec_search_cache;
  118. PyObject *codec_error_registry;
  119. int codecs_initialized;
  120. PyConfig config;
  121. unsigned long feature_flags;
  122. PyObject *dict; /* Stores per-interpreter state */
  123. PyObject *sysdict_copy;
  124. PyObject *builtins_copy;
  125. // Initialized to _PyEval_EvalFrameDefault().
  126. _PyFrameEvalFunction eval_frame;
  127. PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
  128. // One bit is set for each non-NULL entry in func_watchers
  129. uint8_t active_func_watchers;
  130. Py_ssize_t co_extra_user_count;
  131. freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
  132. #ifdef HAVE_FORK
  133. PyObject *before_forkers;
  134. PyObject *after_forkers_parent;
  135. PyObject *after_forkers_child;
  136. #endif
  137. struct _warnings_runtime_state warnings;
  138. struct atexit_state atexit;
  139. struct _obmalloc_state obmalloc;
  140. PyObject *audit_hooks;
  141. PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
  142. PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
  143. // One bit is set for each non-NULL entry in code_watchers
  144. uint8_t active_code_watchers;
  145. struct _py_object_state object_state;
  146. struct _Py_unicode_state unicode;
  147. struct _Py_float_state float_state;
  148. struct _Py_long_state long_state;
  149. struct _dtoa_state dtoa;
  150. struct _py_func_state func_state;
  151. /* Using a cache is very effective since typically only a single slice is
  152. created and then deleted again. */
  153. PySliceObject *slice_cache;
  154. struct _Py_tuple_state tuple;
  155. struct _Py_list_state list;
  156. struct _Py_dict_state dict_state;
  157. struct _Py_async_gen_state async_gen;
  158. struct _Py_context_state context;
  159. struct _Py_exc_state exc_state;
  160. struct ast_state ast;
  161. struct types_state types;
  162. struct callable_cache callable_cache;
  163. PyCodeObject *interpreter_trampoline;
  164. _Py_GlobalMonitors monitors;
  165. bool f_opcode_trace_set;
  166. bool sys_profile_initialized;
  167. bool sys_trace_initialized;
  168. Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */
  169. Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */
  170. PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS];
  171. PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS];
  172. struct _Py_interp_cached_objects cached_objects;
  173. struct _Py_interp_static_objects static_objects;
  174. // XXX Remove this field once we have a tp_* slot.
  175. struct _xidregistry xidregistry;
  176. /* The thread currently executing in the __main__ module, if any. */
  177. PyThreadState *threads_main;
  178. /* The ID of the OS thread in which we are finalizing.
  179. We use _Py_atomic_address instead of adding a new _Py_atomic_ulong. */
  180. _Py_atomic_address _finalizing_id;
  181. /* the initial PyInterpreterState.threads.head */
  182. PyThreadState _initial_thread;
  183. };
  184. /* other API */
  185. extern void _PyInterpreterState_Clear(PyThreadState *tstate);
  186. static inline PyThreadState*
  187. _PyInterpreterState_GetFinalizing(PyInterpreterState *interp) {
  188. return (PyThreadState*)_Py_atomic_load_relaxed(&interp->_finalizing);
  189. }
  190. static inline unsigned long
  191. _PyInterpreterState_GetFinalizingID(PyInterpreterState *interp) {
  192. return (unsigned long)_Py_atomic_load_relaxed(&interp->_finalizing_id);
  193. }
  194. static inline void
  195. _PyInterpreterState_SetFinalizing(PyInterpreterState *interp, PyThreadState *tstate) {
  196. _Py_atomic_store_relaxed(&interp->_finalizing, (uintptr_t)tstate);
  197. if (tstate == NULL) {
  198. _Py_atomic_store_relaxed(&interp->_finalizing_id, 0);
  199. }
  200. else {
  201. // XXX Re-enable this assert once gh-109860 is fixed.
  202. //assert(tstate->thread_id == PyThread_get_thread_ident());
  203. _Py_atomic_store_relaxed(&interp->_finalizing_id,
  204. (uintptr_t)tstate->thread_id);
  205. }
  206. }
  207. PyAPI_FUNC(PyInterpreterState*) _PyInterpreterState_LookUpID(int64_t);
  208. PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
  209. PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
  210. PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #endif /* !Py_INTERNAL_INTERP_H */