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