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