Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Python / pystate.c
blobb62fe7cd20506469056946594b9fe1196b02f42a
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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.
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.
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.
30 ******************************************************************/
32 /* Thread and interpreter state structures and their interfaces */
34 #include "Python.h"
36 #define ZAP(x) { \
37 PyObject *tmp = (PyObject *)(x); \
38 (x) = NULL; \
39 Py_XDECREF(tmp); \
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
55 static PyInterpreterState *interp_head = NULL;
57 PyThreadState *_PyThreadState_Current = NULL;
60 PyInterpreterState *
61 PyInterpreterState_New()
63 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
65 if (interp != NULL) {
66 HEAD_INIT();
67 interp->modules = NULL;
68 interp->sysdict = NULL;
69 interp->builtins = NULL;
70 interp->checkinterval = 10;
71 interp->tstate_head = NULL;
73 interp->next = interp_head;
74 interp_head = interp;
77 return interp;
81 void
82 PyInterpreterState_Clear(interp)
83 PyInterpreterState *interp;
85 PyThreadState *p;
86 HEAD_LOCK();
87 for (p = interp->tstate_head; p != NULL; p = p->next)
88 PyThreadState_Clear(p);
89 HEAD_UNLOCK();
90 ZAP(interp->modules);
91 ZAP(interp->sysdict);
92 ZAP(interp->builtins);
96 static void
97 zapthreads(interp)
98 PyInterpreterState *interp;
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) {
104 PyThreadState_Delete(p);
109 void
110 PyInterpreterState_Delete(interp)
111 PyInterpreterState *interp;
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;
122 if (interp->tstate_head != NULL)
123 Py_FatalError("PyInterpreterState_Delete: remaining threads");
124 *p = interp->next;
125 PyMem_DEL(interp);
129 PyThreadState *
130 PyThreadState_New(interp)
131 PyInterpreterState *interp;
133 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
135 if (tstate != NULL) {
136 tstate->interp = interp;
138 tstate->frame = NULL;
139 tstate->recursion_depth = 0;
140 tstate->ticker = 0;
141 tstate->tracing = 0;
143 tstate->dict = NULL;
145 tstate->curexc_type = NULL;
146 tstate->curexc_value = NULL;
147 tstate->curexc_traceback = NULL;
149 tstate->exc_type = NULL;
150 tstate->exc_value = NULL;
151 tstate->exc_traceback = NULL;
153 tstate->sys_profilefunc = NULL;
154 tstate->sys_tracefunc = NULL;
156 HEAD_LOCK();
157 tstate->next = interp->tstate_head;
158 interp->tstate_head = tstate;
159 HEAD_UNLOCK();
162 return tstate;
166 void
167 PyThreadState_Clear(tstate)
168 PyThreadState *tstate;
170 if (Py_VerboseFlag && tstate->frame != NULL)
171 fprintf(stderr,
172 "PyThreadState_Clear: warning: thread still has a frame\n");
174 ZAP(tstate->frame);
176 ZAP(tstate->dict);
178 ZAP(tstate->curexc_type);
179 ZAP(tstate->curexc_value);
180 ZAP(tstate->curexc_traceback);
182 ZAP(tstate->exc_type);
183 ZAP(tstate->exc_value);
184 ZAP(tstate->exc_traceback);
186 ZAP(tstate->sys_profilefunc);
187 ZAP(tstate->sys_tracefunc);
191 void
192 PyThreadState_Delete(tstate)
193 PyThreadState *tstate;
195 PyInterpreterState *interp;
196 PyThreadState **p;
197 if (tstate == NULL)
198 Py_FatalError("PyThreadState_Delete: NULL tstate");
199 if (tstate == _PyThreadState_Current)
200 Py_FatalError("PyThreadState_Delete: tstate is still current");
201 interp = tstate->interp;
202 if (interp == NULL)
203 Py_FatalError("PyThreadState_Delete: NULL interp");
204 HEAD_LOCK();
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;
212 *p = tstate->next;
213 HEAD_UNLOCK();
214 PyMem_DEL(tstate);
218 PyThreadState *
219 PyThreadState_Get()
221 if (_PyThreadState_Current == NULL)
222 Py_FatalError("PyThreadState_Get: no current thread");
224 return _PyThreadState_Current;
228 PyThreadState *
229 PyThreadState_Swap(new)
230 PyThreadState *new;
232 PyThreadState *old = _PyThreadState_Current;
234 _PyThreadState_Current = new;
236 return old;
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. */
245 PyObject *
246 PyThreadState_GetDict()
248 if (_PyThreadState_Current == NULL)
249 Py_FatalError("PyThreadState_GetDict: no current thread");
251 if (_PyThreadState_Current->dict == NULL)
252 _PyThreadState_Current->dict = PyDict_New();
253 return _PyThreadState_Current->dict;