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