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