Bump version to 0.9.1.
[python/dscho.git] / Python / pystate.c
blob1cd9cca8feafa5c1cc22cfe04dee247e67de1ed8
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Thread and interpreter state structures and their interfaces */
13 #include "Python.h"
15 #define ZAP(x) { \
16 PyObject *tmp = (PyObject *)(x); \
17 (x) = NULL; \
18 Py_XDECREF(tmp); \
22 #ifdef WITH_THREAD
23 #include "pythread.h"
24 static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
25 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
26 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
27 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
28 #else
29 #define HEAD_INIT() /* Nothing */
30 #define HEAD_LOCK() /* Nothing */
31 #define HEAD_UNLOCK() /* Nothing */
32 #endif
34 static PyInterpreterState *interp_head = NULL;
36 PyThreadState *_PyThreadState_Current = NULL;
39 PyInterpreterState *
40 PyInterpreterState_New(void)
42 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
44 if (interp != NULL) {
45 HEAD_INIT();
46 interp->modules = NULL;
47 interp->sysdict = NULL;
48 interp->builtins = NULL;
49 interp->checkinterval = 10;
50 interp->tstate_head = NULL;
52 interp->next = interp_head;
53 interp_head = interp;
56 return interp;
60 void
61 PyInterpreterState_Clear(PyInterpreterState *interp)
63 PyThreadState *p;
64 HEAD_LOCK();
65 for (p = interp->tstate_head; p != NULL; p = p->next)
66 PyThreadState_Clear(p);
67 HEAD_UNLOCK();
68 ZAP(interp->modules);
69 ZAP(interp->sysdict);
70 ZAP(interp->builtins);
74 static void
75 zapthreads(PyInterpreterState *interp)
77 PyThreadState *p;
78 /* No need to lock the mutex here because this should only happen
79 when the threads are all really dead (XXX famous last words). */
80 while ((p = interp->tstate_head) != NULL) {
81 PyThreadState_Delete(p);
86 void
87 PyInterpreterState_Delete(PyInterpreterState *interp)
89 PyInterpreterState **p;
90 zapthreads(interp);
91 for (p = &interp_head; ; p = &(*p)->next) {
92 if (*p == NULL)
93 Py_FatalError(
94 "PyInterpreterState_Delete: invalid interp");
95 if (*p == interp)
96 break;
98 if (interp->tstate_head != NULL)
99 Py_FatalError("PyInterpreterState_Delete: remaining threads");
100 *p = interp->next;
101 PyMem_DEL(interp);
105 PyThreadState *
106 PyThreadState_New(PyInterpreterState *interp)
108 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
110 if (tstate != NULL) {
111 tstate->interp = interp;
113 tstate->frame = NULL;
114 tstate->recursion_depth = 0;
115 tstate->ticker = 0;
116 tstate->tracing = 0;
118 tstate->dict = NULL;
120 tstate->curexc_type = NULL;
121 tstate->curexc_value = NULL;
122 tstate->curexc_traceback = NULL;
124 tstate->exc_type = NULL;
125 tstate->exc_value = NULL;
126 tstate->exc_traceback = NULL;
128 tstate->sys_profilefunc = NULL;
129 tstate->sys_tracefunc = NULL;
131 HEAD_LOCK();
132 tstate->next = interp->tstate_head;
133 interp->tstate_head = tstate;
134 HEAD_UNLOCK();
137 return tstate;
141 void
142 PyThreadState_Clear(PyThreadState *tstate)
144 if (Py_VerboseFlag && tstate->frame != NULL)
145 fprintf(stderr,
146 "PyThreadState_Clear: warning: thread still has a frame\n");
148 ZAP(tstate->frame);
150 ZAP(tstate->dict);
152 ZAP(tstate->curexc_type);
153 ZAP(tstate->curexc_value);
154 ZAP(tstate->curexc_traceback);
156 ZAP(tstate->exc_type);
157 ZAP(tstate->exc_value);
158 ZAP(tstate->exc_traceback);
160 ZAP(tstate->sys_profilefunc);
161 ZAP(tstate->sys_tracefunc);
165 void
166 PyThreadState_Delete(PyThreadState *tstate)
168 PyInterpreterState *interp;
169 PyThreadState **p;
170 if (tstate == NULL)
171 Py_FatalError("PyThreadState_Delete: NULL tstate");
172 if (tstate == _PyThreadState_Current)
173 Py_FatalError("PyThreadState_Delete: tstate is still current");
174 interp = tstate->interp;
175 if (interp == NULL)
176 Py_FatalError("PyThreadState_Delete: NULL interp");
177 HEAD_LOCK();
178 for (p = &interp->tstate_head; ; p = &(*p)->next) {
179 if (*p == NULL)
180 Py_FatalError(
181 "PyThreadState_Delete: invalid tstate");
182 if (*p == tstate)
183 break;
185 *p = tstate->next;
186 HEAD_UNLOCK();
187 PyMem_DEL(tstate);
191 PyThreadState *
192 PyThreadState_Get(void)
194 if (_PyThreadState_Current == NULL)
195 Py_FatalError("PyThreadState_Get: no current thread");
197 return _PyThreadState_Current;
201 PyThreadState *
202 PyThreadState_Swap(PyThreadState *new)
204 PyThreadState *old = _PyThreadState_Current;
206 _PyThreadState_Current = new;
208 return old;
211 /* An extension mechanism to store arbitrary additional per-thread state.
212 PyThreadState_GetDict() returns a dictionary that can be used to hold such
213 state; the caller should pick a unique key and store its state there. If
214 PyThreadState_GetDict() returns NULL, an exception has been raised (most
215 likely MemoryError) and the caller should pass on the exception. */
217 PyObject *
218 PyThreadState_GetDict(void)
220 if (_PyThreadState_Current == NULL)
221 Py_FatalError("PyThreadState_GetDict: no current thread");
223 if (_PyThreadState_Current->dict == NULL)
224 _PyThreadState_Current->dict = PyDict_New();
225 return _PyThreadState_Current->dict;