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