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