pystate.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifndef Py_CPYTHON_PYSTATE_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
  8. PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
  9. PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *);
  10. /* State unique per thread */
  11. /* Py_tracefunc return -1 when raising an exception, or 0 for success. */
  12. typedef int (*Py_tracefunc)(PyObject *, PyFrameObject *, int, PyObject *);
  13. /* The following values are used for 'what' for tracefunc functions
  14. *
  15. * To add a new kind of trace event, also update "trace_init" in
  16. * Python/sysmodule.c to define the Python level event name
  17. */
  18. #define PyTrace_CALL 0
  19. #define PyTrace_EXCEPTION 1
  20. #define PyTrace_LINE 2
  21. #define PyTrace_RETURN 3
  22. #define PyTrace_C_CALL 4
  23. #define PyTrace_C_EXCEPTION 5
  24. #define PyTrace_C_RETURN 6
  25. #define PyTrace_OPCODE 7
  26. typedef struct _err_stackitem {
  27. /* This struct represents an entry on the exception stack, which is a
  28. * per-coroutine state. (Coroutine in the computer science sense,
  29. * including the thread and generators).
  30. * This ensures that the exception state is not impacted by "yields"
  31. * from an except handler.
  32. */
  33. PyObject *exc_type, *exc_value, *exc_traceback;
  34. struct _err_stackitem *previous_item;
  35. } _PyErr_StackItem;
  36. // The PyThreadState typedef is in Include/pystate.h.
  37. struct _ts {
  38. /* See Python/ceval.c for comments explaining most fields */
  39. struct _ts *prev;
  40. struct _ts *next;
  41. PyInterpreterState *interp;
  42. /* Borrowed reference to the current frame (it can be NULL) */
  43. PyFrameObject *frame;
  44. int recursion_depth;
  45. char overflowed; /* The stack has overflowed. Allow 50 more calls
  46. to handle the runtime error. */
  47. char recursion_critical; /* The current calls must not cause
  48. a stack overflow. */
  49. int stackcheck_counter;
  50. /* 'tracing' keeps track of the execution depth when tracing/profiling.
  51. This is to prevent the actual trace/profile code from being recorded in
  52. the trace/profile. */
  53. int tracing;
  54. int use_tracing;
  55. Py_tracefunc c_profilefunc;
  56. Py_tracefunc c_tracefunc;
  57. PyObject *c_profileobj;
  58. PyObject *c_traceobj;
  59. /* The exception currently being raised */
  60. PyObject *curexc_type;
  61. PyObject *curexc_value;
  62. PyObject *curexc_traceback;
  63. /* The exception currently being handled, if no coroutines/generators
  64. * are present. Always last element on the stack referred to be exc_info.
  65. */
  66. _PyErr_StackItem exc_state;
  67. /* Pointer to the top of the stack of the exceptions currently
  68. * being handled */
  69. _PyErr_StackItem *exc_info;
  70. PyObject *dict; /* Stores per-thread state */
  71. int gilstate_counter;
  72. PyObject *async_exc; /* Asynchronous exception to raise */
  73. unsigned long thread_id; /* Thread id where this tstate was created */
  74. int trash_delete_nesting;
  75. PyObject *trash_delete_later;
  76. /* Called when a thread state is deleted normally, but not when it
  77. * is destroyed after fork().
  78. * Pain: to prevent rare but fatal shutdown errors (issue 18808),
  79. * Thread.join() must wait for the join'ed thread's tstate to be unlinked
  80. * from the tstate chain. That happens at the end of a thread's life,
  81. * in pystate.c.
  82. * The obvious way doesn't quite work: create a lock which the tstate
  83. * unlinking code releases, and have Thread.join() wait to acquire that
  84. * lock. The problem is that we _are_ at the end of the thread's life:
  85. * if the thread holds the last reference to the lock, decref'ing the
  86. * lock will delete the lock, and that may trigger arbitrary Python code
  87. * if there's a weakref, with a callback, to the lock. But by this time
  88. * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
  89. * of C code can be allowed to run (in particular it must not be possible to
  90. * release the GIL).
  91. * So instead of holding the lock directly, the tstate holds a weakref to
  92. * the lock: that's the value of on_delete_data below. Decref'ing a
  93. * weakref is harmless.
  94. * on_delete points to _threadmodule.c's static release_sentinel() function.
  95. * After the tstate is unlinked, release_sentinel is called with the
  96. * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
  97. * the indirectly held lock.
  98. */
  99. void (*on_delete)(void *);
  100. void *on_delete_data;
  101. int coroutine_origin_tracking_depth;
  102. PyObject *async_gen_firstiter;
  103. PyObject *async_gen_finalizer;
  104. PyObject *context;
  105. uint64_t context_ver;
  106. /* Unique thread state id. */
  107. uint64_t id;
  108. /* XXX signal handlers should also be here */
  109. };
  110. // Alias for backward compatibility with Python 3.8
  111. #define _PyInterpreterState_Get PyInterpreterState_Get
  112. PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
  113. /* Similar to PyThreadState_Get(), but don't issue a fatal error
  114. * if it is NULL. */
  115. PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
  116. PyAPI_FUNC(PyObject *) _PyThreadState_GetDict(PyThreadState *tstate);
  117. /* PyGILState */
  118. /* Helper/diagnostic function - return 1 if the current thread
  119. currently holds the GIL, 0 otherwise.
  120. The function returns 1 if _PyGILState_check_enabled is non-zero. */
  121. PyAPI_FUNC(int) PyGILState_Check(void);
  122. /* Get the single PyInterpreterState used by this process' GILState
  123. implementation.
  124. This function doesn't check for error. Return NULL before _PyGILState_Init()
  125. is called and after _PyGILState_Fini() is called.
  126. See also _PyInterpreterState_Get() and _PyInterpreterState_GET(). */
  127. PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
  128. /* The implementation of sys._current_frames() Returns a dict mapping
  129. thread id to that thread's current frame.
  130. */
  131. PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
  132. /* Routines for advanced debuggers, requested by David Beazley.
  133. Don't use unless you know what you are doing! */
  134. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
  135. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
  136. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
  137. PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
  138. PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
  139. PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
  140. /* Frame evaluation API */
  141. typedef PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, PyFrameObject *, int);
  142. PyAPI_FUNC(_PyFrameEvalFunction) _PyInterpreterState_GetEvalFrameFunc(
  143. PyInterpreterState *interp);
  144. PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(
  145. PyInterpreterState *interp,
  146. _PyFrameEvalFunction eval_frame);
  147. PyAPI_FUNC(const PyConfig*) _PyInterpreterState_GetConfig(PyInterpreterState *interp);
  148. // Get the configuration of the currrent interpreter.
  149. // The caller must hold the GIL.
  150. PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
  151. /* cross-interpreter data */
  152. struct _xid;
  153. // _PyCrossInterpreterData is similar to Py_buffer as an effectively
  154. // opaque struct that holds data outside the object machinery. This
  155. // is necessary to pass safely between interpreters in the same process.
  156. typedef struct _xid {
  157. // data is the cross-interpreter-safe derivation of a Python object
  158. // (see _PyObject_GetCrossInterpreterData). It will be NULL if the
  159. // new_object func (below) encodes the data.
  160. void *data;
  161. // obj is the Python object from which the data was derived. This
  162. // is non-NULL only if the data remains bound to the object in some
  163. // way, such that the object must be "released" (via a decref) when
  164. // the data is released. In that case the code that sets the field,
  165. // likely a registered "crossinterpdatafunc", is responsible for
  166. // ensuring it owns the reference (i.e. incref).
  167. PyObject *obj;
  168. // interp is the ID of the owning interpreter of the original
  169. // object. It corresponds to the active interpreter when
  170. // _PyObject_GetCrossInterpreterData() was called. This should only
  171. // be set by the cross-interpreter machinery.
  172. //
  173. // We use the ID rather than the PyInterpreterState to avoid issues
  174. // with deleted interpreters. Note that IDs are never re-used, so
  175. // each one will always correspond to a specific interpreter
  176. // (whether still alive or not).
  177. int64_t interp;
  178. // new_object is a function that returns a new object in the current
  179. // interpreter given the data. The resulting object (a new
  180. // reference) will be equivalent to the original object. This field
  181. // is required.
  182. PyObject *(*new_object)(struct _xid *);
  183. // free is called when the data is released. If it is NULL then
  184. // nothing will be done to free the data. For some types this is
  185. // okay (e.g. bytes) and for those types this field should be set
  186. // to NULL. However, for most the data was allocated just for
  187. // cross-interpreter use, so it must be freed when
  188. // _PyCrossInterpreterData_Release is called or the memory will
  189. // leak. In that case, at the very least this field should be set
  190. // to PyMem_RawFree (the default if not explicitly set to NULL).
  191. // The call will happen with the original interpreter activated.
  192. void (*free)(void *);
  193. } _PyCrossInterpreterData;
  194. PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
  195. PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
  196. PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
  197. PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
  198. /* cross-interpreter data registry */
  199. typedef int (*crossinterpdatafunc)(PyObject *, struct _xid *);
  200. PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
  201. PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
  202. #ifdef __cplusplus
  203. }
  204. #endif