pycore_pystate.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #ifndef Py_INTERNAL_PYSTATE_H
  2. #define Py_INTERNAL_PYSTATE_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 "cpython/initconfig.h"
  10. #include "fileobject.h"
  11. #include "pystate.h"
  12. #include "pythread.h"
  13. #include "sysmodule.h"
  14. #include "pycore_gil.h" /* _gil_runtime_state */
  15. #include "pycore_pathconfig.h"
  16. #include "pycore_pymem.h"
  17. #include "pycore_warnings.h"
  18. /* ceval state */
  19. struct _pending_calls {
  20. int finishing;
  21. PyThread_type_lock lock;
  22. /* Request for running pending calls. */
  23. _Py_atomic_int calls_to_do;
  24. /* Request for looking at the `async_exc` field of the current
  25. thread state.
  26. Guarded by the GIL. */
  27. int async_exc;
  28. #define NPENDINGCALLS 32
  29. struct {
  30. int (*func)(void *);
  31. void *arg;
  32. } calls[NPENDINGCALLS];
  33. int first;
  34. int last;
  35. };
  36. struct _ceval_runtime_state {
  37. int recursion_limit;
  38. /* Records whether tracing is on for any thread. Counts the number
  39. of threads for which tstate->c_tracefunc is non-NULL, so if the
  40. value is 0, we know we don't have to check this thread's
  41. c_tracefunc. This speeds up the if statement in
  42. PyEval_EvalFrameEx() after fast_next_opcode. */
  43. int tracing_possible;
  44. /* This single variable consolidates all requests to break out of
  45. the fast path in the eval loop. */
  46. _Py_atomic_int eval_breaker;
  47. /* Request for dropping the GIL */
  48. _Py_atomic_int gil_drop_request;
  49. struct _pending_calls pending;
  50. /* Request for checking signals. */
  51. _Py_atomic_int signals_pending;
  52. struct _gil_runtime_state gil;
  53. };
  54. /* interpreter state */
  55. typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
  56. // The PyInterpreterState typedef is in Include/pystate.h.
  57. struct _is {
  58. struct _is *next;
  59. struct _ts *tstate_head;
  60. int64_t id;
  61. int64_t id_refcount;
  62. int requires_idref;
  63. PyThread_type_lock id_mutex;
  64. int finalizing;
  65. PyObject *modules;
  66. PyObject *modules_by_index;
  67. PyObject *sysdict;
  68. PyObject *builtins;
  69. PyObject *importlib;
  70. /* Used in Python/sysmodule.c. */
  71. int check_interval;
  72. /* Used in Modules/_threadmodule.c. */
  73. long num_threads;
  74. /* Support for runtime thread stack size tuning.
  75. A value of 0 means using the platform's default stack size
  76. or the size specified by the THREAD_STACK_SIZE macro. */
  77. /* Used in Python/thread.c. */
  78. size_t pythread_stacksize;
  79. PyObject *codec_search_path;
  80. PyObject *codec_search_cache;
  81. PyObject *codec_error_registry;
  82. int codecs_initialized;
  83. /* fs_codec.encoding is initialized to NULL.
  84. Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */
  85. struct {
  86. char *encoding; /* Filesystem encoding (encoded to UTF-8) */
  87. char *errors; /* Filesystem errors (encoded to UTF-8) */
  88. _Py_error_handler error_handler;
  89. } fs_codec;
  90. PyConfig config;
  91. #ifdef HAVE_DLOPEN
  92. int dlopenflags;
  93. #endif
  94. PyObject *dict; /* Stores per-interpreter state */
  95. PyObject *builtins_copy;
  96. PyObject *import_func;
  97. /* Initialized to PyEval_EvalFrameDefault(). */
  98. _PyFrameEvalFunction eval_frame;
  99. Py_ssize_t co_extra_user_count;
  100. freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
  101. #ifdef HAVE_FORK
  102. PyObject *before_forkers;
  103. PyObject *after_forkers_parent;
  104. PyObject *after_forkers_child;
  105. #endif
  106. /* AtExit module */
  107. void (*pyexitfunc)(PyObject *);
  108. PyObject *pyexitmodule;
  109. uint64_t tstate_next_unique_id;
  110. struct _warnings_runtime_state warnings;
  111. PyObject *audit_hooks;
  112. };
  113. PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(PY_INT64_T);
  114. PyAPI_FUNC(int) _PyInterpreterState_IDInitref(struct _is *);
  115. PyAPI_FUNC(void) _PyInterpreterState_IDIncref(struct _is *);
  116. PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *);
  117. /* cross-interpreter data registry */
  118. /* For now we use a global registry of shareable classes. An
  119. alternative would be to add a tp_* slot for a class's
  120. crossinterpdatafunc. It would be simpler and more efficient. */
  121. struct _xidregitem;
  122. struct _xidregitem {
  123. PyTypeObject *cls;
  124. crossinterpdatafunc getdata;
  125. struct _xidregitem *next;
  126. };
  127. /* runtime audit hook state */
  128. typedef struct _Py_AuditHookEntry {
  129. struct _Py_AuditHookEntry *next;
  130. Py_AuditHookFunction hookCFunction;
  131. void *userData;
  132. } _Py_AuditHookEntry;
  133. /* GIL state */
  134. struct _gilstate_runtime_state {
  135. int check_enabled;
  136. /* Assuming the current thread holds the GIL, this is the
  137. PyThreadState for the current thread. */
  138. _Py_atomic_address tstate_current;
  139. PyThreadFrameGetter getframe;
  140. /* The single PyInterpreterState used by this process'
  141. GILState implementation
  142. */
  143. /* TODO: Given interp_main, it may be possible to kill this ref */
  144. PyInterpreterState *autoInterpreterState;
  145. Py_tss_t autoTSSkey;
  146. };
  147. /* hook for PyEval_GetFrame(), requested for Psyco */
  148. #define _PyThreadState_GetFrame _PyRuntime.gilstate.getframe
  149. /* Issue #26558: Flag to disable PyGILState_Check().
  150. If set to non-zero, PyGILState_Check() always return 1. */
  151. #define _PyGILState_check_enabled _PyRuntime.gilstate.check_enabled
  152. /* Full Python runtime state */
  153. typedef struct pyruntimestate {
  154. /* Is running Py_PreInitialize()? */
  155. int preinitializing;
  156. /* Is Python preinitialized? Set to 1 by Py_PreInitialize() */
  157. int preinitialized;
  158. /* Is Python core initialized? Set to 1 by _Py_InitializeCore() */
  159. int core_initialized;
  160. /* Is Python fully initialized? Set to 1 by Py_Initialize() */
  161. int initialized;
  162. /* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
  163. is called again. */
  164. PyThreadState *finalizing;
  165. struct pyinterpreters {
  166. PyThread_type_lock mutex;
  167. PyInterpreterState *head;
  168. PyInterpreterState *main;
  169. /* _next_interp_id is an auto-numbered sequence of small
  170. integers. It gets initialized in _PyInterpreterState_Init(),
  171. which is called in Py_Initialize(), and used in
  172. PyInterpreterState_New(). A negative interpreter ID
  173. indicates an error occurred. The main interpreter will
  174. always have an ID of 0. Overflow results in a RuntimeError.
  175. If that becomes a problem later then we can adjust, e.g. by
  176. using a Python int. */
  177. int64_t next_id;
  178. } interpreters;
  179. // XXX Remove this field once we have a tp_* slot.
  180. struct _xidregistry {
  181. PyThread_type_lock mutex;
  182. struct _xidregitem *head;
  183. } xidregistry;
  184. unsigned long main_thread;
  185. #define NEXITFUNCS 32
  186. void (*exitfuncs[NEXITFUNCS])(void);
  187. int nexitfuncs;
  188. struct _gc_runtime_state gc;
  189. struct _ceval_runtime_state ceval;
  190. struct _gilstate_runtime_state gilstate;
  191. PyPreConfig preconfig;
  192. Py_OpenCodeHookFunction open_code_hook;
  193. void *open_code_userdata;
  194. _Py_AuditHookEntry *audit_hook_head;
  195. // XXX Consolidate globals found via the check-c-globals script.
  196. } _PyRuntimeState;
  197. #define _PyRuntimeState_INIT \
  198. {.preinitialized = 0, .core_initialized = 0, .initialized = 0}
  199. /* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */
  200. PyAPI_DATA(_PyRuntimeState) _PyRuntime;
  201. PyAPI_FUNC(PyStatus) _PyRuntimeState_Init(_PyRuntimeState *runtime);
  202. PyAPI_FUNC(void) _PyRuntimeState_Fini(_PyRuntimeState *runtime);
  203. PyAPI_FUNC(void) _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime);
  204. /* Initialize _PyRuntimeState.
  205. Return NULL on success, or return an error message on failure. */
  206. PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void);
  207. PyAPI_FUNC(void) _PyRuntime_Finalize(void);
  208. #define _Py_CURRENTLY_FINALIZING(runtime, tstate) \
  209. (runtime->finalizing == tstate)
  210. /* Variable and macro for in-line access to current thread
  211. and interpreter state */
  212. #define _PyRuntimeState_GetThreadState(runtime) \
  213. ((PyThreadState*)_Py_atomic_load_relaxed(&(runtime)->gilstate.tstate_current))
  214. /* Get the current Python thread state.
  215. Efficient macro reading directly the 'gilstate.tstate_current' atomic
  216. variable. The macro is unsafe: it does not check for error and it can
  217. return NULL.
  218. The caller must hold the GIL.
  219. See also PyThreadState_Get() and PyThreadState_GET(). */
  220. #define _PyThreadState_GET() _PyRuntimeState_GetThreadState(&_PyRuntime)
  221. /* Redefine PyThreadState_GET() as an alias to _PyThreadState_GET() */
  222. #undef PyThreadState_GET
  223. #define PyThreadState_GET() _PyThreadState_GET()
  224. /* Get the current interpreter state.
  225. The macro is unsafe: it does not check for error and it can return NULL.
  226. The caller must hold the GIL.
  227. See also _PyInterpreterState_Get()
  228. and _PyGILState_GetInterpreterStateUnsafe(). */
  229. #define _PyInterpreterState_GET_UNSAFE() (_PyThreadState_GET()->interp)
  230. /* Other */
  231. PyAPI_FUNC(void) _PyThreadState_Init(
  232. _PyRuntimeState *runtime,
  233. PyThreadState *tstate);
  234. PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
  235. _PyRuntimeState *runtime,
  236. PyThreadState *tstate);
  237. PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
  238. struct _gilstate_runtime_state *gilstate,
  239. PyThreadState *newts);
  240. PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime);
  241. PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
  242. PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime);
  243. #ifdef __cplusplus
  244. }
  245. #endif
  246. #endif /* !Py_INTERNAL_PYSTATE_H */