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