blob: b62fe7cd20506469056946594b9fe1196b02f42a [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
Guido van Rossum1d5ad901999-06-18 14:22:24 +000043#ifdef WITH_THREAD
44#include "pythread.h"
45static 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 Rossum25ce5661997-08-02 03:10:38 +000055static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000056
Guido van Rossum18bc7c21998-12-21 18:27:28 +000057PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000058
59
60PyInterpreterState *
61PyInterpreterState_New()
62{
63 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +000064
Guido van Rossuma027efa1997-05-05 20:56:21 +000065 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000066 HEAD_INIT();
Guido van Rossum25ce5661997-08-02 03:10:38 +000067 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000068 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000069 interp->builtins = NULL;
70 interp->checkinterval = 10;
71 interp->tstate_head = NULL;
72
73 interp->next = interp_head;
74 interp_head = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +000075 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000076
Guido van Rossuma027efa1997-05-05 20:56:21 +000077 return interp;
78}
79
80
81void
Guido van Rossum25ce5661997-08-02 03:10:38 +000082PyInterpreterState_Clear(interp)
83 PyInterpreterState *interp;
84{
85 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000086 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000087 for (p = interp->tstate_head; p != NULL; p = p->next)
88 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +000089 HEAD_UNLOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000090 ZAP(interp->modules);
91 ZAP(interp->sysdict);
92 ZAP(interp->builtins);
93}
94
95
96static void
97zapthreads(interp)
98 PyInterpreterState *interp;
99{
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000100 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 Rossum25ce5661997-08-02 03:10:38 +0000104 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105 }
106}
107
108
109void
Guido van Rossumf9cba091997-05-20 22:23:34 +0000110PyInterpreterState_Delete(interp)
111 PyInterpreterState *interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000112{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000113 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 Rossuma027efa1997-05-05 20:56:21 +0000125 PyMem_DEL(interp);
126}
127
128
129PyThreadState *
Guido van Rossumf9cba091997-05-20 22:23:34 +0000130PyThreadState_New(interp)
131 PyInterpreterState *interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132{
133 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000134
Guido van Rossuma027efa1997-05-05 20:56:21 +0000135 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000137
138 tstate->frame = NULL;
139 tstate->recursion_depth = 0;
140 tstate->ticker = 0;
141 tstate->tracing = 0;
142
Guido van Rossumede04391998-04-10 20:18:25 +0000143 tstate->dict = NULL;
144
Guido van Rossuma027efa1997-05-05 20:56:21 +0000145 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 Rossuma027efa1997-05-05 20:56:21 +0000155
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000156 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000157 tstate->next = interp->tstate_head;
158 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000159 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000160 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000161
Guido van Rossuma027efa1997-05-05 20:56:21 +0000162 return tstate;
163}
164
165
166void
Guido van Rossum25ce5661997-08-02 03:10:38 +0000167PyThreadState_Clear(tstate)
168 PyThreadState *tstate;
169{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000170 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000171 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000172 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000173
174 ZAP(tstate->frame);
175
Guido van Rossumede04391998-04-10 20:18:25 +0000176 ZAP(tstate->dict);
177
Guido van Rossum25ce5661997-08-02 03:10:38 +0000178 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
191void
Guido van Rossumf9cba091997-05-20 22:23:34 +0000192PyThreadState_Delete(tstate)
193 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195 PyInterpreterState *interp;
196 PyThreadState **p;
197 if (tstate == NULL)
198 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000199 if (tstate == _PyThreadState_Current)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000200 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 Rossum1d5ad901999-06-18 14:22:24 +0000204 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205 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 Rossum1d5ad901999-06-18 14:22:24 +0000213 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000214 PyMem_DEL(tstate);
215}
216
217
218PyThreadState *
219PyThreadState_Get()
220{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000221 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000222 Py_FatalError("PyThreadState_Get: no current thread");
223
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000224 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000225}
226
227
228PyThreadState *
Guido van Rossumf9cba091997-05-20 22:23:34 +0000229PyThreadState_Swap(new)
230 PyThreadState *new;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000231{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000232 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000233
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000234 _PyThreadState_Current = new;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235
Guido van Rossuma027efa1997-05-05 20:56:21 +0000236 return old;
237}
Guido van Rossumede04391998-04-10 20:18:25 +0000238
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
245PyObject *
246PyThreadState_GetDict()
247{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000248 if (_PyThreadState_Current == NULL)
Guido van Rossumede04391998-04-10 20:18:25 +0000249 Py_FatalError("PyThreadState_GetDict: no current thread");
250
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000251 if (_PyThreadState_Current->dict == NULL)
252 _PyThreadState_Current->dict = PyDict_New();
253 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000254}