17db96d56Sopenharmony_ci/* Thread and interpreter state structures and their interfaces */
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci#ifndef Py_PYSTATE_H
57db96d56Sopenharmony_ci#define Py_PYSTATE_H
67db96d56Sopenharmony_ci#ifdef __cplusplus
77db96d56Sopenharmony_ciextern "C" {
87db96d56Sopenharmony_ci#endif
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci/* This limitation is for performance and simplicity. If needed it can be
117db96d56Sopenharmony_ciremoved (with effort). */
127db96d56Sopenharmony_ci#define MAX_CO_EXTRA_USERS 255
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ciPyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
157db96d56Sopenharmony_ciPyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
167db96d56Sopenharmony_ciPyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
177db96d56Sopenharmony_ci
187db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
197db96d56Sopenharmony_ci/* New in 3.9 */
207db96d56Sopenharmony_ci/* Get the current interpreter state.
217db96d56Sopenharmony_ci
227db96d56Sopenharmony_ci   Issue a fatal error if there no current Python thread state or no current
237db96d56Sopenharmony_ci   interpreter. It cannot return NULL.
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ci   The caller must hold the GIL. */
267db96d56Sopenharmony_ciPyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Get(void);
277db96d56Sopenharmony_ci#endif
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
307db96d56Sopenharmony_ci/* New in 3.8 */
317db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *);
327db96d56Sopenharmony_ci#endif
337db96d56Sopenharmony_ci
347db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
357db96d56Sopenharmony_ci/* New in 3.7 */
367db96d56Sopenharmony_ciPyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);
377db96d56Sopenharmony_ci#endif
387db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
397db96d56Sopenharmony_ci
407db96d56Sopenharmony_ci/* State unique per thread */
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci/* New in 3.3 */
437db96d56Sopenharmony_ciPyAPI_FUNC(int) PyState_AddModule(PyObject*, PyModuleDef*);
447db96d56Sopenharmony_ciPyAPI_FUNC(int) PyState_RemoveModule(PyModuleDef*);
457db96d56Sopenharmony_ci#endif
467db96d56Sopenharmony_ciPyAPI_FUNC(PyObject*) PyState_FindModule(PyModuleDef*);
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ciPyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
497db96d56Sopenharmony_ciPyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
507db96d56Sopenharmony_ciPyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
517db96d56Sopenharmony_ci
527db96d56Sopenharmony_ci/* Get the current thread state.
537db96d56Sopenharmony_ci
547db96d56Sopenharmony_ci   When the current thread state is NULL, this issues a fatal error (so that
557db96d56Sopenharmony_ci   the caller needn't check for NULL).
567db96d56Sopenharmony_ci
577db96d56Sopenharmony_ci   The caller must hold the GIL.
587db96d56Sopenharmony_ci
597db96d56Sopenharmony_ci   See also _PyThreadState_UncheckedGet() and _PyThreadState_GET(). */
607db96d56Sopenharmony_ciPyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci// Alias to PyThreadState_Get()
637db96d56Sopenharmony_ci#define PyThreadState_GET() PyThreadState_Get()
647db96d56Sopenharmony_ci
657db96d56Sopenharmony_ciPyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
667db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
677db96d56Sopenharmony_ciPyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
687db96d56Sopenharmony_ci
697db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
707db96d56Sopenharmony_ci/* New in 3.9 */
717db96d56Sopenharmony_ciPyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate);
727db96d56Sopenharmony_ciPyAPI_FUNC(PyFrameObject*) PyThreadState_GetFrame(PyThreadState *tstate);
737db96d56Sopenharmony_ciPyAPI_FUNC(uint64_t) PyThreadState_GetID(PyThreadState *tstate);
747db96d56Sopenharmony_ci#endif
757db96d56Sopenharmony_ci
767db96d56Sopenharmony_citypedef
777db96d56Sopenharmony_ci    enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
787db96d56Sopenharmony_ci        PyGILState_STATE;
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci
817db96d56Sopenharmony_ci/* Ensure that the current thread is ready to call the Python
827db96d56Sopenharmony_ci   C API, regardless of the current state of Python, or of its
837db96d56Sopenharmony_ci   thread lock.  This may be called as many times as desired
847db96d56Sopenharmony_ci   by a thread so long as each call is matched with a call to
857db96d56Sopenharmony_ci   PyGILState_Release().  In general, other thread-state APIs may
867db96d56Sopenharmony_ci   be used between _Ensure() and _Release() calls, so long as the
877db96d56Sopenharmony_ci   thread-state is restored to its previous state before the Release().
887db96d56Sopenharmony_ci   For example, normal use of the Py_BEGIN_ALLOW_THREADS/
897db96d56Sopenharmony_ci   Py_END_ALLOW_THREADS macros are acceptable.
907db96d56Sopenharmony_ci
917db96d56Sopenharmony_ci   The return value is an opaque "handle" to the thread state when
927db96d56Sopenharmony_ci   PyGILState_Ensure() was called, and must be passed to
937db96d56Sopenharmony_ci   PyGILState_Release() to ensure Python is left in the same state. Even
947db96d56Sopenharmony_ci   though recursive calls are allowed, these handles can *not* be shared -
957db96d56Sopenharmony_ci   each unique call to PyGILState_Ensure must save the handle for its
967db96d56Sopenharmony_ci   call to PyGILState_Release.
977db96d56Sopenharmony_ci
987db96d56Sopenharmony_ci   When the function returns, the current thread will hold the GIL.
997db96d56Sopenharmony_ci
1007db96d56Sopenharmony_ci   Failure is a fatal error.
1017db96d56Sopenharmony_ci*/
1027db96d56Sopenharmony_ciPyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
1037db96d56Sopenharmony_ci
1047db96d56Sopenharmony_ci/* Release any resources previously acquired.  After this call, Python's
1057db96d56Sopenharmony_ci   state will be the same as it was prior to the corresponding
1067db96d56Sopenharmony_ci   PyGILState_Ensure() call (but generally this state will be unknown to
1077db96d56Sopenharmony_ci   the caller, hence the use of the GILState API.)
1087db96d56Sopenharmony_ci
1097db96d56Sopenharmony_ci   Every call to PyGILState_Ensure must be matched by a call to
1107db96d56Sopenharmony_ci   PyGILState_Release on the same thread.
1117db96d56Sopenharmony_ci*/
1127db96d56Sopenharmony_ciPyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
1137db96d56Sopenharmony_ci
1147db96d56Sopenharmony_ci/* Helper/diagnostic function - get the current thread state for
1157db96d56Sopenharmony_ci   this thread.  May return NULL if no GILState API has been used
1167db96d56Sopenharmony_ci   on the current thread.  Note that the main thread always has such a
1177db96d56Sopenharmony_ci   thread-state, even if no auto-thread-state call has been made
1187db96d56Sopenharmony_ci   on the main thread.
1197db96d56Sopenharmony_ci*/
1207db96d56Sopenharmony_ciPyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
1217db96d56Sopenharmony_ci
1227db96d56Sopenharmony_ci
1237db96d56Sopenharmony_ci#ifndef Py_LIMITED_API
1247db96d56Sopenharmony_ci#  define Py_CPYTHON_PYSTATE_H
1257db96d56Sopenharmony_ci#  include "cpython/pystate.h"
1267db96d56Sopenharmony_ci#  undef Py_CPYTHON_PYSTATE_H
1277db96d56Sopenharmony_ci#endif
1287db96d56Sopenharmony_ci
1297db96d56Sopenharmony_ci#ifdef __cplusplus
1307db96d56Sopenharmony_ci}
1317db96d56Sopenharmony_ci#endif
1327db96d56Sopenharmony_ci#endif /* !Py_PYSTATE_H */
133