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