blob: e74e34ca4a868ff59a0b212986e7e7c04230fe4a [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
29
30******************************************************************/
31
32/* Thread and interpreter state structures and their interfaces */
33
34#include "Python.h"
35
Guido van Rossum25ce5661997-08-02 03:10:38 +000036#define ZAP(x) { \
37 PyObject *tmp = (PyObject *)(x); \
38 (x) = NULL; \
39 Py_XDECREF(tmp); \
40}
41
42
43static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000044
45static PyThreadState *current_tstate = NULL;
46
47
48PyInterpreterState *
49PyInterpreterState_New()
50{
51 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +000052
Guido van Rossuma027efa1997-05-05 20:56:21 +000053 if (interp != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000054 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000055 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000056 interp->builtins = NULL;
57 interp->checkinterval = 10;
58 interp->tstate_head = NULL;
59
60 interp->next = interp_head;
61 interp_head = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +000062 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000063
Guido van Rossuma027efa1997-05-05 20:56:21 +000064 return interp;
65}
66
67
68void
Guido van Rossum25ce5661997-08-02 03:10:38 +000069PyInterpreterState_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
81static void
82zapthreads(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
95void
Guido van Rossumf9cba091997-05-20 22:23:34 +000096PyInterpreterState_Delete(interp)
97 PyInterpreterState *interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +000098{
Guido van Rossum25ce5661997-08-02 03:10:38 +000099 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 Rossuma027efa1997-05-05 20:56:21 +0000111 PyMem_DEL(interp);
112}
113
114
115PyThreadState *
Guido van Rossumf9cba091997-05-20 22:23:34 +0000116PyThreadState_New(interp)
117 PyInterpreterState *interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000118{
119 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000120
Guido van Rossuma027efa1997-05-05 20:56:21 +0000121 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000123
124 tstate->frame = NULL;
125 tstate->recursion_depth = 0;
126 tstate->ticker = 0;
127 tstate->tracing = 0;
128
Guido van Rossumede04391998-04-10 20:18:25 +0000129 tstate->dict = NULL;
130
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131 tstate->curexc_type = NULL;
132 tstate->curexc_value = NULL;
133 tstate->curexc_traceback = NULL;
134
135 tstate->exc_type = NULL;
136 tstate->exc_value = NULL;
137 tstate->exc_traceback = NULL;
138
139 tstate->sys_profilefunc = NULL;
140 tstate->sys_tracefunc = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000141
Guido van Rossum25ce5661997-08-02 03:10:38 +0000142 tstate->next = interp->tstate_head;
143 interp->tstate_head = tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000144 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000145
Guido van Rossuma027efa1997-05-05 20:56:21 +0000146 return tstate;
147}
148
149
150void
Guido van Rossum25ce5661997-08-02 03:10:38 +0000151PyThreadState_Clear(tstate)
152 PyThreadState *tstate;
153{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000154 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000155 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000156 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000157
158 ZAP(tstate->frame);
159
Guido van Rossumede04391998-04-10 20:18:25 +0000160 ZAP(tstate->dict);
161
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162 ZAP(tstate->curexc_type);
163 ZAP(tstate->curexc_value);
164 ZAP(tstate->curexc_traceback);
165
166 ZAP(tstate->exc_type);
167 ZAP(tstate->exc_value);
168 ZAP(tstate->exc_traceback);
169
170 ZAP(tstate->sys_profilefunc);
171 ZAP(tstate->sys_tracefunc);
172}
173
174
175void
Guido van Rossumf9cba091997-05-20 22:23:34 +0000176PyThreadState_Delete(tstate)
177 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000178{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000179 PyInterpreterState *interp;
180 PyThreadState **p;
181 if (tstate == NULL)
182 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000183 if (tstate == current_tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000184 Py_FatalError("PyThreadState_Delete: tstate is still current");
185 interp = tstate->interp;
186 if (interp == NULL)
187 Py_FatalError("PyThreadState_Delete: NULL interp");
188 for (p = &interp->tstate_head; ; p = &(*p)->next) {
189 if (*p == NULL)
190 Py_FatalError(
191 "PyThreadState_Delete: invalid tstate");
192 if (*p == tstate)
193 break;
194 }
195 *p = tstate->next;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000196 PyMem_DEL(tstate);
197}
198
199
200PyThreadState *
201PyThreadState_Get()
202{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203 if (current_tstate == NULL)
204 Py_FatalError("PyThreadState_Get: no current thread");
205
Guido van Rossuma027efa1997-05-05 20:56:21 +0000206 return current_tstate;
207}
208
209
210PyThreadState *
Guido van Rossumf9cba091997-05-20 22:23:34 +0000211PyThreadState_Swap(new)
212 PyThreadState *new;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000213{
214 PyThreadState *old = current_tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000215
Guido van Rossuma027efa1997-05-05 20:56:21 +0000216 current_tstate = new;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000217
Guido van Rossuma027efa1997-05-05 20:56:21 +0000218 return old;
219}
Guido van Rossumede04391998-04-10 20:18:25 +0000220
221/* An extension mechanism to store arbitrary additional per-thread state.
222 PyThreadState_GetDict() returns a dictionary that can be used to hold such
223 state; the caller should pick a unique key and store its state there. If
224 PyThreadState_GetDict() returns NULL, an exception has been raised (most
225 likely MemoryError) and the caller should pass on the exception. */
226
227PyObject *
228PyThreadState_GetDict()
229{
230 if (current_tstate == NULL)
231 Py_FatalError("PyThreadState_GetDict: no current thread");
232
233 if (current_tstate->dict == NULL)
234 current_tstate->dict = PyDict_New();
235 return current_tstate->dict;
236}