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 | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | PyInterpreterState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 41 | PyInterpreterState_New(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 42 | { |
| 43 | PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 45 | if (interp != NULL) { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 46 | HEAD_INIT(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 47 | interp->modules = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 48 | interp->sysdict = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 49 | interp->builtins = NULL; |
| 50 | interp->checkinterval = 10; |
| 51 | interp->tstate_head = NULL; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 52 | #ifdef HAVE_DLOPEN |
| 53 | #ifdef RTLD_NOW |
| 54 | interp->dlopenflags = RTLD_NOW; |
| 55 | #else |
| 56 | interp->dlopenflags = RTLD_LAZY; |
| 57 | #endif |
| 58 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 59 | |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 60 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 61 | interp->next = interp_head; |
| 62 | interp_head = interp; |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 63 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 64 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 66 | return interp; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 71 | PyInterpreterState_Clear(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 72 | { |
| 73 | PyThreadState *p; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 74 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 75 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 76 | PyThreadState_Clear(p); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 77 | HEAD_UNLOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 78 | ZAP(interp->modules); |
| 79 | ZAP(interp->sysdict); |
| 80 | ZAP(interp->builtins); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 85 | zapthreads(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 86 | { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 87 | PyThreadState *p; |
| 88 | /* No need to lock the mutex here because this should only happen |
| 89 | when the threads are all really dead (XXX famous last words). */ |
| 90 | while ((p = interp->tstate_head) != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 91 | PyThreadState_Delete(p); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 97 | PyInterpreterState_Delete(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 98 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 99 | PyInterpreterState **p; |
| 100 | zapthreads(interp); |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 101 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 102 | for (p = &interp_head; ; p = &(*p)->next) { |
| 103 | if (*p == NULL) |
| 104 | Py_FatalError( |
| 105 | "PyInterpreterState_Delete: invalid interp"); |
| 106 | if (*p == interp) |
| 107 | break; |
| 108 | } |
| 109 | if (interp->tstate_head != NULL) |
| 110 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 111 | *p = interp->next; |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 112 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 113 | PyMem_DEL(interp); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 118 | PyThreadState_New(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 119 | { |
| 120 | PyThreadState *tstate = PyMem_NEW(PyThreadState, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 122 | if (tstate != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 123 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 124 | |
| 125 | tstate->frame = NULL; |
| 126 | tstate->recursion_depth = 0; |
| 127 | tstate->ticker = 0; |
| 128 | tstate->tracing = 0; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 129 | tstate->use_tracing = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 130 | |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 131 | tstate->dict = NULL; |
| 132 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 133 | tstate->curexc_type = NULL; |
| 134 | tstate->curexc_value = NULL; |
| 135 | tstate->curexc_traceback = NULL; |
| 136 | |
| 137 | tstate->exc_type = NULL; |
| 138 | tstate->exc_value = NULL; |
| 139 | tstate->exc_traceback = NULL; |
| 140 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 141 | tstate->c_profilefunc = NULL; |
| 142 | tstate->c_tracefunc = NULL; |
| 143 | tstate->c_profileobj = NULL; |
| 144 | tstate->c_traceobj = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 146 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 147 | tstate->next = interp->tstate_head; |
| 148 | interp->tstate_head = tstate; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 149 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 150 | } |
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 | return tstate; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 157 | PyThreadState_Clear(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 158 | { |
Guido van Rossum | 22348dc | 1997-11-03 22:08:36 +0000 | [diff] [blame] | 159 | if (Py_VerboseFlag && tstate->frame != NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 160 | fprintf(stderr, |
Guido van Rossum | 5f896a4 | 1997-08-21 02:28:19 +0000 | [diff] [blame] | 161 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 162 | |
| 163 | ZAP(tstate->frame); |
| 164 | |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 165 | ZAP(tstate->dict); |
| 166 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 167 | ZAP(tstate->curexc_type); |
| 168 | ZAP(tstate->curexc_value); |
| 169 | ZAP(tstate->curexc_traceback); |
| 170 | |
| 171 | ZAP(tstate->exc_type); |
| 172 | ZAP(tstate->exc_value); |
| 173 | ZAP(tstate->exc_traceback); |
| 174 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 175 | tstate->c_profilefunc = NULL; |
| 176 | tstate->c_tracefunc = NULL; |
| 177 | ZAP(tstate->c_profileobj); |
| 178 | ZAP(tstate->c_traceobj); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 182 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
| 183 | static void |
| 184 | tstate_delete_common(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 185 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 186 | PyInterpreterState *interp; |
| 187 | PyThreadState **p; |
| 188 | if (tstate == NULL) |
| 189 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 190 | interp = tstate->interp; |
| 191 | if (interp == NULL) |
| 192 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 193 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 194 | for (p = &interp->tstate_head; ; p = &(*p)->next) { |
| 195 | if (*p == NULL) |
| 196 | Py_FatalError( |
| 197 | "PyThreadState_Delete: invalid tstate"); |
| 198 | if (*p == tstate) |
| 199 | break; |
| 200 | } |
| 201 | *p = tstate->next; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 202 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 203 | PyMem_DEL(tstate); |
| 204 | } |
| 205 | |
| 206 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 207 | void |
| 208 | PyThreadState_Delete(PyThreadState *tstate) |
| 209 | { |
| 210 | if (tstate == _PyThreadState_Current) |
| 211 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
| 212 | tstate_delete_common(tstate); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | #ifdef WITH_THREAD |
| 217 | void |
| 218 | PyThreadState_DeleteCurrent() |
| 219 | { |
| 220 | PyThreadState *tstate = _PyThreadState_Current; |
| 221 | if (tstate == NULL) |
| 222 | Py_FatalError( |
| 223 | "PyThreadState_DeleteCurrent: no current tstate"); |
| 224 | _PyThreadState_Current = NULL; |
| 225 | tstate_delete_common(tstate); |
| 226 | PyEval_ReleaseLock(); |
| 227 | } |
| 228 | #endif /* WITH_THREAD */ |
| 229 | |
| 230 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 231 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 232 | PyThreadState_Get(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 233 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 234 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 235 | Py_FatalError("PyThreadState_Get: no current thread"); |
| 236 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 237 | return _PyThreadState_Current; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |
| 241 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 242 | PyThreadState_Swap(PyThreadState *new) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 243 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 244 | PyThreadState *old = _PyThreadState_Current; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 246 | _PyThreadState_Current = new; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 248 | return old; |
| 249 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 250 | |
| 251 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 252 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 253 | state; the caller should pick a unique key and store its state there. If |
| 254 | PyThreadState_GetDict() returns NULL, an exception has been raised (most |
| 255 | likely MemoryError) and the caller should pass on the exception. */ |
| 256 | |
| 257 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 258 | PyThreadState_GetDict(void) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 259 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 260 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 261 | Py_FatalError("PyThreadState_GetDict: no current thread"); |
| 262 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 263 | if (_PyThreadState_Current->dict == NULL) |
| 264 | _PyThreadState_Current->dict = PyDict_New(); |
| 265 | return _PyThreadState_Current->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 266 | } |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 267 | |
| 268 | |
| 269 | /* Routines for advanced debuggers, requested by David Beazley. |
| 270 | Don't use unless you know what you are doing! */ |
| 271 | |
| 272 | PyInterpreterState * |
| 273 | PyInterpreterState_Head(void) |
| 274 | { |
| 275 | return interp_head; |
| 276 | } |
| 277 | |
| 278 | PyInterpreterState * |
| 279 | PyInterpreterState_Next(PyInterpreterState *interp) { |
| 280 | return interp->next; |
| 281 | } |
| 282 | |
| 283 | PyThreadState * |
| 284 | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
| 285 | return interp->tstate_head; |
| 286 | } |
| 287 | |
| 288 | PyThreadState * |
| 289 | PyThreadState_Next(PyThreadState *tstate) { |
| 290 | return tstate->next; |
| 291 | } |