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