blob: ba5e9883306a70cbc42112ad97d317f4598fb445 [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001/* Thread and interpreter state structures and their interfaces */
2
3
Fred Drake5eb6d4e2000-07-08 23:37:28 +00004#ifndef Py_PYSTATE_H
5#define Py_PYSTATE_H
6#ifdef __cplusplus
7extern "C" {
8#endif
9
Eric Snow4c6955e2018-02-16 18:53:40 -070010#include "pythread.h"
Victor Stinner6c785c02018-08-01 17:56:14 +020011#include "coreconfig.h"
Eric Snow4c6955e2018-02-16 18:53:40 -070012
Brett Cannon5c4de282016-09-07 11:16:41 -070013/* This limitation is for performance and simplicity. If needed it can be
14removed (with effort). */
15#define MAX_CO_EXTRA_USERS 255
16
Victor Stinner9bdd2de2018-11-27 23:54:59 +010017/* Forward declarations for PyFrameObject, PyThreadState
18 and PyInterpreterState */
19struct _frame;
20struct _ts;
21struct _is;
Guido van Rossuma027efa1997-05-05 20:56:21 +000022
Victor Stinner9bdd2de2018-11-27 23:54:59 +010023#ifdef Py_LIMITED_API
24typedef struct _ts PyThreadState;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000025typedef struct _is PyInterpreterState;
Victor Stinner9bdd2de2018-11-27 23:54:59 +010026#else
27/* PyThreadState and PyInterpreterState are defined in cpython/pystate.h */
28#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +000029
30/* State unique per thread */
31
Victor Stinner9bdd2de2018-11-27 23:54:59 +010032PyAPI_FUNC(struct _is *) PyInterpreterState_New(void);
33PyAPI_FUNC(void) PyInterpreterState_Clear(struct _is *);
34PyAPI_FUNC(void) PyInterpreterState_Delete(struct _is *);
Victor Stinner50b48572018-11-01 01:51:40 +010035
Eric Snowe3774162017-05-22 19:46:40 -070036#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
37/* New in 3.7 */
Victor Stinner9bdd2de2018-11-27 23:54:59 +010038PyAPI_FUNC(int64_t) PyInterpreterState_GetID(struct _is *);
Eric Snowe3774162017-05-22 19:46:40 -070039#endif
Martin v. Löwis7800f752012-06-22 12:20:55 +020040#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
41/* New in 3.3 */
42PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
43PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
44#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +000045PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
Guido van Rossuma027efa1997-05-05 20:56:21 +000046
Victor Stinner9bdd2de2018-11-27 23:54:59 +010047PyAPI_FUNC(struct _ts *) PyThreadState_New(struct _is *);
48PyAPI_FUNC(void) PyThreadState_Clear(struct _ts *);
49PyAPI_FUNC(void) PyThreadState_Delete(struct _ts *);
Mark Hammond91a681d2002-08-12 07:21:58 +000050PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
Guido van Rossuma027efa1997-05-05 20:56:21 +000051
Victor Stinner50b48572018-11-01 01:51:40 +010052/* Get the current thread state.
53
54 When the current thread state is NULL, this issues a fatal error (so that
55 the caller needn't check for NULL).
56
57 The caller must hold the GIL.
58
59 See also PyThreadState_GET() and _PyThreadState_GET(). */
Victor Stinner9bdd2de2018-11-27 23:54:59 +010060PyAPI_FUNC(struct _ts *) PyThreadState_Get(void);
Victor Stinnerbfd316e2016-01-20 11:12:38 +010061
Victor Stinner50b48572018-11-01 01:51:40 +010062/* Get the current Python thread state.
63
64 Macro using PyThreadState_Get() or _PyThreadState_GET() depending if
Victor Stinner621cebe2018-11-12 16:53:38 +010065 pycore_pystate.h is included or not (this header redefines the macro).
Victor Stinner50b48572018-11-01 01:51:40 +010066
67 If PyThreadState_Get() is used, issue a fatal error if the current thread
68 state is NULL.
69
70 See also PyThreadState_Get() and _PyThreadState_GET(). */
71#define PyThreadState_GET() PyThreadState_Get()
72
Victor Stinner9bdd2de2018-11-27 23:54:59 +010073PyAPI_FUNC(struct _ts *) PyThreadState_Swap(struct _ts *);
Mark Hammond91a681d2002-08-12 07:21:58 +000074PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020075PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
Guido van Rossuma027efa1997-05-05 20:56:21 +000076
Tim Peters174175b2004-03-29 02:24:26 +000077typedef
Mark Hammond8d98d2c2003-04-19 15:41:53 +000078 enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
79 PyGILState_STATE;
80
Victor Stinner8e4d4072011-04-26 23:34:58 +020081
Mark Hammond8d98d2c2003-04-19 15:41:53 +000082/* Ensure that the current thread is ready to call the Python
83 C API, regardless of the current state of Python, or of its
84 thread lock. This may be called as many times as desired
Tim Peters174175b2004-03-29 02:24:26 +000085 by a thread so long as each call is matched with a call to
86 PyGILState_Release(). In general, other thread-state APIs may
87 be used between _Ensure() and _Release() calls, so long as the
Mark Hammond8d98d2c2003-04-19 15:41:53 +000088 thread-state is restored to its previous state before the Release().
89 For example, normal use of the Py_BEGIN_ALLOW_THREADS/
90 Py_END_ALLOW_THREADS macros are acceptable.
91
92 The return value is an opaque "handle" to the thread state when
Raymond Hettinger4eec95a2004-03-13 20:45:47 +000093 PyGILState_Ensure() was called, and must be passed to
Mark Hammond8d98d2c2003-04-19 15:41:53 +000094 PyGILState_Release() to ensure Python is left in the same state. Even
Tim Peters174175b2004-03-29 02:24:26 +000095 though recursive calls are allowed, these handles can *not* be shared -
96 each unique call to PyGILState_Ensure must save the handle for its
Mark Hammond8d98d2c2003-04-19 15:41:53 +000097 call to PyGILState_Release.
98
99 When the function returns, the current thread will hold the GIL.
100
101 Failure is a fatal error.
102*/
103PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
104
105/* Release any resources previously acquired. After this call, Python's
106 state will be the same as it was prior to the corresponding
Tim Peters174175b2004-03-29 02:24:26 +0000107 PyGILState_Ensure() call (but generally this state will be unknown to
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000108 the caller, hence the use of the GILState API.)
109
Tim Peters174175b2004-03-29 02:24:26 +0000110 Every call to PyGILState_Ensure must be matched by a call to
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000111 PyGILState_Release on the same thread.
112*/
113PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
114
115/* Helper/diagnostic function - get the current thread state for
Tim Peters174175b2004-03-29 02:24:26 +0000116 this thread. May return NULL if no GILState API has been used
Sandro Tosi61baee02011-08-08 00:16:54 +0200117 on the current thread. Note that the main thread always has such a
Tim Peters174175b2004-03-29 02:24:26 +0000118 thread-state, even if no auto-thread-state call has been made
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000119 on the main thread.
120*/
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100121PyAPI_FUNC(struct _ts *) PyGILState_GetThisThreadState(void);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000122
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100123
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100124#ifndef Py_LIMITED_API
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100125# define Py_CPYTHON_PYSTATE_H
126# include "cpython/pystate.h"
127# undef Py_CPYTHON_PYSTATE_H
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000128#endif
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000129
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130#ifdef __cplusplus
131}
132#endif
133#endif /* !Py_PYSTATE_H */