This commit was manufactured by cvs2svn to create tag 'r221c2'.
[python/dscho.git] / Python / pystate.c
blob9a41ccffd462574a15fd4050d5120d036aa4a2fd
2 /* Thread and interpreter state structures and their interfaces */
4 #include "Python.h"
6 #ifdef HAVE_DLOPEN
7 #ifdef HAVE_DLFCN_H
8 #include <dlfcn.h>
9 #endif
10 #ifndef RTLD_LAZY
11 #define RTLD_LAZY 1
12 #endif
13 #endif
16 #define ZAP(x) { \
17 PyObject *tmp = (PyObject *)(x); \
18 (x) = NULL; \
19 Py_XDECREF(tmp); \
23 #ifdef WITH_THREAD
24 #include "pythread.h"
25 static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
26 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
27 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
28 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
29 #else
30 #define HEAD_INIT() /* Nothing */
31 #define HEAD_LOCK() /* Nothing */
32 #define HEAD_UNLOCK() /* Nothing */
33 #endif
35 static PyInterpreterState *interp_head = NULL;
37 PyThreadState *_PyThreadState_Current = NULL;
40 PyInterpreterState *
41 PyInterpreterState_New(void)
43 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
45 if (interp != NULL) {
46 HEAD_INIT();
47 interp->modules = NULL;
48 interp->sysdict = NULL;
49 interp->builtins = NULL;
50 interp->checkinterval = 10;
51 interp->tstate_head = NULL;
52 #ifdef HAVE_DLOPEN
53 #ifdef RTLD_NOW
54 interp->dlopenflags = RTLD_NOW;
55 #else
56 interp->dlopenflags = RTLD_LAZY;
57 #endif
58 #endif
60 HEAD_LOCK();
61 interp->next = interp_head;
62 interp_head = interp;
63 HEAD_UNLOCK();
66 return interp;
70 void
71 PyInterpreterState_Clear(PyInterpreterState *interp)
73 PyThreadState *p;
74 HEAD_LOCK();
75 for (p = interp->tstate_head; p != NULL; p = p->next)
76 PyThreadState_Clear(p);
77 HEAD_UNLOCK();
78 ZAP(interp->modules);
79 ZAP(interp->sysdict);
80 ZAP(interp->builtins);
84 static void
85 zapthreads(PyInterpreterState *interp)
87 PyThreadState *p;
88 /* No need to lock the mutex here because this should only happen
89 when the threads are all really dead (XXX famous last words). */
90 while ((p = interp->tstate_head) != NULL) {
91 PyThreadState_Delete(p);
96 void
97 PyInterpreterState_Delete(PyInterpreterState *interp)
99 PyInterpreterState **p;
100 zapthreads(interp);
101 HEAD_LOCK();
102 for (p = &interp_head; ; p = &(*p)->next) {
103 if (*p == NULL)
104 Py_FatalError(
105 "PyInterpreterState_Delete: invalid interp");
106 if (*p == interp)
107 break;
109 if (interp->tstate_head != NULL)
110 Py_FatalError("PyInterpreterState_Delete: remaining threads");
111 *p = interp->next;
112 HEAD_UNLOCK();
113 PyMem_DEL(interp);
117 PyThreadState *
118 PyThreadState_New(PyInterpreterState *interp)
120 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
122 if (tstate != NULL) {
123 tstate->interp = interp;
125 tstate->frame = NULL;
126 tstate->recursion_depth = 0;
127 tstate->ticker = 0;
128 tstate->tracing = 0;
129 tstate->use_tracing = 0;
131 tstate->dict = NULL;
133 tstate->curexc_type = NULL;
134 tstate->curexc_value = NULL;
135 tstate->curexc_traceback = NULL;
137 tstate->exc_type = NULL;
138 tstate->exc_value = NULL;
139 tstate->exc_traceback = NULL;
141 tstate->c_profilefunc = NULL;
142 tstate->c_tracefunc = NULL;
143 tstate->c_profileobj = NULL;
144 tstate->c_traceobj = NULL;
146 HEAD_LOCK();
147 tstate->next = interp->tstate_head;
148 interp->tstate_head = tstate;
149 HEAD_UNLOCK();
152 return tstate;
156 void
157 PyThreadState_Clear(PyThreadState *tstate)
159 if (Py_VerboseFlag && tstate->frame != NULL)
160 fprintf(stderr,
161 "PyThreadState_Clear: warning: thread still has a frame\n");
163 ZAP(tstate->frame);
165 ZAP(tstate->dict);
167 ZAP(tstate->curexc_type);
168 ZAP(tstate->curexc_value);
169 ZAP(tstate->curexc_traceback);
171 ZAP(tstate->exc_type);
172 ZAP(tstate->exc_value);
173 ZAP(tstate->exc_traceback);
175 tstate->c_profilefunc = NULL;
176 tstate->c_tracefunc = NULL;
177 ZAP(tstate->c_profileobj);
178 ZAP(tstate->c_traceobj);
182 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
183 static void
184 tstate_delete_common(PyThreadState *tstate)
186 PyInterpreterState *interp;
187 PyThreadState **p;
188 if (tstate == NULL)
189 Py_FatalError("PyThreadState_Delete: NULL tstate");
190 interp = tstate->interp;
191 if (interp == NULL)
192 Py_FatalError("PyThreadState_Delete: NULL interp");
193 HEAD_LOCK();
194 for (p = &interp->tstate_head; ; p = &(*p)->next) {
195 if (*p == NULL)
196 Py_FatalError(
197 "PyThreadState_Delete: invalid tstate");
198 if (*p == tstate)
199 break;
201 *p = tstate->next;
202 HEAD_UNLOCK();
203 PyMem_DEL(tstate);
207 void
208 PyThreadState_Delete(PyThreadState *tstate)
210 if (tstate == _PyThreadState_Current)
211 Py_FatalError("PyThreadState_Delete: tstate is still current");
212 tstate_delete_common(tstate);
216 #ifdef WITH_THREAD
217 void
218 PyThreadState_DeleteCurrent()
220 PyThreadState *tstate = _PyThreadState_Current;
221 if (tstate == NULL)
222 Py_FatalError(
223 "PyThreadState_DeleteCurrent: no current tstate");
224 _PyThreadState_Current = NULL;
225 tstate_delete_common(tstate);
226 PyEval_ReleaseLock();
228 #endif /* WITH_THREAD */
231 PyThreadState *
232 PyThreadState_Get(void)
234 if (_PyThreadState_Current == NULL)
235 Py_FatalError("PyThreadState_Get: no current thread");
237 return _PyThreadState_Current;
241 PyThreadState *
242 PyThreadState_Swap(PyThreadState *new)
244 PyThreadState *old = _PyThreadState_Current;
246 _PyThreadState_Current = new;
248 return old;
251 /* An extension mechanism to store arbitrary additional per-thread state.
252 PyThreadState_GetDict() returns a dictionary that can be used to hold such
253 state; the caller should pick a unique key and store its state there. If
254 PyThreadState_GetDict() returns NULL, an exception has been raised (most
255 likely MemoryError) and the caller should pass on the exception. */
257 PyObject *
258 PyThreadState_GetDict(void)
260 if (_PyThreadState_Current == NULL)
261 Py_FatalError("PyThreadState_GetDict: no current thread");
263 if (_PyThreadState_Current->dict == NULL)
264 _PyThreadState_Current->dict = PyDict_New();
265 return _PyThreadState_Current->dict;
269 /* Routines for advanced debuggers, requested by David Beazley.
270 Don't use unless you know what you are doing! */
272 PyInterpreterState *
273 PyInterpreterState_Head(void)
275 return interp_head;
278 PyInterpreterState *
279 PyInterpreterState_Next(PyInterpreterState *interp) {
280 return interp->next;
283 PyThreadState *
284 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
285 return interp->tstate_head;
288 PyThreadState *
289 PyThreadState_Next(PyThreadState *tstate) {
290 return tstate->next;