pystate.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* Thread and interpreter state structures and their interfaces */
  2. #ifndef Py_PYSTATE_H
  3. #define Py_PYSTATE_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #include "pythread.h"
  8. /* This limitation is for performance and simplicity. If needed it can be
  9. removed (with effort). */
  10. #define MAX_CO_EXTRA_USERS 255
  11. /* State shared between threads */
  12. struct _ts; /* Forward */
  13. struct _is; /* Forward */
  14. struct _frame; /* Forward declaration for PyFrameObject. */
  15. #ifdef Py_LIMITED_API
  16. typedef struct _is PyInterpreterState;
  17. #else
  18. typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
  19. typedef struct {
  20. int install_signal_handlers; /* Install signal handlers? -1 means unset */
  21. int ignore_environment; /* -E, Py_IgnoreEnvironmentFlag */
  22. int use_hash_seed; /* PYTHONHASHSEED=x */
  23. unsigned long hash_seed;
  24. const char *allocator; /* Memory allocator: _PyMem_SetupAllocators() */
  25. int dev_mode; /* PYTHONDEVMODE, -X dev */
  26. int faulthandler; /* PYTHONFAULTHANDLER, -X faulthandler */
  27. int tracemalloc; /* PYTHONTRACEMALLOC, -X tracemalloc=N */
  28. int import_time; /* PYTHONPROFILEIMPORTTIME, -X importtime */
  29. int show_ref_count; /* -X showrefcount */
  30. int show_alloc_count; /* -X showalloccount */
  31. int dump_refs; /* PYTHONDUMPREFS */
  32. int malloc_stats; /* PYTHONMALLOCSTATS */
  33. int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
  34. int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
  35. int utf8_mode; /* PYTHONUTF8, -X utf8; -1 means unknown */
  36. wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
  37. int argc; /* Number of command line arguments,
  38. -1 means unset */
  39. wchar_t **argv; /* Command line arguments */
  40. wchar_t *program; /* argv[0] or "" */
  41. int nxoption; /* Number of -X options */
  42. wchar_t **xoptions; /* -X options */
  43. int nwarnoption; /* Number of warnings options */
  44. wchar_t **warnoptions; /* Warnings options */
  45. /* Path configuration inputs */
  46. wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
  47. wchar_t *home; /* PYTHONHOME environment variable,
  48. see also Py_SetPythonHome(). */
  49. /* Path configuration outputs */
  50. int nmodule_search_path; /* Number of sys.path paths,
  51. -1 means unset */
  52. wchar_t **module_search_paths; /* sys.path paths */
  53. wchar_t *executable; /* sys.executable */
  54. wchar_t *prefix; /* sys.prefix */
  55. wchar_t *base_prefix; /* sys.base_prefix */
  56. wchar_t *exec_prefix; /* sys.exec_prefix */
  57. wchar_t *base_exec_prefix; /* sys.base_exec_prefix */
  58. /* Private fields */
  59. int _disable_importlib; /* Needed by freeze_importlib */
  60. } _PyCoreConfig;
  61. #define _PyCoreConfig_INIT \
  62. (_PyCoreConfig){ \
  63. .install_signal_handlers = -1, \
  64. .ignore_environment = -1, \
  65. .use_hash_seed = -1, \
  66. .coerce_c_locale = -1, \
  67. .faulthandler = -1, \
  68. .tracemalloc = -1, \
  69. .utf8_mode = -1, \
  70. .argc = -1, \
  71. .nmodule_search_path = -1}
  72. /* Note: _PyCoreConfig_INIT sets other fields to 0/NULL */
  73. /* Placeholders while working on the new configuration API
  74. *
  75. * See PEP 432 for final anticipated contents
  76. */
  77. typedef struct {
  78. int install_signal_handlers; /* Install signal handlers? -1 means unset */
  79. PyObject *argv; /* sys.argv list, can be NULL */
  80. PyObject *executable; /* sys.executable str */
  81. PyObject *prefix; /* sys.prefix str */
  82. PyObject *base_prefix; /* sys.base_prefix str, can be NULL */
  83. PyObject *exec_prefix; /* sys.exec_prefix str */
  84. PyObject *base_exec_prefix; /* sys.base_exec_prefix str, can be NULL */
  85. PyObject *warnoptions; /* sys.warnoptions list, can be NULL */
  86. PyObject *xoptions; /* sys._xoptions dict, can be NULL */
  87. PyObject *module_search_path; /* sys.path list */
  88. } _PyMainInterpreterConfig;
  89. #define _PyMainInterpreterConfig_INIT \
  90. (_PyMainInterpreterConfig){.install_signal_handlers = -1}
  91. /* Note: _PyMainInterpreterConfig_INIT sets other fields to 0/NULL */
  92. typedef struct _is {
  93. struct _is *next;
  94. struct _ts *tstate_head;
  95. int64_t id;
  96. int64_t id_refcount;
  97. PyThread_type_lock id_mutex;
  98. PyObject *modules;
  99. PyObject *modules_by_index;
  100. PyObject *sysdict;
  101. PyObject *builtins;
  102. PyObject *importlib;
  103. /* Used in Python/sysmodule.c. */
  104. int check_interval;
  105. /* Used in Modules/_threadmodule.c. */
  106. long num_threads;
  107. /* Support for runtime thread stack size tuning.
  108. A value of 0 means using the platform's default stack size
  109. or the size specified by the THREAD_STACK_SIZE macro. */
  110. /* Used in Python/thread.c. */
  111. size_t pythread_stacksize;
  112. PyObject *codec_search_path;
  113. PyObject *codec_search_cache;
  114. PyObject *codec_error_registry;
  115. int codecs_initialized;
  116. int fscodec_initialized;
  117. _PyCoreConfig core_config;
  118. _PyMainInterpreterConfig config;
  119. #ifdef HAVE_DLOPEN
  120. int dlopenflags;
  121. #endif
  122. PyObject *builtins_copy;
  123. PyObject *import_func;
  124. /* Initialized to PyEval_EvalFrameDefault(). */
  125. _PyFrameEvalFunction eval_frame;
  126. Py_ssize_t co_extra_user_count;
  127. freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
  128. #ifdef HAVE_FORK
  129. PyObject *before_forkers;
  130. PyObject *after_forkers_parent;
  131. PyObject *after_forkers_child;
  132. #endif
  133. /* AtExit module */
  134. void (*pyexitfunc)(PyObject *);
  135. PyObject *pyexitmodule;
  136. uint64_t tstate_next_unique_id;
  137. } PyInterpreterState;
  138. #endif /* !Py_LIMITED_API */
  139. /* State unique per thread */
  140. #ifndef Py_LIMITED_API
  141. /* Py_tracefunc return -1 when raising an exception, or 0 for success. */
  142. typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
  143. /* The following values are used for 'what' for tracefunc functions
  144. *
  145. * To add a new kind of trace event, also update "trace_init" in
  146. * Python/sysmodule.c to define the Python level event name
  147. */
  148. #define PyTrace_CALL 0
  149. #define PyTrace_EXCEPTION 1
  150. #define PyTrace_LINE 2
  151. #define PyTrace_RETURN 3
  152. #define PyTrace_C_CALL 4
  153. #define PyTrace_C_EXCEPTION 5
  154. #define PyTrace_C_RETURN 6
  155. #define PyTrace_OPCODE 7
  156. #endif /* Py_LIMITED_API */
  157. #ifdef Py_LIMITED_API
  158. typedef struct _ts PyThreadState;
  159. #else
  160. typedef struct _err_stackitem {
  161. /* This struct represents an entry on the exception stack, which is a
  162. * per-coroutine state. (Coroutine in the computer science sense,
  163. * including the thread and generators).
  164. * This ensures that the exception state is not impacted by "yields"
  165. * from an except handler.
  166. */
  167. PyObject *exc_type, *exc_value, *exc_traceback;
  168. struct _err_stackitem *previous_item;
  169. } _PyErr_StackItem;
  170. typedef struct _ts {
  171. /* See Python/ceval.c for comments explaining most fields */
  172. struct _ts *prev;
  173. struct _ts *next;
  174. PyInterpreterState *interp;
  175. /* Borrowed reference to the current frame (it can be NULL) */
  176. struct _frame *frame;
  177. int recursion_depth;
  178. char overflowed; /* The stack has overflowed. Allow 50 more calls
  179. to handle the runtime error. */
  180. char recursion_critical; /* The current calls must not cause
  181. a stack overflow. */
  182. int stackcheck_counter;
  183. /* 'tracing' keeps track of the execution depth when tracing/profiling.
  184. This is to prevent the actual trace/profile code from being recorded in
  185. the trace/profile. */
  186. int tracing;
  187. int use_tracing;
  188. Py_tracefunc c_profilefunc;
  189. Py_tracefunc c_tracefunc;
  190. PyObject *c_profileobj;
  191. PyObject *c_traceobj;
  192. /* The exception currently being raised */
  193. PyObject *curexc_type;
  194. PyObject *curexc_value;
  195. PyObject *curexc_traceback;
  196. /* The exception currently being handled, if no coroutines/generators
  197. * are present. Always last element on the stack referred to be exc_info.
  198. */
  199. _PyErr_StackItem exc_state;
  200. /* Pointer to the top of the stack of the exceptions currently
  201. * being handled */
  202. _PyErr_StackItem *exc_info;
  203. PyObject *dict; /* Stores per-thread state */
  204. int gilstate_counter;
  205. PyObject *async_exc; /* Asynchronous exception to raise */
  206. unsigned long thread_id; /* Thread id where this tstate was created */
  207. int trash_delete_nesting;
  208. PyObject *trash_delete_later;
  209. /* Called when a thread state is deleted normally, but not when it
  210. * is destroyed after fork().
  211. * Pain: to prevent rare but fatal shutdown errors (issue 18808),
  212. * Thread.join() must wait for the join'ed thread's tstate to be unlinked
  213. * from the tstate chain. That happens at the end of a thread's life,
  214. * in pystate.c.
  215. * The obvious way doesn't quite work: create a lock which the tstate
  216. * unlinking code releases, and have Thread.join() wait to acquire that
  217. * lock. The problem is that we _are_ at the end of the thread's life:
  218. * if the thread holds the last reference to the lock, decref'ing the
  219. * lock will delete the lock, and that may trigger arbitrary Python code
  220. * if there's a weakref, with a callback, to the lock. But by this time
  221. * _PyThreadState_Current is already NULL, so only the simplest of C code
  222. * can be allowed to run (in particular it must not be possible to
  223. * release the GIL).
  224. * So instead of holding the lock directly, the tstate holds a weakref to
  225. * the lock: that's the value of on_delete_data below. Decref'ing a
  226. * weakref is harmless.
  227. * on_delete points to _threadmodule.c's static release_sentinel() function.
  228. * After the tstate is unlinked, release_sentinel is called with the
  229. * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
  230. * the indirectly held lock.
  231. */
  232. void (*on_delete)(void *);
  233. void *on_delete_data;
  234. int coroutine_origin_tracking_depth;
  235. PyObject *coroutine_wrapper;
  236. int in_coroutine_wrapper;
  237. PyObject *async_gen_firstiter;
  238. PyObject *async_gen_finalizer;
  239. PyObject *context;
  240. uint64_t context_ver;
  241. /* Unique thread state id. */
  242. uint64_t id;
  243. /* XXX signal handlers should also be here */
  244. } PyThreadState;
  245. #endif /* !Py_LIMITED_API */
  246. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
  247. PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
  248. PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
  249. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
  250. /* New in 3.7 */
  251. PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);
  252. #endif
  253. #ifndef Py_LIMITED_API
  254. PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
  255. #endif /* !Py_LIMITED_API */
  256. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  257. /* New in 3.3 */
  258. PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
  259. PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
  260. #endif
  261. PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
  262. #ifndef Py_LIMITED_API
  263. PyAPI_FUNC(void) _PyState_ClearModules(void);
  264. #endif
  265. PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
  266. #ifndef Py_LIMITED_API
  267. PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
  268. PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
  269. #endif /* !Py_LIMITED_API */
  270. PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
  271. PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
  272. #ifndef Py_LIMITED_API
  273. PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
  274. #endif /* !Py_LIMITED_API */
  275. PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
  276. #ifndef Py_LIMITED_API
  277. PyAPI_FUNC(void) _PyGILState_Reinit(void);
  278. #endif /* !Py_LIMITED_API */
  279. /* Return the current thread state. The global interpreter lock must be held.
  280. * When the current thread state is NULL, this issues a fatal error (so that
  281. * the caller needn't check for NULL). */
  282. PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
  283. #ifndef Py_LIMITED_API
  284. /* Similar to PyThreadState_Get(), but don't issue a fatal error
  285. * if it is NULL. */
  286. PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
  287. #endif /* !Py_LIMITED_API */
  288. PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
  289. PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
  290. PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
  291. /* Variable and macro for in-line access to current thread state */
  292. /* Assuming the current thread holds the GIL, this is the
  293. PyThreadState for the current thread. */
  294. #ifdef Py_BUILD_CORE
  295. # define _PyThreadState_Current _PyRuntime.gilstate.tstate_current
  296. # define PyThreadState_GET() \
  297. ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
  298. #else
  299. # define PyThreadState_GET() PyThreadState_Get()
  300. #endif
  301. typedef
  302. enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
  303. PyGILState_STATE;
  304. /* Ensure that the current thread is ready to call the Python
  305. C API, regardless of the current state of Python, or of its
  306. thread lock. This may be called as many times as desired
  307. by a thread so long as each call is matched with a call to
  308. PyGILState_Release(). In general, other thread-state APIs may
  309. be used between _Ensure() and _Release() calls, so long as the
  310. thread-state is restored to its previous state before the Release().
  311. For example, normal use of the Py_BEGIN_ALLOW_THREADS/
  312. Py_END_ALLOW_THREADS macros are acceptable.
  313. The return value is an opaque "handle" to the thread state when
  314. PyGILState_Ensure() was called, and must be passed to
  315. PyGILState_Release() to ensure Python is left in the same state. Even
  316. though recursive calls are allowed, these handles can *not* be shared -
  317. each unique call to PyGILState_Ensure must save the handle for its
  318. call to PyGILState_Release.
  319. When the function returns, the current thread will hold the GIL.
  320. Failure is a fatal error.
  321. */
  322. PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
  323. /* Release any resources previously acquired. After this call, Python's
  324. state will be the same as it was prior to the corresponding
  325. PyGILState_Ensure() call (but generally this state will be unknown to
  326. the caller, hence the use of the GILState API.)
  327. Every call to PyGILState_Ensure must be matched by a call to
  328. PyGILState_Release on the same thread.
  329. */
  330. PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
  331. /* Helper/diagnostic function - get the current thread state for
  332. this thread. May return NULL if no GILState API has been used
  333. on the current thread. Note that the main thread always has such a
  334. thread-state, even if no auto-thread-state call has been made
  335. on the main thread.
  336. */
  337. PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
  338. #ifndef Py_LIMITED_API
  339. /* Helper/diagnostic function - return 1 if the current thread
  340. currently holds the GIL, 0 otherwise.
  341. The function returns 1 if _PyGILState_check_enabled is non-zero. */
  342. PyAPI_FUNC(int) PyGILState_Check(void);
  343. /* Unsafe function to get the single PyInterpreterState used by this process'
  344. GILState implementation.
  345. Return NULL before _PyGILState_Init() is called and after _PyGILState_Fini()
  346. is called. */
  347. PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
  348. #endif /* !Py_LIMITED_API */
  349. /* The implementation of sys._current_frames() Returns a dict mapping
  350. thread id to that thread's current frame.
  351. */
  352. #ifndef Py_LIMITED_API
  353. PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
  354. #endif
  355. /* Routines for advanced debuggers, requested by David Beazley.
  356. Don't use unless you know what you are doing! */
  357. #ifndef Py_LIMITED_API
  358. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
  359. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
  360. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
  361. PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
  362. PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
  363. typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
  364. #endif
  365. #ifdef __cplusplus
  366. }
  367. #endif
  368. #endif /* !Py_PYSTATE_H */