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