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