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 | |
| 43 | static PyInterpreterState *interp_head = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 44 | |
| 45 | static PyThreadState *current_tstate = NULL; |
| 46 | |
| 47 | |
| 48 | PyInterpreterState * |
| 49 | PyInterpreterState_New() |
| 50 | { |
| 51 | PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 53 | if (interp != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 54 | interp->modules = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 55 | interp->sysdict = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 56 | interp->builtins = NULL; |
| 57 | interp->checkinterval = 10; |
| 58 | interp->tstate_head = NULL; |
| 59 | |
| 60 | interp->next = interp_head; |
| 61 | interp_head = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 62 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 64 | return interp; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | void |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 69 | PyInterpreterState_Clear(interp) |
| 70 | PyInterpreterState *interp; |
| 71 | { |
| 72 | PyThreadState *p; |
| 73 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 74 | PyThreadState_Clear(p); |
| 75 | ZAP(interp->modules); |
| 76 | ZAP(interp->sysdict); |
| 77 | ZAP(interp->builtins); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static void |
| 82 | zapthreads(interp) |
| 83 | PyInterpreterState *interp; |
| 84 | { |
| 85 | PyThreadState *p, *q; |
| 86 | p = interp->tstate_head; |
| 87 | while (p != NULL) { |
| 88 | q = p->next; |
| 89 | PyThreadState_Delete(p); |
| 90 | p = q; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 96 | PyInterpreterState_Delete(interp) |
| 97 | 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); |
| 101 | for (p = &interp_head; ; p = &(*p)->next) { |
| 102 | if (*p == NULL) |
| 103 | Py_FatalError( |
| 104 | "PyInterpreterState_Delete: invalid interp"); |
| 105 | if (*p == interp) |
| 106 | break; |
| 107 | } |
| 108 | if (interp->tstate_head != NULL) |
| 109 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 110 | *p = interp->next; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 111 | PyMem_DEL(interp); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | PyThreadState * |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 116 | PyThreadState_New(interp) |
| 117 | PyInterpreterState *interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 118 | { |
| 119 | PyThreadState *tstate = PyMem_NEW(PyThreadState, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 121 | if (tstate != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 122 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 123 | |
| 124 | tstate->frame = NULL; |
| 125 | tstate->recursion_depth = 0; |
| 126 | tstate->ticker = 0; |
| 127 | tstate->tracing = 0; |
| 128 | |
| 129 | tstate->curexc_type = NULL; |
| 130 | tstate->curexc_value = NULL; |
| 131 | tstate->curexc_traceback = NULL; |
| 132 | |
| 133 | tstate->exc_type = NULL; |
| 134 | tstate->exc_value = NULL; |
| 135 | tstate->exc_traceback = NULL; |
| 136 | |
| 137 | tstate->sys_profilefunc = NULL; |
| 138 | tstate->sys_tracefunc = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 140 | tstate->next = interp->tstate_head; |
| 141 | interp->tstate_head = tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 142 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 144 | return tstate; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | void |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 149 | PyThreadState_Clear(tstate) |
| 150 | PyThreadState *tstate; |
| 151 | { |
Guido van Rossum | 22348dc | 1997-11-03 22:08:36 +0000 | [diff] [blame] | 152 | if (Py_VerboseFlag && tstate->frame != NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 153 | fprintf(stderr, |
Guido van Rossum | 5f896a4 | 1997-08-21 02:28:19 +0000 | [diff] [blame] | 154 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 155 | |
| 156 | ZAP(tstate->frame); |
| 157 | |
| 158 | ZAP(tstate->curexc_type); |
| 159 | ZAP(tstate->curexc_value); |
| 160 | ZAP(tstate->curexc_traceback); |
| 161 | |
| 162 | ZAP(tstate->exc_type); |
| 163 | ZAP(tstate->exc_value); |
| 164 | ZAP(tstate->exc_traceback); |
| 165 | |
| 166 | ZAP(tstate->sys_profilefunc); |
| 167 | ZAP(tstate->sys_tracefunc); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | void |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 172 | PyThreadState_Delete(tstate) |
| 173 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 174 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 175 | PyInterpreterState *interp; |
| 176 | PyThreadState **p; |
| 177 | if (tstate == NULL) |
| 178 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 179 | if (tstate == current_tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 180 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
| 181 | interp = tstate->interp; |
| 182 | if (interp == NULL) |
| 183 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
| 184 | for (p = &interp->tstate_head; ; p = &(*p)->next) { |
| 185 | if (*p == NULL) |
| 186 | Py_FatalError( |
| 187 | "PyThreadState_Delete: invalid tstate"); |
| 188 | if (*p == tstate) |
| 189 | break; |
| 190 | } |
| 191 | *p = tstate->next; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 192 | PyMem_DEL(tstate); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | PyThreadState * |
| 197 | PyThreadState_Get() |
| 198 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 199 | if (current_tstate == NULL) |
| 200 | Py_FatalError("PyThreadState_Get: no current thread"); |
| 201 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 202 | return current_tstate; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | PyThreadState * |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 207 | PyThreadState_Swap(new) |
| 208 | PyThreadState *new; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 209 | { |
| 210 | PyThreadState *old = current_tstate; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 212 | current_tstate = new; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 214 | return old; |
| 215 | } |