blob: 4c25e3f7ef84b5d8c3f22ef4b4f5cc5db7d1d357 [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"
11
Brett Cannon5c4de282016-09-07 11:16:41 -070012/* This limitation is for performance and simplicity. If needed it can be
13removed (with effort). */
14#define MAX_CO_EXTRA_USERS 255
15
Victor Stinner9bdd2de2018-11-27 23:54:59 +010016/* Forward declarations for PyFrameObject, PyThreadState
17 and PyInterpreterState */
18struct _frame;
19struct _ts;
20struct _is;
Guido van Rossuma027efa1997-05-05 20:56:21 +000021
Eric Snowbe3b2952019-02-23 11:35:52 -070022/* struct _ts is defined in cpython/pystate.h */
Victor Stinner9bdd2de2018-11-27 23:54:59 +010023typedef struct _ts PyThreadState;
Eric Snowbe3b2952019-02-23 11:35:52 -070024/* struct _is is defined in internal/pycore_pystate.h */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000025typedef struct _is PyInterpreterState;
Guido van Rossuma027efa1997-05-05 20:56:21 +000026
Eric Snowbe3b2952019-02-23 11:35:52 -070027PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
28PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
29PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
Victor Stinner50b48572018-11-01 01:51:40 +010030
Eric Snowd2fdd1f2019-03-15 17:47:43 -060031#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
32/* New in 3.8 */
33PyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *);
34#endif
35
Eric Snowe3774162017-05-22 19:46:40 -070036#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
37/* New in 3.7 */
Eric Snowbe3b2952019-02-23 11:35:52 -070038PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);
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
Eric Snowd2fdd1f2019-03-15 17:47:43 -060041
42/* State unique per thread */
43
Martin v. Löwis7800f752012-06-22 12:20:55 +020044/* New in 3.3 */
45PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
46PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
47#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +000048PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
Guido van Rossuma027efa1997-05-05 20:56:21 +000049
Eric Snowbe3b2952019-02-23 11:35:52 -070050PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
51PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
52PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
Mark Hammond91a681d2002-08-12 07:21:58 +000053PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
Guido van Rossuma027efa1997-05-05 20:56:21 +000054
Victor Stinner50b48572018-11-01 01:51:40 +010055/* Get the current thread state.
56
57 When the current thread state is NULL, this issues a fatal error (so that
58 the caller needn't check for NULL).
59
60 The caller must hold the GIL.
61
62 See also PyThreadState_GET() and _PyThreadState_GET(). */
Eric Snowbe3b2952019-02-23 11:35:52 -070063PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
Victor Stinnerbfd316e2016-01-20 11:12:38 +010064
Victor Stinner50b48572018-11-01 01:51:40 +010065/* Get the current Python thread state.
66
67 Macro using PyThreadState_Get() or _PyThreadState_GET() depending if
Victor Stinner621cebe2018-11-12 16:53:38 +010068 pycore_pystate.h is included or not (this header redefines the macro).
Victor Stinner50b48572018-11-01 01:51:40 +010069
70 If PyThreadState_Get() is used, issue a fatal error if the current thread
71 state is NULL.
72
73 See also PyThreadState_Get() and _PyThreadState_GET(). */
74#define PyThreadState_GET() PyThreadState_Get()
75
Eric Snowbe3b2952019-02-23 11:35:52 -070076PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
Mark Hammond91a681d2002-08-12 07:21:58 +000077PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020078PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
Guido van Rossuma027efa1997-05-05 20:56:21 +000079
Tim Peters174175b2004-03-29 02:24:26 +000080typedef
Mark Hammond8d98d2c2003-04-19 15:41:53 +000081 enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
82 PyGILState_STATE;
83
Victor Stinner8e4d4072011-04-26 23:34:58 +020084
Mark Hammond8d98d2c2003-04-19 15:41:53 +000085/* Ensure that the current thread is ready to call the Python
86 C API, regardless of the current state of Python, or of its
87 thread lock. This may be called as many times as desired
Tim Peters174175b2004-03-29 02:24:26 +000088 by a thread so long as each call is matched with a call to
89 PyGILState_Release(). In general, other thread-state APIs may
90 be used between _Ensure() and _Release() calls, so long as the
Mark Hammond8d98d2c2003-04-19 15:41:53 +000091 thread-state is restored to its previous state before the Release().
92 For example, normal use of the Py_BEGIN_ALLOW_THREADS/
93 Py_END_ALLOW_THREADS macros are acceptable.
94
95 The return value is an opaque "handle" to the thread state when
Raymond Hettinger4eec95a2004-03-13 20:45:47 +000096 PyGILState_Ensure() was called, and must be passed to
Mark Hammond8d98d2c2003-04-19 15:41:53 +000097 PyGILState_Release() to ensure Python is left in the same state. Even
Tim Peters174175b2004-03-29 02:24:26 +000098 though recursive calls are allowed, these handles can *not* be shared -
99 each unique call to PyGILState_Ensure must save the handle for its
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000100 call to PyGILState_Release.
101
102 When the function returns, the current thread will hold the GIL.
103
104 Failure is a fatal error.
105*/
106PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
107
108/* Release any resources previously acquired. After this call, Python's
109 state will be the same as it was prior to the corresponding
Tim Peters174175b2004-03-29 02:24:26 +0000110 PyGILState_Ensure() call (but generally this state will be unknown to
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000111 the caller, hence the use of the GILState API.)
112
Tim Peters174175b2004-03-29 02:24:26 +0000113 Every call to PyGILState_Ensure must be matched by a call to
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000114 PyGILState_Release on the same thread.
115*/
116PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
117
118/* Helper/diagnostic function - get the current thread state for
Tim Peters174175b2004-03-29 02:24:26 +0000119 this thread. May return NULL if no GILState API has been used
Sandro Tosi61baee02011-08-08 00:16:54 +0200120 on the current thread. Note that the main thread always has such a
Tim Peters174175b2004-03-29 02:24:26 +0000121 thread-state, even if no auto-thread-state call has been made
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000122 on the main thread.
123*/
Eric Snowbe3b2952019-02-23 11:35:52 -0700124PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000125
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100126
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100127#ifndef Py_LIMITED_API
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100128# define Py_CPYTHON_PYSTATE_H
129# include "cpython/pystate.h"
130# undef Py_CPYTHON_PYSTATE_H
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000131#endif
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000132
Guido van Rossuma027efa1997-05-05 20:56:21 +0000133#ifdef __cplusplus
134}
135#endif
136#endif /* !Py_PYSTATE_H */