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" |
| 5 | |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 6 | #define GET_TSTATE() \ |
| 7 | ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current)) |
| 8 | #define SET_TSTATE(value) \ |
| 9 | _Py_atomic_store_relaxed(&_PyThreadState_Current, (Py_uintptr_t)(value)) |
| 10 | #define GET_INTERP_STATE() \ |
| 11 | (GET_TSTATE()->interp) |
| 12 | |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 13 | |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 14 | /* -------------------------------------------------------------------------- |
| 15 | CAUTION |
| 16 | |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 17 | Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A |
| 18 | number of these functions are advertised as safe to call when the GIL isn't |
| 19 | held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's |
| 20 | debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL |
| 21 | to avoid the expense of doing their own locking). |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 22 | -------------------------------------------------------------------------- */ |
| 23 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 24 | #ifdef HAVE_DLOPEN |
| 25 | #ifdef HAVE_DLFCN_H |
| 26 | #include <dlfcn.h> |
| 27 | #endif |
| 28 | #ifndef RTLD_LAZY |
| 29 | #define RTLD_LAZY 1 |
| 30 | #endif |
| 31 | #endif |
| 32 | |
Benjamin Peterson | 43162b8 | 2012-04-13 11:58:27 -0400 | [diff] [blame] | 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 37 | #ifdef WITH_THREAD |
| 38 | #include "pythread.h" |
| 39 | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ |
Moshe Zadka | 9fb6af9 | 2000-08-04 21:27:47 +0000 | [diff] [blame] | 40 | #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 41 | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 42 | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 43 | |
| 44 | /* The single PyInterpreterState used by this process' |
| 45 | GILState implementation |
| 46 | */ |
| 47 | static PyInterpreterState *autoInterpreterState = NULL; |
| 48 | static int autoTLSkey = 0; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 49 | #else |
| 50 | #define HEAD_INIT() /* Nothing */ |
| 51 | #define HEAD_LOCK() /* Nothing */ |
| 52 | #define HEAD_UNLOCK() /* Nothing */ |
| 53 | #endif |
| 54 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 55 | static PyInterpreterState *interp_head = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 56 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 57 | /* Assuming the current thread holds the GIL, this is the |
| 58 | PyThreadState for the current thread. */ |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 59 | _Py_atomic_address _PyThreadState_Current = {0}; |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 60 | PyThreadFrameGetter _PyThreadState_GetFrame = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 61 | |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 62 | #ifdef WITH_THREAD |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 63 | static void _PyGILState_NoteThreadState(PyThreadState* tstate); |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 64 | #endif |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 66 | |
| 67 | PyInterpreterState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 68 | PyInterpreterState_New(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 69 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | PyInterpreterState *interp = (PyInterpreterState *) |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 71 | PyMem_RawMalloc(sizeof(PyInterpreterState)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 72 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | if (interp != NULL) { |
| 74 | HEAD_INIT(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | if (head_mutex == NULL) |
| 77 | Py_FatalError("Can't initialize threads for interpreter"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | interp->modules = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | interp->modules_by_index = NULL; |
| 81 | interp->sysdict = NULL; |
| 82 | interp->builtins = NULL; |
Serhiy Storchaka | 87a5c51 | 2014-02-10 18:21:34 +0200 | [diff] [blame] | 83 | interp->builtins_copy = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | interp->tstate_head = NULL; |
| 85 | interp->codec_search_path = NULL; |
| 86 | interp->codec_search_cache = NULL; |
| 87 | interp->codec_error_registry = NULL; |
| 88 | interp->codecs_initialized = 0; |
Victor Stinner | 3cbf14b | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 89 | interp->fscodec_initialized = 0; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 90 | interp->importlib = NULL; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 91 | #ifdef HAVE_DLOPEN |
| 92 | #ifdef RTLD_NOW |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | interp->dlopenflags = RTLD_NOW; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 94 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | interp->dlopenflags = RTLD_LAZY; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 96 | #endif |
| 97 | #endif |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 98 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | interp->tscdump = 0; |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 100 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | HEAD_LOCK(); |
| 103 | interp->next = interp_head; |
| 104 | interp_head = interp; |
| 105 | HEAD_UNLOCK(); |
| 106 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | return interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 113 | PyInterpreterState_Clear(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | PyThreadState *p; |
| 116 | HEAD_LOCK(); |
| 117 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 118 | PyThreadState_Clear(p); |
| 119 | HEAD_UNLOCK(); |
| 120 | Py_CLEAR(interp->codec_search_path); |
| 121 | Py_CLEAR(interp->codec_search_cache); |
| 122 | Py_CLEAR(interp->codec_error_registry); |
| 123 | Py_CLEAR(interp->modules); |
| 124 | Py_CLEAR(interp->modules_by_index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | Py_CLEAR(interp->sysdict); |
| 126 | Py_CLEAR(interp->builtins); |
Serhiy Storchaka | 87a5c51 | 2014-02-10 18:21:34 +0200 | [diff] [blame] | 127 | Py_CLEAR(interp->builtins_copy); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 128 | Py_CLEAR(interp->importlib); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 133 | zapthreads(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | PyThreadState *p; |
| 136 | /* No need to lock the mutex here because this should only happen |
| 137 | when the threads are all really dead (XXX famous last words). */ |
| 138 | while ((p = interp->tstate_head) != NULL) { |
| 139 | PyThreadState_Delete(p); |
| 140 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 145 | PyInterpreterState_Delete(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | PyInterpreterState **p; |
| 148 | zapthreads(interp); |
| 149 | HEAD_LOCK(); |
| 150 | for (p = &interp_head; ; p = &(*p)->next) { |
| 151 | if (*p == NULL) |
| 152 | Py_FatalError( |
| 153 | "PyInterpreterState_Delete: invalid interp"); |
| 154 | if (*p == interp) |
| 155 | break; |
| 156 | } |
| 157 | if (interp->tstate_head != NULL) |
| 158 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 159 | *p = interp->next; |
| 160 | HEAD_UNLOCK(); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 161 | PyMem_RawFree(interp); |
Antoine Pitrou | 8db076c | 2011-10-30 19:13:55 +0100 | [diff] [blame] | 162 | #ifdef WITH_THREAD |
| 163 | if (interp_head == NULL && head_mutex != NULL) { |
| 164 | PyThread_free_lock(head_mutex); |
| 165 | head_mutex = NULL; |
| 166 | } |
| 167 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 171 | /* Default implementation for _PyThreadState_GetFrame */ |
| 172 | static struct _frame * |
| 173 | threadstate_getframe(PyThreadState *self) |
| 174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | return self->frame; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 178 | static PyThreadState * |
| 179 | new_threadstate(PyInterpreterState *interp, int init) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 180 | { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 181 | PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState)); |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | if (_PyThreadState_GetFrame == NULL) |
| 184 | _PyThreadState_GetFrame = threadstate_getframe; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | if (tstate != NULL) { |
| 187 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | tstate->frame = NULL; |
| 190 | tstate->recursion_depth = 0; |
| 191 | tstate->overflowed = 0; |
| 192 | tstate->recursion_critical = 0; |
| 193 | tstate->tracing = 0; |
| 194 | tstate->use_tracing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | tstate->gilstate_counter = 0; |
| 196 | tstate->async_exc = NULL; |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 197 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | tstate->thread_id = PyThread_get_thread_ident(); |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 199 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | tstate->thread_id = 0; |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 201 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | tstate->dict = NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | tstate->curexc_type = NULL; |
| 206 | tstate->curexc_value = NULL; |
| 207 | tstate->curexc_traceback = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | tstate->exc_type = NULL; |
| 210 | tstate->exc_value = NULL; |
| 211 | tstate->exc_traceback = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | tstate->c_profilefunc = NULL; |
| 214 | tstate->c_tracefunc = NULL; |
| 215 | tstate->c_profileobj = NULL; |
| 216 | tstate->c_traceobj = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 217 | |
Antoine Pitrou | 2b0218a | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 218 | tstate->trash_delete_nesting = 0; |
| 219 | tstate->trash_delete_later = NULL; |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 220 | tstate->on_delete = NULL; |
| 221 | tstate->on_delete_data = NULL; |
Antoine Pitrou | 2b0218a | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 222 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 223 | tstate->coroutine_wrapper = NULL; |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 224 | tstate->in_coroutine_wrapper = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | if (init) |
| 227 | _PyThreadState_Init(tstate); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | HEAD_LOCK(); |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 230 | tstate->prev = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | tstate->next = interp->tstate_head; |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 232 | if (tstate->next) |
| 233 | tstate->next->prev = tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | interp->tstate_head = tstate; |
| 235 | HEAD_UNLOCK(); |
| 236 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 241 | PyThreadState * |
| 242 | PyThreadState_New(PyInterpreterState *interp) |
| 243 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | return new_threadstate(interp, 1); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | PyThreadState * |
| 248 | _PyThreadState_Prealloc(PyInterpreterState *interp) |
| 249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | return new_threadstate(interp, 0); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
| 254 | _PyThreadState_Init(PyThreadState *tstate) |
| 255 | { |
| 256 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | _PyGILState_NoteThreadState(tstate); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 258 | #endif |
| 259 | } |
| 260 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 261 | PyObject* |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 262 | PyState_FindModule(struct PyModuleDef* module) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 263 | { |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 264 | Py_ssize_t index = module->m_base.m_index; |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 265 | PyInterpreterState *state = GET_INTERP_STATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | PyObject *res; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 267 | if (module->m_slots) { |
| 268 | return NULL; |
| 269 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | if (index == 0) |
| 271 | return NULL; |
| 272 | if (state->modules_by_index == NULL) |
| 273 | return NULL; |
Antoine Pitrou | 75506e8 | 2012-08-20 19:30:46 +0200 | [diff] [blame] | 274 | if (index >= PyList_GET_SIZE(state->modules_by_index)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | return NULL; |
| 276 | res = PyList_GET_ITEM(state->modules_by_index, index); |
| 277 | return res==Py_None ? NULL : res; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | int |
| 281 | _PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
| 282 | { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 283 | PyInterpreterState *state; |
Berker Peksag | 4b7b565 | 2016-08-22 18:05:56 +0300 | [diff] [blame] | 284 | if (!def) { |
| 285 | assert(PyErr_Occurred()); |
| 286 | return -1; |
| 287 | } |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 288 | if (def->m_slots) { |
| 289 | PyErr_SetString(PyExc_SystemError, |
| 290 | "PyState_AddModule called on module with slots"); |
| 291 | return -1; |
| 292 | } |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 293 | state = GET_INTERP_STATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | if (!state->modules_by_index) { |
| 295 | state->modules_by_index = PyList_New(0); |
| 296 | if (!state->modules_by_index) |
| 297 | return -1; |
| 298 | } |
| 299 | while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index) |
| 300 | if (PyList_Append(state->modules_by_index, Py_None) < 0) |
| 301 | return -1; |
| 302 | Py_INCREF(module); |
| 303 | return PyList_SetItem(state->modules_by_index, |
| 304 | def->m_base.m_index, module); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 305 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 306 | |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 307 | int |
| 308 | PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
| 309 | { |
| 310 | Py_ssize_t index; |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 311 | PyInterpreterState *state = GET_INTERP_STATE(); |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 312 | if (!def) { |
| 313 | Py_FatalError("PyState_AddModule: Module Definition is NULL"); |
| 314 | return -1; |
| 315 | } |
| 316 | index = def->m_base.m_index; |
| 317 | if (state->modules_by_index) { |
| 318 | if(PyList_GET_SIZE(state->modules_by_index) >= index) { |
| 319 | if(module == PyList_GET_ITEM(state->modules_by_index, index)) { |
| 320 | Py_FatalError("PyState_AddModule: Module already added!"); |
| 321 | return -1; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | return _PyState_AddModule(module, def); |
| 326 | } |
| 327 | |
| 328 | int |
| 329 | PyState_RemoveModule(struct PyModuleDef* def) |
| 330 | { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 331 | PyInterpreterState *state; |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 332 | Py_ssize_t index = def->m_base.m_index; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 333 | if (def->m_slots) { |
| 334 | PyErr_SetString(PyExc_SystemError, |
| 335 | "PyState_RemoveModule called on module with slots"); |
| 336 | return -1; |
| 337 | } |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 338 | state = GET_INTERP_STATE(); |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 339 | if (index == 0) { |
| 340 | Py_FatalError("PyState_RemoveModule: Module index invalid."); |
| 341 | return -1; |
| 342 | } |
| 343 | if (state->modules_by_index == NULL) { |
| 344 | Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible."); |
| 345 | return -1; |
| 346 | } |
| 347 | if (index > PyList_GET_SIZE(state->modules_by_index)) { |
| 348 | Py_FatalError("PyState_RemoveModule: Module index out of bounds."); |
| 349 | return -1; |
| 350 | } |
| 351 | return PyList_SetItem(state->modules_by_index, index, Py_None); |
| 352 | } |
| 353 | |
Antoine Pitrou | 40322e6 | 2013-08-11 00:30:09 +0200 | [diff] [blame] | 354 | /* used by import.c:PyImport_Cleanup */ |
| 355 | void |
| 356 | _PyState_ClearModules(void) |
| 357 | { |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 358 | PyInterpreterState *state = GET_INTERP_STATE(); |
Antoine Pitrou | 40322e6 | 2013-08-11 00:30:09 +0200 | [diff] [blame] | 359 | if (state->modules_by_index) { |
| 360 | Py_ssize_t i; |
| 361 | for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) { |
| 362 | PyObject *m = PyList_GET_ITEM(state->modules_by_index, i); |
| 363 | if (PyModule_Check(m)) { |
| 364 | /* cleanup the saved copy of module dicts */ |
| 365 | PyModuleDef *md = PyModule_GetDef(m); |
| 366 | if (md) |
| 367 | Py_CLEAR(md->m_base.m_copy); |
| 368 | } |
| 369 | } |
| 370 | /* Setting modules_by_index to NULL could be dangerous, so we |
| 371 | clear the list instead. */ |
| 372 | if (PyList_SetSlice(state->modules_by_index, |
| 373 | 0, PyList_GET_SIZE(state->modules_by_index), |
| 374 | NULL)) |
| 375 | PyErr_WriteUnraisable(state->modules_by_index); |
| 376 | } |
| 377 | } |
| 378 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 379 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 380 | PyThreadState_Clear(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 381 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | if (Py_VerboseFlag && tstate->frame != NULL) |
| 383 | fprintf(stderr, |
| 384 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | Py_CLEAR(tstate->frame); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | Py_CLEAR(tstate->dict); |
| 389 | Py_CLEAR(tstate->async_exc); |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | Py_CLEAR(tstate->curexc_type); |
| 392 | Py_CLEAR(tstate->curexc_value); |
| 393 | Py_CLEAR(tstate->curexc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | Py_CLEAR(tstate->exc_type); |
| 396 | Py_CLEAR(tstate->exc_value); |
| 397 | Py_CLEAR(tstate->exc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | tstate->c_profilefunc = NULL; |
| 400 | tstate->c_tracefunc = NULL; |
| 401 | Py_CLEAR(tstate->c_profileobj); |
| 402 | Py_CLEAR(tstate->c_traceobj); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 403 | |
| 404 | Py_CLEAR(tstate->coroutine_wrapper); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 408 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
| 409 | static void |
| 410 | tstate_delete_common(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | PyInterpreterState *interp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | if (tstate == NULL) |
| 414 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
| 415 | interp = tstate->interp; |
| 416 | if (interp == NULL) |
| 417 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
| 418 | HEAD_LOCK(); |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 419 | if (tstate->prev) |
| 420 | tstate->prev->next = tstate->next; |
| 421 | else |
| 422 | interp->tstate_head = tstate->next; |
| 423 | if (tstate->next) |
| 424 | tstate->next->prev = tstate->prev; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | HEAD_UNLOCK(); |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 426 | if (tstate->on_delete != NULL) { |
| 427 | tstate->on_delete(tstate->on_delete_data); |
| 428 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 429 | PyMem_RawFree(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 433 | void |
| 434 | PyThreadState_Delete(PyThreadState *tstate) |
| 435 | { |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 436 | if (tstate == GET_TSTATE()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
Tim Peters | f4e6928 | 2006-02-27 17:15:31 +0000 | [diff] [blame] | 438 | #ifdef WITH_THREAD |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 439 | if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | PyThread_delete_key_value(autoTLSkey); |
Tim Peters | f4e6928 | 2006-02-27 17:15:31 +0000 | [diff] [blame] | 441 | #endif /* WITH_THREAD */ |
Christian Heimes | b9dbc7d | 2013-07-01 23:42:28 +0200 | [diff] [blame] | 442 | tstate_delete_common(tstate); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | |
| 446 | #ifdef WITH_THREAD |
| 447 | void |
| 448 | PyThreadState_DeleteCurrent() |
| 449 | { |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 450 | PyThreadState *tstate = GET_TSTATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | if (tstate == NULL) |
| 452 | Py_FatalError( |
| 453 | "PyThreadState_DeleteCurrent: no current tstate"); |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 454 | SET_TSTATE(NULL); |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 455 | if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | PyThread_delete_key_value(autoTLSkey); |
Christian Heimes | b9dbc7d | 2013-07-01 23:42:28 +0200 | [diff] [blame] | 457 | tstate_delete_common(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | PyEval_ReleaseLock(); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 459 | } |
| 460 | #endif /* WITH_THREAD */ |
| 461 | |
| 462 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 463 | /* |
| 464 | * Delete all thread states except the one passed as argument. |
| 465 | * Note that, if there is a current thread state, it *must* be the one |
| 466 | * passed as argument. Also, this won't touch any other interpreters |
| 467 | * than the current one, since we don't know which thread state should |
| 468 | * be kept in those other interpreteres. |
| 469 | */ |
| 470 | void |
| 471 | _PyThreadState_DeleteExcept(PyThreadState *tstate) |
| 472 | { |
| 473 | PyInterpreterState *interp = tstate->interp; |
| 474 | PyThreadState *p, *next, *garbage; |
| 475 | HEAD_LOCK(); |
| 476 | /* Remove all thread states, except tstate, from the linked list of |
| 477 | thread states. This will allow calling PyThreadState_Clear() |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 478 | without holding the lock. */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 479 | garbage = interp->tstate_head; |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 480 | if (garbage == tstate) |
| 481 | garbage = tstate->next; |
| 482 | if (tstate->prev) |
| 483 | tstate->prev->next = tstate->next; |
| 484 | if (tstate->next) |
| 485 | tstate->next->prev = tstate->prev; |
| 486 | tstate->prev = tstate->next = NULL; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 487 | interp->tstate_head = tstate; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 488 | HEAD_UNLOCK(); |
| 489 | /* Clear and deallocate all stale thread states. Even if this |
| 490 | executes Python code, we should be safe since it executes |
| 491 | in the current thread, not one of the stale threads. */ |
| 492 | for (p = garbage; p; p = next) { |
| 493 | next = p->next; |
| 494 | PyThreadState_Clear(p); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 495 | PyMem_RawFree(p); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 500 | PyThreadState * |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 501 | _PyThreadState_UncheckedGet(void) |
| 502 | { |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 503 | return GET_TSTATE(); |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | |
| 507 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 508 | PyThreadState_Get(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 509 | { |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 510 | PyThreadState *tstate = GET_TSTATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | if (tstate == NULL) |
| 512 | Py_FatalError("PyThreadState_Get: no current thread"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | |
| 518 | PyThreadState * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 519 | PyThreadState_Swap(PyThreadState *newts) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 520 | { |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 521 | PyThreadState *oldts = GET_TSTATE(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 522 | |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 523 | SET_TSTATE(newts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | /* It should not be possible for more than one thread state |
| 525 | to be used for a thread. Check this the best we can in debug |
| 526 | builds. |
| 527 | */ |
Martin v. Löwis | 9e29625 | 2003-05-01 05:25:29 +0000 | [diff] [blame] | 528 | #if defined(Py_DEBUG) && defined(WITH_THREAD) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | if (newts) { |
| 530 | /* This can be called from PyEval_RestoreThread(). Similar |
| 531 | to it, we need to ensure errno doesn't change. |
| 532 | */ |
| 533 | int err = errno; |
| 534 | PyThreadState *check = PyGILState_GetThisThreadState(); |
| 535 | if (check && check->interp == newts->interp && check != newts) |
| 536 | Py_FatalError("Invalid thread state for this thread"); |
| 537 | errno = err; |
| 538 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 539 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | return oldts; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 541 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 542 | |
| 543 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 544 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 545 | 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] | 546 | PyThreadState_GetDict() returns NULL, an exception has *not* been raised |
| 547 | and the caller should assume no per-thread state is available. */ |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 548 | |
| 549 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 550 | PyThreadState_GetDict(void) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 551 | { |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 552 | PyThreadState *tstate = GET_TSTATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | if (tstate == NULL) |
| 554 | return NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 556 | if (tstate->dict == NULL) { |
| 557 | PyObject *d; |
| 558 | tstate->dict = d = PyDict_New(); |
| 559 | if (d == NULL) |
| 560 | PyErr_Clear(); |
| 561 | } |
| 562 | return tstate->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 563 | } |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 564 | |
| 565 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 566 | /* Asynchronously raise an exception in a thread. |
| 567 | Requested by Just van Rossum and Alex Martelli. |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 568 | To prevent naive misuse, you must write your own extension |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 569 | to call this, or use ctypes. Must be called with the GIL held. |
| 570 | Returns the number of tstates modified (normally 1, but 0 if `id` didn't |
| 571 | match any known thread id). Can be called with exc=NULL to clear an |
| 572 | existing async exception. This raises no exceptions. */ |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 573 | |
| 574 | int |
| 575 | PyThreadState_SetAsyncExc(long id, PyObject *exc) { |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 576 | PyInterpreterState *interp = GET_INTERP_STATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | PyThreadState *p; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | /* Although the GIL is held, a few C API functions can be called |
| 580 | * without the GIL held, and in particular some that create and |
| 581 | * destroy thread and interpreter states. Those can mutate the |
| 582 | * list of thread states we're traversing, so to prevent that we lock |
| 583 | * head_mutex for the duration. |
| 584 | */ |
| 585 | HEAD_LOCK(); |
| 586 | for (p = interp->tstate_head; p != NULL; p = p->next) { |
| 587 | if (p->thread_id == id) { |
| 588 | /* Tricky: we need to decref the current value |
| 589 | * (if any) in p->async_exc, but that can in turn |
| 590 | * allow arbitrary Python code to run, including |
| 591 | * perhaps calls to this function. To prevent |
| 592 | * deadlock, we need to release head_mutex before |
| 593 | * the decref. |
| 594 | */ |
| 595 | PyObject *old_exc = p->async_exc; |
| 596 | Py_XINCREF(exc); |
| 597 | p->async_exc = exc; |
| 598 | HEAD_UNLOCK(); |
| 599 | Py_XDECREF(old_exc); |
| 600 | _PyEval_SignalAsyncExc(); |
| 601 | return 1; |
| 602 | } |
| 603 | } |
| 604 | HEAD_UNLOCK(); |
| 605 | return 0; |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 609 | /* Routines for advanced debuggers, requested by David Beazley. |
| 610 | Don't use unless you know what you are doing! */ |
| 611 | |
| 612 | PyInterpreterState * |
| 613 | PyInterpreterState_Head(void) |
| 614 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | return interp_head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | PyInterpreterState * |
| 619 | PyInterpreterState_Next(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | return interp->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | PyThreadState * |
| 624 | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | return interp->tstate_head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | PyThreadState * |
| 629 | PyThreadState_Next(PyThreadState *tstate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | return tstate->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 631 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 632 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 633 | /* The implementation of sys._current_frames(). This is intended to be |
| 634 | called with the GIL held, as it will be when called via |
| 635 | sys._current_frames(). It's possible it would work fine even without |
| 636 | the GIL held, but haven't thought enough about that. |
| 637 | */ |
| 638 | PyObject * |
| 639 | _PyThread_CurrentFrames(void) |
| 640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | PyObject *result; |
| 642 | PyInterpreterState *i; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | result = PyDict_New(); |
| 645 | if (result == NULL) |
| 646 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | /* for i in all interpreters: |
| 649 | * for t in all of i's thread states: |
| 650 | * 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] | 651 | * Because these lists can mutate even when the GIL is held, we |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | * need to grab head_mutex for the duration. |
| 653 | */ |
| 654 | HEAD_LOCK(); |
| 655 | for (i = interp_head; i != NULL; i = i->next) { |
| 656 | PyThreadState *t; |
| 657 | for (t = i->tstate_head; t != NULL; t = t->next) { |
| 658 | PyObject *id; |
| 659 | int stat; |
| 660 | struct _frame *frame = t->frame; |
| 661 | if (frame == NULL) |
| 662 | continue; |
| 663 | id = PyLong_FromLong(t->thread_id); |
| 664 | if (id == NULL) |
| 665 | goto Fail; |
| 666 | stat = PyDict_SetItem(result, id, (PyObject *)frame); |
| 667 | Py_DECREF(id); |
| 668 | if (stat < 0) |
| 669 | goto Fail; |
| 670 | } |
| 671 | } |
| 672 | HEAD_UNLOCK(); |
| 673 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 674 | |
| 675 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | HEAD_UNLOCK(); |
| 677 | Py_DECREF(result); |
| 678 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 679 | } |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 680 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 681 | /* Python "auto thread state" API. */ |
| 682 | #ifdef WITH_THREAD |
| 683 | |
| 684 | /* Keep this as a static, as it is not reliable! It can only |
| 685 | ever be compared to the state for the *current* thread. |
| 686 | * If not equal, then it doesn't matter that the actual |
| 687 | value may change immediately after comparison, as it can't |
| 688 | possibly change to the current thread's state. |
| 689 | * If equal, then the current thread holds the lock, so the value can't |
| 690 | change until we yield the lock. |
| 691 | */ |
| 692 | static int |
| 693 | PyThreadState_IsCurrent(PyThreadState *tstate) |
| 694 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | /* Must be the tstate for this thread */ |
| 696 | assert(PyGILState_GetThisThreadState()==tstate); |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 697 | return tstate == GET_TSTATE(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 700 | /* Internal initialization/finalization functions called by |
| 701 | Py_Initialize/Py_Finalize |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 702 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 703 | void |
| 704 | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 705 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | assert(i && t); /* must init with valid states */ |
| 707 | autoTLSkey = PyThread_create_key(); |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 708 | if (autoTLSkey == -1) |
| 709 | Py_FatalError("Could not allocate TLS entry"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | autoInterpreterState = i; |
| 711 | assert(PyThread_get_key_value(autoTLSkey) == NULL); |
| 712 | assert(t->gilstate_counter == 0); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | _PyGILState_NoteThreadState(t); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 717 | void |
| 718 | _PyGILState_Fini(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 719 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | PyThread_delete_key(autoTLSkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | autoInterpreterState = NULL; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Charles-François Natali | a233df8 | 2011-11-22 19:49:51 +0100 | [diff] [blame] | 724 | /* Reset the TLS key - called by PyOS_AfterFork(). |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 725 | * This should not be necessary, but some - buggy - pthread implementations |
Charles-François Natali | a233df8 | 2011-11-22 19:49:51 +0100 | [diff] [blame] | 726 | * don't reset TLS upon fork(), see issue #10517. |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 727 | */ |
| 728 | void |
| 729 | _PyGILState_Reinit(void) |
| 730 | { |
| 731 | PyThreadState *tstate = PyGILState_GetThisThreadState(); |
| 732 | PyThread_delete_key(autoTLSkey); |
| 733 | if ((autoTLSkey = PyThread_create_key()) == -1) |
| 734 | Py_FatalError("Could not allocate TLS entry"); |
| 735 | |
Charles-François Natali | a233df8 | 2011-11-22 19:49:51 +0100 | [diff] [blame] | 736 | /* If the thread had an associated auto thread state, reassociate it with |
| 737 | * the new key. */ |
| 738 | if (tstate && PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 739 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
| 740 | } |
| 741 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 742 | /* When a thread state is created for a thread by some mechanism other than |
| 743 | PyGILState_Ensure, it's important that the GILState machinery knows about |
| 744 | it so it doesn't try to create another thread state for the thread (this is |
| 745 | a better fix for SF bug #1010677 than the first one attempted). |
| 746 | */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 747 | static void |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 748 | _PyGILState_NoteThreadState(PyThreadState* tstate) |
| 749 | { |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 750 | /* If autoTLSkey isn't initialized, this must be the very first |
| 751 | threadstate created in Py_Initialize(). Don't do anything for now |
| 752 | (we'll be back here when _PyGILState_Init is called). */ |
| 753 | if (!autoInterpreterState) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | /* Stick the thread state for this thread in thread local storage. |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | The only situation where you can legitimately have more than one |
| 759 | thread state for an OS level thread is when there are multiple |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 760 | interpreters. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 761 | |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 762 | You shouldn't really be using the PyGILState_ APIs anyway (see issues |
| 763 | #10915 and #15751). |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 764 | |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 765 | The first thread state created for that given OS level thread will |
| 766 | "win", which seems reasonable behaviour. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | */ |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 768 | if (PyThread_get_key_value(autoTLSkey) == NULL) { |
| 769 | if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
| 770 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
| 771 | } |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | /* PyGILState_Release must not try to delete this thread state. */ |
| 774 | tstate->gilstate_counter = 1; |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 777 | /* The public functions */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 778 | PyThreadState * |
| 779 | PyGILState_GetThisThreadState(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 780 | { |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 781 | if (autoInterpreterState == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | return NULL; |
| 783 | return (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 786 | int |
| 787 | PyGILState_Check(void) |
| 788 | { |
Victor Stinner | 244e120 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 789 | PyThreadState *tstate = GET_TSTATE(); |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 790 | return tstate && (tstate == PyGILState_GetThisThreadState()); |
| 791 | } |
| 792 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 793 | PyGILState_STATE |
| 794 | PyGILState_Ensure(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 795 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | int current; |
| 797 | PyThreadState *tcur; |
| 798 | /* Note that we do not auto-init Python here - apart from |
| 799 | potential races with 2 threads auto-initializing, pep-311 |
| 800 | spells out other issues. Embedders are expected to have |
| 801 | called Py_Initialize() and usually PyEval_InitThreads(). |
| 802 | */ |
| 803 | assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ |
| 804 | tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
| 805 | if (tcur == NULL) { |
Victor Stinner | 62ca100 | 2013-12-13 01:46:43 +0100 | [diff] [blame] | 806 | /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is |
| 807 | called from a new thread for the first time, we need the create the |
| 808 | GIL. */ |
| 809 | PyEval_InitThreads(); |
| 810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | /* Create a new thread state for this thread */ |
| 812 | tcur = PyThreadState_New(autoInterpreterState); |
| 813 | if (tcur == NULL) |
| 814 | Py_FatalError("Couldn't create thread-state for new thread"); |
| 815 | /* This is our thread state! We'll need to delete it in the |
| 816 | matching call to PyGILState_Release(). */ |
| 817 | tcur->gilstate_counter = 0; |
| 818 | current = 0; /* new thread state is never current */ |
| 819 | } |
| 820 | else |
| 821 | current = PyThreadState_IsCurrent(tcur); |
| 822 | if (current == 0) |
| 823 | PyEval_RestoreThread(tcur); |
| 824 | /* Update our counter in the thread-state - no need for locks: |
| 825 | - tcur will remain valid as we hold the GIL. |
| 826 | - the counter is safe as we are the only thread "allowed" |
| 827 | to modify this value |
| 828 | */ |
| 829 | ++tcur->gilstate_counter; |
| 830 | return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 833 | void |
| 834 | PyGILState_Release(PyGILState_STATE oldstate) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( |
| 837 | autoTLSkey); |
| 838 | if (tcur == NULL) |
| 839 | Py_FatalError("auto-releasing thread-state, " |
| 840 | "but no thread-state for this thread"); |
| 841 | /* We must hold the GIL and have our thread state current */ |
| 842 | /* XXX - remove the check - the assert should be fine, |
| 843 | but while this is very new (April 2003), the extra check |
| 844 | by release-only users can't hurt. |
| 845 | */ |
| 846 | if (! PyThreadState_IsCurrent(tcur)) |
| 847 | Py_FatalError("This thread state must be current when releasing"); |
| 848 | assert(PyThreadState_IsCurrent(tcur)); |
| 849 | --tcur->gilstate_counter; |
| 850 | assert(tcur->gilstate_counter >= 0); /* illegal counter value */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 851 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | /* If we're going to destroy this thread-state, we must |
| 853 | * clear it while the GIL is held, as destructors may run. |
| 854 | */ |
| 855 | if (tcur->gilstate_counter == 0) { |
| 856 | /* can't have been locked when we created it */ |
| 857 | assert(oldstate == PyGILState_UNLOCKED); |
| 858 | PyThreadState_Clear(tcur); |
| 859 | /* Delete the thread-state. Note this releases the GIL too! |
| 860 | * It's vital that the GIL be held here, to avoid shutdown |
| 861 | * races; see bugs 225673 and 1061968 (that nasty bug has a |
| 862 | * habit of coming back). |
| 863 | */ |
| 864 | PyThreadState_DeleteCurrent(); |
| 865 | } |
| 866 | /* Release the lock if necessary */ |
| 867 | else if (oldstate == PyGILState_UNLOCKED) |
| 868 | PyEval_SaveThread(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 869 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 870 | |
Benjamin Peterson | 3bf0175 | 2012-04-13 18:06:36 -0400 | [diff] [blame] | 871 | #endif /* WITH_THREAD */ |
| 872 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 873 | #ifdef __cplusplus |
| 874 | } |
| 875 | #endif |
| 876 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 877 | |