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 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 6 | #define ZAP(x) { \ |
| 7 | PyObject *tmp = (PyObject *)(x); \ |
| 8 | (x) = NULL; \ |
| 9 | Py_XDECREF(tmp); \ |
| 10 | } |
| 11 | |
| 12 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 13 | #ifdef WITH_THREAD |
| 14 | #include "pythread.h" |
| 15 | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ |
Moshe Zadka | 9fb6af9 | 2000-08-04 21:27:47 +0000 | [diff] [blame] | 16 | #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 17 | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 18 | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) |
| 19 | #else |
| 20 | #define HEAD_INIT() /* Nothing */ |
| 21 | #define HEAD_LOCK() /* Nothing */ |
| 22 | #define HEAD_UNLOCK() /* Nothing */ |
| 23 | #endif |
| 24 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 25 | static PyInterpreterState *interp_head = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 27 | PyThreadState *_PyThreadState_Current = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 28 | |
| 29 | |
| 30 | PyInterpreterState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 31 | PyInterpreterState_New(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 32 | { |
| 33 | PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 35 | if (interp != NULL) { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 36 | HEAD_INIT(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 37 | interp->modules = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 38 | interp->sysdict = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 39 | interp->builtins = NULL; |
| 40 | interp->checkinterval = 10; |
| 41 | interp->tstate_head = NULL; |
| 42 | |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 43 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 44 | interp->next = interp_head; |
| 45 | interp_head = interp; |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 46 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 47 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 49 | return interp; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 54 | PyInterpreterState_Clear(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 55 | { |
| 56 | PyThreadState *p; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 57 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 58 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 59 | PyThreadState_Clear(p); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 60 | HEAD_UNLOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 61 | ZAP(interp->modules); |
| 62 | ZAP(interp->sysdict); |
| 63 | ZAP(interp->builtins); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 68 | zapthreads(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 69 | { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 70 | PyThreadState *p; |
| 71 | /* No need to lock the mutex here because this should only happen |
| 72 | when the threads are all really dead (XXX famous last words). */ |
| 73 | while ((p = interp->tstate_head) != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 74 | PyThreadState_Delete(p); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 80 | PyInterpreterState_Delete(PyInterpreterState *interp) |
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 | PyInterpreterState **p; |
| 83 | zapthreads(interp); |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 84 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 85 | for (p = &interp_head; ; p = &(*p)->next) { |
| 86 | if (*p == NULL) |
| 87 | Py_FatalError( |
| 88 | "PyInterpreterState_Delete: invalid interp"); |
| 89 | if (*p == interp) |
| 90 | break; |
| 91 | } |
| 92 | if (interp->tstate_head != NULL) |
| 93 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 94 | *p = interp->next; |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 95 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 96 | PyMem_DEL(interp); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 101 | PyThreadState_New(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 102 | { |
| 103 | PyThreadState *tstate = PyMem_NEW(PyThreadState, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 105 | if (tstate != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 106 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 107 | |
| 108 | tstate->frame = NULL; |
| 109 | tstate->recursion_depth = 0; |
| 110 | tstate->ticker = 0; |
| 111 | tstate->tracing = 0; |
| 112 | |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 113 | tstate->dict = NULL; |
| 114 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 115 | tstate->curexc_type = NULL; |
| 116 | tstate->curexc_value = NULL; |
| 117 | tstate->curexc_traceback = NULL; |
| 118 | |
| 119 | tstate->exc_type = NULL; |
| 120 | tstate->exc_value = NULL; |
| 121 | tstate->exc_traceback = NULL; |
| 122 | |
| 123 | tstate->sys_profilefunc = NULL; |
| 124 | tstate->sys_tracefunc = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 126 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 127 | tstate->next = interp->tstate_head; |
| 128 | interp->tstate_head = tstate; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 129 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 130 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 132 | return tstate; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 137 | PyThreadState_Clear(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 138 | { |
Guido van Rossum | 22348dc | 1997-11-03 22:08:36 +0000 | [diff] [blame] | 139 | if (Py_VerboseFlag && tstate->frame != NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 140 | fprintf(stderr, |
Guido van Rossum | 5f896a4 | 1997-08-21 02:28:19 +0000 | [diff] [blame] | 141 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 142 | |
| 143 | ZAP(tstate->frame); |
| 144 | |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 145 | ZAP(tstate->dict); |
| 146 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 147 | ZAP(tstate->curexc_type); |
| 148 | ZAP(tstate->curexc_value); |
| 149 | ZAP(tstate->curexc_traceback); |
| 150 | |
| 151 | ZAP(tstate->exc_type); |
| 152 | ZAP(tstate->exc_value); |
| 153 | ZAP(tstate->exc_traceback); |
| 154 | |
| 155 | ZAP(tstate->sys_profilefunc); |
| 156 | ZAP(tstate->sys_tracefunc); |
| 157 | } |
| 158 | |
| 159 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 160 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
| 161 | static void |
| 162 | tstate_delete_common(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 163 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 164 | PyInterpreterState *interp; |
| 165 | PyThreadState **p; |
| 166 | if (tstate == NULL) |
| 167 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 168 | interp = tstate->interp; |
| 169 | if (interp == NULL) |
| 170 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 171 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 172 | for (p = &interp->tstate_head; ; p = &(*p)->next) { |
| 173 | if (*p == NULL) |
| 174 | Py_FatalError( |
| 175 | "PyThreadState_Delete: invalid tstate"); |
| 176 | if (*p == tstate) |
| 177 | break; |
| 178 | } |
| 179 | *p = tstate->next; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 180 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 181 | PyMem_DEL(tstate); |
| 182 | } |
| 183 | |
| 184 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 185 | void |
| 186 | PyThreadState_Delete(PyThreadState *tstate) |
| 187 | { |
| 188 | if (tstate == _PyThreadState_Current) |
| 189 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
| 190 | tstate_delete_common(tstate); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | #ifdef WITH_THREAD |
| 195 | void |
| 196 | PyThreadState_DeleteCurrent() |
| 197 | { |
| 198 | PyThreadState *tstate = _PyThreadState_Current; |
| 199 | if (tstate == NULL) |
| 200 | Py_FatalError( |
| 201 | "PyThreadState_DeleteCurrent: no current tstate"); |
| 202 | _PyThreadState_Current = NULL; |
| 203 | tstate_delete_common(tstate); |
| 204 | PyEval_ReleaseLock(); |
| 205 | } |
| 206 | #endif /* WITH_THREAD */ |
| 207 | |
| 208 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 209 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 210 | PyThreadState_Get(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 211 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 212 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 213 | Py_FatalError("PyThreadState_Get: no current thread"); |
| 214 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 215 | return _PyThreadState_Current; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 220 | PyThreadState_Swap(PyThreadState *new) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 221 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 222 | PyThreadState *old = _PyThreadState_Current; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 223 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 224 | _PyThreadState_Current = new; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 226 | return old; |
| 227 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 228 | |
| 229 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 230 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 231 | state; the caller should pick a unique key and store its state there. If |
| 232 | PyThreadState_GetDict() returns NULL, an exception has been raised (most |
| 233 | likely MemoryError) and the caller should pass on the exception. */ |
| 234 | |
| 235 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 236 | PyThreadState_GetDict(void) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 237 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 238 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 239 | Py_FatalError("PyThreadState_GetDict: no current thread"); |
| 240 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 241 | if (_PyThreadState_Current->dict == NULL) |
| 242 | _PyThreadState_Current->dict = PyDict_New(); |
| 243 | return _PyThreadState_Current->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 244 | } |