pystate.h 4.6 KB

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