pystate.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #include "cpython/initconfig.h"
  8. PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
  9. PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
  10. PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *);
  11. /* State unique per thread */
  12. /* Py_tracefunc return -1 when raising an exception, or 0 for success. */
  13. typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
  14. /* The following values are used for 'what' for tracefunc functions
  15. *
  16. * To add a new kind of trace event, also update "trace_init" in
  17. * Python/sysmodule.c to define the Python level event name
  18. */
  19. #define PyTrace_CALL 0
  20. #define PyTrace_EXCEPTION 1
  21. #define PyTrace_LINE 2
  22. #define PyTrace_RETURN 3
  23. #define PyTrace_C_CALL 4
  24. #define PyTrace_C_EXCEPTION 5
  25. #define PyTrace_C_RETURN 6
  26. #define PyTrace_OPCODE 7
  27. typedef struct _err_stackitem {
  28. /* This struct represents an entry on the exception stack, which is a
  29. * per-coroutine state. (Coroutine in the computer science sense,
  30. * including the thread and generators).
  31. * This ensures that the exception state is not impacted by "yields"
  32. * from an except handler.
  33. */
  34. PyObject *exc_type, *exc_value, *exc_traceback;
  35. struct _err_stackitem *previous_item;
  36. } _PyErr_StackItem;
  37. // The PyThreadState typedef is in Include/pystate.h.
  38. struct _ts {
  39. /* See Python/ceval.c for comments explaining most fields */
  40. struct _ts *prev;
  41. struct _ts *next;
  42. PyInterpreterState *interp;
  43. struct _frame *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. /* Get the current interpreter state.
  111. Issue a fatal error if there no current Python thread state or no current
  112. interpreter. It cannot return NULL.
  113. The caller must hold the GIL.*/
  114. PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
  115. PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
  116. PyAPI_FUNC(void) _PyState_ClearModules(void);
  117. PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
  118. /* Similar to PyThreadState_Get(), but don't issue a fatal error
  119. * if it is NULL. */
  120. PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
  121. /* PyGILState */
  122. /* Helper/diagnostic function - return 1 if the current thread
  123. currently holds the GIL, 0 otherwise.
  124. The function returns 1 if _PyGILState_check_enabled is non-zero. */
  125. PyAPI_FUNC(int) PyGILState_Check(void);
  126. /* Get the single PyInterpreterState used by this process' GILState
  127. implementation.
  128. This function doesn't check for error. Return NULL before _PyGILState_Init()
  129. is called and after _PyGILState_Fini() is called.
  130. See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */
  131. PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
  132. /* The implementation of sys._current_frames() Returns a dict mapping
  133. thread id to that thread's current frame.
  134. */
  135. PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
  136. /* Routines for advanced debuggers, requested by David Beazley.
  137. Don't use unless you know what you are doing! */
  138. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
  139. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
  140. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
  141. PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
  142. PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
  143. typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
  144. /* cross-interpreter data */
  145. struct _xid;
  146. // _PyCrossInterpreterData is similar to Py_buffer as an effectively
  147. // opaque struct that holds data outside the object machinery. This
  148. // is necessary to pass safely between interpreters in the same process.
  149. typedef struct _xid {
  150. // data is the cross-interpreter-safe derivation of a Python object
  151. // (see _PyObject_GetCrossInterpreterData). It will be NULL if the
  152. // new_object func (below) encodes the data.
  153. void *data;
  154. // obj is the Python object from which the data was derived. This
  155. // is non-NULL only if the data remains bound to the object in some
  156. // way, such that the object must be "released" (via a decref) when
  157. // the data is released. In that case the code that sets the field,
  158. // likely a registered "crossinterpdatafunc", is responsible for
  159. // ensuring it owns the reference (i.e. incref).
  160. PyObject *obj;
  161. // interp is the ID of the owning interpreter of the original
  162. // object. It corresponds to the active interpreter when
  163. // _PyObject_GetCrossInterpreterData() was called. This should only
  164. // be set by the cross-interpreter machinery.
  165. //
  166. // We use the ID rather than the PyInterpreterState to avoid issues
  167. // with deleted interpreters. Note that IDs are never re-used, so
  168. // each one will always correspond to a specific interpreter
  169. // (whether still alive or not).
  170. int64_t interp;
  171. // new_object is a function that returns a new object in the current
  172. // interpreter given the data. The resulting object (a new
  173. // reference) will be equivalent to the original object. This field
  174. // is required.
  175. PyObject *(*new_object)(struct _xid *);
  176. // free is called when the data is released. If it is NULL then
  177. // nothing will be done to free the data. For some types this is
  178. // okay (e.g. bytes) and for those types this field should be set
  179. // to NULL. However, for most the data was allocated just for
  180. // cross-interpreter use, so it must be freed when
  181. // _PyCrossInterpreterData_Release is called or the memory will
  182. // leak. In that case, at the very least this field should be set
  183. // to PyMem_RawFree (the default if not explicitly set to NULL).
  184. // The call will happen with the original interpreter activated.
  185. void (*free)(void *);
  186. } _PyCrossInterpreterData;
  187. PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
  188. PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
  189. PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
  190. PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
  191. /* cross-interpreter data registry */
  192. typedef int (*crossinterpdatafunc)(PyObject *, struct _xid *);
  193. PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
  194. PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
  195. #ifdef __cplusplus
  196. }
  197. #endif