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 | |
Eric Snow | 4c6955e | 2018-02-16 18:53:40 -0700 | [diff] [blame] | 10 | #include "pythread.h" |
Victor Stinner | 6c785c0 | 2018-08-01 17:56:14 +0200 | [diff] [blame] | 11 | #include "coreconfig.h" |
Eric Snow | 4c6955e | 2018-02-16 18:53:40 -0700 | [diff] [blame] | 12 | |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 13 | /* This limitation is for performance and simplicity. If needed it can be |
| 14 | removed (with effort). */ |
| 15 | #define MAX_CO_EXTRA_USERS 255 |
| 16 | |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame^] | 17 | /* Forward declarations for PyFrameObject, PyThreadState |
| 18 | and PyInterpreterState */ |
| 19 | struct _frame; |
| 20 | struct _ts; |
| 21 | struct _is; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 22 | |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame^] | 23 | #ifdef Py_LIMITED_API |
| 24 | typedef struct _ts PyThreadState; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 25 | typedef struct _is PyInterpreterState; |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame^] | 26 | #else |
| 27 | /* PyThreadState and PyInterpreterState are defined in cpython/pystate.h */ |
| 28 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 29 | |
| 30 | /* State unique per thread */ |
| 31 | |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame^] | 32 | PyAPI_FUNC(struct _is *) PyInterpreterState_New(void); |
| 33 | PyAPI_FUNC(void) PyInterpreterState_Clear(struct _is *); |
| 34 | PyAPI_FUNC(void) PyInterpreterState_Delete(struct _is *); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 35 | |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 36 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000 |
| 37 | /* New in 3.7 */ |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame^] | 38 | PyAPI_FUNC(int64_t) PyInterpreterState_GetID(struct _is *); |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 39 | #endif |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 40 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 |
| 41 | /* New in 3.3 */ |
| 42 | PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*); |
| 43 | PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*); |
| 44 | #endif |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 45 | PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 46 | |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame^] | 47 | PyAPI_FUNC(struct _ts *) PyThreadState_New(struct _is *); |
| 48 | PyAPI_FUNC(void) PyThreadState_Clear(struct _ts *); |
| 49 | PyAPI_FUNC(void) PyThreadState_Delete(struct _ts *); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 50 | PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 51 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 52 | /* 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 Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame^] | 60 | PyAPI_FUNC(struct _ts *) PyThreadState_Get(void); |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 61 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 62 | /* Get the current Python thread state. |
| 63 | |
| 64 | Macro using PyThreadState_Get() or _PyThreadState_GET() depending if |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 65 | pycore_pystate.h is included or not (this header redefines the macro). |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 66 | |
| 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 Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame^] | 73 | PyAPI_FUNC(struct _ts *) PyThreadState_Swap(struct _ts *); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 74 | PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void); |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 75 | PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 76 | |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 77 | typedef |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 78 | enum {PyGILState_LOCKED, PyGILState_UNLOCKED} |
| 79 | PyGILState_STATE; |
| 80 | |
Victor Stinner | 8e4d407 | 2011-04-26 23:34:58 +0200 | [diff] [blame] | 81 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 82 | /* 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 Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 85 | 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 Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 88 | 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 Hettinger | 4eec95a | 2004-03-13 20:45:47 +0000 | [diff] [blame] | 93 | PyGILState_Ensure() was called, and must be passed to |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 94 | PyGILState_Release() to ensure Python is left in the same state. Even |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 95 | 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 Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 97 | 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 | */ |
| 103 | PyAPI_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 Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 107 | PyGILState_Ensure() call (but generally this state will be unknown to |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 108 | the caller, hence the use of the GILState API.) |
| 109 | |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 110 | Every call to PyGILState_Ensure must be matched by a call to |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 111 | PyGILState_Release on the same thread. |
| 112 | */ |
| 113 | PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE); |
| 114 | |
| 115 | /* Helper/diagnostic function - get the current thread state for |
Tim Peters | 174175b | 2004-03-29 02:24:26 +0000 | [diff] [blame] | 116 | this thread. May return NULL if no GILState API has been used |
Sandro Tosi | 61baee0 | 2011-08-08 00:16:54 +0200 | [diff] [blame] | 117 | 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] | 118 | thread-state, even if no auto-thread-state call has been made |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 119 | on the main thread. |
| 120 | */ |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame^] | 121 | PyAPI_FUNC(struct _ts *) PyGILState_GetThisThreadState(void); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 122 | |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 123 | |
Martin v. Löwis | 1c0689c | 2014-01-03 21:36:49 +0100 | [diff] [blame] | 124 | #ifndef Py_LIMITED_API |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 125 | # define Py_CPYTHON_PYSTATE_H |
| 126 | # include "cpython/pystate.h" |
| 127 | # undef Py_CPYTHON_PYSTATE_H |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 128 | #endif |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 130 | #ifdef __cplusplus |
| 131 | } |
| 132 | #endif |
| 133 | #endif /* !Py_PYSTATE_H */ |