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