_make_boundary(): Fix for SF bug #745478, broken boundary calculation
[python/dscho.git] / Python / pystate.c
blobb083f8cb944b2c37535d48191ec65d815ebaa148
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 PyThreadFrameGetter _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->tstate_head = NULL;
52 interp->codec_search_path = NULL;
53 interp->codec_search_cache = NULL;
54 interp->codec_error_registry = 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->codec_error_registry);
84 ZAP(interp->modules);
85 ZAP(interp->sysdict);
86 ZAP(interp->builtins);
90 static void
91 zapthreads(PyInterpreterState *interp)
93 PyThreadState *p;
94 /* No need to lock the mutex here because this should only happen
95 when the threads are all really dead (XXX famous last words). */
96 while ((p = interp->tstate_head) != NULL) {
97 PyThreadState_Delete(p);
102 void
103 PyInterpreterState_Delete(PyInterpreterState *interp)
105 PyInterpreterState **p;
106 zapthreads(interp);
107 HEAD_LOCK();
108 for (p = &interp_head; ; p = &(*p)->next) {
109 if (*p == NULL)
110 Py_FatalError(
111 "PyInterpreterState_Delete: invalid interp");
112 if (*p == interp)
113 break;
115 if (interp->tstate_head != NULL)
116 Py_FatalError("PyInterpreterState_Delete: remaining threads");
117 *p = interp->next;
118 HEAD_UNLOCK();
119 PyMem_DEL(interp);
123 /* Default implementation for _PyThreadState_GetFrame */
124 static struct _frame *
125 threadstate_getframe(PyThreadState *self)
127 return self->frame;
130 PyThreadState *
131 PyThreadState_New(PyInterpreterState *interp)
133 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
134 if (_PyThreadState_GetFrame == NULL)
135 _PyThreadState_GetFrame = threadstate_getframe;
137 if (tstate != NULL) {
138 tstate->interp = interp;
140 tstate->frame = NULL;
141 tstate->recursion_depth = 0;
142 tstate->tracing = 0;
143 tstate->use_tracing = 0;
144 tstate->tick_counter = 0;
145 tstate->gilstate_counter = 0;
147 tstate->dict = NULL;
149 tstate->curexc_type = NULL;
150 tstate->curexc_value = NULL;
151 tstate->curexc_traceback = NULL;
153 tstate->exc_type = NULL;
154 tstate->exc_value = NULL;
155 tstate->exc_traceback = NULL;
157 tstate->c_profilefunc = NULL;
158 tstate->c_tracefunc = NULL;
159 tstate->c_profileobj = NULL;
160 tstate->c_traceobj = NULL;
162 HEAD_LOCK();
163 tstate->next = interp->tstate_head;
164 interp->tstate_head = tstate;
165 HEAD_UNLOCK();
168 return tstate;
172 void
173 PyThreadState_Clear(PyThreadState *tstate)
175 if (Py_VerboseFlag && tstate->frame != NULL)
176 fprintf(stderr,
177 "PyThreadState_Clear: warning: thread still has a frame\n");
179 ZAP(tstate->frame);
181 ZAP(tstate->dict);
183 ZAP(tstate->curexc_type);
184 ZAP(tstate->curexc_value);
185 ZAP(tstate->curexc_traceback);
187 ZAP(tstate->exc_type);
188 ZAP(tstate->exc_value);
189 ZAP(tstate->exc_traceback);
191 tstate->c_profilefunc = NULL;
192 tstate->c_tracefunc = NULL;
193 ZAP(tstate->c_profileobj);
194 ZAP(tstate->c_traceobj);
198 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
199 static void
200 tstate_delete_common(PyThreadState *tstate)
202 PyInterpreterState *interp;
203 PyThreadState **p;
204 if (tstate == NULL)
205 Py_FatalError("PyThreadState_Delete: NULL tstate");
206 interp = tstate->interp;
207 if (interp == NULL)
208 Py_FatalError("PyThreadState_Delete: NULL interp");
209 HEAD_LOCK();
210 for (p = &interp->tstate_head; ; p = &(*p)->next) {
211 if (*p == NULL)
212 Py_FatalError(
213 "PyThreadState_Delete: invalid tstate");
214 if (*p == tstate)
215 break;
217 *p = tstate->next;
218 HEAD_UNLOCK();
219 PyMem_DEL(tstate);
223 void
224 PyThreadState_Delete(PyThreadState *tstate)
226 if (tstate == _PyThreadState_Current)
227 Py_FatalError("PyThreadState_Delete: tstate is still current");
228 tstate_delete_common(tstate);
232 #ifdef WITH_THREAD
233 void
234 PyThreadState_DeleteCurrent()
236 PyThreadState *tstate = _PyThreadState_Current;
237 if (tstate == NULL)
238 Py_FatalError(
239 "PyThreadState_DeleteCurrent: no current tstate");
240 _PyThreadState_Current = NULL;
241 tstate_delete_common(tstate);
242 PyEval_ReleaseLock();
244 #endif /* WITH_THREAD */
247 PyThreadState *
248 PyThreadState_Get(void)
250 if (_PyThreadState_Current == NULL)
251 Py_FatalError("PyThreadState_Get: no current thread");
253 return _PyThreadState_Current;
257 PyThreadState *
258 PyThreadState_Swap(PyThreadState *new)
260 PyThreadState *old = _PyThreadState_Current;
262 _PyThreadState_Current = new;
263 /* It should not be possible for more than one thread state
264 to be used for a thread. Check this the best we can in debug
265 builds.
267 #if defined(Py_DEBUG) && defined(WITH_THREAD)
268 if (new) {
269 PyThreadState *check = PyGILState_GetThisThreadState();
270 if (check && check != new)
271 Py_FatalError("Invalid thread state for this thread");
273 #endif
274 return old;
277 /* An extension mechanism to store arbitrary additional per-thread state.
278 PyThreadState_GetDict() returns a dictionary that can be used to hold such
279 state; the caller should pick a unique key and store its state there. If
280 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
281 and the caller should assume no per-thread state is available. */
283 PyObject *
284 PyThreadState_GetDict(void)
286 if (_PyThreadState_Current == NULL)
287 return NULL;
289 if (_PyThreadState_Current->dict == NULL) {
290 PyObject *d;
291 _PyThreadState_Current->dict = d = PyDict_New();
292 if (d == NULL)
293 PyErr_Clear();
295 return _PyThreadState_Current->dict;
299 /* Routines for advanced debuggers, requested by David Beazley.
300 Don't use unless you know what you are doing! */
302 PyInterpreterState *
303 PyInterpreterState_Head(void)
305 return interp_head;
308 PyInterpreterState *
309 PyInterpreterState_Next(PyInterpreterState *interp) {
310 return interp->next;
313 PyThreadState *
314 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
315 return interp->tstate_head;
318 PyThreadState *
319 PyThreadState_Next(PyThreadState *tstate) {
320 return tstate->next;
323 /* Python "auto thread state" API. */
324 #ifdef WITH_THREAD
326 /* Keep this as a static, as it is not reliable! It can only
327 ever be compared to the state for the *current* thread.
328 * If not equal, then it doesn't matter that the actual
329 value may change immediately after comparison, as it can't
330 possibly change to the current thread's state.
331 * If equal, then the current thread holds the lock, so the value can't
332 change until we yield the lock.
334 static int
335 PyThreadState_IsCurrent(PyThreadState *tstate)
337 /* Must be the tstate for this thread */
338 assert(PyGILState_GetThisThreadState()==tstate);
339 /* On Windows at least, simple reads and writes to 32 bit values
340 are atomic.
342 return tstate == _PyThreadState_Current;
345 /* The single PyInterpreterState used by this process'
346 GILState implementation
348 static PyInterpreterState *autoInterpreterState = NULL;
349 static int autoTLSkey = 0;
351 /* Internal initialization/finalization functions called by
352 Py_Initialize/Py_Finalize
354 void _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
356 assert(i && t); /* must init with a valid states */
357 autoTLSkey = PyThread_create_key();
358 autoInterpreterState = i;
359 /* Now stash the thread state for this thread in TLS */
360 PyThread_set_key_value(autoTLSkey, (void *)t);
361 assert(t->gilstate_counter==0); /* must be a new thread state */
362 t->gilstate_counter = 1;
365 void _PyGILState_Fini(void)
367 PyThread_delete_key(autoTLSkey);
368 autoTLSkey = 0;
369 autoInterpreterState = NULL;;
372 /* The public functions */
373 PyThreadState *PyGILState_GetThisThreadState(void)
375 if (autoInterpreterState==NULL || autoTLSkey==0)
376 return NULL;
377 return (PyThreadState *) PyThread_get_key_value(autoTLSkey);
380 PyGILState_STATE PyGILState_Ensure(void)
382 int current;
383 PyThreadState *tcur;
384 /* Note that we do not auto-init Python here - apart from
385 potential races with 2 threads auto-initializing, pep-311
386 spells out other issues. Embedders are expected to have
387 called Py_Initialize() and usually PyEval_InitThreads().
389 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
390 tcur = PyThread_get_key_value(autoTLSkey);
391 if (tcur==NULL) {
392 /* Create a new thread state for this thread */
393 tcur = PyThreadState_New(autoInterpreterState);
394 if (tcur==NULL)
395 Py_FatalError("Couldn't create thread-state for new thread");
396 PyThread_set_key_value(autoTLSkey, (void *)tcur);
397 current = 0; /* new thread state is never current */
398 } else
399 current = PyThreadState_IsCurrent(tcur);
400 if (!current)
401 PyEval_RestoreThread(tcur);
402 /* Update our counter in the thread-state - no need for locks:
403 - tcur will remain valid as we hold the GIL.
404 - the counter is safe as we are the only thread "allowed"
405 to modify this value
407 tcur->gilstate_counter++;
408 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
411 void PyGILState_Release(PyGILState_STATE oldstate)
413 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
414 if (tcur==NULL)
415 Py_FatalError("auto-releasing thread-state, "
416 "but no thread-state for this thread");
417 /* We must hold the GIL and have our thread state current */
418 /* XXX - remove the check - the assert should be fine,
419 but while this is very new (April 2003), the extra check
420 by release-only users can't hurt.
422 if (!PyThreadState_IsCurrent(tcur))
423 Py_FatalError("This thread state must be current when releasing");
424 assert (PyThreadState_IsCurrent(tcur));
425 tcur->gilstate_counter -= 1;
426 assert (tcur->gilstate_counter >= 0); /* illegal counter value */
428 /* If we are about to destroy this thread-state, we must
429 clear it while the lock is held, as destructors may run
431 if (tcur->gilstate_counter==0) {
432 /* can't have been locked when we created it */
433 assert(oldstate==PyGILState_UNLOCKED);
434 PyThreadState_Clear(tcur);
437 /* Release the lock if necessary */
438 if (oldstate==PyGILState_UNLOCKED)
439 PyEval_ReleaseThread(tcur);
441 /* Now complete destruction of the thread if necessary */
442 if (tcur->gilstate_counter==0) {
443 /* Delete this thread from our TLS */
444 PyThread_delete_key_value(autoTLSkey);
445 /* Delete the thread-state */
446 PyThreadState_Delete(tcur);
449 #endif /* WITH_THREAD */