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" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 5 | #include "pycore_pymem.h" |
| 6 | #include "pycore_pystate.h" |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 7 | |
Victor Stinner | 9204fb8 | 2018-10-30 15:13:17 +0100 | [diff] [blame] | 8 | #define _PyThreadState_SET(value) \ |
| 9 | _Py_atomic_store_relaxed(&_PyRuntime.gilstate.tstate_current, \ |
| 10 | (uintptr_t)(value)) |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 11 | |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 12 | |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 13 | /* -------------------------------------------------------------------------- |
| 14 | CAUTION |
| 15 | |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 16 | Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A |
| 17 | number of these functions are advertised as safe to call when the GIL isn't |
| 18 | held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's |
| 19 | debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL |
| 20 | to avoid the expense of doing their own locking). |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 21 | -------------------------------------------------------------------------- */ |
| 22 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 23 | #ifdef HAVE_DLOPEN |
| 24 | #ifdef HAVE_DLFCN_H |
| 25 | #include <dlfcn.h> |
| 26 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 27 | #if !HAVE_DECL_RTLD_LAZY |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 28 | #define RTLD_LAZY 1 |
| 29 | #endif |
| 30 | #endif |
| 31 | |
Benjamin Peterson | 43162b8 | 2012-04-13 11:58:27 -0400 | [diff] [blame] | 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 35 | |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 36 | static _PyInitError |
| 37 | _PyRuntimeState_Init_impl(_PyRuntimeState *runtime) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 38 | { |
| 39 | memset(runtime, 0, sizeof(*runtime)); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 40 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 41 | _PyGC_Initialize(&runtime->gc); |
| 42 | _PyEval_Initialize(&runtime->ceval); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 43 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 44 | runtime->gilstate.check_enabled = 1; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 45 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 46 | /* A TSS key must be initialized with Py_tss_NEEDS_INIT |
| 47 | in accordance with the specification. */ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 48 | Py_tss_t initial = Py_tss_NEEDS_INIT; |
| 49 | runtime->gilstate.autoTSSkey = initial; |
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(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 52 | if (runtime->interpreters.mutex == NULL) { |
| 53 | return _Py_INIT_ERR("Can't initialize threads for interpreter"); |
| 54 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 55 | runtime->interpreters.next_id = -1; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 56 | |
| 57 | runtime->xidregistry.mutex = PyThread_allocate_lock(); |
| 58 | if (runtime->xidregistry.mutex == NULL) { |
| 59 | return _Py_INIT_ERR("Can't initialize threads for cross-interpreter data registry"); |
| 60 | } |
| 61 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 62 | return _Py_INIT_OK(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 63 | } |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 64 | |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 65 | _PyInitError |
| 66 | _PyRuntimeState_Init(_PyRuntimeState *runtime) |
| 67 | { |
| 68 | /* Force default allocator, since _PyRuntimeState_Fini() must |
| 69 | use the same allocator than this function. */ |
| 70 | PyMemAllocatorEx old_alloc; |
| 71 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 72 | |
| 73 | _PyInitError err = _PyRuntimeState_Init_impl(runtime); |
| 74 | |
| 75 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 76 | return err; |
| 77 | } |
| 78 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 79 | void |
| 80 | _PyRuntimeState_Fini(_PyRuntimeState *runtime) |
| 81 | { |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 82 | /* Force the allocator used by _PyRuntimeState_Init(). */ |
| 83 | PyMemAllocatorEx old_alloc; |
| 84 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | ccb0442 | 2017-11-16 03:20:31 -0800 | [diff] [blame] | 85 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 86 | if (runtime->interpreters.mutex != NULL) { |
| 87 | PyThread_free_lock(runtime->interpreters.mutex); |
| 88 | runtime->interpreters.mutex = NULL; |
| 89 | } |
Victor Stinner | ccb0442 | 2017-11-16 03:20:31 -0800 | [diff] [blame] | 90 | |
| 91 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | #define HEAD_LOCK() PyThread_acquire_lock(_PyRuntime.interpreters.mutex, \ |
| 95 | WAIT_LOCK) |
| 96 | #define HEAD_UNLOCK() PyThread_release_lock(_PyRuntime.interpreters.mutex) |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 97 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 98 | static void _PyGILState_NoteThreadState(PyThreadState* tstate); |
| 99 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 100 | _PyInitError |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 101 | _PyInterpreterState_Enable(_PyRuntimeState *runtime) |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 102 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 103 | runtime->interpreters.next_id = 0; |
Victor Stinner | 5d92647 | 2018-03-06 14:31:37 +0100 | [diff] [blame] | 104 | |
| 105 | /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex. |
| 106 | Create a new mutex if needed. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 107 | if (runtime->interpreters.mutex == NULL) { |
Victor Stinner | 5d92647 | 2018-03-06 14:31:37 +0100 | [diff] [blame] | 108 | /* Force default allocator, since _PyRuntimeState_Fini() must |
| 109 | use the same allocator than this function. */ |
| 110 | PyMemAllocatorEx old_alloc; |
| 111 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 112 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 113 | runtime->interpreters.mutex = PyThread_allocate_lock(); |
Victor Stinner | 5d92647 | 2018-03-06 14:31:37 +0100 | [diff] [blame] | 114 | |
| 115 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 116 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 117 | if (runtime->interpreters.mutex == NULL) { |
| 118 | return _Py_INIT_ERR("Can't initialize threads for interpreter"); |
| 119 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 120 | } |
Victor Stinner | 5d92647 | 2018-03-06 14:31:37 +0100 | [diff] [blame] | 121 | |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 122 | return _Py_INIT_OK(); |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 123 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 124 | |
| 125 | PyInterpreterState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 126 | PyInterpreterState_New(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 127 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | PyInterpreterState *interp = (PyInterpreterState *) |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 129 | PyMem_RawMalloc(sizeof(PyInterpreterState)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 130 | |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 131 | if (interp == NULL) { |
| 132 | return NULL; |
| 133 | } |
| 134 | |
Eric Snow | 4c6955e | 2018-02-16 18:53:40 -0700 | [diff] [blame] | 135 | interp->id_refcount = -1; |
| 136 | interp->id_mutex = NULL; |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 137 | interp->modules = NULL; |
| 138 | interp->modules_by_index = NULL; |
| 139 | interp->sysdict = NULL; |
| 140 | interp->builtins = NULL; |
| 141 | interp->builtins_copy = NULL; |
| 142 | interp->tstate_head = NULL; |
| 143 | interp->check_interval = 100; |
| 144 | interp->num_threads = 0; |
| 145 | interp->pythread_stacksize = 0; |
| 146 | interp->codec_search_path = NULL; |
| 147 | interp->codec_search_cache = NULL; |
| 148 | interp->codec_error_registry = NULL; |
| 149 | interp->codecs_initialized = 0; |
| 150 | interp->fscodec_initialized = 0; |
| 151 | interp->core_config = _PyCoreConfig_INIT; |
| 152 | interp->config = _PyMainInterpreterConfig_INIT; |
| 153 | interp->importlib = NULL; |
| 154 | interp->import_func = NULL; |
| 155 | interp->eval_frame = _PyEval_EvalFrameDefault; |
| 156 | interp->co_extra_user_count = 0; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 157 | #ifdef HAVE_DLOPEN |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 158 | #if HAVE_DECL_RTLD_NOW |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 159 | interp->dlopenflags = RTLD_NOW; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 160 | #else |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 161 | interp->dlopenflags = RTLD_LAZY; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 162 | #endif |
| 163 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 164 | #ifdef HAVE_FORK |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 165 | interp->before_forkers = NULL; |
| 166 | interp->after_forkers_parent = NULL; |
| 167 | interp->after_forkers_child = NULL; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 168 | #endif |
Marcel Plch | 776407f | 2017-12-20 11:17:58 +0100 | [diff] [blame] | 169 | interp->pyexitfunc = NULL; |
| 170 | interp->pyexitmodule = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 171 | |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 172 | HEAD_LOCK(); |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 173 | if (_PyRuntime.interpreters.next_id < 0) { |
| 174 | /* overflow or Py_Initialize() not called! */ |
| 175 | PyErr_SetString(PyExc_RuntimeError, |
| 176 | "failed to get an interpreter ID"); |
Pablo Galindo | 95d630e | 2018-08-31 22:49:29 +0100 | [diff] [blame] | 177 | PyMem_RawFree(interp); |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 178 | interp = NULL; |
| 179 | } else { |
| 180 | interp->id = _PyRuntime.interpreters.next_id; |
| 181 | _PyRuntime.interpreters.next_id += 1; |
Pablo Galindo | 95d630e | 2018-08-31 22:49:29 +0100 | [diff] [blame] | 182 | interp->next = _PyRuntime.interpreters.head; |
| 183 | if (_PyRuntime.interpreters.main == NULL) { |
| 184 | _PyRuntime.interpreters.main = interp; |
| 185 | } |
| 186 | _PyRuntime.interpreters.head = interp; |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 187 | } |
| 188 | HEAD_UNLOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 189 | |
Pablo Galindo | 95d630e | 2018-08-31 22:49:29 +0100 | [diff] [blame] | 190 | if (interp == NULL) { |
| 191 | return NULL; |
| 192 | } |
| 193 | |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 194 | interp->tstate_next_unique_id = 0; |
| 195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | return interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
| 200 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 201 | PyInterpreterState_Clear(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 202 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | PyThreadState *p; |
| 204 | HEAD_LOCK(); |
| 205 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 206 | PyThreadState_Clear(p); |
| 207 | HEAD_UNLOCK(); |
Victor Stinner | da27341 | 2017-12-15 01:46:02 +0100 | [diff] [blame] | 208 | _PyCoreConfig_Clear(&interp->core_config); |
| 209 | _PyMainInterpreterConfig_Clear(&interp->config); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | Py_CLEAR(interp->codec_search_path); |
| 211 | Py_CLEAR(interp->codec_search_cache); |
| 212 | Py_CLEAR(interp->codec_error_registry); |
Eric Snow | 93c92f7 | 2017-09-13 23:46:04 -0700 | [diff] [blame] | 213 | Py_CLEAR(interp->modules); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | Py_CLEAR(interp->modules_by_index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | Py_CLEAR(interp->sysdict); |
| 216 | Py_CLEAR(interp->builtins); |
Serhiy Storchaka | 87a5c51 | 2014-02-10 18:21:34 +0200 | [diff] [blame] | 217 | Py_CLEAR(interp->builtins_copy); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 218 | Py_CLEAR(interp->importlib); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 219 | Py_CLEAR(interp->import_func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 220 | #ifdef HAVE_FORK |
| 221 | Py_CLEAR(interp->before_forkers); |
| 222 | Py_CLEAR(interp->after_forkers_parent); |
| 223 | Py_CLEAR(interp->after_forkers_child); |
| 224 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
| 228 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 229 | zapthreads(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | PyThreadState *p; |
| 232 | /* No need to lock the mutex here because this should only happen |
| 233 | when the threads are all really dead (XXX famous last words). */ |
| 234 | while ((p = interp->tstate_head) != NULL) { |
| 235 | PyThreadState_Delete(p); |
| 236 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
| 240 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 241 | PyInterpreterState_Delete(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | PyInterpreterState **p; |
| 244 | zapthreads(interp); |
| 245 | HEAD_LOCK(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 246 | for (p = &_PyRuntime.interpreters.head; ; p = &(*p)->next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | if (*p == NULL) |
| 248 | Py_FatalError( |
| 249 | "PyInterpreterState_Delete: invalid interp"); |
| 250 | if (*p == interp) |
| 251 | break; |
| 252 | } |
| 253 | if (interp->tstate_head != NULL) |
| 254 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 255 | *p = interp->next; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 256 | if (_PyRuntime.interpreters.main == interp) { |
| 257 | _PyRuntime.interpreters.main = NULL; |
| 258 | if (_PyRuntime.interpreters.head != NULL) |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 259 | Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters"); |
| 260 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | HEAD_UNLOCK(); |
Eric Snow | 4c6955e | 2018-02-16 18:53:40 -0700 | [diff] [blame] | 262 | if (interp->id_mutex != NULL) { |
| 263 | PyThread_free_lock(interp->id_mutex); |
| 264 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 265 | PyMem_RawFree(interp); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 269 | /* |
| 270 | * Delete all interpreter states except the main interpreter. If there |
| 271 | * is a current interpreter state, it *must* be the main interpreter. |
| 272 | */ |
| 273 | void |
| 274 | _PyInterpreterState_DeleteExceptMain() |
| 275 | { |
| 276 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 277 | if (tstate != NULL && tstate->interp != _PyRuntime.interpreters.main) { |
| 278 | Py_FatalError("PyInterpreterState_DeleteExceptMain: not main interpreter"); |
| 279 | } |
| 280 | |
| 281 | HEAD_LOCK(); |
| 282 | PyInterpreterState *interp = _PyRuntime.interpreters.head; |
| 283 | _PyRuntime.interpreters.head = NULL; |
| 284 | for (; interp != NULL; interp = interp->next) { |
| 285 | if (interp == _PyRuntime.interpreters.main) { |
| 286 | _PyRuntime.interpreters.main->next = NULL; |
| 287 | _PyRuntime.interpreters.head = interp; |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | PyInterpreterState_Clear(interp); // XXX must activate? |
| 292 | zapthreads(interp); |
| 293 | if (interp->id_mutex != NULL) { |
| 294 | PyThread_free_lock(interp->id_mutex); |
| 295 | } |
| 296 | PyMem_RawFree(interp); |
| 297 | } |
| 298 | HEAD_UNLOCK(); |
| 299 | |
| 300 | if (_PyRuntime.interpreters.head == NULL) { |
| 301 | Py_FatalError("PyInterpreterState_DeleteExceptMain: missing main"); |
| 302 | } |
| 303 | PyThreadState_Swap(tstate); |
| 304 | } |
| 305 | |
| 306 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 307 | PyInterpreterState * |
| 308 | _PyInterpreterState_Get(void) |
| 309 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 310 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 311 | if (tstate == NULL) { |
| 312 | Py_FatalError("_PyInterpreterState_Get(): no current thread state"); |
| 313 | } |
| 314 | PyInterpreterState *interp = tstate->interp; |
| 315 | if (interp == NULL) { |
| 316 | Py_FatalError("_PyInterpreterState_Get(): no current interpreter"); |
| 317 | } |
| 318 | return interp; |
| 319 | } |
| 320 | |
| 321 | |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 322 | int64_t |
| 323 | PyInterpreterState_GetID(PyInterpreterState *interp) |
| 324 | { |
| 325 | if (interp == NULL) { |
| 326 | PyErr_SetString(PyExc_RuntimeError, "no interpreter provided"); |
| 327 | return -1; |
| 328 | } |
| 329 | return interp->id; |
| 330 | } |
| 331 | |
| 332 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 333 | PyInterpreterState * |
| 334 | _PyInterpreterState_LookUpID(PY_INT64_T requested_id) |
| 335 | { |
| 336 | if (requested_id < 0) |
| 337 | goto error; |
| 338 | |
| 339 | PyInterpreterState *interp = PyInterpreterState_Head(); |
| 340 | while (interp != NULL) { |
| 341 | PY_INT64_T id = PyInterpreterState_GetID(interp); |
| 342 | if (id < 0) |
| 343 | return NULL; |
| 344 | if (requested_id == id) |
| 345 | return interp; |
| 346 | interp = PyInterpreterState_Next(interp); |
| 347 | } |
| 348 | |
| 349 | error: |
| 350 | PyErr_Format(PyExc_RuntimeError, |
| 351 | "unrecognized interpreter ID %lld", requested_id); |
| 352 | return NULL; |
| 353 | } |
| 354 | |
Eric Snow | 4c6955e | 2018-02-16 18:53:40 -0700 | [diff] [blame] | 355 | |
| 356 | int |
| 357 | _PyInterpreterState_IDInitref(PyInterpreterState *interp) |
| 358 | { |
| 359 | if (interp->id_mutex != NULL) { |
| 360 | return 0; |
| 361 | } |
| 362 | interp->id_mutex = PyThread_allocate_lock(); |
| 363 | if (interp->id_mutex == NULL) { |
| 364 | PyErr_SetString(PyExc_RuntimeError, |
| 365 | "failed to create init interpreter ID mutex"); |
| 366 | return -1; |
| 367 | } |
| 368 | interp->id_refcount = 0; |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | void |
| 374 | _PyInterpreterState_IDIncref(PyInterpreterState *interp) |
| 375 | { |
| 376 | if (interp->id_mutex == NULL) { |
| 377 | return; |
| 378 | } |
| 379 | PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK); |
| 380 | interp->id_refcount += 1; |
| 381 | PyThread_release_lock(interp->id_mutex); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | void |
| 386 | _PyInterpreterState_IDDecref(PyInterpreterState *interp) |
| 387 | { |
| 388 | if (interp->id_mutex == NULL) { |
| 389 | return; |
| 390 | } |
| 391 | PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK); |
| 392 | assert(interp->id_refcount != 0); |
| 393 | interp->id_refcount -= 1; |
| 394 | int64_t refcount = interp->id_refcount; |
| 395 | PyThread_release_lock(interp->id_mutex); |
| 396 | |
| 397 | if (refcount == 0) { |
Eric Snow | f53d9f2 | 2018-02-20 16:30:17 -0700 | [diff] [blame] | 398 | // XXX Using the "head" thread isn't strictly correct. |
| 399 | PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); |
| 400 | // XXX Possible GILState issues? |
| 401 | PyThreadState *save_tstate = PyThreadState_Swap(tstate); |
Eric Snow | 4c6955e | 2018-02-16 18:53:40 -0700 | [diff] [blame] | 402 | Py_EndInterpreter(tstate); |
| 403 | PyThreadState_Swap(save_tstate); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 408 | /* Default implementation for _PyThreadState_GetFrame */ |
| 409 | static struct _frame * |
| 410 | threadstate_getframe(PyThreadState *self) |
| 411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | return self->frame; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 415 | static PyThreadState * |
| 416 | new_threadstate(PyInterpreterState *interp, int init) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 417 | { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 418 | PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState)); |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | if (_PyThreadState_GetFrame == NULL) |
| 421 | _PyThreadState_GetFrame = threadstate_getframe; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | if (tstate != NULL) { |
| 424 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | tstate->frame = NULL; |
| 427 | tstate->recursion_depth = 0; |
| 428 | tstate->overflowed = 0; |
| 429 | tstate->recursion_critical = 0; |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 430 | tstate->stackcheck_counter = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | tstate->tracing = 0; |
| 432 | tstate->use_tracing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | tstate->gilstate_counter = 0; |
| 434 | tstate->async_exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | tstate->thread_id = PyThread_get_thread_ident(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | tstate->dict = NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | tstate->curexc_type = NULL; |
| 440 | tstate->curexc_value = NULL; |
| 441 | tstate->curexc_traceback = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 442 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 443 | tstate->exc_state.exc_type = NULL; |
| 444 | tstate->exc_state.exc_value = NULL; |
| 445 | tstate->exc_state.exc_traceback = NULL; |
| 446 | tstate->exc_state.previous_item = NULL; |
| 447 | tstate->exc_info = &tstate->exc_state; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | tstate->c_profilefunc = NULL; |
| 450 | tstate->c_tracefunc = NULL; |
| 451 | tstate->c_profileobj = NULL; |
| 452 | tstate->c_traceobj = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 453 | |
Antoine Pitrou | 2b0218a | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 454 | tstate->trash_delete_nesting = 0; |
| 455 | tstate->trash_delete_later = NULL; |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 456 | tstate->on_delete = NULL; |
| 457 | tstate->on_delete_data = NULL; |
Antoine Pitrou | 2b0218a | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 458 | |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 459 | tstate->coroutine_origin_tracking_depth = 0; |
| 460 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 461 | tstate->coroutine_wrapper = NULL; |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 462 | tstate->in_coroutine_wrapper = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 463 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 464 | tstate->async_gen_firstiter = NULL; |
| 465 | tstate->async_gen_finalizer = NULL; |
| 466 | |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 467 | tstate->context = NULL; |
| 468 | tstate->context_ver = 1; |
| 469 | |
| 470 | tstate->id = ++interp->tstate_next_unique_id; |
| 471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | if (init) |
| 473 | _PyThreadState_Init(tstate); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | HEAD_LOCK(); |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 476 | tstate->prev = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | tstate->next = interp->tstate_head; |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 478 | if (tstate->next) |
| 479 | tstate->next->prev = tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | interp->tstate_head = tstate; |
| 481 | HEAD_UNLOCK(); |
| 482 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 487 | PyThreadState * |
| 488 | PyThreadState_New(PyInterpreterState *interp) |
| 489 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | return new_threadstate(interp, 1); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | PyThreadState * |
| 494 | _PyThreadState_Prealloc(PyInterpreterState *interp) |
| 495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | return new_threadstate(interp, 0); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void |
| 500 | _PyThreadState_Init(PyThreadState *tstate) |
| 501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | _PyGILState_NoteThreadState(tstate); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 505 | PyObject* |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 506 | PyState_FindModule(struct PyModuleDef* module) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 507 | { |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 508 | Py_ssize_t index = module->m_base.m_index; |
Victor Stinner | 9204fb8 | 2018-10-30 15:13:17 +0100 | [diff] [blame] | 509 | PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | PyObject *res; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 511 | if (module->m_slots) { |
| 512 | return NULL; |
| 513 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | if (index == 0) |
| 515 | return NULL; |
| 516 | if (state->modules_by_index == NULL) |
| 517 | return NULL; |
Antoine Pitrou | 75506e8 | 2012-08-20 19:30:46 +0200 | [diff] [blame] | 518 | if (index >= PyList_GET_SIZE(state->modules_by_index)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | return NULL; |
| 520 | res = PyList_GET_ITEM(state->modules_by_index, index); |
| 521 | return res==Py_None ? NULL : res; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | int |
| 525 | _PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
| 526 | { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 527 | PyInterpreterState *state; |
Berker Peksag | 4b7b565 | 2016-08-22 18:05:56 +0300 | [diff] [blame] | 528 | if (!def) { |
| 529 | assert(PyErr_Occurred()); |
| 530 | return -1; |
| 531 | } |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 532 | if (def->m_slots) { |
| 533 | PyErr_SetString(PyExc_SystemError, |
| 534 | "PyState_AddModule called on module with slots"); |
| 535 | return -1; |
| 536 | } |
Victor Stinner | 9204fb8 | 2018-10-30 15:13:17 +0100 | [diff] [blame] | 537 | state = _PyInterpreterState_GET_UNSAFE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | if (!state->modules_by_index) { |
| 539 | state->modules_by_index = PyList_New(0); |
| 540 | if (!state->modules_by_index) |
| 541 | return -1; |
| 542 | } |
| 543 | while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index) |
| 544 | if (PyList_Append(state->modules_by_index, Py_None) < 0) |
| 545 | return -1; |
| 546 | Py_INCREF(module); |
| 547 | return PyList_SetItem(state->modules_by_index, |
| 548 | def->m_base.m_index, module); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 549 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 550 | |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 551 | int |
| 552 | PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
| 553 | { |
| 554 | Py_ssize_t index; |
Victor Stinner | 9204fb8 | 2018-10-30 15:13:17 +0100 | [diff] [blame] | 555 | PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE(); |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 556 | if (!def) { |
| 557 | Py_FatalError("PyState_AddModule: Module Definition is NULL"); |
| 558 | return -1; |
| 559 | } |
| 560 | index = def->m_base.m_index; |
| 561 | if (state->modules_by_index) { |
| 562 | if(PyList_GET_SIZE(state->modules_by_index) >= index) { |
| 563 | if(module == PyList_GET_ITEM(state->modules_by_index, index)) { |
| 564 | Py_FatalError("PyState_AddModule: Module already added!"); |
| 565 | return -1; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | return _PyState_AddModule(module, def); |
| 570 | } |
| 571 | |
| 572 | int |
| 573 | PyState_RemoveModule(struct PyModuleDef* def) |
| 574 | { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 575 | PyInterpreterState *state; |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 576 | Py_ssize_t index = def->m_base.m_index; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 577 | if (def->m_slots) { |
| 578 | PyErr_SetString(PyExc_SystemError, |
| 579 | "PyState_RemoveModule called on module with slots"); |
| 580 | return -1; |
| 581 | } |
Victor Stinner | 9204fb8 | 2018-10-30 15:13:17 +0100 | [diff] [blame] | 582 | state = _PyInterpreterState_GET_UNSAFE(); |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 583 | if (index == 0) { |
| 584 | Py_FatalError("PyState_RemoveModule: Module index invalid."); |
| 585 | return -1; |
| 586 | } |
| 587 | if (state->modules_by_index == NULL) { |
| 588 | Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible."); |
| 589 | return -1; |
| 590 | } |
| 591 | if (index > PyList_GET_SIZE(state->modules_by_index)) { |
| 592 | Py_FatalError("PyState_RemoveModule: Module index out of bounds."); |
| 593 | return -1; |
| 594 | } |
Zackery Spytz | 2a89343 | 2018-12-05 00:14:00 -0700 | [diff] [blame] | 595 | Py_INCREF(Py_None); |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 596 | return PyList_SetItem(state->modules_by_index, index, Py_None); |
| 597 | } |
| 598 | |
Antoine Pitrou | 40322e6 | 2013-08-11 00:30:09 +0200 | [diff] [blame] | 599 | /* used by import.c:PyImport_Cleanup */ |
| 600 | void |
| 601 | _PyState_ClearModules(void) |
| 602 | { |
Victor Stinner | 9204fb8 | 2018-10-30 15:13:17 +0100 | [diff] [blame] | 603 | PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE(); |
Antoine Pitrou | 40322e6 | 2013-08-11 00:30:09 +0200 | [diff] [blame] | 604 | if (state->modules_by_index) { |
| 605 | Py_ssize_t i; |
| 606 | for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) { |
| 607 | PyObject *m = PyList_GET_ITEM(state->modules_by_index, i); |
| 608 | if (PyModule_Check(m)) { |
| 609 | /* cleanup the saved copy of module dicts */ |
| 610 | PyModuleDef *md = PyModule_GetDef(m); |
| 611 | if (md) |
| 612 | Py_CLEAR(md->m_base.m_copy); |
| 613 | } |
| 614 | } |
| 615 | /* Setting modules_by_index to NULL could be dangerous, so we |
| 616 | clear the list instead. */ |
| 617 | if (PyList_SetSlice(state->modules_by_index, |
| 618 | 0, PyList_GET_SIZE(state->modules_by_index), |
| 619 | NULL)) |
| 620 | PyErr_WriteUnraisable(state->modules_by_index); |
| 621 | } |
| 622 | } |
| 623 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 624 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 625 | PyThreadState_Clear(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 626 | { |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 627 | int verbose = tstate->interp->core_config.verbose; |
| 628 | |
| 629 | if (verbose && tstate->frame != NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | fprintf(stderr, |
| 631 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | Py_CLEAR(tstate->frame); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | Py_CLEAR(tstate->dict); |
| 636 | Py_CLEAR(tstate->async_exc); |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | Py_CLEAR(tstate->curexc_type); |
| 639 | Py_CLEAR(tstate->curexc_value); |
| 640 | Py_CLEAR(tstate->curexc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 641 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 642 | Py_CLEAR(tstate->exc_state.exc_type); |
| 643 | Py_CLEAR(tstate->exc_state.exc_value); |
| 644 | Py_CLEAR(tstate->exc_state.exc_traceback); |
Serhiy Storchaka | bdf4298 | 2017-10-26 16:59:40 +0300 | [diff] [blame] | 645 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 646 | /* The stack of exception states should contain just this thread. */ |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 647 | if (verbose && tstate->exc_info != &tstate->exc_state) { |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 648 | fprintf(stderr, |
| 649 | "PyThreadState_Clear: warning: thread still has a generator\n"); |
| 650 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | tstate->c_profilefunc = NULL; |
| 653 | tstate->c_tracefunc = NULL; |
| 654 | Py_CLEAR(tstate->c_profileobj); |
| 655 | Py_CLEAR(tstate->c_traceobj); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 656 | |
| 657 | Py_CLEAR(tstate->coroutine_wrapper); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 658 | Py_CLEAR(tstate->async_gen_firstiter); |
| 659 | Py_CLEAR(tstate->async_gen_finalizer); |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 660 | |
| 661 | Py_CLEAR(tstate->context); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 665 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
| 666 | static void |
| 667 | tstate_delete_common(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 668 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | PyInterpreterState *interp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | if (tstate == NULL) |
| 671 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
| 672 | interp = tstate->interp; |
| 673 | if (interp == NULL) |
| 674 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
| 675 | HEAD_LOCK(); |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 676 | if (tstate->prev) |
| 677 | tstate->prev->next = tstate->next; |
| 678 | else |
| 679 | interp->tstate_head = tstate->next; |
| 680 | if (tstate->next) |
| 681 | tstate->next->prev = tstate->prev; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | HEAD_UNLOCK(); |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 683 | if (tstate->on_delete != NULL) { |
| 684 | tstate->on_delete(tstate->on_delete_data); |
| 685 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 686 | PyMem_RawFree(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 690 | void |
| 691 | PyThreadState_Delete(PyThreadState *tstate) |
| 692 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 693 | if (tstate == _PyThreadState_GET()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 695 | if (_PyRuntime.gilstate.autoInterpreterState && |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 696 | PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == tstate) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 697 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 698 | PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, NULL); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 699 | } |
Christian Heimes | b9dbc7d | 2013-07-01 23:42:28 +0200 | [diff] [blame] | 700 | tstate_delete_common(tstate); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 704 | void |
| 705 | PyThreadState_DeleteCurrent() |
| 706 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 707 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | if (tstate == NULL) |
| 709 | Py_FatalError( |
| 710 | "PyThreadState_DeleteCurrent: no current tstate"); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 711 | tstate_delete_common(tstate); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 712 | if (_PyRuntime.gilstate.autoInterpreterState && |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 713 | PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == tstate) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 714 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 715 | PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, NULL); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 716 | } |
Victor Stinner | 9204fb8 | 2018-10-30 15:13:17 +0100 | [diff] [blame] | 717 | _PyThreadState_SET(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | PyEval_ReleaseLock(); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 719 | } |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 720 | |
| 721 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 722 | /* |
| 723 | * Delete all thread states except the one passed as argument. |
| 724 | * Note that, if there is a current thread state, it *must* be the one |
| 725 | * passed as argument. Also, this won't touch any other interpreters |
| 726 | * than the current one, since we don't know which thread state should |
| 727 | * be kept in those other interpreteres. |
| 728 | */ |
| 729 | void |
| 730 | _PyThreadState_DeleteExcept(PyThreadState *tstate) |
| 731 | { |
| 732 | PyInterpreterState *interp = tstate->interp; |
| 733 | PyThreadState *p, *next, *garbage; |
| 734 | HEAD_LOCK(); |
| 735 | /* Remove all thread states, except tstate, from the linked list of |
| 736 | thread states. This will allow calling PyThreadState_Clear() |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 737 | without holding the lock. */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 738 | garbage = interp->tstate_head; |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 739 | if (garbage == tstate) |
| 740 | garbage = tstate->next; |
| 741 | if (tstate->prev) |
| 742 | tstate->prev->next = tstate->next; |
| 743 | if (tstate->next) |
| 744 | tstate->next->prev = tstate->prev; |
| 745 | tstate->prev = tstate->next = NULL; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 746 | interp->tstate_head = tstate; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 747 | HEAD_UNLOCK(); |
| 748 | /* Clear and deallocate all stale thread states. Even if this |
| 749 | executes Python code, we should be safe since it executes |
| 750 | in the current thread, not one of the stale threads. */ |
| 751 | for (p = garbage; p; p = next) { |
| 752 | next = p->next; |
| 753 | PyThreadState_Clear(p); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 754 | PyMem_RawFree(p); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 759 | PyThreadState * |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 760 | _PyThreadState_UncheckedGet(void) |
| 761 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 762 | return _PyThreadState_GET(); |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | |
| 766 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 767 | PyThreadState_Get(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 768 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 769 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | if (tstate == NULL) |
| 771 | Py_FatalError("PyThreadState_Get: no current thread"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | |
| 777 | PyThreadState * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 778 | PyThreadState_Swap(PyThreadState *newts) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 779 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 780 | PyThreadState *oldts = _PyThreadState_GET(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 781 | |
Victor Stinner | 9204fb8 | 2018-10-30 15:13:17 +0100 | [diff] [blame] | 782 | _PyThreadState_SET(newts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | /* It should not be possible for more than one thread state |
| 784 | to be used for a thread. Check this the best we can in debug |
| 785 | builds. |
| 786 | */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 787 | #if defined(Py_DEBUG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | if (newts) { |
| 789 | /* This can be called from PyEval_RestoreThread(). Similar |
| 790 | to it, we need to ensure errno doesn't change. |
| 791 | */ |
| 792 | int err = errno; |
| 793 | PyThreadState *check = PyGILState_GetThisThreadState(); |
| 794 | if (check && check->interp == newts->interp && check != newts) |
| 795 | Py_FatalError("Invalid thread state for this thread"); |
| 796 | errno = err; |
| 797 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 798 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | return oldts; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 800 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 801 | |
| 802 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 803 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 804 | 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] | 805 | PyThreadState_GetDict() returns NULL, an exception has *not* been raised |
| 806 | and the caller should assume no per-thread state is available. */ |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 807 | |
| 808 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 809 | PyThreadState_GetDict(void) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 810 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 811 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | if (tstate == NULL) |
| 813 | return NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | if (tstate->dict == NULL) { |
| 816 | PyObject *d; |
| 817 | tstate->dict = d = PyDict_New(); |
| 818 | if (d == NULL) |
| 819 | PyErr_Clear(); |
| 820 | } |
| 821 | return tstate->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 822 | } |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 823 | |
| 824 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 825 | /* Asynchronously raise an exception in a thread. |
| 826 | Requested by Just van Rossum and Alex Martelli. |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 827 | To prevent naive misuse, you must write your own extension |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 828 | to call this, or use ctypes. Must be called with the GIL held. |
| 829 | Returns the number of tstates modified (normally 1, but 0 if `id` didn't |
| 830 | match any known thread id). Can be called with exc=NULL to clear an |
| 831 | existing async exception. This raises no exceptions. */ |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 832 | |
| 833 | int |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 834 | PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) |
| 835 | { |
Victor Stinner | 9204fb8 | 2018-10-30 15:13:17 +0100 | [diff] [blame] | 836 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | PyThreadState *p; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | /* Although the GIL is held, a few C API functions can be called |
| 840 | * without the GIL held, and in particular some that create and |
| 841 | * destroy thread and interpreter states. Those can mutate the |
| 842 | * list of thread states we're traversing, so to prevent that we lock |
| 843 | * head_mutex for the duration. |
| 844 | */ |
| 845 | HEAD_LOCK(); |
| 846 | for (p = interp->tstate_head; p != NULL; p = p->next) { |
| 847 | if (p->thread_id == id) { |
| 848 | /* Tricky: we need to decref the current value |
| 849 | * (if any) in p->async_exc, but that can in turn |
| 850 | * allow arbitrary Python code to run, including |
| 851 | * perhaps calls to this function. To prevent |
| 852 | * deadlock, we need to release head_mutex before |
| 853 | * the decref. |
| 854 | */ |
| 855 | PyObject *old_exc = p->async_exc; |
| 856 | Py_XINCREF(exc); |
| 857 | p->async_exc = exc; |
| 858 | HEAD_UNLOCK(); |
| 859 | Py_XDECREF(old_exc); |
| 860 | _PyEval_SignalAsyncExc(); |
| 861 | return 1; |
| 862 | } |
| 863 | } |
| 864 | HEAD_UNLOCK(); |
| 865 | return 0; |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 869 | /* Routines for advanced debuggers, requested by David Beazley. |
| 870 | Don't use unless you know what you are doing! */ |
| 871 | |
| 872 | PyInterpreterState * |
| 873 | PyInterpreterState_Head(void) |
| 874 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 875 | return _PyRuntime.interpreters.head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | PyInterpreterState * |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 879 | PyInterpreterState_Main(void) |
| 880 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 881 | return _PyRuntime.interpreters.main; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | PyInterpreterState * |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 885 | PyInterpreterState_Next(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | return interp->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | PyThreadState * |
| 890 | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | return interp->tstate_head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | PyThreadState * |
| 895 | PyThreadState_Next(PyThreadState *tstate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | return tstate->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 897 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 898 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 899 | /* The implementation of sys._current_frames(). This is intended to be |
| 900 | called with the GIL held, as it will be when called via |
| 901 | sys._current_frames(). It's possible it would work fine even without |
| 902 | the GIL held, but haven't thought enough about that. |
| 903 | */ |
| 904 | PyObject * |
| 905 | _PyThread_CurrentFrames(void) |
| 906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | PyObject *result; |
| 908 | PyInterpreterState *i; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | result = PyDict_New(); |
| 911 | if (result == NULL) |
| 912 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | /* for i in all interpreters: |
| 915 | * for t in all of i's thread states: |
| 916 | * 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] | 917 | * Because these lists can mutate even when the GIL is held, we |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | * need to grab head_mutex for the duration. |
| 919 | */ |
| 920 | HEAD_LOCK(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 921 | for (i = _PyRuntime.interpreters.head; i != NULL; i = i->next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | PyThreadState *t; |
| 923 | for (t = i->tstate_head; t != NULL; t = t->next) { |
| 924 | PyObject *id; |
| 925 | int stat; |
| 926 | struct _frame *frame = t->frame; |
| 927 | if (frame == NULL) |
| 928 | continue; |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 929 | id = PyLong_FromUnsignedLong(t->thread_id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | if (id == NULL) |
| 931 | goto Fail; |
| 932 | stat = PyDict_SetItem(result, id, (PyObject *)frame); |
| 933 | Py_DECREF(id); |
| 934 | if (stat < 0) |
| 935 | goto Fail; |
| 936 | } |
| 937 | } |
| 938 | HEAD_UNLOCK(); |
| 939 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 940 | |
| 941 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | HEAD_UNLOCK(); |
| 943 | Py_DECREF(result); |
| 944 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 945 | } |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 946 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 947 | /* Python "auto thread state" API. */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 948 | |
| 949 | /* Keep this as a static, as it is not reliable! It can only |
| 950 | ever be compared to the state for the *current* thread. |
| 951 | * If not equal, then it doesn't matter that the actual |
| 952 | value may change immediately after comparison, as it can't |
| 953 | possibly change to the current thread's state. |
| 954 | * If equal, then the current thread holds the lock, so the value can't |
| 955 | change until we yield the lock. |
| 956 | */ |
| 957 | static int |
| 958 | PyThreadState_IsCurrent(PyThreadState *tstate) |
| 959 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | /* Must be the tstate for this thread */ |
| 961 | assert(PyGILState_GetThisThreadState()==tstate); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 962 | return tstate == _PyThreadState_GET(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 965 | /* Internal initialization/finalization functions called by |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 966 | Py_Initialize/Py_FinalizeEx |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 967 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 968 | void |
| 969 | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 970 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | assert(i && t); /* must init with valid states */ |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 972 | if (PyThread_tss_create(&_PyRuntime.gilstate.autoTSSkey) != 0) { |
| 973 | Py_FatalError("Could not allocate TSS entry"); |
| 974 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 975 | _PyRuntime.gilstate.autoInterpreterState = i; |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 976 | assert(PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | assert(t->gilstate_counter == 0); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | _PyGILState_NoteThreadState(t); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 982 | PyInterpreterState * |
| 983 | _PyGILState_GetInterpreterStateUnsafe(void) |
| 984 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 985 | return _PyRuntime.gilstate.autoInterpreterState; |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 986 | } |
| 987 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 988 | void |
| 989 | _PyGILState_Fini(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 990 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 991 | PyThread_tss_delete(&_PyRuntime.gilstate.autoTSSkey); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 992 | _PyRuntime.gilstate.autoInterpreterState = NULL; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 995 | /* Reset the TSS key - called by PyOS_AfterFork_Child(). |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 996 | * This should not be necessary, but some - buggy - pthread implementations |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 997 | * don't reset TSS upon fork(), see issue #10517. |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 998 | */ |
| 999 | void |
| 1000 | _PyGILState_Reinit(void) |
| 1001 | { |
Victor Stinner | 5d92647 | 2018-03-06 14:31:37 +0100 | [diff] [blame] | 1002 | /* Force default allocator, since _PyRuntimeState_Fini() must |
| 1003 | use the same allocator than this function. */ |
| 1004 | PyMemAllocatorEx old_alloc; |
| 1005 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 1006 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1007 | _PyRuntime.interpreters.mutex = PyThread_allocate_lock(); |
Victor Stinner | 5d92647 | 2018-03-06 14:31:37 +0100 | [diff] [blame] | 1008 | |
| 1009 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 1010 | |
| 1011 | if (_PyRuntime.interpreters.mutex == NULL) { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1012 | Py_FatalError("Can't initialize threads for interpreter"); |
Victor Stinner | 5d92647 | 2018-03-06 14:31:37 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 1015 | PyThreadState *tstate = PyGILState_GetThisThreadState(); |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1016 | PyThread_tss_delete(&_PyRuntime.gilstate.autoTSSkey); |
| 1017 | if (PyThread_tss_create(&_PyRuntime.gilstate.autoTSSkey) != 0) { |
| 1018 | Py_FatalError("Could not allocate TSS entry"); |
| 1019 | } |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 1020 | |
Charles-François Natali | a233df8 | 2011-11-22 19:49:51 +0100 | [diff] [blame] | 1021 | /* If the thread had an associated auto thread state, reassociate it with |
| 1022 | * the new key. */ |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1023 | if (tstate && |
| 1024 | PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, (void *)tstate) != 0) |
| 1025 | { |
| 1026 | Py_FatalError("Couldn't create autoTSSkey mapping"); |
| 1027 | } |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 1030 | /* When a thread state is created for a thread by some mechanism other than |
| 1031 | PyGILState_Ensure, it's important that the GILState machinery knows about |
| 1032 | it so it doesn't try to create another thread state for the thread (this is |
| 1033 | a better fix for SF bug #1010677 than the first one attempted). |
| 1034 | */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1035 | static void |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 1036 | _PyGILState_NoteThreadState(PyThreadState* tstate) |
| 1037 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1038 | /* If autoTSSkey isn't initialized, this must be the very first |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 1039 | threadstate created in Py_Initialize(). Don't do anything for now |
| 1040 | (we'll be back here when _PyGILState_Init is called). */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1041 | if (!_PyRuntime.gilstate.autoInterpreterState) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1043 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1044 | /* Stick the thread state for this thread in thread specific storage. |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | The only situation where you can legitimately have more than one |
| 1047 | thread state for an OS level thread is when there are multiple |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 1048 | interpreters. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1049 | |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 1050 | You shouldn't really be using the PyGILState_ APIs anyway (see issues |
| 1051 | #10915 and #15751). |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 1052 | |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 1053 | The first thread state created for that given OS level thread will |
| 1054 | "win", which seems reasonable behaviour. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | */ |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1056 | if (PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == NULL) { |
| 1057 | if ((PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, (void *)tstate) |
| 1058 | ) != 0) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1059 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1060 | Py_FatalError("Couldn't create autoTSSkey mapping"); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1061 | } |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 1062 | } |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | /* PyGILState_Release must not try to delete this thread state. */ |
| 1065 | tstate->gilstate_counter = 1; |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1068 | /* The public functions */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 1069 | PyThreadState * |
| 1070 | PyGILState_GetThisThreadState(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1071 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1072 | if (_PyRuntime.gilstate.autoInterpreterState == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | return NULL; |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1074 | return (PyThreadState *)PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 1077 | int |
| 1078 | PyGILState_Check(void) |
| 1079 | { |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1080 | PyThreadState *tstate; |
| 1081 | |
| 1082 | if (!_PyGILState_check_enabled) |
| 1083 | return 1; |
| 1084 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1085 | if (!PyThread_tss_is_created(&_PyRuntime.gilstate.autoTSSkey)) { |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1086 | return 1; |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1087 | } |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1088 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1089 | tstate = _PyThreadState_GET(); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1090 | if (tstate == NULL) |
| 1091 | return 0; |
| 1092 | |
| 1093 | return (tstate == PyGILState_GetThisThreadState()); |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 1096 | PyGILState_STATE |
| 1097 | PyGILState_Ensure(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1098 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | int current; |
| 1100 | PyThreadState *tcur; |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 1101 | int need_init_threads = 0; |
| 1102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | /* Note that we do not auto-init Python here - apart from |
| 1104 | potential races with 2 threads auto-initializing, pep-311 |
| 1105 | spells out other issues. Embedders are expected to have |
| 1106 | called Py_Initialize() and usually PyEval_InitThreads(). |
| 1107 | */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1108 | /* Py_Initialize() hasn't been called! */ |
| 1109 | assert(_PyRuntime.gilstate.autoInterpreterState); |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 1110 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1111 | tcur = (PyThreadState *)PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | if (tcur == NULL) { |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 1113 | need_init_threads = 1; |
Victor Stinner | 62ca100 | 2013-12-13 01:46:43 +0100 | [diff] [blame] | 1114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | /* Create a new thread state for this thread */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1116 | tcur = PyThreadState_New(_PyRuntime.gilstate.autoInterpreterState); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | if (tcur == NULL) |
| 1118 | Py_FatalError("Couldn't create thread-state for new thread"); |
| 1119 | /* This is our thread state! We'll need to delete it in the |
| 1120 | matching call to PyGILState_Release(). */ |
| 1121 | tcur->gilstate_counter = 0; |
| 1122 | current = 0; /* new thread state is never current */ |
| 1123 | } |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 1124 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | current = PyThreadState_IsCurrent(tcur); |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | if (current == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | PyEval_RestoreThread(tcur); |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | /* Update our counter in the thread-state - no need for locks: |
| 1133 | - tcur will remain valid as we hold the GIL. |
| 1134 | - the counter is safe as we are the only thread "allowed" |
| 1135 | to modify this value |
| 1136 | */ |
| 1137 | ++tcur->gilstate_counter; |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 1138 | |
| 1139 | if (need_init_threads) { |
| 1140 | /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is |
| 1141 | called from a new thread for the first time, we need the create the |
| 1142 | GIL. */ |
| 1143 | PyEval_InitThreads(); |
| 1144 | } |
| 1145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 1149 | void |
| 1150 | PyGILState_Release(PyGILState_STATE oldstate) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1151 | { |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1152 | PyThreadState *tcur = (PyThreadState *)PyThread_tss_get( |
| 1153 | &_PyRuntime.gilstate.autoTSSkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | if (tcur == NULL) |
| 1155 | Py_FatalError("auto-releasing thread-state, " |
| 1156 | "but no thread-state for this thread"); |
| 1157 | /* We must hold the GIL and have our thread state current */ |
| 1158 | /* XXX - remove the check - the assert should be fine, |
| 1159 | but while this is very new (April 2003), the extra check |
| 1160 | by release-only users can't hurt. |
| 1161 | */ |
| 1162 | if (! PyThreadState_IsCurrent(tcur)) |
| 1163 | Py_FatalError("This thread state must be current when releasing"); |
| 1164 | assert(PyThreadState_IsCurrent(tcur)); |
| 1165 | --tcur->gilstate_counter; |
| 1166 | assert(tcur->gilstate_counter >= 0); /* illegal counter value */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | /* If we're going to destroy this thread-state, we must |
| 1169 | * clear it while the GIL is held, as destructors may run. |
| 1170 | */ |
| 1171 | if (tcur->gilstate_counter == 0) { |
| 1172 | /* can't have been locked when we created it */ |
| 1173 | assert(oldstate == PyGILState_UNLOCKED); |
| 1174 | PyThreadState_Clear(tcur); |
| 1175 | /* Delete the thread-state. Note this releases the GIL too! |
| 1176 | * It's vital that the GIL be held here, to avoid shutdown |
| 1177 | * races; see bugs 225673 and 1061968 (that nasty bug has a |
| 1178 | * habit of coming back). |
| 1179 | */ |
| 1180 | PyThreadState_DeleteCurrent(); |
| 1181 | } |
| 1182 | /* Release the lock if necessary */ |
| 1183 | else if (oldstate == PyGILState_UNLOCKED) |
| 1184 | PyEval_SaveThread(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1185 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1186 | |
Benjamin Peterson | 3bf0175 | 2012-04-13 18:06:36 -0400 | [diff] [blame] | 1187 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1188 | /**************************/ |
| 1189 | /* cross-interpreter data */ |
| 1190 | /**************************/ |
| 1191 | |
| 1192 | /* cross-interpreter data */ |
| 1193 | |
| 1194 | crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *); |
| 1195 | |
| 1196 | /* This is a separate func from _PyCrossInterpreterData_Lookup in order |
| 1197 | to keep the registry code separate. */ |
| 1198 | static crossinterpdatafunc |
| 1199 | _lookup_getdata(PyObject *obj) |
| 1200 | { |
| 1201 | crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj); |
| 1202 | if (getdata == NULL && PyErr_Occurred() == 0) |
| 1203 | PyErr_Format(PyExc_ValueError, |
| 1204 | "%S does not support cross-interpreter data", obj); |
| 1205 | return getdata; |
| 1206 | } |
| 1207 | |
| 1208 | int |
| 1209 | _PyObject_CheckCrossInterpreterData(PyObject *obj) |
| 1210 | { |
| 1211 | crossinterpdatafunc getdata = _lookup_getdata(obj); |
| 1212 | if (getdata == NULL) { |
| 1213 | return -1; |
| 1214 | } |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | static int |
| 1219 | _check_xidata(_PyCrossInterpreterData *data) |
| 1220 | { |
| 1221 | // data->data can be anything, including NULL, so we don't check it. |
| 1222 | |
| 1223 | // data->obj may be NULL, so we don't check it. |
| 1224 | |
| 1225 | if (data->interp < 0) { |
| 1226 | PyErr_SetString(PyExc_SystemError, "missing interp"); |
| 1227 | return -1; |
| 1228 | } |
| 1229 | |
| 1230 | if (data->new_object == NULL) { |
| 1231 | PyErr_SetString(PyExc_SystemError, "missing new_object func"); |
| 1232 | return -1; |
| 1233 | } |
| 1234 | |
| 1235 | // data->free may be NULL, so we don't check it. |
| 1236 | |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | int |
| 1241 | _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data) |
| 1242 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1243 | // _PyInterpreterState_Get() aborts if lookup fails, so we don't need |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1244 | // to check the result for NULL. |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1245 | PyInterpreterState *interp = _PyInterpreterState_Get(); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1246 | |
| 1247 | // Reset data before re-populating. |
| 1248 | *data = (_PyCrossInterpreterData){0}; |
| 1249 | data->free = PyMem_RawFree; // Set a default that may be overridden. |
| 1250 | |
| 1251 | // Call the "getdata" func for the object. |
| 1252 | Py_INCREF(obj); |
| 1253 | crossinterpdatafunc getdata = _lookup_getdata(obj); |
| 1254 | if (getdata == NULL) { |
| 1255 | Py_DECREF(obj); |
| 1256 | return -1; |
| 1257 | } |
| 1258 | int res = getdata(obj, data); |
| 1259 | Py_DECREF(obj); |
| 1260 | if (res != 0) { |
| 1261 | return -1; |
| 1262 | } |
| 1263 | |
| 1264 | // Fill in the blanks and validate the result. |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1265 | data->interp = interp->id; |
| 1266 | if (_check_xidata(data) != 0) { |
| 1267 | _PyCrossInterpreterData_Release(data); |
| 1268 | return -1; |
| 1269 | } |
| 1270 | |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
Eric Snow | 6379913 | 2018-06-01 18:45:20 -0600 | [diff] [blame] | 1274 | static void |
| 1275 | _release_xidata(void *arg) |
| 1276 | { |
| 1277 | _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg; |
| 1278 | if (data->free != NULL) { |
| 1279 | data->free(data->data); |
| 1280 | } |
| 1281 | Py_XDECREF(data->obj); |
| 1282 | } |
| 1283 | |
| 1284 | static void |
| 1285 | _call_in_interpreter(PyInterpreterState *interp, |
| 1286 | void (*func)(void *), void *arg) |
| 1287 | { |
| 1288 | /* We would use Py_AddPendingCall() if it weren't specific to the |
| 1289 | * main interpreter (see bpo-33608). In the meantime we take a |
| 1290 | * naive approach. |
| 1291 | */ |
| 1292 | PyThreadState *save_tstate = NULL; |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1293 | if (interp != _PyInterpreterState_Get()) { |
Eric Snow | 6379913 | 2018-06-01 18:45:20 -0600 | [diff] [blame] | 1294 | // XXX Using the "head" thread isn't strictly correct. |
| 1295 | PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); |
| 1296 | // XXX Possible GILState issues? |
| 1297 | save_tstate = PyThreadState_Swap(tstate); |
| 1298 | } |
| 1299 | |
| 1300 | func(arg); |
| 1301 | |
| 1302 | // Switch back. |
| 1303 | if (save_tstate != NULL) { |
| 1304 | PyThreadState_Swap(save_tstate); |
| 1305 | } |
| 1306 | } |
| 1307 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1308 | void |
| 1309 | _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data) |
| 1310 | { |
| 1311 | if (data->data == NULL && data->obj == NULL) { |
| 1312 | // Nothing to release! |
| 1313 | return; |
| 1314 | } |
| 1315 | |
| 1316 | // Switch to the original interpreter. |
| 1317 | PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp); |
| 1318 | if (interp == NULL) { |
| 1319 | // The intepreter was already destroyed. |
| 1320 | if (data->free != NULL) { |
| 1321 | // XXX Someone leaked some memory... |
| 1322 | } |
| 1323 | return; |
| 1324 | } |
Eric Snow | f53d9f2 | 2018-02-20 16:30:17 -0700 | [diff] [blame] | 1325 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1326 | // "Release" the data and/or the object. |
Eric Snow | 6379913 | 2018-06-01 18:45:20 -0600 | [diff] [blame] | 1327 | _call_in_interpreter(interp, _release_xidata, data); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | PyObject * |
| 1331 | _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data) |
| 1332 | { |
| 1333 | return data->new_object(data); |
| 1334 | } |
| 1335 | |
| 1336 | /* registry of {type -> crossinterpdatafunc} */ |
| 1337 | |
| 1338 | /* For now we use a global registry of shareable classes. An |
| 1339 | alternative would be to add a tp_* slot for a class's |
| 1340 | crossinterpdatafunc. It would be simpler and more efficient. */ |
| 1341 | |
| 1342 | static int |
| 1343 | _register_xidata(PyTypeObject *cls, crossinterpdatafunc getdata) |
| 1344 | { |
| 1345 | // Note that we effectively replace already registered classes |
| 1346 | // rather than failing. |
| 1347 | struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem)); |
| 1348 | if (newhead == NULL) |
| 1349 | return -1; |
| 1350 | newhead->cls = cls; |
| 1351 | newhead->getdata = getdata; |
| 1352 | newhead->next = _PyRuntime.xidregistry.head; |
| 1353 | _PyRuntime.xidregistry.head = newhead; |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | static void _register_builtins_for_crossinterpreter_data(void); |
| 1358 | |
| 1359 | int |
| 1360 | _PyCrossInterpreterData_Register_Class(PyTypeObject *cls, |
| 1361 | crossinterpdatafunc getdata) |
| 1362 | { |
| 1363 | if (!PyType_Check(cls)) { |
| 1364 | PyErr_Format(PyExc_ValueError, "only classes may be registered"); |
| 1365 | return -1; |
| 1366 | } |
| 1367 | if (getdata == NULL) { |
| 1368 | PyErr_Format(PyExc_ValueError, "missing 'getdata' func"); |
| 1369 | return -1; |
| 1370 | } |
| 1371 | |
| 1372 | // Make sure the class isn't ever deallocated. |
| 1373 | Py_INCREF((PyObject *)cls); |
| 1374 | |
| 1375 | PyThread_acquire_lock(_PyRuntime.xidregistry.mutex, WAIT_LOCK); |
| 1376 | if (_PyRuntime.xidregistry.head == NULL) { |
| 1377 | _register_builtins_for_crossinterpreter_data(); |
| 1378 | } |
| 1379 | int res = _register_xidata(cls, getdata); |
| 1380 | PyThread_release_lock(_PyRuntime.xidregistry.mutex); |
| 1381 | return res; |
| 1382 | } |
| 1383 | |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1384 | /* Cross-interpreter objects are looked up by exact match on the class. |
| 1385 | We can reassess this policy when we move from a global registry to a |
| 1386 | tp_* slot. */ |
| 1387 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1388 | crossinterpdatafunc |
| 1389 | _PyCrossInterpreterData_Lookup(PyObject *obj) |
| 1390 | { |
| 1391 | PyObject *cls = PyObject_Type(obj); |
| 1392 | crossinterpdatafunc getdata = NULL; |
| 1393 | PyThread_acquire_lock(_PyRuntime.xidregistry.mutex, WAIT_LOCK); |
| 1394 | struct _xidregitem *cur = _PyRuntime.xidregistry.head; |
| 1395 | if (cur == NULL) { |
| 1396 | _register_builtins_for_crossinterpreter_data(); |
| 1397 | cur = _PyRuntime.xidregistry.head; |
| 1398 | } |
| 1399 | for(; cur != NULL; cur = cur->next) { |
| 1400 | if (cur->cls == (PyTypeObject *)cls) { |
| 1401 | getdata = cur->getdata; |
| 1402 | break; |
| 1403 | } |
| 1404 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1405 | Py_DECREF(cls); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1406 | PyThread_release_lock(_PyRuntime.xidregistry.mutex); |
| 1407 | return getdata; |
| 1408 | } |
| 1409 | |
| 1410 | /* cross-interpreter data for builtin types */ |
| 1411 | |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1412 | struct _shared_bytes_data { |
| 1413 | char *bytes; |
| 1414 | Py_ssize_t len; |
| 1415 | }; |
| 1416 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1417 | static PyObject * |
| 1418 | _new_bytes_object(_PyCrossInterpreterData *data) |
| 1419 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1420 | struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data); |
| 1421 | return PyBytes_FromStringAndSize(shared->bytes, shared->len); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | static int |
| 1425 | _bytes_shared(PyObject *obj, _PyCrossInterpreterData *data) |
| 1426 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1427 | struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1); |
| 1428 | if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) { |
| 1429 | return -1; |
| 1430 | } |
| 1431 | data->data = (void *)shared; |
Eric Snow | 6379913 | 2018-06-01 18:45:20 -0600 | [diff] [blame] | 1432 | Py_INCREF(obj); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1433 | data->obj = obj; // Will be "released" (decref'ed) when data released. |
| 1434 | data->new_object = _new_bytes_object; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1435 | data->free = PyMem_Free; |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | struct _shared_str_data { |
| 1440 | int kind; |
| 1441 | const void *buffer; |
| 1442 | Py_ssize_t len; |
| 1443 | }; |
| 1444 | |
| 1445 | static PyObject * |
| 1446 | _new_str_object(_PyCrossInterpreterData *data) |
| 1447 | { |
| 1448 | struct _shared_str_data *shared = (struct _shared_str_data *)(data->data); |
| 1449 | return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len); |
| 1450 | } |
| 1451 | |
| 1452 | static int |
| 1453 | _str_shared(PyObject *obj, _PyCrossInterpreterData *data) |
| 1454 | { |
| 1455 | struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1); |
| 1456 | shared->kind = PyUnicode_KIND(obj); |
| 1457 | shared->buffer = PyUnicode_DATA(obj); |
| 1458 | shared->len = PyUnicode_GET_LENGTH(obj) - 1; |
| 1459 | data->data = (void *)shared; |
Eric Snow | 6379913 | 2018-06-01 18:45:20 -0600 | [diff] [blame] | 1460 | Py_INCREF(obj); |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1461 | data->obj = obj; // Will be "released" (decref'ed) when data released. |
| 1462 | data->new_object = _new_str_object; |
| 1463 | data->free = PyMem_Free; |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | static PyObject * |
| 1468 | _new_long_object(_PyCrossInterpreterData *data) |
| 1469 | { |
Alexey Izbyshev | 16f842d | 2019-02-12 19:06:43 +0300 | [diff] [blame] | 1470 | return PyLong_FromSsize_t((Py_ssize_t)(data->data)); |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | static int |
| 1474 | _long_shared(PyObject *obj, _PyCrossInterpreterData *data) |
| 1475 | { |
Alexey Izbyshev | 16f842d | 2019-02-12 19:06:43 +0300 | [diff] [blame] | 1476 | /* Note that this means the size of shareable ints is bounded by |
| 1477 | * sys.maxsize. Hence on 32-bit architectures that is half the |
| 1478 | * size of maximum shareable ints on 64-bit. |
| 1479 | */ |
| 1480 | Py_ssize_t value = PyLong_AsSsize_t(obj); |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1481 | if (value == -1 && PyErr_Occurred()) { |
| 1482 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 1483 | PyErr_SetString(PyExc_OverflowError, "try sending as bytes"); |
| 1484 | } |
| 1485 | return -1; |
| 1486 | } |
| 1487 | data->data = (void *)value; |
| 1488 | data->obj = NULL; |
| 1489 | data->new_object = _new_long_object; |
| 1490 | data->free = NULL; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | static PyObject * |
| 1495 | _new_none_object(_PyCrossInterpreterData *data) |
| 1496 | { |
| 1497 | // XXX Singleton refcounts are problematic across interpreters... |
| 1498 | Py_INCREF(Py_None); |
| 1499 | return Py_None; |
| 1500 | } |
| 1501 | |
| 1502 | static int |
| 1503 | _none_shared(PyObject *obj, _PyCrossInterpreterData *data) |
| 1504 | { |
| 1505 | data->data = NULL; |
| 1506 | // data->obj remains NULL |
| 1507 | data->new_object = _new_none_object; |
| 1508 | data->free = NULL; // There is nothing to free. |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | static void |
| 1513 | _register_builtins_for_crossinterpreter_data(void) |
| 1514 | { |
| 1515 | // None |
| 1516 | if (_register_xidata((PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) { |
| 1517 | Py_FatalError("could not register None for cross-interpreter sharing"); |
| 1518 | } |
| 1519 | |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1520 | // int |
| 1521 | if (_register_xidata(&PyLong_Type, _long_shared) != 0) { |
| 1522 | Py_FatalError("could not register int for cross-interpreter sharing"); |
| 1523 | } |
| 1524 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1525 | // bytes |
| 1526 | if (_register_xidata(&PyBytes_Type, _bytes_shared) != 0) { |
| 1527 | Py_FatalError("could not register bytes for cross-interpreter sharing"); |
| 1528 | } |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1529 | |
| 1530 | // str |
| 1531 | if (_register_xidata(&PyUnicode_Type, _str_shared) != 0) { |
| 1532 | Py_FatalError("could not register str for cross-interpreter sharing"); |
| 1533 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1537 | #ifdef __cplusplus |
| 1538 | } |
| 1539 | #endif |