Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
| 16 | |
| 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Thread and interpreter state structures and their interfaces */ |
| 33 | |
| 34 | #include "Python.h" |
| 35 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 36 | #define ZAP(x) { \ |
| 37 | PyObject *tmp = (PyObject *)(x); \ |
| 38 | (x) = NULL; \ |
| 39 | Py_XDECREF(tmp); \ |
| 40 | } |
| 41 | |
| 42 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 43 | #ifdef WITH_THREAD |
| 44 | #include "pythread.h" |
| 45 | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ |
| 46 | #define HEAD_INIT() (head_mutex || (head_mutex = PyThread_allocate_lock())) |
| 47 | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 48 | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) |
| 49 | #else |
| 50 | #define HEAD_INIT() /* Nothing */ |
| 51 | #define HEAD_LOCK() /* Nothing */ |
| 52 | #define HEAD_UNLOCK() /* Nothing */ |
| 53 | #endif |
| 54 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 55 | static PyInterpreterState *interp_head = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 57 | PyThreadState *_PyThreadState_Current = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | PyInterpreterState * |
| 61 | PyInterpreterState_New() |
| 62 | { |
| 63 | PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 65 | if (interp != NULL) { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 66 | HEAD_INIT(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 67 | interp->modules = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 68 | interp->sysdict = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 69 | interp->builtins = NULL; |
| 70 | interp->checkinterval = 10; |
| 71 | interp->tstate_head = NULL; |
| 72 | |
| 73 | interp->next = interp_head; |
| 74 | interp_head = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 75 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 77 | return interp; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | void |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 82 | PyInterpreterState_Clear(interp) |
| 83 | PyInterpreterState *interp; |
| 84 | { |
| 85 | PyThreadState *p; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 86 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 87 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 88 | PyThreadState_Clear(p); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 89 | HEAD_UNLOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 90 | ZAP(interp->modules); |
| 91 | ZAP(interp->sysdict); |
| 92 | ZAP(interp->builtins); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static void |
| 97 | zapthreads(interp) |
| 98 | PyInterpreterState *interp; |
| 99 | { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 100 | PyThreadState *p; |
| 101 | /* No need to lock the mutex here because this should only happen |
| 102 | when the threads are all really dead (XXX famous last words). */ |
| 103 | while ((p = interp->tstate_head) != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 104 | PyThreadState_Delete(p); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 110 | PyInterpreterState_Delete(interp) |
| 111 | PyInterpreterState *interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 112 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 113 | PyInterpreterState **p; |
| 114 | zapthreads(interp); |
| 115 | for (p = &interp_head; ; p = &(*p)->next) { |
| 116 | if (*p == NULL) |
| 117 | Py_FatalError( |
| 118 | "PyInterpreterState_Delete: invalid interp"); |
| 119 | if (*p == interp) |
| 120 | break; |
| 121 | } |
| 122 | if (interp->tstate_head != NULL) |
| 123 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 124 | *p = interp->next; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 125 | PyMem_DEL(interp); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | PyThreadState * |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 130 | PyThreadState_New(interp) |
| 131 | PyInterpreterState *interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 132 | { |
| 133 | PyThreadState *tstate = PyMem_NEW(PyThreadState, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 135 | if (tstate != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 136 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 137 | |
| 138 | tstate->frame = NULL; |
| 139 | tstate->recursion_depth = 0; |
| 140 | tstate->ticker = 0; |
| 141 | tstate->tracing = 0; |
| 142 | |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 143 | tstate->dict = NULL; |
| 144 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 145 | tstate->curexc_type = NULL; |
| 146 | tstate->curexc_value = NULL; |
| 147 | tstate->curexc_traceback = NULL; |
| 148 | |
| 149 | tstate->exc_type = NULL; |
| 150 | tstate->exc_value = NULL; |
| 151 | tstate->exc_traceback = NULL; |
| 152 | |
| 153 | tstate->sys_profilefunc = NULL; |
| 154 | tstate->sys_tracefunc = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 156 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 157 | tstate->next = interp->tstate_head; |
| 158 | interp->tstate_head = tstate; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 159 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 160 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 162 | return tstate; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | void |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 167 | PyThreadState_Clear(tstate) |
| 168 | PyThreadState *tstate; |
| 169 | { |
Guido van Rossum | 22348dc | 1997-11-03 22:08:36 +0000 | [diff] [blame] | 170 | if (Py_VerboseFlag && tstate->frame != NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 171 | fprintf(stderr, |
Guido van Rossum | 5f896a4 | 1997-08-21 02:28:19 +0000 | [diff] [blame] | 172 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 173 | |
| 174 | ZAP(tstate->frame); |
| 175 | |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 176 | ZAP(tstate->dict); |
| 177 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 178 | ZAP(tstate->curexc_type); |
| 179 | ZAP(tstate->curexc_value); |
| 180 | ZAP(tstate->curexc_traceback); |
| 181 | |
| 182 | ZAP(tstate->exc_type); |
| 183 | ZAP(tstate->exc_value); |
| 184 | ZAP(tstate->exc_traceback); |
| 185 | |
| 186 | ZAP(tstate->sys_profilefunc); |
| 187 | ZAP(tstate->sys_tracefunc); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 192 | PyThreadState_Delete(tstate) |
| 193 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 194 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 195 | PyInterpreterState *interp; |
| 196 | PyThreadState **p; |
| 197 | if (tstate == NULL) |
| 198 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 199 | if (tstate == _PyThreadState_Current) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 200 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
| 201 | interp = tstate->interp; |
| 202 | if (interp == NULL) |
| 203 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 204 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 205 | for (p = &interp->tstate_head; ; p = &(*p)->next) { |
| 206 | if (*p == NULL) |
| 207 | Py_FatalError( |
| 208 | "PyThreadState_Delete: invalid tstate"); |
| 209 | if (*p == tstate) |
| 210 | break; |
| 211 | } |
| 212 | *p = tstate->next; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 213 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 214 | PyMem_DEL(tstate); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | PyThreadState * |
| 219 | PyThreadState_Get() |
| 220 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 221 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 222 | Py_FatalError("PyThreadState_Get: no current thread"); |
| 223 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 224 | return _PyThreadState_Current; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
| 228 | PyThreadState * |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 229 | PyThreadState_Swap(new) |
| 230 | PyThreadState *new; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 231 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 232 | PyThreadState *old = _PyThreadState_Current; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 234 | _PyThreadState_Current = new; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 236 | return old; |
| 237 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 238 | |
| 239 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 240 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 241 | state; the caller should pick a unique key and store its state there. If |
| 242 | PyThreadState_GetDict() returns NULL, an exception has been raised (most |
| 243 | likely MemoryError) and the caller should pass on the exception. */ |
| 244 | |
| 245 | PyObject * |
| 246 | PyThreadState_GetDict() |
| 247 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 248 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 249 | Py_FatalError("PyThreadState_GetDict: no current thread"); |
| 250 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 251 | if (_PyThreadState_Current->dict == NULL) |
| 252 | _PyThreadState_Current->dict = PyDict_New(); |
| 253 | return _PyThreadState_Current->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 254 | } |