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