Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1 | |
| 2 | /* Thread and interpreter state structures and their interfaces */ |
| 3 | |
| 4 | #include "Python.h" |
| 5 | |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 6 | #define GET_TSTATE() \ |
| 7 | ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current)) |
| 8 | #define SET_TSTATE(value) \ |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9 | _Py_atomic_store_relaxed(&_PyThreadState_Current, (uintptr_t)(value)) |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 10 | #define GET_INTERP_STATE() \ |
| 11 | (GET_TSTATE()->interp) |
| 12 | |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 13 | |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 14 | /* -------------------------------------------------------------------------- |
| 15 | CAUTION |
| 16 | |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 17 | Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A |
| 18 | number of these functions are advertised as safe to call when the GIL isn't |
| 19 | held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's |
| 20 | debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL |
| 21 | to avoid the expense of doing their own locking). |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 22 | -------------------------------------------------------------------------- */ |
| 23 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 24 | #ifdef HAVE_DLOPEN |
| 25 | #ifdef HAVE_DLFCN_H |
| 26 | #include <dlfcn.h> |
| 27 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 28 | #if !HAVE_DECL_RTLD_LAZY |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 29 | #define RTLD_LAZY 1 |
| 30 | #endif |
| 31 | #endif |
| 32 | |
Benjamin Peterson | 43162b8 | 2012-04-13 11:58:27 -0400 | [diff] [blame] | 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 36 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 37 | int _PyGILState_check_enabled = 1; |
| 38 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 39 | #ifdef WITH_THREAD |
| 40 | #include "pythread.h" |
| 41 | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ |
Moshe Zadka | 9fb6af9 | 2000-08-04 21:27:47 +0000 | [diff] [blame] | 42 | #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 43 | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 44 | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 45 | |
| 46 | /* The single PyInterpreterState used by this process' |
| 47 | GILState implementation |
| 48 | */ |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 49 | /* TODO: Given interp_main, it may be possible to kill this ref */ |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 50 | static PyInterpreterState *autoInterpreterState = NULL; |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 51 | static int autoTLSkey = -1; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 52 | #else |
| 53 | #define HEAD_INIT() /* Nothing */ |
| 54 | #define HEAD_LOCK() /* Nothing */ |
| 55 | #define HEAD_UNLOCK() /* Nothing */ |
| 56 | #endif |
| 57 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 58 | static PyInterpreterState *interp_head = NULL; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 59 | static PyInterpreterState *interp_main = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 60 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 61 | /* Assuming the current thread holds the GIL, this is the |
| 62 | PyThreadState for the current thread. */ |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 63 | _Py_atomic_address _PyThreadState_Current = {0}; |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 64 | PyThreadFrameGetter _PyThreadState_GetFrame = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 65 | |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 66 | #ifdef WITH_THREAD |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 67 | static void _PyGILState_NoteThreadState(PyThreadState* tstate); |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 68 | #endif |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 69 | |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 70 | /* _next_interp_id is an auto-numbered sequence of small integers. |
| 71 | It gets initialized in _PyInterpreterState_Init(), which is called |
| 72 | in Py_Initialize(), and used in PyInterpreterState_New(). A negative |
| 73 | interpreter ID indicates an error occurred. The main interpreter |
| 74 | will always have an ID of 0. Overflow results in a RuntimeError. |
| 75 | If that becomes a problem later then we can adjust, e.g. by using |
| 76 | a Python int. |
| 77 | |
| 78 | We initialize this to -1 so that the pre-Py_Initialize() value |
| 79 | results in an error. */ |
| 80 | static int64_t _next_interp_id = -1; |
| 81 | |
| 82 | void |
| 83 | _PyInterpreterState_Init(void) |
| 84 | { |
| 85 | _next_interp_id = 0; |
| 86 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 87 | |
| 88 | PyInterpreterState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 89 | PyInterpreterState_New(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 90 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | PyInterpreterState *interp = (PyInterpreterState *) |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 92 | PyMem_RawMalloc(sizeof(PyInterpreterState)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 93 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | if (interp != NULL) { |
| 95 | HEAD_INIT(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 96 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | if (head_mutex == NULL) |
| 98 | Py_FatalError("Can't initialize threads for interpreter"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 99 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | interp->modules = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | interp->modules_by_index = NULL; |
| 102 | interp->sysdict = NULL; |
| 103 | interp->builtins = NULL; |
Serhiy Storchaka | 87a5c51 | 2014-02-10 18:21:34 +0200 | [diff] [blame] | 104 | interp->builtins_copy = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | interp->tstate_head = NULL; |
| 106 | interp->codec_search_path = NULL; |
| 107 | interp->codec_search_cache = NULL; |
| 108 | interp->codec_error_registry = NULL; |
| 109 | interp->codecs_initialized = 0; |
Victor Stinner | 3cbf14b | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 110 | interp->fscodec_initialized = 0; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 111 | interp->importlib = NULL; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 112 | interp->import_func = NULL; |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 113 | interp->eval_frame = _PyEval_EvalFrameDefault; |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame^] | 114 | interp->co_extra_user_count = 0; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 115 | #ifdef HAVE_DLOPEN |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 116 | #if HAVE_DECL_RTLD_NOW |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | interp->dlopenflags = RTLD_NOW; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 118 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | interp->dlopenflags = RTLD_LAZY; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 120 | #endif |
| 121 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 122 | #ifdef HAVE_FORK |
| 123 | interp->before_forkers = NULL; |
| 124 | interp->after_forkers_parent = NULL; |
| 125 | interp->after_forkers_child = NULL; |
| 126 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | HEAD_LOCK(); |
| 129 | interp->next = interp_head; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 130 | if (interp_main == NULL) { |
| 131 | interp_main = interp; |
| 132 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | interp_head = interp; |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 134 | if (_next_interp_id < 0) { |
| 135 | /* overflow or Py_Initialize() not called! */ |
| 136 | PyErr_SetString(PyExc_RuntimeError, |
| 137 | "failed to get an interpreter ID"); |
| 138 | interp = NULL; |
| 139 | } else { |
| 140 | interp->id = _next_interp_id; |
| 141 | _next_interp_id += 1; |
| 142 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | HEAD_UNLOCK(); |
| 144 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | return interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 151 | PyInterpreterState_Clear(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | PyThreadState *p; |
| 154 | HEAD_LOCK(); |
| 155 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 156 | PyThreadState_Clear(p); |
| 157 | HEAD_UNLOCK(); |
| 158 | Py_CLEAR(interp->codec_search_path); |
| 159 | Py_CLEAR(interp->codec_search_cache); |
| 160 | Py_CLEAR(interp->codec_error_registry); |
| 161 | Py_CLEAR(interp->modules); |
| 162 | Py_CLEAR(interp->modules_by_index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | Py_CLEAR(interp->sysdict); |
| 164 | Py_CLEAR(interp->builtins); |
Serhiy Storchaka | 87a5c51 | 2014-02-10 18:21:34 +0200 | [diff] [blame] | 165 | Py_CLEAR(interp->builtins_copy); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 166 | Py_CLEAR(interp->importlib); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 167 | Py_CLEAR(interp->import_func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 168 | #ifdef HAVE_FORK |
| 169 | Py_CLEAR(interp->before_forkers); |
| 170 | Py_CLEAR(interp->after_forkers_parent); |
| 171 | Py_CLEAR(interp->after_forkers_child); |
| 172 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
| 176 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 177 | zapthreads(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | PyThreadState *p; |
| 180 | /* No need to lock the mutex here because this should only happen |
| 181 | when the threads are all really dead (XXX famous last words). */ |
| 182 | while ((p = interp->tstate_head) != NULL) { |
| 183 | PyThreadState_Delete(p); |
| 184 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
| 188 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 189 | PyInterpreterState_Delete(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 190 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | PyInterpreterState **p; |
| 192 | zapthreads(interp); |
| 193 | HEAD_LOCK(); |
| 194 | for (p = &interp_head; ; p = &(*p)->next) { |
| 195 | if (*p == NULL) |
| 196 | Py_FatalError( |
| 197 | "PyInterpreterState_Delete: invalid interp"); |
| 198 | if (*p == interp) |
| 199 | break; |
| 200 | } |
| 201 | if (interp->tstate_head != NULL) |
| 202 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 203 | *p = interp->next; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 204 | if (interp_main == interp) { |
| 205 | interp_main = NULL; |
| 206 | if (interp_head != NULL) |
| 207 | Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters"); |
| 208 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | HEAD_UNLOCK(); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 210 | PyMem_RawFree(interp); |
Antoine Pitrou | 8db076c | 2011-10-30 19:13:55 +0100 | [diff] [blame] | 211 | #ifdef WITH_THREAD |
| 212 | if (interp_head == NULL && head_mutex != NULL) { |
| 213 | PyThread_free_lock(head_mutex); |
| 214 | head_mutex = NULL; |
| 215 | } |
| 216 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 220 | int64_t |
| 221 | PyInterpreterState_GetID(PyInterpreterState *interp) |
| 222 | { |
| 223 | if (interp == NULL) { |
| 224 | PyErr_SetString(PyExc_RuntimeError, "no interpreter provided"); |
| 225 | return -1; |
| 226 | } |
| 227 | return interp->id; |
| 228 | } |
| 229 | |
| 230 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 231 | /* Default implementation for _PyThreadState_GetFrame */ |
| 232 | static struct _frame * |
| 233 | threadstate_getframe(PyThreadState *self) |
| 234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | return self->frame; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 238 | static PyThreadState * |
| 239 | new_threadstate(PyInterpreterState *interp, int init) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 240 | { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 241 | PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState)); |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | if (_PyThreadState_GetFrame == NULL) |
| 244 | _PyThreadState_GetFrame = threadstate_getframe; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | if (tstate != NULL) { |
| 247 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | tstate->frame = NULL; |
| 250 | tstate->recursion_depth = 0; |
| 251 | tstate->overflowed = 0; |
| 252 | tstate->recursion_critical = 0; |
| 253 | tstate->tracing = 0; |
| 254 | tstate->use_tracing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | tstate->gilstate_counter = 0; |
| 256 | tstate->async_exc = NULL; |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 257 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | tstate->thread_id = PyThread_get_thread_ident(); |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 259 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | tstate->thread_id = 0; |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 261 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | tstate->dict = NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | tstate->curexc_type = NULL; |
| 266 | tstate->curexc_value = NULL; |
| 267 | tstate->curexc_traceback = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | tstate->exc_type = NULL; |
| 270 | tstate->exc_value = NULL; |
| 271 | tstate->exc_traceback = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | tstate->c_profilefunc = NULL; |
| 274 | tstate->c_tracefunc = NULL; |
| 275 | tstate->c_profileobj = NULL; |
| 276 | tstate->c_traceobj = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | 2b0218a | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 278 | tstate->trash_delete_nesting = 0; |
| 279 | tstate->trash_delete_later = NULL; |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 280 | tstate->on_delete = NULL; |
| 281 | tstate->on_delete_data = NULL; |
Antoine Pitrou | 2b0218a | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 282 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 283 | tstate->coroutine_wrapper = NULL; |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 284 | tstate->in_coroutine_wrapper = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 285 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 286 | tstate->async_gen_firstiter = NULL; |
| 287 | tstate->async_gen_finalizer = NULL; |
| 288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | if (init) |
| 290 | _PyThreadState_Init(tstate); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | HEAD_LOCK(); |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 293 | tstate->prev = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | tstate->next = interp->tstate_head; |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 295 | if (tstate->next) |
| 296 | tstate->next->prev = tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | interp->tstate_head = tstate; |
| 298 | HEAD_UNLOCK(); |
| 299 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 304 | PyThreadState * |
| 305 | PyThreadState_New(PyInterpreterState *interp) |
| 306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | return new_threadstate(interp, 1); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | PyThreadState * |
| 311 | _PyThreadState_Prealloc(PyInterpreterState *interp) |
| 312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | return new_threadstate(interp, 0); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void |
| 317 | _PyThreadState_Init(PyThreadState *tstate) |
| 318 | { |
| 319 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | _PyGILState_NoteThreadState(tstate); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 321 | #endif |
| 322 | } |
| 323 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 324 | PyObject* |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 325 | PyState_FindModule(struct PyModuleDef* module) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 326 | { |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 327 | Py_ssize_t index = module->m_base.m_index; |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 328 | PyInterpreterState *state = GET_INTERP_STATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | PyObject *res; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 330 | if (module->m_slots) { |
| 331 | return NULL; |
| 332 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | if (index == 0) |
| 334 | return NULL; |
| 335 | if (state->modules_by_index == NULL) |
| 336 | return NULL; |
Antoine Pitrou | 75506e8 | 2012-08-20 19:30:46 +0200 | [diff] [blame] | 337 | if (index >= PyList_GET_SIZE(state->modules_by_index)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | return NULL; |
| 339 | res = PyList_GET_ITEM(state->modules_by_index, index); |
| 340 | return res==Py_None ? NULL : res; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | int |
| 344 | _PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
| 345 | { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 346 | PyInterpreterState *state; |
Berker Peksag | 4b7b565 | 2016-08-22 18:05:56 +0300 | [diff] [blame] | 347 | if (!def) { |
| 348 | assert(PyErr_Occurred()); |
| 349 | return -1; |
| 350 | } |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 351 | if (def->m_slots) { |
| 352 | PyErr_SetString(PyExc_SystemError, |
| 353 | "PyState_AddModule called on module with slots"); |
| 354 | return -1; |
| 355 | } |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 356 | state = GET_INTERP_STATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | if (!state->modules_by_index) { |
| 358 | state->modules_by_index = PyList_New(0); |
| 359 | if (!state->modules_by_index) |
| 360 | return -1; |
| 361 | } |
| 362 | while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index) |
| 363 | if (PyList_Append(state->modules_by_index, Py_None) < 0) |
| 364 | return -1; |
| 365 | Py_INCREF(module); |
| 366 | return PyList_SetItem(state->modules_by_index, |
| 367 | def->m_base.m_index, module); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 368 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 369 | |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 370 | int |
| 371 | PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
| 372 | { |
| 373 | Py_ssize_t index; |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 374 | PyInterpreterState *state = GET_INTERP_STATE(); |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 375 | if (!def) { |
| 376 | Py_FatalError("PyState_AddModule: Module Definition is NULL"); |
| 377 | return -1; |
| 378 | } |
| 379 | index = def->m_base.m_index; |
| 380 | if (state->modules_by_index) { |
| 381 | if(PyList_GET_SIZE(state->modules_by_index) >= index) { |
| 382 | if(module == PyList_GET_ITEM(state->modules_by_index, index)) { |
| 383 | Py_FatalError("PyState_AddModule: Module already added!"); |
| 384 | return -1; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | return _PyState_AddModule(module, def); |
| 389 | } |
| 390 | |
| 391 | int |
| 392 | PyState_RemoveModule(struct PyModuleDef* def) |
| 393 | { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 394 | PyInterpreterState *state; |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 395 | Py_ssize_t index = def->m_base.m_index; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 396 | if (def->m_slots) { |
| 397 | PyErr_SetString(PyExc_SystemError, |
| 398 | "PyState_RemoveModule called on module with slots"); |
| 399 | return -1; |
| 400 | } |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 401 | state = GET_INTERP_STATE(); |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 402 | if (index == 0) { |
| 403 | Py_FatalError("PyState_RemoveModule: Module index invalid."); |
| 404 | return -1; |
| 405 | } |
| 406 | if (state->modules_by_index == NULL) { |
| 407 | Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible."); |
| 408 | return -1; |
| 409 | } |
| 410 | if (index > PyList_GET_SIZE(state->modules_by_index)) { |
| 411 | Py_FatalError("PyState_RemoveModule: Module index out of bounds."); |
| 412 | return -1; |
| 413 | } |
| 414 | return PyList_SetItem(state->modules_by_index, index, Py_None); |
| 415 | } |
| 416 | |
Antoine Pitrou | 40322e6 | 2013-08-11 00:30:09 +0200 | [diff] [blame] | 417 | /* used by import.c:PyImport_Cleanup */ |
| 418 | void |
| 419 | _PyState_ClearModules(void) |
| 420 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 421 | PyInterpreterState *state = GET_INTERP_STATE(); |
Antoine Pitrou | 40322e6 | 2013-08-11 00:30:09 +0200 | [diff] [blame] | 422 | if (state->modules_by_index) { |
| 423 | Py_ssize_t i; |
| 424 | for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) { |
| 425 | PyObject *m = PyList_GET_ITEM(state->modules_by_index, i); |
| 426 | if (PyModule_Check(m)) { |
| 427 | /* cleanup the saved copy of module dicts */ |
| 428 | PyModuleDef *md = PyModule_GetDef(m); |
| 429 | if (md) |
| 430 | Py_CLEAR(md->m_base.m_copy); |
| 431 | } |
| 432 | } |
| 433 | /* Setting modules_by_index to NULL could be dangerous, so we |
| 434 | clear the list instead. */ |
| 435 | if (PyList_SetSlice(state->modules_by_index, |
| 436 | 0, PyList_GET_SIZE(state->modules_by_index), |
| 437 | NULL)) |
| 438 | PyErr_WriteUnraisable(state->modules_by_index); |
| 439 | } |
| 440 | } |
| 441 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 442 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 443 | PyThreadState_Clear(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | if (Py_VerboseFlag && tstate->frame != NULL) |
| 446 | fprintf(stderr, |
| 447 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | Py_CLEAR(tstate->frame); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | Py_CLEAR(tstate->dict); |
| 452 | Py_CLEAR(tstate->async_exc); |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | Py_CLEAR(tstate->curexc_type); |
| 455 | Py_CLEAR(tstate->curexc_value); |
| 456 | Py_CLEAR(tstate->curexc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | Py_CLEAR(tstate->exc_type); |
| 459 | Py_CLEAR(tstate->exc_value); |
| 460 | Py_CLEAR(tstate->exc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | tstate->c_profilefunc = NULL; |
| 463 | tstate->c_tracefunc = NULL; |
| 464 | Py_CLEAR(tstate->c_profileobj); |
| 465 | Py_CLEAR(tstate->c_traceobj); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 466 | |
| 467 | Py_CLEAR(tstate->coroutine_wrapper); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 468 | Py_CLEAR(tstate->async_gen_firstiter); |
| 469 | Py_CLEAR(tstate->async_gen_finalizer); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 473 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
| 474 | static void |
| 475 | tstate_delete_common(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 476 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | PyInterpreterState *interp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | if (tstate == NULL) |
| 479 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
| 480 | interp = tstate->interp; |
| 481 | if (interp == NULL) |
| 482 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
| 483 | HEAD_LOCK(); |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 484 | if (tstate->prev) |
| 485 | tstate->prev->next = tstate->next; |
| 486 | else |
| 487 | interp->tstate_head = tstate->next; |
| 488 | if (tstate->next) |
| 489 | tstate->next->prev = tstate->prev; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | HEAD_UNLOCK(); |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 491 | if (tstate->on_delete != NULL) { |
| 492 | tstate->on_delete(tstate->on_delete_data); |
| 493 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 494 | PyMem_RawFree(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 498 | void |
| 499 | PyThreadState_Delete(PyThreadState *tstate) |
| 500 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 501 | if (tstate == GET_TSTATE()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
Tim Peters | f4e6928 | 2006-02-27 17:15:31 +0000 | [diff] [blame] | 503 | #ifdef WITH_THREAD |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 504 | if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | PyThread_delete_key_value(autoTLSkey); |
Tim Peters | f4e6928 | 2006-02-27 17:15:31 +0000 | [diff] [blame] | 506 | #endif /* WITH_THREAD */ |
Christian Heimes | b9dbc7d | 2013-07-01 23:42:28 +0200 | [diff] [blame] | 507 | tstate_delete_common(tstate); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | |
| 511 | #ifdef WITH_THREAD |
| 512 | void |
| 513 | PyThreadState_DeleteCurrent() |
| 514 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 515 | PyThreadState *tstate = GET_TSTATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | if (tstate == NULL) |
| 517 | Py_FatalError( |
| 518 | "PyThreadState_DeleteCurrent: no current tstate"); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 519 | tstate_delete_common(tstate); |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 520 | if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | PyThread_delete_key_value(autoTLSkey); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 522 | SET_TSTATE(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | PyEval_ReleaseLock(); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 524 | } |
| 525 | #endif /* WITH_THREAD */ |
| 526 | |
| 527 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 528 | /* |
| 529 | * Delete all thread states except the one passed as argument. |
| 530 | * Note that, if there is a current thread state, it *must* be the one |
| 531 | * passed as argument. Also, this won't touch any other interpreters |
| 532 | * than the current one, since we don't know which thread state should |
| 533 | * be kept in those other interpreteres. |
| 534 | */ |
| 535 | void |
| 536 | _PyThreadState_DeleteExcept(PyThreadState *tstate) |
| 537 | { |
| 538 | PyInterpreterState *interp = tstate->interp; |
| 539 | PyThreadState *p, *next, *garbage; |
| 540 | HEAD_LOCK(); |
| 541 | /* Remove all thread states, except tstate, from the linked list of |
| 542 | thread states. This will allow calling PyThreadState_Clear() |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 543 | without holding the lock. */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 544 | garbage = interp->tstate_head; |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 545 | if (garbage == tstate) |
| 546 | garbage = tstate->next; |
| 547 | if (tstate->prev) |
| 548 | tstate->prev->next = tstate->next; |
| 549 | if (tstate->next) |
| 550 | tstate->next->prev = tstate->prev; |
| 551 | tstate->prev = tstate->next = NULL; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 552 | interp->tstate_head = tstate; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 553 | HEAD_UNLOCK(); |
| 554 | /* Clear and deallocate all stale thread states. Even if this |
| 555 | executes Python code, we should be safe since it executes |
| 556 | in the current thread, not one of the stale threads. */ |
| 557 | for (p = garbage; p; p = next) { |
| 558 | next = p->next; |
| 559 | PyThreadState_Clear(p); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 560 | PyMem_RawFree(p); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
| 564 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 565 | PyThreadState * |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 566 | _PyThreadState_UncheckedGet(void) |
| 567 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 568 | return GET_TSTATE(); |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
| 572 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 573 | PyThreadState_Get(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 574 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 575 | PyThreadState *tstate = GET_TSTATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | if (tstate == NULL) |
| 577 | Py_FatalError("PyThreadState_Get: no current thread"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
| 583 | PyThreadState * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 584 | PyThreadState_Swap(PyThreadState *newts) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 585 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 586 | PyThreadState *oldts = GET_TSTATE(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 587 | |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 588 | SET_TSTATE(newts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | /* It should not be possible for more than one thread state |
| 590 | to be used for a thread. Check this the best we can in debug |
| 591 | builds. |
| 592 | */ |
Martin v. Löwis | 9e29625 | 2003-05-01 05:25:29 +0000 | [diff] [blame] | 593 | #if defined(Py_DEBUG) && defined(WITH_THREAD) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | if (newts) { |
| 595 | /* This can be called from PyEval_RestoreThread(). Similar |
| 596 | to it, we need to ensure errno doesn't change. |
| 597 | */ |
| 598 | int err = errno; |
| 599 | PyThreadState *check = PyGILState_GetThisThreadState(); |
| 600 | if (check && check->interp == newts->interp && check != newts) |
| 601 | Py_FatalError("Invalid thread state for this thread"); |
| 602 | errno = err; |
| 603 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 604 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | return oldts; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 606 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 607 | |
| 608 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 609 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 610 | state; the caller should pick a unique key and store its state there. If |
Guido van Rossum | 0fc8f00 | 2003-04-15 15:12:39 +0000 | [diff] [blame] | 611 | PyThreadState_GetDict() returns NULL, an exception has *not* been raised |
| 612 | and the caller should assume no per-thread state is available. */ |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 613 | |
| 614 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 615 | PyThreadState_GetDict(void) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 616 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 617 | PyThreadState *tstate = GET_TSTATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | if (tstate == NULL) |
| 619 | return NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | if (tstate->dict == NULL) { |
| 622 | PyObject *d; |
| 623 | tstate->dict = d = PyDict_New(); |
| 624 | if (d == NULL) |
| 625 | PyErr_Clear(); |
| 626 | } |
| 627 | return tstate->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 628 | } |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 629 | |
| 630 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 631 | /* Asynchronously raise an exception in a thread. |
| 632 | Requested by Just van Rossum and Alex Martelli. |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 633 | To prevent naive misuse, you must write your own extension |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 634 | to call this, or use ctypes. Must be called with the GIL held. |
| 635 | Returns the number of tstates modified (normally 1, but 0 if `id` didn't |
| 636 | match any known thread id). Can be called with exc=NULL to clear an |
| 637 | existing async exception. This raises no exceptions. */ |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 638 | |
| 639 | int |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 640 | PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) |
| 641 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 642 | PyInterpreterState *interp = GET_INTERP_STATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | PyThreadState *p; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | /* Although the GIL is held, a few C API functions can be called |
| 646 | * without the GIL held, and in particular some that create and |
| 647 | * destroy thread and interpreter states. Those can mutate the |
| 648 | * list of thread states we're traversing, so to prevent that we lock |
| 649 | * head_mutex for the duration. |
| 650 | */ |
| 651 | HEAD_LOCK(); |
| 652 | for (p = interp->tstate_head; p != NULL; p = p->next) { |
| 653 | if (p->thread_id == id) { |
| 654 | /* Tricky: we need to decref the current value |
| 655 | * (if any) in p->async_exc, but that can in turn |
| 656 | * allow arbitrary Python code to run, including |
| 657 | * perhaps calls to this function. To prevent |
| 658 | * deadlock, we need to release head_mutex before |
| 659 | * the decref. |
| 660 | */ |
| 661 | PyObject *old_exc = p->async_exc; |
| 662 | Py_XINCREF(exc); |
| 663 | p->async_exc = exc; |
| 664 | HEAD_UNLOCK(); |
| 665 | Py_XDECREF(old_exc); |
| 666 | _PyEval_SignalAsyncExc(); |
| 667 | return 1; |
| 668 | } |
| 669 | } |
| 670 | HEAD_UNLOCK(); |
| 671 | return 0; |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 675 | /* Routines for advanced debuggers, requested by David Beazley. |
| 676 | Don't use unless you know what you are doing! */ |
| 677 | |
| 678 | PyInterpreterState * |
| 679 | PyInterpreterState_Head(void) |
| 680 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | return interp_head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | PyInterpreterState * |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 685 | PyInterpreterState_Main(void) |
| 686 | { |
| 687 | return interp_main; |
| 688 | } |
| 689 | |
| 690 | PyInterpreterState * |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 691 | PyInterpreterState_Next(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | return interp->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | PyThreadState * |
| 696 | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | return interp->tstate_head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | PyThreadState * |
| 701 | PyThreadState_Next(PyThreadState *tstate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | return tstate->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 703 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 704 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 705 | /* The implementation of sys._current_frames(). This is intended to be |
| 706 | called with the GIL held, as it will be when called via |
| 707 | sys._current_frames(). It's possible it would work fine even without |
| 708 | the GIL held, but haven't thought enough about that. |
| 709 | */ |
| 710 | PyObject * |
| 711 | _PyThread_CurrentFrames(void) |
| 712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | PyObject *result; |
| 714 | PyInterpreterState *i; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | result = PyDict_New(); |
| 717 | if (result == NULL) |
| 718 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | /* for i in all interpreters: |
| 721 | * for t in all of i's thread states: |
| 722 | * if t's frame isn't NULL, map t's id to its frame |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 723 | * Because these lists can mutate even when the GIL is held, we |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 724 | * need to grab head_mutex for the duration. |
| 725 | */ |
| 726 | HEAD_LOCK(); |
| 727 | for (i = interp_head; i != NULL; i = i->next) { |
| 728 | PyThreadState *t; |
| 729 | for (t = i->tstate_head; t != NULL; t = t->next) { |
| 730 | PyObject *id; |
| 731 | int stat; |
| 732 | struct _frame *frame = t->frame; |
| 733 | if (frame == NULL) |
| 734 | continue; |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 735 | id = PyLong_FromUnsignedLong(t->thread_id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | if (id == NULL) |
| 737 | goto Fail; |
| 738 | stat = PyDict_SetItem(result, id, (PyObject *)frame); |
| 739 | Py_DECREF(id); |
| 740 | if (stat < 0) |
| 741 | goto Fail; |
| 742 | } |
| 743 | } |
| 744 | HEAD_UNLOCK(); |
| 745 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 746 | |
| 747 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | HEAD_UNLOCK(); |
| 749 | Py_DECREF(result); |
| 750 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 751 | } |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 752 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 753 | /* Python "auto thread state" API. */ |
| 754 | #ifdef WITH_THREAD |
| 755 | |
| 756 | /* Keep this as a static, as it is not reliable! It can only |
| 757 | ever be compared to the state for the *current* thread. |
| 758 | * If not equal, then it doesn't matter that the actual |
| 759 | value may change immediately after comparison, as it can't |
| 760 | possibly change to the current thread's state. |
| 761 | * If equal, then the current thread holds the lock, so the value can't |
| 762 | change until we yield the lock. |
| 763 | */ |
| 764 | static int |
| 765 | PyThreadState_IsCurrent(PyThreadState *tstate) |
| 766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | /* Must be the tstate for this thread */ |
| 768 | assert(PyGILState_GetThisThreadState()==tstate); |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 769 | return tstate == GET_TSTATE(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 772 | /* Internal initialization/finalization functions called by |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 773 | Py_Initialize/Py_FinalizeEx |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 774 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 775 | void |
| 776 | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | assert(i && t); /* must init with valid states */ |
| 779 | autoTLSkey = PyThread_create_key(); |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 780 | if (autoTLSkey == -1) |
| 781 | Py_FatalError("Could not allocate TLS entry"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | autoInterpreterState = i; |
| 783 | assert(PyThread_get_key_value(autoTLSkey) == NULL); |
| 784 | assert(t->gilstate_counter == 0); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | _PyGILState_NoteThreadState(t); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 789 | PyInterpreterState * |
| 790 | _PyGILState_GetInterpreterStateUnsafe(void) |
| 791 | { |
| 792 | return autoInterpreterState; |
| 793 | } |
| 794 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 795 | void |
| 796 | _PyGILState_Fini(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 797 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | PyThread_delete_key(autoTLSkey); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 799 | autoTLSkey = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | autoInterpreterState = NULL; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 803 | /* Reset the TLS key - called by PyOS_AfterFork_Child(). |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 804 | * This should not be necessary, but some - buggy - pthread implementations |
Charles-François Natali | a233df8 | 2011-11-22 19:49:51 +0100 | [diff] [blame] | 805 | * don't reset TLS upon fork(), see issue #10517. |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 806 | */ |
| 807 | void |
| 808 | _PyGILState_Reinit(void) |
| 809 | { |
Jason Fried | f82c951 | 2017-05-22 16:58:55 -0700 | [diff] [blame] | 810 | #ifdef WITH_THREAD |
| 811 | head_mutex = NULL; |
| 812 | HEAD_INIT(); |
| 813 | #endif |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 814 | PyThreadState *tstate = PyGILState_GetThisThreadState(); |
| 815 | PyThread_delete_key(autoTLSkey); |
| 816 | if ((autoTLSkey = PyThread_create_key()) == -1) |
| 817 | Py_FatalError("Could not allocate TLS entry"); |
| 818 | |
Charles-François Natali | a233df8 | 2011-11-22 19:49:51 +0100 | [diff] [blame] | 819 | /* If the thread had an associated auto thread state, reassociate it with |
| 820 | * the new key. */ |
| 821 | if (tstate && PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 822 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
| 823 | } |
| 824 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 825 | /* When a thread state is created for a thread by some mechanism other than |
| 826 | PyGILState_Ensure, it's important that the GILState machinery knows about |
| 827 | it so it doesn't try to create another thread state for the thread (this is |
| 828 | a better fix for SF bug #1010677 than the first one attempted). |
| 829 | */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 830 | static void |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 831 | _PyGILState_NoteThreadState(PyThreadState* tstate) |
| 832 | { |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 833 | /* If autoTLSkey isn't initialized, this must be the very first |
| 834 | threadstate created in Py_Initialize(). Don't do anything for now |
| 835 | (we'll be back here when _PyGILState_Init is called). */ |
| 836 | if (!autoInterpreterState) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | /* Stick the thread state for this thread in thread local storage. |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | The only situation where you can legitimately have more than one |
| 842 | thread state for an OS level thread is when there are multiple |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 843 | interpreters. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 844 | |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 845 | You shouldn't really be using the PyGILState_ APIs anyway (see issues |
| 846 | #10915 and #15751). |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 847 | |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 848 | The first thread state created for that given OS level thread will |
| 849 | "win", which seems reasonable behaviour. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | */ |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 851 | if (PyThread_get_key_value(autoTLSkey) == NULL) { |
| 852 | if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
| 853 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
| 854 | } |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | /* PyGILState_Release must not try to delete this thread state. */ |
| 857 | tstate->gilstate_counter = 1; |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 860 | /* The public functions */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 861 | PyThreadState * |
| 862 | PyGILState_GetThisThreadState(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 863 | { |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 864 | if (autoInterpreterState == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | return NULL; |
| 866 | return (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 869 | int |
| 870 | PyGILState_Check(void) |
| 871 | { |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 872 | PyThreadState *tstate; |
| 873 | |
| 874 | if (!_PyGILState_check_enabled) |
| 875 | return 1; |
| 876 | |
| 877 | if (autoTLSkey == -1) |
| 878 | return 1; |
| 879 | |
| 880 | tstate = GET_TSTATE(); |
| 881 | if (tstate == NULL) |
| 882 | return 0; |
| 883 | |
| 884 | return (tstate == PyGILState_GetThisThreadState()); |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 887 | PyGILState_STATE |
| 888 | PyGILState_Ensure(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | int current; |
| 891 | PyThreadState *tcur; |
| 892 | /* Note that we do not auto-init Python here - apart from |
| 893 | potential races with 2 threads auto-initializing, pep-311 |
| 894 | spells out other issues. Embedders are expected to have |
| 895 | called Py_Initialize() and usually PyEval_InitThreads(). |
| 896 | */ |
| 897 | assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ |
| 898 | tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
| 899 | if (tcur == NULL) { |
Victor Stinner | 62ca100 | 2013-12-13 01:46:43 +0100 | [diff] [blame] | 900 | /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is |
| 901 | called from a new thread for the first time, we need the create the |
| 902 | GIL. */ |
| 903 | PyEval_InitThreads(); |
| 904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | /* Create a new thread state for this thread */ |
| 906 | tcur = PyThreadState_New(autoInterpreterState); |
| 907 | if (tcur == NULL) |
| 908 | Py_FatalError("Couldn't create thread-state for new thread"); |
| 909 | /* This is our thread state! We'll need to delete it in the |
| 910 | matching call to PyGILState_Release(). */ |
| 911 | tcur->gilstate_counter = 0; |
| 912 | current = 0; /* new thread state is never current */ |
| 913 | } |
| 914 | else |
| 915 | current = PyThreadState_IsCurrent(tcur); |
| 916 | if (current == 0) |
| 917 | PyEval_RestoreThread(tcur); |
| 918 | /* Update our counter in the thread-state - no need for locks: |
| 919 | - tcur will remain valid as we hold the GIL. |
| 920 | - the counter is safe as we are the only thread "allowed" |
| 921 | to modify this value |
| 922 | */ |
| 923 | ++tcur->gilstate_counter; |
| 924 | return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 927 | void |
| 928 | PyGILState_Release(PyGILState_STATE oldstate) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 929 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( |
| 931 | autoTLSkey); |
| 932 | if (tcur == NULL) |
| 933 | Py_FatalError("auto-releasing thread-state, " |
| 934 | "but no thread-state for this thread"); |
| 935 | /* We must hold the GIL and have our thread state current */ |
| 936 | /* XXX - remove the check - the assert should be fine, |
| 937 | but while this is very new (April 2003), the extra check |
| 938 | by release-only users can't hurt. |
| 939 | */ |
| 940 | if (! PyThreadState_IsCurrent(tcur)) |
| 941 | Py_FatalError("This thread state must be current when releasing"); |
| 942 | assert(PyThreadState_IsCurrent(tcur)); |
| 943 | --tcur->gilstate_counter; |
| 944 | assert(tcur->gilstate_counter >= 0); /* illegal counter value */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | /* If we're going to destroy this thread-state, we must |
| 947 | * clear it while the GIL is held, as destructors may run. |
| 948 | */ |
| 949 | if (tcur->gilstate_counter == 0) { |
| 950 | /* can't have been locked when we created it */ |
| 951 | assert(oldstate == PyGILState_UNLOCKED); |
| 952 | PyThreadState_Clear(tcur); |
| 953 | /* Delete the thread-state. Note this releases the GIL too! |
| 954 | * It's vital that the GIL be held here, to avoid shutdown |
| 955 | * races; see bugs 225673 and 1061968 (that nasty bug has a |
| 956 | * habit of coming back). |
| 957 | */ |
| 958 | PyThreadState_DeleteCurrent(); |
| 959 | } |
| 960 | /* Release the lock if necessary */ |
| 961 | else if (oldstate == PyGILState_UNLOCKED) |
| 962 | PyEval_SaveThread(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 963 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 964 | |
Benjamin Peterson | 3bf0175 | 2012-04-13 18:06:36 -0400 | [diff] [blame] | 965 | #endif /* WITH_THREAD */ |
| 966 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 967 | #ifdef __cplusplus |
| 968 | } |
| 969 | #endif |
| 970 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 971 | |