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