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
|
|
|
|
|
2018-02-16 21:53:40 -04:00
|
|
|
#include "pythread.h"
|
|
|
|
|
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 _frame;
|
|
|
|
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;
|
2019-02-23 14:35:52 -04:00
|
|
|
/* struct _is is defined in internal/pycore_pystate.h */
|
2010-12-03 16:14:31 -04:00
|
|
|
typedef struct _is PyInterpreterState;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
|
|
|
/* State unique per thread */
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
/* 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 *);
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
|
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.
|
|
|
|
|
|
|
|
See also PyThreadState_GET() 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
|
|
|
|
2018-10-31 21:51:40 -03:00
|
|
|
/* Get the current Python thread state.
|
|
|
|
|
|
|
|
Macro using PyThreadState_Get() or _PyThreadState_GET() depending if
|
2018-11-12 11:53:38 -04:00
|
|
|
pycore_pystate.h is included or not (this header redefines the macro).
|
2018-10-31 21:51:40 -03:00
|
|
|
|
|
|
|
If PyThreadState_Get() is used, issue a fatal error if the current thread
|
|
|
|
state is NULL.
|
|
|
|
|
|
|
|
See also PyThreadState_Get() and _PyThreadState_GET(). */
|
|
|
|
#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
|
|
|
|
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
|
|
|
|
# include "cpython/pystate.h"
|
|
|
|
# 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 */
|