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