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