| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | /* Thread and interpreter state structures and their interfaces */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-08 23:37:28 +00:00
										 |  |  | #ifndef Py_PYSTATE_H
 | 
					
						
							|  |  |  | #define Py_PYSTATE_H
 | 
					
						
							|  |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 18:53:40 -07:00
										 |  |  | #include "pythread.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:16:41 -07:00
										 |  |  | /* This limitation is for performance and simplicity. If needed it can be
 | 
					
						
							|  |  |  | removed (with effort). */ | 
					
						
							|  |  |  | #define MAX_CO_EXTRA_USERS 255
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-27 23:54:59 +01:00
										 |  |  | /* Forward declarations for PyFrameObject, PyThreadState
 | 
					
						
							|  |  |  |    and PyInterpreterState */ | 
					
						
							|  |  |  | struct _frame; | 
					
						
							|  |  |  | struct _ts; | 
					
						
							|  |  |  | struct _is; | 
					
						
							| 
									
										
										
										
											1997-08-02 02:56:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 11:35:52 -07:00
										 |  |  | /* struct _ts is defined in cpython/pystate.h */ | 
					
						
							| 
									
										
										
										
											2018-11-27 23:54:59 +01:00
										 |  |  | typedef struct _ts PyThreadState; | 
					
						
							| 
									
										
										
										
											2019-02-23 11:35:52 -07:00
										 |  |  | /* struct _is is defined in internal/pycore_pystate.h */ | 
					
						
							| 
									
										
										
										
											2010-12-03 20:14:31 +00:00
										 |  |  | typedef struct _is PyInterpreterState; | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 11:35:52 -07:00
										 |  |  | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void); | 
					
						
							|  |  |  | PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *); | 
					
						
							|  |  |  | PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *); | 
					
						
							| 
									
										
										
										
											2018-11-01 01:51:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-15 17:47:43 -06:00
										 |  |  | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
 | 
					
						
							|  |  |  | /* New in 3.8 */ | 
					
						
							|  |  |  | PyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 19:46:40 -07:00
										 |  |  | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
 | 
					
						
							|  |  |  | /* New in 3.7 */ | 
					
						
							| 
									
										
										
										
											2019-02-23 11:35:52 -07:00
										 |  |  | PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *); | 
					
						
							| 
									
										
										
										
											2017-05-22 19:46:40 -07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2012-06-22 12:20:55 +02:00
										 |  |  | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
 | 
					
						
							| 
									
										
										
										
											2019-03-15 17:47:43 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* State unique per thread */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-22 12:20:55 +02:00
										 |  |  | /* New in 3.3 */ | 
					
						
							|  |  |  | PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*); | 
					
						
							|  |  |  | PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 11:35:52 -07:00
										 |  |  | PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *); | 
					
						
							|  |  |  | PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *); | 
					
						
							|  |  |  | PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-01 01:51:40 +01:00
										 |  |  | /* Get the current thread state.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    When the current thread state is NULL, this issues a fatal error (so that | 
					
						
							|  |  |  |    the caller needn't check for NULL). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The caller must hold the GIL. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    See also PyThreadState_GET() and _PyThreadState_GET(). */ | 
					
						
							| 
									
										
										
										
											2019-02-23 11:35:52 -07:00
										 |  |  | PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void); | 
					
						
							| 
									
										
										
										
											2016-01-20 11:12:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-01 01:51:40 +01:00
										 |  |  | /* Get the current Python thread state.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Macro using PyThreadState_Get() or _PyThreadState_GET() depending if | 
					
						
							| 
									
										
										
										
											2018-11-12 16:53:38 +01:00
										 |  |  |    pycore_pystate.h is included or not (this header redefines the macro). | 
					
						
							| 
									
										
										
										
											2018-11-01 01:51:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |    If PyThreadState_Get() is used, issue a fatal error if the current thread | 
					
						
							|  |  |  |    state is NULL. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    See also PyThreadState_Get() and _PyThreadState_GET(). */ | 
					
						
							|  |  |  | #define PyThreadState_GET() PyThreadState_Get()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 11:35:52 -07:00
										 |  |  | PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *); | 
					
						
							| 
									
										
										
										
											2002-08-12 07:21:58 +00:00
										 |  |  | PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void); | 
					
						
							| 
									
										
										
										
											2017-03-23 15:48:39 +02:00
										 |  |  | PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-29 02:24:26 +00:00
										 |  |  | typedef | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  |     enum {PyGILState_LOCKED, PyGILState_UNLOCKED} | 
					
						
							|  |  |  |         PyGILState_STATE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-26 23:34:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | /* Ensure that the current thread is ready to call the Python
 | 
					
						
							|  |  |  |    C API, regardless of the current state of Python, or of its | 
					
						
							|  |  |  |    thread lock.  This may be called as many times as desired | 
					
						
							| 
									
										
										
										
											2004-03-29 02:24:26 +00:00
										 |  |  |    by a thread so long as each call is matched with a call to | 
					
						
							|  |  |  |    PyGILState_Release().  In general, other thread-state APIs may | 
					
						
							|  |  |  |    be used between _Ensure() and _Release() calls, so long as the | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  |    thread-state is restored to its previous state before the Release(). | 
					
						
							|  |  |  |    For example, normal use of the Py_BEGIN_ALLOW_THREADS/ | 
					
						
							|  |  |  |    Py_END_ALLOW_THREADS macros are acceptable. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The return value is an opaque "handle" to the thread state when | 
					
						
							| 
									
										
										
										
											2004-03-13 20:45:47 +00:00
										 |  |  |    PyGILState_Ensure() was called, and must be passed to | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  |    PyGILState_Release() to ensure Python is left in the same state. Even | 
					
						
							| 
									
										
										
										
											2004-03-29 02:24:26 +00:00
										 |  |  |    though recursive calls are allowed, these handles can *not* be shared - | 
					
						
							|  |  |  |    each unique call to PyGILState_Ensure must save the handle for its | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  |    call to PyGILState_Release. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    When the function returns, the current thread will hold the GIL. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Failure is a fatal error. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Release any resources previously acquired.  After this call, Python's
 | 
					
						
							|  |  |  |    state will be the same as it was prior to the corresponding | 
					
						
							| 
									
										
										
										
											2004-03-29 02:24:26 +00:00
										 |  |  |    PyGILState_Ensure() call (but generally this state will be unknown to | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  |    the caller, hence the use of the GILState API.) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-29 02:24:26 +00:00
										 |  |  |    Every call to PyGILState_Ensure must be matched by a call to | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  |    PyGILState_Release on the same thread. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Helper/diagnostic function - get the current thread state for
 | 
					
						
							| 
									
										
										
										
											2004-03-29 02:24:26 +00:00
										 |  |  |    this thread.  May return NULL if no GILState API has been used | 
					
						
							| 
									
										
										
										
											2011-08-08 00:16:54 +02:00
										 |  |  |    on the current thread.  Note that the main thread always has such a | 
					
						
							| 
									
										
										
										
											2004-03-29 02:24:26 +00:00
										 |  |  |    thread-state, even if no auto-thread-state call has been made | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  |    on the main thread. | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2019-02-23 11:35:52 -07:00
										 |  |  | PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void); | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-16 22:45:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 20:14:31 +00:00
										 |  |  | #ifndef Py_LIMITED_API
 | 
					
						
							| 
									
										
										
										
											2018-11-27 00:20:00 +01:00
										 |  |  | #  define Py_CPYTHON_PYSTATE_H
 | 
					
						
							|  |  |  | #  include  "cpython/pystate.h"
 | 
					
						
							|  |  |  | #  undef Py_CPYTHON_PYSTATE_H
 | 
					
						
							| 
									
										
										
										
											2010-12-03 20:14:31 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2003-02-19 15:53:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif /* !Py_PYSTATE_H */
 |