1997-05-05 17:56:21 -03:00
|
|
|
/* Thread and interpreter state structures and their interfaces */
|
|
|
|
|
|
|
|
|
2000-07-08 20:37:28 -03:00
|
|
|
#ifndef Py_PYSTATE_H
|
|
|
|
#define Py_PYSTATE_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-09-07 15:16:41 -03:00
|
|
|
/* This limitation is for performance and simplicity. If needed it can be
|
|
|
|
removed (with effort). */
|
|
|
|
#define MAX_CO_EXTRA_USERS 255
|
|
|
|
|
2018-11-27 18:54:59 -04:00
|
|
|
/* Forward declarations for PyFrameObject, PyThreadState
|
|
|
|
and PyInterpreterState */
|
|
|
|
struct _ts;
|
|
|
|
struct _is;
|
1997-08-01 23:56:48 -03:00
|
|
|
|
2019-02-23 14:35:52 -04:00
|
|
|
/* struct _ts is defined in cpython/pystate.h */
|
2018-11-27 18:54:59 -04:00
|
|
|
typedef struct _ts PyThreadState;
|
2020-05-14 23:11:00 -03:00
|
|
|
/* struct _is is defined in internal/pycore_interp.h */
|
2010-12-03 16:14:31 -04:00
|
|
|
typedef struct _is PyInterpreterState;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2019-02-23 14:35:52 -04:00
|
|
|
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
|
|
|
|
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
|
|
|
|
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
|
2018-10-31 21:51:40 -03:00
|
|
|
|
2020-03-13 14:15:33 -03:00
|
|
|
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
|
|
|
|
/* New in 3.9 */
|
|
|
|
/* Get the current interpreter state.
|
|
|
|
|
|
|
|
Issue a fatal error if there no current Python thread state or no current
|
|
|
|
interpreter. It cannot return NULL.
|
|
|
|
|
|
|
|
The caller must hold the GIL. */
|
|
|
|
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Get(void);
|
|
|
|
#endif
|
|
|
|
|
2019-03-15 20:47:43 -03:00
|
|
|
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
|
|
|
|
/* New in 3.8 */
|
|
|
|
PyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *);
|
|
|
|
#endif
|
|
|
|
|
2017-05-22 23:46:40 -03:00
|
|
|
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
|
|
|
|
/* New in 3.7 */
|
2019-02-23 14:35:52 -04:00
|
|
|
PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);
|
2017-05-22 23:46:40 -03:00
|
|
|
#endif
|
2012-06-22 07:20:55 -03:00
|
|
|
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
|
2019-03-15 20:47:43 -03:00
|
|
|
|
|
|
|
/* State unique per thread */
|
|
|
|
|
2012-06-22 07:20:55 -03:00
|
|
|
/* New in 3.3 */
|
|
|
|
PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
|
|
|
|
PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
|
|
|
|
#endif
|
2008-06-11 02:26:20 -03:00
|
|
|
PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2019-02-23 14:35:52 -04:00
|
|
|
PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
|
|
|
|
PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
|
|
|
|
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2018-10-31 21:51:40 -03:00
|
|
|
/* Get the current thread state.
|
|
|
|
|
|
|
|
When the current thread state is NULL, this issues a fatal error (so that
|
|
|
|
the caller needn't check for NULL).
|
|
|
|
|
|
|
|
The caller must hold the GIL.
|
|
|
|
|
2021-10-13 09:09:13 -03:00
|
|
|
See also _PyThreadState_UncheckedGet() and _PyThreadState_GET(). */
|
2019-02-23 14:35:52 -04:00
|
|
|
PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
|
2016-01-20 06:12:38 -04:00
|
|
|
|
2021-10-13 09:09:13 -03:00
|
|
|
// Alias to PyThreadState_Get()
|
2018-10-31 21:51:40 -03:00
|
|
|
#define PyThreadState_GET() PyThreadState_Get()
|
|
|
|
|
2019-02-23 14:35:52 -04:00
|
|
|
PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
|
2017-03-23 10:48:39 -03:00
|
|
|
PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2020-03-13 19:38:08 -03:00
|
|
|
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
|
|
|
|
/* New in 3.9 */
|
2020-03-20 11:51:45 -03:00
|
|
|
PyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate);
|
2020-04-28 11:32:48 -03:00
|
|
|
PyAPI_FUNC(PyFrameObject*) PyThreadState_GetFrame(PyThreadState *tstate);
|
2020-03-25 17:23:53 -03:00
|
|
|
PyAPI_FUNC(uint64_t) PyThreadState_GetID(PyThreadState *tstate);
|
2020-03-13 19:38:08 -03:00
|
|
|
#endif
|
|
|
|
|
2004-03-28 22:24:26 -04:00
|
|
|
typedef
|
2003-04-19 12:41:53 -03:00
|
|
|
enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
|
|
|
|
PyGILState_STATE;
|
|
|
|
|
2011-04-26 18:34:58 -03:00
|
|
|
|
2003-04-19 12:41:53 -03:00
|
|
|
/* Ensure that the current thread is ready to call the Python
|
|
|
|
C API, regardless of the current state of Python, or of its
|
|
|
|
thread lock. This may be called as many times as desired
|
2004-03-28 22:24:26 -04:00
|
|
|
by a thread so long as each call is matched with a call to
|
|
|
|
PyGILState_Release(). In general, other thread-state APIs may
|
|
|
|
be used between _Ensure() and _Release() calls, so long as the
|
2003-04-19 12:41:53 -03:00
|
|
|
thread-state is restored to its previous state before the Release().
|
|
|
|
For example, normal use of the Py_BEGIN_ALLOW_THREADS/
|
|
|
|
Py_END_ALLOW_THREADS macros are acceptable.
|
|
|
|
|
|
|
|
The return value is an opaque "handle" to the thread state when
|
2004-03-13 16:45:47 -04:00
|
|
|
PyGILState_Ensure() was called, and must be passed to
|
2003-04-19 12:41:53 -03:00
|
|
|
PyGILState_Release() to ensure Python is left in the same state. Even
|
2004-03-28 22:24:26 -04:00
|
|
|
though recursive calls are allowed, these handles can *not* be shared -
|
|
|
|
each unique call to PyGILState_Ensure must save the handle for its
|
2003-04-19 12:41:53 -03:00
|
|
|
call to PyGILState_Release.
|
|
|
|
|
|
|
|
When the function returns, the current thread will hold the GIL.
|
|
|
|
|
|
|
|
Failure is a fatal error.
|
|
|
|
*/
|
|
|
|
PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
|
|
|
|
|
|
|
|
/* Release any resources previously acquired. After this call, Python's
|
|
|
|
state will be the same as it was prior to the corresponding
|
2004-03-28 22:24:26 -04:00
|
|
|
PyGILState_Ensure() call (but generally this state will be unknown to
|
2003-04-19 12:41:53 -03:00
|
|
|
the caller, hence the use of the GILState API.)
|
|
|
|
|
2004-03-28 22:24:26 -04:00
|
|
|
Every call to PyGILState_Ensure must be matched by a call to
|
2003-04-19 12:41:53 -03:00
|
|
|
PyGILState_Release on the same thread.
|
|
|
|
*/
|
|
|
|
PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
|
|
|
|
|
|
|
|
/* Helper/diagnostic function - get the current thread state for
|
2004-03-28 22:24:26 -04:00
|
|
|
this thread. May return NULL if no GILState API has been used
|
2011-08-07 19:16:54 -03:00
|
|
|
on the current thread. Note that the main thread always has such a
|
2004-03-28 22:24:26 -04:00
|
|
|
thread-state, even if no auto-thread-state call has been made
|
2003-04-19 12:41:53 -03:00
|
|
|
on the main thread.
|
|
|
|
*/
|
2019-02-23 14:35:52 -04:00
|
|
|
PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
|
2003-04-19 12:41:53 -03:00
|
|
|
|
2016-03-16 18:45:24 -03:00
|
|
|
|
2010-12-03 16:14:31 -04:00
|
|
|
#ifndef Py_LIMITED_API
|
2018-11-26 19:20:00 -04:00
|
|
|
# define Py_CPYTHON_PYSTATE_H
|
2021-10-14 20:50:04 -03:00
|
|
|
# include "cpython/pystate.h"
|
2018-11-26 19:20:00 -04:00
|
|
|
# undef Py_CPYTHON_PYSTATE_H
|
2010-12-03 16:14:31 -04:00
|
|
|
#endif
|
2003-02-19 11:53:17 -04:00
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_PYSTATE_H */
|