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 | |
| 9 | Always use malloc() and free() directly in this file. A number of these |
| 10 | functions are advertised as safe to call when the GIL isn't held, and in |
| 11 | a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging |
| 12 | obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid |
| 13 | the expense of doing their own locking). |
| 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 | |
| 25 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 26 | #ifdef WITH_THREAD |
| 27 | #include "pythread.h" |
| 28 | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ |
Moshe Zadka | 9fb6af9 | 2000-08-04 21:27:47 +0000 | [diff] [blame] | 29 | #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 30 | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 31 | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 32 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 37 | /* The single PyInterpreterState used by this process' |
| 38 | GILState implementation |
| 39 | */ |
| 40 | static PyInterpreterState *autoInterpreterState = NULL; |
| 41 | static int autoTLSkey = 0; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 42 | #else |
| 43 | #define HEAD_INIT() /* Nothing */ |
| 44 | #define HEAD_LOCK() /* Nothing */ |
| 45 | #define HEAD_UNLOCK() /* Nothing */ |
| 46 | #endif |
| 47 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 48 | static PyInterpreterState *interp_head = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 49 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 50 | /* Assuming the current thread holds the GIL, this is the |
| 51 | PyThreadState for the current thread. */ |
| 52 | _Py_atomic_address _PyThreadState_Current = {NULL}; |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 53 | PyThreadFrameGetter _PyThreadState_GetFrame = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 54 | |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 55 | #ifdef WITH_THREAD |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 56 | static void _PyGILState_NoteThreadState(PyThreadState* tstate); |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 57 | #endif |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 59 | |
| 60 | PyInterpreterState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 61 | PyInterpreterState_New(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 62 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | PyInterpreterState *interp = (PyInterpreterState *) |
| 64 | malloc(sizeof(PyInterpreterState)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | if (interp != NULL) { |
| 67 | HEAD_INIT(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | if (head_mutex == NULL) |
| 70 | Py_FatalError("Can't initialize threads for interpreter"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 71 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | interp->modules = NULL; |
| 73 | interp->modules_reloading = NULL; |
| 74 | interp->modules_by_index = NULL; |
| 75 | interp->sysdict = NULL; |
| 76 | interp->builtins = NULL; |
| 77 | interp->tstate_head = NULL; |
| 78 | interp->codec_search_path = NULL; |
| 79 | interp->codec_search_cache = NULL; |
| 80 | interp->codec_error_registry = NULL; |
| 81 | interp->codecs_initialized = 0; |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 82 | interp->fscodec_initialized = 0; |
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); |
| 117 | Py_CLEAR(interp->modules_reloading); |
| 118 | Py_CLEAR(interp->sysdict); |
| 119 | Py_CLEAR(interp->builtins); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
| 123 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 124 | zapthreads(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | PyThreadState *p; |
| 127 | /* No need to lock the mutex here because this should only happen |
| 128 | when the threads are all really dead (XXX famous last words). */ |
| 129 | while ((p = interp->tstate_head) != NULL) { |
| 130 | PyThreadState_Delete(p); |
| 131 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 136 | PyInterpreterState_Delete(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | PyInterpreterState **p; |
| 139 | zapthreads(interp); |
| 140 | HEAD_LOCK(); |
| 141 | for (p = &interp_head; ; p = &(*p)->next) { |
| 142 | if (*p == NULL) |
| 143 | Py_FatalError( |
| 144 | "PyInterpreterState_Delete: invalid interp"); |
| 145 | if (*p == interp) |
| 146 | break; |
| 147 | } |
| 148 | if (interp->tstate_head != NULL) |
| 149 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 150 | *p = interp->next; |
| 151 | HEAD_UNLOCK(); |
| 152 | free(interp); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 156 | /* Default implementation for _PyThreadState_GetFrame */ |
| 157 | static struct _frame * |
| 158 | threadstate_getframe(PyThreadState *self) |
| 159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | return self->frame; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 163 | static PyThreadState * |
| 164 | new_threadstate(PyInterpreterState *interp, int init) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState)); |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | if (_PyThreadState_GetFrame == NULL) |
| 169 | _PyThreadState_GetFrame = threadstate_getframe; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | if (tstate != NULL) { |
| 172 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | tstate->frame = NULL; |
| 175 | tstate->recursion_depth = 0; |
| 176 | tstate->overflowed = 0; |
| 177 | tstate->recursion_critical = 0; |
| 178 | tstate->tracing = 0; |
| 179 | tstate->use_tracing = 0; |
| 180 | tstate->tick_counter = 0; |
| 181 | tstate->gilstate_counter = 0; |
| 182 | tstate->async_exc = NULL; |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 183 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | tstate->thread_id = PyThread_get_thread_ident(); |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 185 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | tstate->thread_id = 0; |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 187 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | tstate->dict = NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | tstate->curexc_type = NULL; |
| 192 | tstate->curexc_value = NULL; |
| 193 | tstate->curexc_traceback = NULL; |
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->exc_type = NULL; |
| 196 | tstate->exc_value = NULL; |
| 197 | tstate->exc_traceback = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | tstate->c_profilefunc = NULL; |
| 200 | tstate->c_tracefunc = NULL; |
| 201 | tstate->c_profileobj = NULL; |
| 202 | tstate->c_traceobj = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | if (init) |
| 205 | _PyThreadState_Init(tstate); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | HEAD_LOCK(); |
| 208 | tstate->next = interp->tstate_head; |
| 209 | interp->tstate_head = tstate; |
| 210 | HEAD_UNLOCK(); |
| 211 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 216 | PyThreadState * |
| 217 | PyThreadState_New(PyInterpreterState *interp) |
| 218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | return new_threadstate(interp, 1); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | PyThreadState * |
| 223 | _PyThreadState_Prealloc(PyInterpreterState *interp) |
| 224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | return new_threadstate(interp, 0); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
| 229 | _PyThreadState_Init(PyThreadState *tstate) |
| 230 | { |
| 231 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | _PyGILState_NoteThreadState(tstate); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 233 | #endif |
| 234 | } |
| 235 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 236 | PyObject* |
| 237 | PyState_FindModule(struct PyModuleDef* m) |
| 238 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | Py_ssize_t index = m->m_base.m_index; |
| 240 | PyInterpreterState *state = PyThreadState_GET()->interp; |
| 241 | PyObject *res; |
| 242 | if (index == 0) |
| 243 | return NULL; |
| 244 | if (state->modules_by_index == NULL) |
| 245 | return NULL; |
| 246 | if (index > PyList_GET_SIZE(state->modules_by_index)) |
| 247 | return NULL; |
| 248 | res = PyList_GET_ITEM(state->modules_by_index, index); |
| 249 | return res==Py_None ? NULL : res; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | int |
| 253 | _PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
| 254 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | PyInterpreterState *state = PyThreadState_GET()->interp; |
| 256 | if (!def) |
| 257 | return -1; |
| 258 | if (!state->modules_by_index) { |
| 259 | state->modules_by_index = PyList_New(0); |
| 260 | if (!state->modules_by_index) |
| 261 | return -1; |
| 262 | } |
| 263 | while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index) |
| 264 | if (PyList_Append(state->modules_by_index, Py_None) < 0) |
| 265 | return -1; |
| 266 | Py_INCREF(module); |
| 267 | return PyList_SetItem(state->modules_by_index, |
| 268 | def->m_base.m_index, module); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 269 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 270 | |
| 271 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 272 | PyThreadState_Clear(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | if (Py_VerboseFlag && tstate->frame != NULL) |
| 275 | fprintf(stderr, |
| 276 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | Py_CLEAR(tstate->frame); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | Py_CLEAR(tstate->dict); |
| 281 | Py_CLEAR(tstate->async_exc); |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | Py_CLEAR(tstate->curexc_type); |
| 284 | Py_CLEAR(tstate->curexc_value); |
| 285 | Py_CLEAR(tstate->curexc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | Py_CLEAR(tstate->exc_type); |
| 288 | Py_CLEAR(tstate->exc_value); |
| 289 | Py_CLEAR(tstate->exc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | tstate->c_profilefunc = NULL; |
| 292 | tstate->c_tracefunc = NULL; |
| 293 | Py_CLEAR(tstate->c_profileobj); |
| 294 | Py_CLEAR(tstate->c_traceobj); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 298 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
| 299 | static void |
| 300 | tstate_delete_common(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | PyInterpreterState *interp; |
| 303 | PyThreadState **p; |
| 304 | PyThreadState *prev_p = NULL; |
| 305 | if (tstate == NULL) |
| 306 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
| 307 | interp = tstate->interp; |
| 308 | if (interp == NULL) |
| 309 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
| 310 | HEAD_LOCK(); |
| 311 | for (p = &interp->tstate_head; ; p = &(*p)->next) { |
| 312 | if (*p == NULL) |
| 313 | Py_FatalError( |
| 314 | "PyThreadState_Delete: invalid tstate"); |
| 315 | if (*p == tstate) |
| 316 | break; |
| 317 | /* Sanity check. These states should never happen but if |
| 318 | * they do we must abort. Otherwise we'll end up spinning in |
| 319 | * in a tight loop with the lock held. A similar check is done |
| 320 | * in thread.c find_key(). */ |
| 321 | if (*p == prev_p) |
| 322 | Py_FatalError( |
| 323 | "PyThreadState_Delete: small circular list(!)" |
| 324 | " and tstate not found."); |
| 325 | prev_p = *p; |
| 326 | if ((*p)->next == interp->tstate_head) |
| 327 | Py_FatalError( |
| 328 | "PyThreadState_Delete: circular list(!) and" |
| 329 | " tstate not found."); |
| 330 | } |
| 331 | *p = tstate->next; |
| 332 | HEAD_UNLOCK(); |
| 333 | free(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 337 | void |
| 338 | PyThreadState_Delete(PyThreadState *tstate) |
| 339 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | if (tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current)) |
| 341 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
| 342 | tstate_delete_common(tstate); |
Tim Peters | f4e6928 | 2006-02-27 17:15:31 +0000 | [diff] [blame] | 343 | #ifdef WITH_THREAD |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 344 | if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | PyThread_delete_key_value(autoTLSkey); |
Tim Peters | f4e6928 | 2006-02-27 17:15:31 +0000 | [diff] [blame] | 346 | #endif /* WITH_THREAD */ |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | |
| 350 | #ifdef WITH_THREAD |
| 351 | void |
| 352 | PyThreadState_DeleteCurrent() |
| 353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed( |
| 355 | &_PyThreadState_Current); |
| 356 | if (tstate == NULL) |
| 357 | Py_FatalError( |
| 358 | "PyThreadState_DeleteCurrent: no current tstate"); |
| 359 | _Py_atomic_store_relaxed(&_PyThreadState_Current, NULL); |
| 360 | tstate_delete_common(tstate); |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 361 | if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | PyThread_delete_key_value(autoTLSkey); |
| 363 | PyEval_ReleaseLock(); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 364 | } |
| 365 | #endif /* WITH_THREAD */ |
| 366 | |
| 367 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 368 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 369 | PyThreadState_Get(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 370 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed( |
| 372 | &_PyThreadState_Current); |
| 373 | if (tstate == NULL) |
| 374 | Py_FatalError("PyThreadState_Get: no current thread"); |
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 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
| 380 | PyThreadState * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 381 | PyThreadState_Swap(PyThreadState *newts) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | PyThreadState *oldts = (PyThreadState*)_Py_atomic_load_relaxed( |
| 384 | &_PyThreadState_Current); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | _Py_atomic_store_relaxed(&_PyThreadState_Current, newts); |
| 387 | /* It should not be possible for more than one thread state |
| 388 | to be used for a thread. Check this the best we can in debug |
| 389 | builds. |
| 390 | */ |
Martin v. Löwis | 9e29625 | 2003-05-01 05:25:29 +0000 | [diff] [blame] | 391 | #if defined(Py_DEBUG) && defined(WITH_THREAD) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | if (newts) { |
| 393 | /* This can be called from PyEval_RestoreThread(). Similar |
| 394 | to it, we need to ensure errno doesn't change. |
| 395 | */ |
| 396 | int err = errno; |
| 397 | PyThreadState *check = PyGILState_GetThisThreadState(); |
| 398 | if (check && check->interp == newts->interp && check != newts) |
| 399 | Py_FatalError("Invalid thread state for this thread"); |
| 400 | errno = err; |
| 401 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 402 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | return oldts; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 404 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 405 | |
| 406 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 407 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 408 | 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] | 409 | PyThreadState_GetDict() returns NULL, an exception has *not* been raised |
| 410 | and the caller should assume no per-thread state is available. */ |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 411 | |
| 412 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 413 | PyThreadState_GetDict(void) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed( |
| 416 | &_PyThreadState_Current); |
| 417 | if (tstate == NULL) |
| 418 | return NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | if (tstate->dict == NULL) { |
| 421 | PyObject *d; |
| 422 | tstate->dict = d = PyDict_New(); |
| 423 | if (d == NULL) |
| 424 | PyErr_Clear(); |
| 425 | } |
| 426 | return tstate->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 427 | } |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 428 | |
| 429 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 430 | /* Asynchronously raise an exception in a thread. |
| 431 | Requested by Just van Rossum and Alex Martelli. |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 432 | To prevent naive misuse, you must write your own extension |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 433 | to call this, or use ctypes. Must be called with the GIL held. |
| 434 | Returns the number of tstates modified (normally 1, but 0 if `id` didn't |
| 435 | match any known thread id). Can be called with exc=NULL to clear an |
| 436 | existing async exception. This raises no exceptions. */ |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 437 | |
| 438 | int |
| 439 | PyThreadState_SetAsyncExc(long id, PyObject *exc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | PyThreadState *tstate = PyThreadState_GET(); |
| 441 | PyInterpreterState *interp = tstate->interp; |
| 442 | PyThreadState *p; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | /* Although the GIL is held, a few C API functions can be called |
| 445 | * without the GIL held, and in particular some that create and |
| 446 | * destroy thread and interpreter states. Those can mutate the |
| 447 | * list of thread states we're traversing, so to prevent that we lock |
| 448 | * head_mutex for the duration. |
| 449 | */ |
| 450 | HEAD_LOCK(); |
| 451 | for (p = interp->tstate_head; p != NULL; p = p->next) { |
| 452 | if (p->thread_id == id) { |
| 453 | /* Tricky: we need to decref the current value |
| 454 | * (if any) in p->async_exc, but that can in turn |
| 455 | * allow arbitrary Python code to run, including |
| 456 | * perhaps calls to this function. To prevent |
| 457 | * deadlock, we need to release head_mutex before |
| 458 | * the decref. |
| 459 | */ |
| 460 | PyObject *old_exc = p->async_exc; |
| 461 | Py_XINCREF(exc); |
| 462 | p->async_exc = exc; |
| 463 | HEAD_UNLOCK(); |
| 464 | Py_XDECREF(old_exc); |
| 465 | _PyEval_SignalAsyncExc(); |
| 466 | return 1; |
| 467 | } |
| 468 | } |
| 469 | HEAD_UNLOCK(); |
| 470 | return 0; |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 474 | /* Routines for advanced debuggers, requested by David Beazley. |
| 475 | Don't use unless you know what you are doing! */ |
| 476 | |
| 477 | PyInterpreterState * |
| 478 | PyInterpreterState_Head(void) |
| 479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | return interp_head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | PyInterpreterState * |
| 484 | PyInterpreterState_Next(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | return interp->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | PyThreadState * |
| 489 | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | return interp->tstate_head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | PyThreadState * |
| 494 | PyThreadState_Next(PyThreadState *tstate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | return tstate->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 496 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 497 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 498 | /* The implementation of sys._current_frames(). This is intended to be |
| 499 | called with the GIL held, as it will be when called via |
| 500 | sys._current_frames(). It's possible it would work fine even without |
| 501 | the GIL held, but haven't thought enough about that. |
| 502 | */ |
| 503 | PyObject * |
| 504 | _PyThread_CurrentFrames(void) |
| 505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | PyObject *result; |
| 507 | PyInterpreterState *i; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | result = PyDict_New(); |
| 510 | if (result == NULL) |
| 511 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | /* for i in all interpreters: |
| 514 | * for t in all of i's thread states: |
| 515 | * 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] | 516 | * Because these lists can mutate even when the GIL is held, we |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | * need to grab head_mutex for the duration. |
| 518 | */ |
| 519 | HEAD_LOCK(); |
| 520 | for (i = interp_head; i != NULL; i = i->next) { |
| 521 | PyThreadState *t; |
| 522 | for (t = i->tstate_head; t != NULL; t = t->next) { |
| 523 | PyObject *id; |
| 524 | int stat; |
| 525 | struct _frame *frame = t->frame; |
| 526 | if (frame == NULL) |
| 527 | continue; |
| 528 | id = PyLong_FromLong(t->thread_id); |
| 529 | if (id == NULL) |
| 530 | goto Fail; |
| 531 | stat = PyDict_SetItem(result, id, (PyObject *)frame); |
| 532 | Py_DECREF(id); |
| 533 | if (stat < 0) |
| 534 | goto Fail; |
| 535 | } |
| 536 | } |
| 537 | HEAD_UNLOCK(); |
| 538 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 539 | |
| 540 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | HEAD_UNLOCK(); |
| 542 | Py_DECREF(result); |
| 543 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 544 | } |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 545 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 546 | /* Python "auto thread state" API. */ |
| 547 | #ifdef WITH_THREAD |
| 548 | |
| 549 | /* Keep this as a static, as it is not reliable! It can only |
| 550 | ever be compared to the state for the *current* thread. |
| 551 | * If not equal, then it doesn't matter that the actual |
| 552 | value may change immediately after comparison, as it can't |
| 553 | possibly change to the current thread's state. |
| 554 | * If equal, then the current thread holds the lock, so the value can't |
| 555 | change until we yield the lock. |
| 556 | */ |
| 557 | static int |
| 558 | PyThreadState_IsCurrent(PyThreadState *tstate) |
| 559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | /* Must be the tstate for this thread */ |
| 561 | assert(PyGILState_GetThisThreadState()==tstate); |
| 562 | return tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 565 | /* Internal initialization/finalization functions called by |
| 566 | Py_Initialize/Py_Finalize |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 567 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 568 | void |
| 569 | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | assert(i && t); /* must init with valid states */ |
| 572 | autoTLSkey = PyThread_create_key(); |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 573 | if (autoTLSkey == -1) |
| 574 | Py_FatalError("Could not allocate TLS entry"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | autoInterpreterState = i; |
| 576 | assert(PyThread_get_key_value(autoTLSkey) == NULL); |
| 577 | assert(t->gilstate_counter == 0); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | _PyGILState_NoteThreadState(t); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 582 | void |
| 583 | _PyGILState_Fini(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 584 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | PyThread_delete_key(autoTLSkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | autoInterpreterState = NULL; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 589 | /* Reset the TLS key - called by PyOS_AfterFork. |
| 590 | * This should not be necessary, but some - buggy - pthread implementations |
| 591 | * don't flush TLS on fork, see issue #10517. |
| 592 | */ |
| 593 | void |
| 594 | _PyGILState_Reinit(void) |
| 595 | { |
| 596 | PyThreadState *tstate = PyGILState_GetThisThreadState(); |
| 597 | PyThread_delete_key(autoTLSkey); |
| 598 | if ((autoTLSkey = PyThread_create_key()) == -1) |
| 599 | Py_FatalError("Could not allocate TLS entry"); |
| 600 | |
| 601 | /* re-associate the current thread state with the new key */ |
| 602 | if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
| 603 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
| 604 | } |
| 605 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 606 | /* When a thread state is created for a thread by some mechanism other than |
| 607 | PyGILState_Ensure, it's important that the GILState machinery knows about |
| 608 | it so it doesn't try to create another thread state for the thread (this is |
| 609 | a better fix for SF bug #1010677 than the first one attempted). |
| 610 | */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 611 | static void |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 612 | _PyGILState_NoteThreadState(PyThreadState* tstate) |
| 613 | { |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 614 | /* If autoTLSkey isn't initialized, this must be the very first |
| 615 | threadstate created in Py_Initialize(). Don't do anything for now |
| 616 | (we'll be back here when _PyGILState_Init is called). */ |
| 617 | if (!autoInterpreterState) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | /* Stick the thread state for this thread in thread local storage. |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | The only situation where you can legitimately have more than one |
| 623 | thread state for an OS level thread is when there are multiple |
| 624 | interpreters, when: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | a) You shouldn't really be using the PyGILState_ APIs anyway, |
| 627 | and: |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | b) The slightly odd way PyThread_set_key_value works (see |
| 630 | comments by its implementation) means that the first thread |
| 631 | state created for that given OS level thread will "win", |
| 632 | which seems reasonable behaviour. |
| 633 | */ |
| 634 | if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
| 635 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | /* PyGILState_Release must not try to delete this thread state. */ |
| 638 | tstate->gilstate_counter = 1; |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 641 | /* The public functions */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 642 | PyThreadState * |
| 643 | PyGILState_GetThisThreadState(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 644 | { |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 645 | if (autoInterpreterState == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | return NULL; |
| 647 | return (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 650 | PyGILState_STATE |
| 651 | PyGILState_Ensure(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | int current; |
| 654 | PyThreadState *tcur; |
| 655 | /* Note that we do not auto-init Python here - apart from |
| 656 | potential races with 2 threads auto-initializing, pep-311 |
| 657 | spells out other issues. Embedders are expected to have |
| 658 | called Py_Initialize() and usually PyEval_InitThreads(). |
| 659 | */ |
| 660 | assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ |
| 661 | tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
| 662 | if (tcur == NULL) { |
| 663 | /* Create a new thread state for this thread */ |
| 664 | tcur = PyThreadState_New(autoInterpreterState); |
| 665 | if (tcur == NULL) |
| 666 | Py_FatalError("Couldn't create thread-state for new thread"); |
| 667 | /* This is our thread state! We'll need to delete it in the |
| 668 | matching call to PyGILState_Release(). */ |
| 669 | tcur->gilstate_counter = 0; |
| 670 | current = 0; /* new thread state is never current */ |
| 671 | } |
| 672 | else |
| 673 | current = PyThreadState_IsCurrent(tcur); |
| 674 | if (current == 0) |
| 675 | PyEval_RestoreThread(tcur); |
| 676 | /* Update our counter in the thread-state - no need for locks: |
| 677 | - tcur will remain valid as we hold the GIL. |
| 678 | - the counter is safe as we are the only thread "allowed" |
| 679 | to modify this value |
| 680 | */ |
| 681 | ++tcur->gilstate_counter; |
| 682 | return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 685 | void |
| 686 | PyGILState_Release(PyGILState_STATE oldstate) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( |
| 689 | autoTLSkey); |
| 690 | if (tcur == NULL) |
| 691 | Py_FatalError("auto-releasing thread-state, " |
| 692 | "but no thread-state for this thread"); |
| 693 | /* We must hold the GIL and have our thread state current */ |
| 694 | /* XXX - remove the check - the assert should be fine, |
| 695 | but while this is very new (April 2003), the extra check |
| 696 | by release-only users can't hurt. |
| 697 | */ |
| 698 | if (! PyThreadState_IsCurrent(tcur)) |
| 699 | Py_FatalError("This thread state must be current when releasing"); |
| 700 | assert(PyThreadState_IsCurrent(tcur)); |
| 701 | --tcur->gilstate_counter; |
| 702 | assert(tcur->gilstate_counter >= 0); /* illegal counter value */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | /* If we're going to destroy this thread-state, we must |
| 705 | * clear it while the GIL is held, as destructors may run. |
| 706 | */ |
| 707 | if (tcur->gilstate_counter == 0) { |
| 708 | /* can't have been locked when we created it */ |
| 709 | assert(oldstate == PyGILState_UNLOCKED); |
| 710 | PyThreadState_Clear(tcur); |
| 711 | /* Delete the thread-state. Note this releases the GIL too! |
| 712 | * It's vital that the GIL be held here, to avoid shutdown |
| 713 | * races; see bugs 225673 and 1061968 (that nasty bug has a |
| 714 | * habit of coming back). |
| 715 | */ |
| 716 | PyThreadState_DeleteCurrent(); |
| 717 | } |
| 718 | /* Release the lock if necessary */ |
| 719 | else if (oldstate == PyGILState_UNLOCKED) |
| 720 | PyEval_SaveThread(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 721 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 722 | |
| 723 | #ifdef __cplusplus |
| 724 | } |
| 725 | #endif |
| 726 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 727 | #endif /* WITH_THREAD */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 728 | |
| 729 | |