pystate.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /* This limitation is for performance and simplicity. If needed it can be
  8. removed (with effort). */
  9. #define MAX_CO_EXTRA_USERS 255
  10. /* Forward declarations for PyFrameObject, PyThreadState
  11. and PyInterpreterState */
  12. struct _ts;
  13. struct _is;
  14. /* struct _ts is defined in cpython/pystate.h */
  15. typedef struct _ts PyThreadState;
  16. /* struct _is is defined in internal/pycore_interp.h */
  17. typedef struct _is PyInterpreterState;
  18. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
  19. PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
  20. PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
  21. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
  22. /* New in 3.9 */
  23. /* Get the current interpreter state.
  24. Issue a fatal error if there no current Python thread state or no current
  25. interpreter. It cannot return NULL.
  26. The caller must hold the GIL. */
  27. PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Get(void);
  28. #endif
  29. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
  30. /* New in 3.8 */
  31. PyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *);
  32. #endif
  33. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
  34. /* New in 3.7 */
  35. PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);
  36. #endif
  37. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  38. /* State unique per thread */
  39. /* New in 3.3 */
  40. PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
  41. PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
  42. #endif
  43. PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
  44. PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
  45. PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
  46. PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
  47. /* Get the current thread state.
  48. When the current thread state is NULL, this issues a fatal error (so that
  49. the caller needn't check for NULL).
  50. The caller must hold the GIL.
  51. See also PyThreadState_GET() and _PyThreadState_GET(). */
  52. PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
  53. /* Get the current Python thread state.
  54. Macro using PyThreadState_Get() or _PyThreadState_GET() depending if
  55. pycore_pystate.h is included or not (this header redefines the macro).
  56. If PyThreadState_Get() is used, issue a fatal error if the current thread
  57. state is NULL.
  58. See also PyThreadState_Get() and _PyThreadState_GET(). */
  59. #define PyThreadState_GET() PyThreadState_Get()
  60. PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
  61. PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
  62. PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
  63. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
  64. /* New in 3.9 */
  65. PyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate);
  66. PyAPI_FUNC(PyFrameObject*) PyThreadState_GetFrame(PyThreadState *tstate);
  67. PyAPI_FUNC(uint64_t) PyThreadState_GetID(PyThreadState *tstate);
  68. #endif
  69. typedef
  70. enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
  71. PyGILState_STATE;
  72. /* Ensure that the current thread is ready to call the Python
  73. C API, regardless of the current state of Python, or of its
  74. thread lock. This may be called as many times as desired
  75. by a thread so long as each call is matched with a call to
  76. PyGILState_Release(). In general, other thread-state APIs may
  77. be used between _Ensure() and _Release() calls, so long as the
  78. thread-state is restored to its previous state before the Release().
  79. For example, normal use of the Py_BEGIN_ALLOW_THREADS/
  80. Py_END_ALLOW_THREADS macros are acceptable.
  81. The return value is an opaque "handle" to the thread state when
  82. PyGILState_Ensure() was called, and must be passed to
  83. PyGILState_Release() to ensure Python is left in the same state. Even
  84. though recursive calls are allowed, these handles can *not* be shared -
  85. each unique call to PyGILState_Ensure must save the handle for its
  86. call to PyGILState_Release.
  87. When the function returns, the current thread will hold the GIL.
  88. Failure is a fatal error.
  89. */
  90. PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
  91. /* Release any resources previously acquired. After this call, Python's
  92. state will be the same as it was prior to the corresponding
  93. PyGILState_Ensure() call (but generally this state will be unknown to
  94. the caller, hence the use of the GILState API.)
  95. Every call to PyGILState_Ensure must be matched by a call to
  96. PyGILState_Release on the same thread.
  97. */
  98. PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
  99. /* Helper/diagnostic function - get the current thread state for
  100. this thread. May return NULL if no GILState API has been used
  101. on the current thread. Note that the main thread always has such a
  102. thread-state, even if no auto-thread-state call has been made
  103. on the main thread.
  104. */
  105. PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
  106. #ifndef Py_LIMITED_API
  107. # define Py_CPYTHON_PYSTATE_H
  108. # include "cpython/pystate.h"
  109. # undef Py_CPYTHON_PYSTATE_H
  110. #endif
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. #endif /* !Py_PYSTATE_H */