pystate.h 4.5 KB

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