Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1 | /* Thread and interpreter state structures and their interfaces */ |
| 2 | |
| 3 | |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 4 | #ifndef Py_PYSTATE_H |
| 5 | #define Py_PYSTATE_H |
| 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 10 | /* This limitation is for performance and simplicity. If needed it can be |
| 11 | removed (with effort). */ |
| 12 | #define MAX_CO_EXTRA_USERS 255 |
| 13 | |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 14 | /* Forward declarations for PyFrameObject, PyThreadState |
| 15 | and PyInterpreterState */ |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 16 | struct _ts; |
| 17 | struct _is; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 18 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 19 | /* struct _ts is defined in cpython/pystate.h */ |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 20 | typedef struct _ts PyThreadState; |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 21 | /* struct _is is defined in internal/pycore_pystate.h */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 22 | typedef struct _is PyInterpreterState; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 23 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 24 | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void); |
| 25 | PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *); |
| 26 | PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 27 | |
Victor Stinner | be79373 | 2020-03-13 18:15:33 +0100 | [diff] [blame] | 28 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 |
| 29 | /* New in 3.9 */ |
| 30 | /* Get the current interpreter state. |
| 31 | |
| 32 | Issue a fatal error if there no current Python thread state or no current |
| 33 | interpreter. It cannot return NULL. |
| 34 | |
| 35 | The caller must hold the GIL. */ |
| 36 | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Get(void); |
| 37 | #endif |
| 38 | |
Eric Snow | d2fdd1f | 2019-03-15 17:47:43 -0600 | [diff] [blame] | 39 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000 |
| 40 | /* New in 3.8 */ |
| 41 | PyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *); |
| 42 | #endif |
| 43 | |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 44 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000 |
| 45 | /* New in 3.7 */ |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 46 | PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *); |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 47 | #endif |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 48 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 |
Eric Snow | d2fdd1f | 2019-03-15 17:47:43 -0600 | [diff] [blame] | 49 | |
| 50 | /* State unique per thread */ |
| 51 | |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 52 | /* New in 3.3 */ |
| 53 | PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*); |
| 54 | PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*); |
| 55 | #endif |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 56 | PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 57 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 58 | PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *); |
| 59 | PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *); |
| 60 | PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 61 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 62 | /* Get the current thread state. |
| 63 | |
| 64 | When the current thread state is NULL, this issues a fatal error (so that |
| 65 | the caller needn't check for NULL). |
| 66 | |
| 67 | The caller must hold the GIL. |
| 68 | |
| 69 | See also PyThreadState_GET() and _PyThreadState_GET(). */ |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 70 | PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void); |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 71 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 72 | /* Get the current Python thread state. |
| 73 | |
| 74 | Macro using PyThreadState_Get() or _PyThreadState_GET() depending if |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 75 | pycore_pystate.h is included or not (this header redefines the macro). |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 76 | |
| 77 | If PyThreadState_Get() is used, issue a fatal error if the current thread |
| 78 | state is NULL. |
| 79 | |
| 80 | See also PyThreadState_Get() and _PyThreadState_GET(). */ |
| 81 | #define PyThreadState_GET() PyThreadState_Get() |
| 82 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 83 | PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 84 | PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void); |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 85 | PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 86 | |
Victor Stinner | 8fb02b6 | 2020-03-13 23:38:08 +0100 | [diff] [blame] | 87 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 |
| 88 | /* New in 3.9 */ |
Victor Stinner | fd1e1a1 | 2020-03-20 15:51:45 +0100 | [diff] [blame] | 89 | PyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate); |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame^] | 90 | PyAPI_FUNC(PyFrameObject*) PyThreadState_GetFrame(PyThreadState *tstate); |
Victor Stinner | 5c3cda0 | 2020-03-25 21:23:53 +0100 | [diff] [blame] | 91 | PyAPI_FUNC(uint64_t) PyThreadState_GetID(PyThreadState *tstate); |
Victor Stinner | 8fb02b6 | 2020-03-13 23:38:08 +0100 | [diff] [blame] | 92 | #endif |
| 93 | |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 94 | typedef |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 95 | enum {PyGILState_LOCKED, PyGILState_UNLOCKED} |
| 96 | PyGILState_STATE; |
| 97 | |
Victor Stinner | 8e4d407 | 2011-04-26 23:34:58 +0200 | [diff] [blame] | 98 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 99 | /* Ensure that the current thread is ready to call the Python |
| 100 | C API, regardless of the current state of Python, or of its |
| 101 | thread lock. This may be called as many times as desired |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 102 | by a thread so long as each call is matched with a call to |
| 103 | PyGILState_Release(). In general, other thread-state APIs may |
| 104 | be used between _Ensure() and _Release() calls, so long as the |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 105 | thread-state is restored to its previous state before the Release(). |
| 106 | For example, normal use of the Py_BEGIN_ALLOW_THREADS/ |
| 107 | Py_END_ALLOW_THREADS macros are acceptable. |
| 108 | |
| 109 | The return value is an opaque "handle" to the thread state when |
Raymond Hettinger | 4eec95a | 2004-03-13 20:45:47 +0000 | [diff] [blame] | 110 | PyGILState_Ensure() was called, and must be passed to |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 111 | PyGILState_Release() to ensure Python is left in the same state. Even |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 112 | though recursive calls are allowed, these handles can *not* be shared - |
| 113 | each unique call to PyGILState_Ensure must save the handle for its |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 114 | call to PyGILState_Release. |
| 115 | |
| 116 | When the function returns, the current thread will hold the GIL. |
| 117 | |
| 118 | Failure is a fatal error. |
| 119 | */ |
| 120 | PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void); |
| 121 | |
| 122 | /* Release any resources previously acquired. After this call, Python's |
| 123 | state will be the same as it was prior to the corresponding |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 124 | PyGILState_Ensure() call (but generally this state will be unknown to |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 125 | the caller, hence the use of the GILState API.) |
| 126 | |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 127 | Every call to PyGILState_Ensure must be matched by a call to |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 128 | PyGILState_Release on the same thread. |
| 129 | */ |
| 130 | PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE); |
| 131 | |
| 132 | /* Helper/diagnostic function - get the current thread state for |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 133 | this thread. May return NULL if no GILState API has been used |
Sandro Tosi | 61baee0 | 2011-08-08 00:16:54 +0200 | [diff] [blame] | 134 | on the current thread. Note that the main thread always has such a |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 135 | thread-state, even if no auto-thread-state call has been made |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 136 | on the main thread. |
| 137 | */ |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 138 | PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 139 | |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 140 | |
Martin v. Löwis | 1c0689c | 2014-01-03 21:36:49 +0100 | [diff] [blame] | 141 | #ifndef Py_LIMITED_API |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 142 | # define Py_CPYTHON_PYSTATE_H |
| 143 | # include "cpython/pystate.h" |
| 144 | # undef Py_CPYTHON_PYSTATE_H |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 145 | #endif |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 147 | #ifdef __cplusplus |
| 148 | } |
| 149 | #endif |
| 150 | #endif /* !Py_PYSTATE_H */ |