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