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