Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 1 | /* Thread and interpreter state structures and their interfaces */ |
| 2 | |
| 3 | |
| 4 | #ifndef Py_PYSTATE_H |
| 5 | #define Py_PYSTATE_H |
| 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
| 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 | |
| 14 | /* Forward declarations for PyFrameObject, PyThreadState |
| 15 | and PyInterpreterState */ |
| 16 | struct _ts; |
| 17 | struct _is; |
| 18 | |
| 19 | /* struct _ts is defined in cpython/pystate.h */ |
| 20 | typedef struct _ts PyThreadState; |
| 21 | /* struct _is is defined in internal/pycore_interp.h */ |
| 22 | typedef struct _is PyInterpreterState; |
| 23 | |
| 24 | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void); |
| 25 | PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *); |
| 26 | PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *); |
| 27 | |
| 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 | |
| 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 | |
| 44 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000 |
| 45 | /* New in 3.7 */ |
| 46 | PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *); |
| 47 | #endif |
| 48 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 |
| 49 | |
| 50 | /* State unique per thread */ |
| 51 | |
| 52 | /* New in 3.3 */ |
| 53 | PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*); |
| 54 | PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*); |
| 55 | #endif |
| 56 | PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*); |
| 57 | |
| 58 | PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *); |
| 59 | PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *); |
| 60 | PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *); |
| 61 | |
| 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(). */ |
| 70 | PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void); |
| 71 | |
| 72 | /* Get the current Python thread state. |
| 73 | |
| 74 | Macro using PyThreadState_Get() or _PyThreadState_GET() depending if |
| 75 | pycore_pystate.h is included or not (this header redefines the macro). |
| 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 | |
| 83 | PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *); |
| 84 | PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void); |
| 85 | PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *); |
| 86 | |
| 87 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 |
| 88 | /* New in 3.9 */ |
| 89 | PyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate); |
| 90 | PyAPI_FUNC(PyFrameObject*) PyThreadState_GetFrame(PyThreadState *tstate); |
| 91 | PyAPI_FUNC(uint64_t) PyThreadState_GetID(PyThreadState *tstate); |
| 92 | #endif |
| 93 | |
| 94 | typedef |
| 95 | enum {PyGILState_LOCKED, PyGILState_UNLOCKED} |
| 96 | PyGILState_STATE; |
| 97 | |
| 98 | |
| 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 |
| 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 |
| 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 |
| 110 | PyGILState_Ensure() was called, and must be passed to |
| 111 | PyGILState_Release() to ensure Python is left in the same state. Even |
| 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 |
| 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 |
| 124 | PyGILState_Ensure() call (but generally this state will be unknown to |
| 125 | the caller, hence the use of the GILState API.) |
| 126 | |
| 127 | Every call to PyGILState_Ensure must be matched by a call to |
| 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 |
| 133 | this thread. May return NULL if no GILState API has been used |
| 134 | on the current thread. Note that the main thread always has such a |
| 135 | thread-state, even if no auto-thread-state call has been made |
| 136 | on the main thread. |
| 137 | */ |
| 138 | PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void); |
| 139 | |
| 140 | |
| 141 | #ifndef Py_LIMITED_API |
| 142 | # define Py_CPYTHON_PYSTATE_H |
| 143 | # include "cpython/pystate.h" |
| 144 | # undef Py_CPYTHON_PYSTATE_H |
| 145 | #endif |
| 146 | |
| 147 | #ifdef __cplusplus |
| 148 | } |
| 149 | #endif |
| 150 | #endif /* !Py_PYSTATE_H */ |