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 | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 26 | #define ZAP(x) { \ |
| 27 | PyObject *tmp = (PyObject *)(x); \ |
| 28 | (x) = NULL; \ |
| 29 | Py_XDECREF(tmp); \ |
| 30 | } |
| 31 | |
| 32 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 33 | #ifdef WITH_THREAD |
| 34 | #include "pythread.h" |
| 35 | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ |
Moshe Zadka | 9fb6af9 | 2000-08-04 21:27:47 +0000 | [diff] [blame] | 36 | #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 37 | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 38 | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) |
| 39 | #else |
| 40 | #define HEAD_INIT() /* Nothing */ |
| 41 | #define HEAD_LOCK() /* Nothing */ |
| 42 | #define HEAD_UNLOCK() /* Nothing */ |
| 43 | #endif |
| 44 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 45 | static PyInterpreterState *interp_head = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 47 | PyThreadState *_PyThreadState_Current = NULL; |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 48 | PyThreadFrameGetter _PyThreadState_GetFrame = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | PyInterpreterState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 52 | PyInterpreterState_New(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 53 | { |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 54 | PyInterpreterState *interp = (PyInterpreterState *) |
| 55 | malloc(sizeof(PyInterpreterState)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 57 | if (interp != NULL) { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 58 | HEAD_INIT(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 59 | interp->modules = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 60 | interp->sysdict = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 61 | interp->builtins = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 62 | interp->tstate_head = NULL; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 63 | interp->codec_search_path = NULL; |
| 64 | interp->codec_search_cache = NULL; |
| 65 | interp->codec_error_registry = NULL; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 66 | #ifdef HAVE_DLOPEN |
| 67 | #ifdef RTLD_NOW |
| 68 | interp->dlopenflags = RTLD_NOW; |
| 69 | #else |
| 70 | interp->dlopenflags = RTLD_LAZY; |
| 71 | #endif |
| 72 | #endif |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 73 | #ifdef WITH_TSC |
| 74 | interp->tscdump = 0; |
| 75 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 76 | |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 77 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 78 | interp->next = interp_head; |
| 79 | interp_head = interp; |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 80 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 81 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 83 | return interp; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 88 | PyInterpreterState_Clear(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 89 | { |
| 90 | PyThreadState *p; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 91 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 92 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 93 | PyThreadState_Clear(p); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 94 | HEAD_UNLOCK(); |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 95 | ZAP(interp->codec_search_path); |
| 96 | ZAP(interp->codec_search_cache); |
| 97 | ZAP(interp->codec_error_registry); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 98 | ZAP(interp->modules); |
| 99 | ZAP(interp->sysdict); |
| 100 | ZAP(interp->builtins); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 105 | zapthreads(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 106 | { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 107 | PyThreadState *p; |
| 108 | /* No need to lock the mutex here because this should only happen |
| 109 | when the threads are all really dead (XXX famous last words). */ |
| 110 | while ((p = interp->tstate_head) != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 111 | PyThreadState_Delete(p); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 117 | PyInterpreterState_Delete(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 118 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 119 | PyInterpreterState **p; |
| 120 | zapthreads(interp); |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 121 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 122 | for (p = &interp_head; ; p = &(*p)->next) { |
| 123 | if (*p == NULL) |
| 124 | Py_FatalError( |
| 125 | "PyInterpreterState_Delete: invalid interp"); |
| 126 | if (*p == interp) |
| 127 | break; |
| 128 | } |
| 129 | if (interp->tstate_head != NULL) |
| 130 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 131 | *p = interp->next; |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 132 | HEAD_UNLOCK(); |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 133 | free(interp); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 137 | /* Default implementation for _PyThreadState_GetFrame */ |
| 138 | static struct _frame * |
| 139 | threadstate_getframe(PyThreadState *self) |
| 140 | { |
| 141 | return self->frame; |
| 142 | } |
| 143 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 144 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 145 | PyThreadState_New(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 146 | { |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 147 | PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState)); |
| 148 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 149 | if (_PyThreadState_GetFrame == NULL) |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 150 | _PyThreadState_GetFrame = threadstate_getframe; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 152 | if (tstate != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 153 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 154 | |
| 155 | tstate->frame = NULL; |
| 156 | tstate->recursion_depth = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 157 | tstate->tracing = 0; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 158 | tstate->use_tracing = 0; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 159 | tstate->tick_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 160 | tstate->gilstate_counter = 0; |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 161 | tstate->async_exc = NULL; |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 162 | #ifdef WITH_THREAD |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 163 | tstate->thread_id = PyThread_get_thread_ident(); |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 164 | #else |
| 165 | tstate->thread_id = 0; |
| 166 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 168 | tstate->dict = NULL; |
| 169 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 170 | tstate->curexc_type = NULL; |
| 171 | tstate->curexc_value = NULL; |
| 172 | tstate->curexc_traceback = NULL; |
| 173 | |
| 174 | tstate->exc_type = NULL; |
| 175 | tstate->exc_value = NULL; |
| 176 | tstate->exc_traceback = NULL; |
| 177 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 178 | tstate->c_profilefunc = NULL; |
| 179 | tstate->c_tracefunc = NULL; |
| 180 | tstate->c_profileobj = NULL; |
| 181 | tstate->c_traceobj = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 183 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 184 | tstate->next = interp->tstate_head; |
| 185 | interp->tstate_head = tstate; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 186 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 187 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 189 | return tstate; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 194 | PyThreadState_Clear(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 195 | { |
Guido van Rossum | 22348dc | 1997-11-03 22:08:36 +0000 | [diff] [blame] | 196 | if (Py_VerboseFlag && tstate->frame != NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 197 | fprintf(stderr, |
Guido van Rossum | 5f896a4 | 1997-08-21 02:28:19 +0000 | [diff] [blame] | 198 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 199 | |
| 200 | ZAP(tstate->frame); |
| 201 | |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 202 | ZAP(tstate->dict); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 203 | ZAP(tstate->async_exc); |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 205 | ZAP(tstate->curexc_type); |
| 206 | ZAP(tstate->curexc_value); |
| 207 | ZAP(tstate->curexc_traceback); |
| 208 | |
| 209 | ZAP(tstate->exc_type); |
| 210 | ZAP(tstate->exc_value); |
| 211 | ZAP(tstate->exc_traceback); |
| 212 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 213 | tstate->c_profilefunc = NULL; |
| 214 | tstate->c_tracefunc = NULL; |
| 215 | ZAP(tstate->c_profileobj); |
| 216 | ZAP(tstate->c_traceobj); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 220 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
| 221 | static void |
| 222 | tstate_delete_common(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 223 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 224 | PyInterpreterState *interp; |
| 225 | PyThreadState **p; |
| 226 | if (tstate == NULL) |
| 227 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 228 | interp = tstate->interp; |
| 229 | if (interp == NULL) |
| 230 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 231 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 232 | for (p = &interp->tstate_head; ; p = &(*p)->next) { |
| 233 | if (*p == NULL) |
| 234 | Py_FatalError( |
| 235 | "PyThreadState_Delete: invalid tstate"); |
| 236 | if (*p == tstate) |
| 237 | break; |
| 238 | } |
| 239 | *p = tstate->next; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 240 | HEAD_UNLOCK(); |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 241 | free(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 245 | void |
| 246 | PyThreadState_Delete(PyThreadState *tstate) |
| 247 | { |
| 248 | if (tstate == _PyThreadState_Current) |
| 249 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
| 250 | tstate_delete_common(tstate); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | #ifdef WITH_THREAD |
| 255 | void |
| 256 | PyThreadState_DeleteCurrent() |
| 257 | { |
| 258 | PyThreadState *tstate = _PyThreadState_Current; |
| 259 | if (tstate == NULL) |
| 260 | Py_FatalError( |
| 261 | "PyThreadState_DeleteCurrent: no current tstate"); |
| 262 | _PyThreadState_Current = NULL; |
| 263 | tstate_delete_common(tstate); |
| 264 | PyEval_ReleaseLock(); |
| 265 | } |
| 266 | #endif /* WITH_THREAD */ |
| 267 | |
| 268 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 269 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 270 | PyThreadState_Get(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 271 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 272 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 273 | Py_FatalError("PyThreadState_Get: no current thread"); |
| 274 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 275 | return _PyThreadState_Current; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
| 279 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 280 | PyThreadState_Swap(PyThreadState *new) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 281 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 282 | PyThreadState *old = _PyThreadState_Current; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 284 | _PyThreadState_Current = new; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 285 | /* It should not be possible for more than one thread state |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 286 | to be used for a thread. Check this the best we can in debug |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 287 | builds. |
| 288 | */ |
Martin v. Löwis | 9e29625 | 2003-05-01 05:25:29 +0000 | [diff] [blame] | 289 | #if defined(Py_DEBUG) && defined(WITH_THREAD) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 290 | if (new) { |
| 291 | PyThreadState *check = PyGILState_GetThisThreadState(); |
| 292 | if (check && check != new) |
| 293 | Py_FatalError("Invalid thread state for this thread"); |
| 294 | } |
| 295 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 296 | return old; |
| 297 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 298 | |
| 299 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 300 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 301 | 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] | 302 | PyThreadState_GetDict() returns NULL, an exception has *not* been raised |
| 303 | and the caller should assume no per-thread state is available. */ |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 304 | |
| 305 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 306 | PyThreadState_GetDict(void) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 307 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 308 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | 0fc8f00 | 2003-04-15 15:12:39 +0000 | [diff] [blame] | 309 | return NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 310 | |
Guido van Rossum | 0fc8f00 | 2003-04-15 15:12:39 +0000 | [diff] [blame] | 311 | if (_PyThreadState_Current->dict == NULL) { |
| 312 | PyObject *d; |
| 313 | _PyThreadState_Current->dict = d = PyDict_New(); |
| 314 | if (d == NULL) |
| 315 | PyErr_Clear(); |
| 316 | } |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 317 | return _PyThreadState_Current->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 318 | } |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 319 | |
| 320 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 321 | /* Asynchronously raise an exception in a thread. |
| 322 | Requested by Just van Rossum and Alex Martelli. |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 323 | To prevent naive misuse, you must write your own extension |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 324 | to call this. Must be called with the GIL held. |
| 325 | Returns the number of tstates modified; if it returns a number |
| 326 | greater than one, you're in trouble, and you should call it again |
| 327 | with exc=NULL to revert the effect. This raises no exceptions. */ |
| 328 | |
| 329 | int |
| 330 | PyThreadState_SetAsyncExc(long id, PyObject *exc) { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 331 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 332 | PyInterpreterState *interp = tstate->interp; |
| 333 | PyThreadState *p; |
| 334 | int count = 0; |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 335 | HEAD_LOCK(); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 336 | for (p = interp->tstate_head; p != NULL; p = p->next) { |
| 337 | if (p->thread_id != id) |
| 338 | continue; |
| 339 | ZAP(p->async_exc); |
| 340 | Py_XINCREF(exc); |
| 341 | p->async_exc = exc; |
| 342 | count += 1; |
| 343 | } |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 344 | HEAD_UNLOCK(); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 345 | return count; |
| 346 | } |
| 347 | |
| 348 | |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 349 | /* Routines for advanced debuggers, requested by David Beazley. |
| 350 | Don't use unless you know what you are doing! */ |
| 351 | |
| 352 | PyInterpreterState * |
| 353 | PyInterpreterState_Head(void) |
| 354 | { |
| 355 | return interp_head; |
| 356 | } |
| 357 | |
| 358 | PyInterpreterState * |
| 359 | PyInterpreterState_Next(PyInterpreterState *interp) { |
| 360 | return interp->next; |
| 361 | } |
| 362 | |
| 363 | PyThreadState * |
| 364 | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
| 365 | return interp->tstate_head; |
| 366 | } |
| 367 | |
| 368 | PyThreadState * |
| 369 | PyThreadState_Next(PyThreadState *tstate) { |
| 370 | return tstate->next; |
| 371 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 372 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 373 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 374 | /* Python "auto thread state" API. */ |
| 375 | #ifdef WITH_THREAD |
| 376 | |
| 377 | /* Keep this as a static, as it is not reliable! It can only |
| 378 | ever be compared to the state for the *current* thread. |
| 379 | * If not equal, then it doesn't matter that the actual |
| 380 | value may change immediately after comparison, as it can't |
| 381 | possibly change to the current thread's state. |
| 382 | * If equal, then the current thread holds the lock, so the value can't |
| 383 | change until we yield the lock. |
| 384 | */ |
| 385 | static int |
| 386 | PyThreadState_IsCurrent(PyThreadState *tstate) |
| 387 | { |
| 388 | /* Must be the tstate for this thread */ |
| 389 | assert(PyGILState_GetThisThreadState()==tstate); |
| 390 | /* On Windows at least, simple reads and writes to 32 bit values |
| 391 | are atomic. |
| 392 | */ |
| 393 | return tstate == _PyThreadState_Current; |
| 394 | } |
| 395 | |
| 396 | /* The single PyInterpreterState used by this process' |
| 397 | GILState implementation |
| 398 | */ |
| 399 | static PyInterpreterState *autoInterpreterState = NULL; |
| 400 | static int autoTLSkey = 0; |
| 401 | |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 402 | /* Internal initialization/finalization functions called by |
| 403 | Py_Initialize/Py_Finalize |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 404 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 405 | void |
| 406 | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 407 | { |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 408 | assert(i && t); /* must init with valid states */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 409 | autoTLSkey = PyThread_create_key(); |
| 410 | autoInterpreterState = i; |
| 411 | /* Now stash the thread state for this thread in TLS */ |
Tim Peters | f9becec | 2004-10-09 22:47:13 +0000 | [diff] [blame] | 412 | assert(PyThread_get_key_value(autoTLSkey) == NULL); |
| 413 | if (PyThread_set_key_value(autoTLSkey, (void *)t) < 0) |
| 414 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 415 | assert(t->gilstate_counter == 0); /* must be a new thread state */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 416 | t->gilstate_counter = 1; |
| 417 | } |
| 418 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 419 | void |
| 420 | _PyGILState_Fini(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 421 | { |
| 422 | PyThread_delete_key(autoTLSkey); |
| 423 | autoTLSkey = 0; |
| 424 | autoInterpreterState = NULL;; |
| 425 | } |
| 426 | |
| 427 | /* The public functions */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 428 | PyThreadState * |
| 429 | PyGILState_GetThisThreadState(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 430 | { |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 431 | if (autoInterpreterState == NULL || autoTLSkey == 0) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 432 | return NULL; |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 433 | return (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 436 | PyGILState_STATE |
| 437 | PyGILState_Ensure(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 438 | { |
| 439 | int current; |
| 440 | PyThreadState *tcur; |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 441 | /* Note that we do not auto-init Python here - apart from |
| 442 | potential races with 2 threads auto-initializing, pep-311 |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 443 | spells out other issues. Embedders are expected to have |
| 444 | called Py_Initialize() and usually PyEval_InitThreads(). |
| 445 | */ |
| 446 | assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ |
| 447 | tcur = PyThread_get_key_value(autoTLSkey); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 448 | if (tcur == NULL) { |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 449 | /* Create a new thread state for this thread */ |
| 450 | tcur = PyThreadState_New(autoInterpreterState); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 451 | if (tcur == NULL) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 452 | Py_FatalError("Couldn't create thread-state for new thread"); |
Tim Peters | f9becec | 2004-10-09 22:47:13 +0000 | [diff] [blame] | 453 | if (PyThread_set_key_value(autoTLSkey, (void *)tcur) < 0) |
| 454 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 455 | current = 0; /* new thread state is never current */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 456 | } |
| 457 | else |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 458 | current = PyThreadState_IsCurrent(tcur); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 459 | if (current == 0) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 460 | PyEval_RestoreThread(tcur); |
| 461 | /* Update our counter in the thread-state - no need for locks: |
| 462 | - tcur will remain valid as we hold the GIL. |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 463 | - the counter is safe as we are the only thread "allowed" |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 464 | to modify this value |
| 465 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 466 | ++tcur->gilstate_counter; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 467 | return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
| 468 | } |
| 469 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 470 | void |
| 471 | PyGILState_Release(PyGILState_STATE oldstate) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 472 | { |
| 473 | PyThreadState *tcur = PyThread_get_key_value(autoTLSkey); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 474 | if (tcur == NULL) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 475 | Py_FatalError("auto-releasing thread-state, " |
| 476 | "but no thread-state for this thread"); |
| 477 | /* We must hold the GIL and have our thread state current */ |
| 478 | /* XXX - remove the check - the assert should be fine, |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 479 | but while this is very new (April 2003), the extra check |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 480 | by release-only users can't hurt. |
| 481 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 482 | if (! PyThreadState_IsCurrent(tcur)) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 483 | Py_FatalError("This thread state must be current when releasing"); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 484 | assert(PyThreadState_IsCurrent(tcur)); |
| 485 | --tcur->gilstate_counter; |
| 486 | assert(tcur->gilstate_counter >= 0); /* illegal counter value */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 487 | |
Tim Peters | fb1ffb0 | 2004-11-08 04:30:21 +0000 | [diff] [blame] | 488 | /* If we're going to destroy this thread-state, we must |
| 489 | * clear it while the GIL is held, as destructors may run. |
| 490 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 491 | if (tcur->gilstate_counter == 0) { |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 492 | /* can't have been locked when we created it */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 493 | assert(oldstate == PyGILState_UNLOCKED); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 494 | PyThreadState_Clear(tcur); |
Tim Peters | fb1ffb0 | 2004-11-08 04:30:21 +0000 | [diff] [blame] | 495 | /* Delete the thread-state. Note this releases the GIL too! |
| 496 | * It's vital that the GIL be held here, to avoid shutdown |
| 497 | * races; see bugs 225673 and 1061968 (that nasty bug has a |
| 498 | * habit of coming back). |
| 499 | */ |
| 500 | PyThreadState_DeleteCurrent(); |
| 501 | /* Delete this thread from our TLS. */ |
Tim Peters | 89c0ec9 | 2004-10-10 05:30:40 +0000 | [diff] [blame] | 502 | PyThread_delete_key_value(autoTLSkey); |
Tim Peters | 89c0ec9 | 2004-10-10 05:30:40 +0000 | [diff] [blame] | 503 | } |
Tim Peters | fb1ffb0 | 2004-11-08 04:30:21 +0000 | [diff] [blame] | 504 | /* Release the lock if necessary */ |
| 505 | else if (oldstate == PyGILState_UNLOCKED) |
Michael W. Hudson | 774479c | 2005-04-18 08:46:17 +0000 | [diff] [blame] | 506 | PyEval_SaveThread(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 507 | } |
| 508 | #endif /* WITH_THREAD */ |