This commit was manufactured by cvs2svn to create tag 'r223c1'.
[python/dscho.git] / Python / pystate.c
blob57fffc73e8cb5bd714133270fdfaefa10b9a4d23
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;
38 unaryfunc _PyThreadState_GetFrame = NULL;
41 PyInterpreterState *
42 PyInterpreterState_New(void)
44 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
46 if (interp != NULL) {
47 HEAD_INIT();
48 interp->modules = NULL;
49 interp->sysdict = NULL;
50 interp->builtins = NULL;
51 interp->checkinterval = 10;
52 interp->tstate_head = NULL;
53 interp->codec_search_path = NULL;
54 interp->codec_search_cache = NULL;
55 #ifdef HAVE_DLOPEN
56 #ifdef RTLD_NOW
57 interp->dlopenflags = RTLD_NOW;
58 #else
59 interp->dlopenflags = RTLD_LAZY;
60 #endif
61 #endif
63 HEAD_LOCK();
64 interp->next = interp_head;
65 interp_head = interp;
66 HEAD_UNLOCK();
69 return interp;
73 void
74 PyInterpreterState_Clear(PyInterpreterState *interp)
76 PyThreadState *p;
77 HEAD_LOCK();
78 for (p = interp->tstate_head; p != NULL; p = p->next)
79 PyThreadState_Clear(p);
80 HEAD_UNLOCK();
81 ZAP(interp->codec_search_path);
82 ZAP(interp->codec_search_cache);
83 ZAP(interp->modules);
84 ZAP(interp->sysdict);
85 ZAP(interp->builtins);
89 static void
90 zapthreads(PyInterpreterState *interp)
92 PyThreadState *p;
93 /* No need to lock the mutex here because this should only happen
94 when the threads are all really dead (XXX famous last words). */
95 while ((p = interp->tstate_head) != NULL) {
96 PyThreadState_Delete(p);
101 void
102 PyInterpreterState_Delete(PyInterpreterState *interp)
104 PyInterpreterState **p;
105 zapthreads(interp);
106 HEAD_LOCK();
107 for (p = &interp_head; ; p = &(*p)->next) {
108 if (*p == NULL)
109 Py_FatalError(
110 "PyInterpreterState_Delete: invalid interp");
111 if (*p == interp)
112 break;
114 if (interp->tstate_head != NULL)
115 Py_FatalError("PyInterpreterState_Delete: remaining threads");
116 *p = interp->next;
117 HEAD_UNLOCK();
118 PyMem_DEL(interp);
122 /* Default implementation for _PyThreadState_GetFrame */
123 static struct _frame *
124 threadstate_getframe(PyThreadState *self)
126 return self->frame;
129 PyThreadState *
130 PyThreadState_New(PyInterpreterState *interp)
132 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
133 if (_PyThreadState_GetFrame == NULL)
134 _PyThreadState_GetFrame = (unaryfunc)threadstate_getframe;
136 if (tstate != NULL) {
137 tstate->interp = interp;
139 tstate->frame = NULL;
140 tstate->recursion_depth = 0;
141 tstate->ticker = 0;
142 tstate->tracing = 0;
143 tstate->use_tracing = 0;
144 tstate->tick_counter = 0;
146 tstate->dict = NULL;
148 tstate->curexc_type = NULL;
149 tstate->curexc_value = NULL;
150 tstate->curexc_traceback = NULL;
152 tstate->exc_type = NULL;
153 tstate->exc_value = NULL;
154 tstate->exc_traceback = NULL;
156 tstate->c_profilefunc = NULL;
157 tstate->c_tracefunc = NULL;
158 tstate->c_profileobj = NULL;
159 tstate->c_traceobj = NULL;
161 HEAD_LOCK();
162 tstate->next = interp->tstate_head;
163 interp->tstate_head = tstate;
164 HEAD_UNLOCK();
167 return tstate;
171 void
172 PyThreadState_Clear(PyThreadState *tstate)
174 if (Py_VerboseFlag && tstate->frame != NULL)
175 fprintf(stderr,
176 "PyThreadState_Clear: warning: thread still has a frame\n");
178 ZAP(tstate->frame);
180 ZAP(tstate->dict);
182 ZAP(tstate->curexc_type);
183 ZAP(tstate->curexc_value);
184 ZAP(tstate->curexc_traceback);
186 ZAP(tstate->exc_type);
187 ZAP(tstate->exc_value);
188 ZAP(tstate->exc_traceback);
190 tstate->c_profilefunc = NULL;
191 tstate->c_tracefunc = NULL;
192 ZAP(tstate->c_profileobj);
193 ZAP(tstate->c_traceobj);
197 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
198 static void
199 tstate_delete_common(PyThreadState *tstate)
201 PyInterpreterState *interp;
202 PyThreadState **p;
203 if (tstate == NULL)
204 Py_FatalError("PyThreadState_Delete: NULL tstate");
205 interp = tstate->interp;
206 if (interp == NULL)
207 Py_FatalError("PyThreadState_Delete: NULL interp");
208 HEAD_LOCK();
209 for (p = &interp->tstate_head; ; p = &(*p)->next) {
210 if (*p == NULL)
211 Py_FatalError(
212 "PyThreadState_Delete: invalid tstate");
213 if (*p == tstate)
214 break;
216 *p = tstate->next;
217 HEAD_UNLOCK();
218 PyMem_DEL(tstate);
222 void
223 PyThreadState_Delete(PyThreadState *tstate)
225 if (tstate == _PyThreadState_Current)
226 Py_FatalError("PyThreadState_Delete: tstate is still current");
227 tstate_delete_common(tstate);
231 #ifdef WITH_THREAD
232 void
233 PyThreadState_DeleteCurrent()
235 PyThreadState *tstate = _PyThreadState_Current;
236 if (tstate == NULL)
237 Py_FatalError(
238 "PyThreadState_DeleteCurrent: no current tstate");
239 _PyThreadState_Current = NULL;
240 tstate_delete_common(tstate);
241 PyEval_ReleaseLock();
243 #endif /* WITH_THREAD */
246 PyThreadState *
247 PyThreadState_Get(void)
249 if (_PyThreadState_Current == NULL)
250 Py_FatalError("PyThreadState_Get: no current thread");
252 return _PyThreadState_Current;
256 PyThreadState *
257 PyThreadState_Swap(PyThreadState *new)
259 PyThreadState *old = _PyThreadState_Current;
261 _PyThreadState_Current = new;
263 return old;
266 /* An extension mechanism to store arbitrary additional per-thread state.
267 PyThreadState_GetDict() returns a dictionary that can be used to hold such
268 state; the caller should pick a unique key and store its state there. If
269 PyThreadState_GetDict() returns NULL, an exception has been raised (most
270 likely MemoryError) and the caller should pass on the exception. */
272 PyObject *
273 PyThreadState_GetDict(void)
275 if (_PyThreadState_Current == NULL)
276 Py_FatalError("PyThreadState_GetDict: no current thread");
278 if (_PyThreadState_Current->dict == NULL)
279 _PyThreadState_Current->dict = PyDict_New();
280 return _PyThreadState_Current->dict;
284 /* Routines for advanced debuggers, requested by David Beazley.
285 Don't use unless you know what you are doing! */
287 PyInterpreterState *
288 PyInterpreterState_Head(void)
290 return interp_head;
293 PyInterpreterState *
294 PyInterpreterState_Next(PyInterpreterState *interp) {
295 return interp->next;
298 PyThreadState *
299 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
300 return interp->tstate_head;
303 PyThreadState *
304 PyThreadState_Next(PyThreadState *tstate) {
305 return tstate->next;