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