Merged revisions 75246 via svnmerge from
[python/dscho.git] / Python / pystate.c
blobfe5de5f4d2219c31dbabe8c6bb12bd3bec956443
2 /* Thread and interpreter state structures and their interfaces */
4 #include "Python.h"
6 /* --------------------------------------------------------------------------
7 CAUTION
9 Always use malloc() and free() directly in this file. A number of these
10 functions are advertised as safe to call when the GIL isn't held, and in
11 a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging
12 obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid
13 the expense of doing their own locking).
14 -------------------------------------------------------------------------- */
16 #ifdef HAVE_DLOPEN
17 #ifdef HAVE_DLFCN_H
18 #include <dlfcn.h>
19 #endif
20 #ifndef RTLD_LAZY
21 #define RTLD_LAZY 1
22 #endif
23 #endif
26 #ifdef WITH_THREAD
27 #include "pythread.h"
28 static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
29 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
30 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
31 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
37 /* The single PyInterpreterState used by this process'
38 GILState implementation
40 static PyInterpreterState *autoInterpreterState = NULL;
41 static int autoTLSkey = 0;
42 #else
43 #define HEAD_INIT() /* Nothing */
44 #define HEAD_LOCK() /* Nothing */
45 #define HEAD_UNLOCK() /* Nothing */
46 #endif
48 static PyInterpreterState *interp_head = NULL;
50 PyThreadState *_PyThreadState_Current = NULL;
51 PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
53 #ifdef WITH_THREAD
54 static void _PyGILState_NoteThreadState(PyThreadState* tstate);
55 #endif
58 PyInterpreterState *
59 PyInterpreterState_New(void)
61 PyInterpreterState *interp = (PyInterpreterState *)
62 malloc(sizeof(PyInterpreterState));
64 if (interp != NULL) {
65 HEAD_INIT();
66 #ifdef WITH_THREAD
67 if (head_mutex == NULL)
68 Py_FatalError("Can't initialize threads for interpreter");
69 #endif
70 interp->modules = NULL;
71 interp->modules_reloading = NULL;
72 interp->modules_by_index = NULL;
73 interp->sysdict = NULL;
74 interp->builtins = NULL;
75 interp->tstate_head = NULL;
76 interp->codec_search_path = NULL;
77 interp->codec_search_cache = NULL;
78 interp->codec_error_registry = NULL;
79 interp->codecs_initialized = 0;
80 #ifdef HAVE_DLOPEN
81 #ifdef RTLD_NOW
82 interp->dlopenflags = RTLD_NOW;
83 #else
84 interp->dlopenflags = RTLD_LAZY;
85 #endif
86 #endif
87 #ifdef WITH_TSC
88 interp->tscdump = 0;
89 #endif
91 HEAD_LOCK();
92 interp->next = interp_head;
93 interp_head = interp;
94 HEAD_UNLOCK();
97 return interp;
101 void
102 PyInterpreterState_Clear(PyInterpreterState *interp)
104 PyThreadState *p;
105 HEAD_LOCK();
106 for (p = interp->tstate_head; p != NULL; p = p->next)
107 PyThreadState_Clear(p);
108 HEAD_UNLOCK();
109 Py_CLEAR(interp->codec_search_path);
110 Py_CLEAR(interp->codec_search_cache);
111 Py_CLEAR(interp->codec_error_registry);
112 Py_CLEAR(interp->modules);
113 Py_CLEAR(interp->modules_by_index);
114 Py_CLEAR(interp->modules_reloading);
115 Py_CLEAR(interp->sysdict);
116 Py_CLEAR(interp->builtins);
120 static void
121 zapthreads(PyInterpreterState *interp)
123 PyThreadState *p;
124 /* No need to lock the mutex here because this should only happen
125 when the threads are all really dead (XXX famous last words). */
126 while ((p = interp->tstate_head) != NULL) {
127 PyThreadState_Delete(p);
132 void
133 PyInterpreterState_Delete(PyInterpreterState *interp)
135 PyInterpreterState **p;
136 zapthreads(interp);
137 HEAD_LOCK();
138 for (p = &interp_head; ; p = &(*p)->next) {
139 if (*p == NULL)
140 Py_FatalError(
141 "PyInterpreterState_Delete: invalid interp");
142 if (*p == interp)
143 break;
145 if (interp->tstate_head != NULL)
146 Py_FatalError("PyInterpreterState_Delete: remaining threads");
147 *p = interp->next;
148 HEAD_UNLOCK();
149 free(interp);
153 /* Default implementation for _PyThreadState_GetFrame */
154 static struct _frame *
155 threadstate_getframe(PyThreadState *self)
157 return self->frame;
160 PyThreadState *
161 PyThreadState_New(PyInterpreterState *interp)
163 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
165 if (_PyThreadState_GetFrame == NULL)
166 _PyThreadState_GetFrame = threadstate_getframe;
168 if (tstate != NULL) {
169 tstate->interp = interp;
171 tstate->frame = NULL;
172 tstate->recursion_depth = 0;
173 tstate->overflowed = 0;
174 tstate->recursion_critical = 0;
175 tstate->tracing = 0;
176 tstate->use_tracing = 0;
177 tstate->tick_counter = 0;
178 tstate->gilstate_counter = 0;
179 tstate->async_exc = NULL;
180 #ifdef WITH_THREAD
181 tstate->thread_id = PyThread_get_thread_ident();
182 #else
183 tstate->thread_id = 0;
184 #endif
186 tstate->dict = NULL;
188 tstate->curexc_type = NULL;
189 tstate->curexc_value = NULL;
190 tstate->curexc_traceback = NULL;
192 tstate->exc_type = NULL;
193 tstate->exc_value = NULL;
194 tstate->exc_traceback = NULL;
196 tstate->c_profilefunc = NULL;
197 tstate->c_tracefunc = NULL;
198 tstate->c_profileobj = NULL;
199 tstate->c_traceobj = NULL;
201 #ifdef WITH_THREAD
202 _PyGILState_NoteThreadState(tstate);
203 #endif
205 HEAD_LOCK();
206 tstate->next = interp->tstate_head;
207 interp->tstate_head = tstate;
208 HEAD_UNLOCK();
211 return tstate;
214 PyObject*
215 PyState_FindModule(struct PyModuleDef* m)
217 Py_ssize_t index = m->m_base.m_index;
218 PyInterpreterState *state = PyThreadState_GET()->interp;
219 PyObject *res;
220 if (index == 0)
221 return NULL;
222 if (state->modules_by_index == NULL)
223 return NULL;
224 if (index > PyList_GET_SIZE(state->modules_by_index))
225 return NULL;
226 res = PyList_GET_ITEM(state->modules_by_index, index);
227 return res==Py_None ? NULL : res;
231 _PyState_AddModule(PyObject* module, struct PyModuleDef* def)
233 PyInterpreterState *state = PyThreadState_GET()->interp;
234 if (!def)
235 return -1;
236 if (!state->modules_by_index) {
237 state->modules_by_index = PyList_New(0);
238 if (!state->modules_by_index)
239 return -1;
241 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
242 if (PyList_Append(state->modules_by_index, Py_None) < 0)
243 return -1;
244 Py_INCREF(module);
245 return PyList_SetItem(state->modules_by_index,
246 def->m_base.m_index, module);
249 void
250 PyThreadState_Clear(PyThreadState *tstate)
252 if (Py_VerboseFlag && tstate->frame != NULL)
253 fprintf(stderr,
254 "PyThreadState_Clear: warning: thread still has a frame\n");
256 Py_CLEAR(tstate->frame);
258 Py_CLEAR(tstate->dict);
259 Py_CLEAR(tstate->async_exc);
261 Py_CLEAR(tstate->curexc_type);
262 Py_CLEAR(tstate->curexc_value);
263 Py_CLEAR(tstate->curexc_traceback);
265 Py_CLEAR(tstate->exc_type);
266 Py_CLEAR(tstate->exc_value);
267 Py_CLEAR(tstate->exc_traceback);
269 tstate->c_profilefunc = NULL;
270 tstate->c_tracefunc = NULL;
271 Py_CLEAR(tstate->c_profileobj);
272 Py_CLEAR(tstate->c_traceobj);
276 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
277 static void
278 tstate_delete_common(PyThreadState *tstate)
280 PyInterpreterState *interp;
281 PyThreadState **p;
282 PyThreadState *prev_p = NULL;
283 if (tstate == NULL)
284 Py_FatalError("PyThreadState_Delete: NULL tstate");
285 interp = tstate->interp;
286 if (interp == NULL)
287 Py_FatalError("PyThreadState_Delete: NULL interp");
288 HEAD_LOCK();
289 for (p = &interp->tstate_head; ; p = &(*p)->next) {
290 if (*p == NULL)
291 Py_FatalError(
292 "PyThreadState_Delete: invalid tstate");
293 if (*p == tstate)
294 break;
295 /* Sanity check. These states should never happen but if
296 * they do we must abort. Otherwise we'll end up spinning in
297 * in a tight loop with the lock held. A similar check is done
298 * in thread.c find_key(). */
299 if (*p == prev_p)
300 Py_FatalError(
301 "PyThreadState_Delete: small circular list(!)"
302 " and tstate not found.");
303 prev_p = *p;
304 if ((*p)->next == interp->tstate_head)
305 Py_FatalError(
306 "PyThreadState_Delete: circular list(!) and"
307 " tstate not found.");
309 *p = tstate->next;
310 HEAD_UNLOCK();
311 free(tstate);
315 void
316 PyThreadState_Delete(PyThreadState *tstate)
318 if (tstate == _PyThreadState_Current)
319 Py_FatalError("PyThreadState_Delete: tstate is still current");
320 tstate_delete_common(tstate);
321 #ifdef WITH_THREAD
322 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
323 PyThread_delete_key_value(autoTLSkey);
324 #endif /* WITH_THREAD */
328 #ifdef WITH_THREAD
329 void
330 PyThreadState_DeleteCurrent()
332 PyThreadState *tstate = _PyThreadState_Current;
333 if (tstate == NULL)
334 Py_FatalError(
335 "PyThreadState_DeleteCurrent: no current tstate");
336 _PyThreadState_Current = NULL;
337 tstate_delete_common(tstate);
338 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
339 PyThread_delete_key_value(autoTLSkey);
340 PyEval_ReleaseLock();
342 #endif /* WITH_THREAD */
345 PyThreadState *
346 PyThreadState_Get(void)
348 if (_PyThreadState_Current == NULL)
349 Py_FatalError("PyThreadState_Get: no current thread");
351 return _PyThreadState_Current;
355 PyThreadState *
356 PyThreadState_Swap(PyThreadState *newts)
358 PyThreadState *oldts = _PyThreadState_Current;
360 _PyThreadState_Current = newts;
361 /* It should not be possible for more than one thread state
362 to be used for a thread. Check this the best we can in debug
363 builds.
365 #if defined(Py_DEBUG) && defined(WITH_THREAD)
366 if (newts) {
367 /* This can be called from PyEval_RestoreThread(). Similar
368 to it, we need to ensure errno doesn't change.
370 int err = errno;
371 PyThreadState *check = PyGILState_GetThisThreadState();
372 if (check && check->interp == newts->interp && check != newts)
373 Py_FatalError("Invalid thread state for this thread");
374 errno = err;
376 #endif
377 return oldts;
380 /* An extension mechanism to store arbitrary additional per-thread state.
381 PyThreadState_GetDict() returns a dictionary that can be used to hold such
382 state; the caller should pick a unique key and store its state there. If
383 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
384 and the caller should assume no per-thread state is available. */
386 PyObject *
387 PyThreadState_GetDict(void)
389 if (_PyThreadState_Current == NULL)
390 return NULL;
392 if (_PyThreadState_Current->dict == NULL) {
393 PyObject *d;
394 _PyThreadState_Current->dict = d = PyDict_New();
395 if (d == NULL)
396 PyErr_Clear();
398 return _PyThreadState_Current->dict;
402 /* Asynchronously raise an exception in a thread.
403 Requested by Just van Rossum and Alex Martelli.
404 To prevent naive misuse, you must write your own extension
405 to call this, or use ctypes. Must be called with the GIL held.
406 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
407 match any known thread id). Can be called with exc=NULL to clear an
408 existing async exception. This raises no exceptions. */
411 PyThreadState_SetAsyncExc(long id, PyObject *exc) {
412 PyThreadState *tstate = PyThreadState_GET();
413 PyInterpreterState *interp = tstate->interp;
414 PyThreadState *p;
416 /* Although the GIL is held, a few C API functions can be called
417 * without the GIL held, and in particular some that create and
418 * destroy thread and interpreter states. Those can mutate the
419 * list of thread states we're traversing, so to prevent that we lock
420 * head_mutex for the duration.
422 HEAD_LOCK();
423 for (p = interp->tstate_head; p != NULL; p = p->next) {
424 if (p->thread_id == id) {
425 /* Tricky: we need to decref the current value
426 * (if any) in p->async_exc, but that can in turn
427 * allow arbitrary Python code to run, including
428 * perhaps calls to this function. To prevent
429 * deadlock, we need to release head_mutex before
430 * the decref.
432 PyObject *old_exc = p->async_exc;
433 Py_XINCREF(exc);
434 p->async_exc = exc;
435 HEAD_UNLOCK();
436 Py_XDECREF(old_exc);
437 return 1;
440 HEAD_UNLOCK();
441 return 0;
445 /* Routines for advanced debuggers, requested by David Beazley.
446 Don't use unless you know what you are doing! */
448 PyInterpreterState *
449 PyInterpreterState_Head(void)
451 return interp_head;
454 PyInterpreterState *
455 PyInterpreterState_Next(PyInterpreterState *interp) {
456 return interp->next;
459 PyThreadState *
460 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
461 return interp->tstate_head;
464 PyThreadState *
465 PyThreadState_Next(PyThreadState *tstate) {
466 return tstate->next;
469 /* The implementation of sys._current_frames(). This is intended to be
470 called with the GIL held, as it will be when called via
471 sys._current_frames(). It's possible it would work fine even without
472 the GIL held, but haven't thought enough about that.
474 PyObject *
475 _PyThread_CurrentFrames(void)
477 PyObject *result;
478 PyInterpreterState *i;
480 result = PyDict_New();
481 if (result == NULL)
482 return NULL;
484 /* for i in all interpreters:
485 * for t in all of i's thread states:
486 * if t's frame isn't NULL, map t's id to its frame
487 * Because these lists can mutute even when the GIL is held, we
488 * need to grab head_mutex for the duration.
490 HEAD_LOCK();
491 for (i = interp_head; i != NULL; i = i->next) {
492 PyThreadState *t;
493 for (t = i->tstate_head; t != NULL; t = t->next) {
494 PyObject *id;
495 int stat;
496 struct _frame *frame = t->frame;
497 if (frame == NULL)
498 continue;
499 id = PyLong_FromLong(t->thread_id);
500 if (id == NULL)
501 goto Fail;
502 stat = PyDict_SetItem(result, id, (PyObject *)frame);
503 Py_DECREF(id);
504 if (stat < 0)
505 goto Fail;
508 HEAD_UNLOCK();
509 return result;
511 Fail:
512 HEAD_UNLOCK();
513 Py_DECREF(result);
514 return NULL;
517 /* Python "auto thread state" API. */
518 #ifdef WITH_THREAD
520 /* Keep this as a static, as it is not reliable! It can only
521 ever be compared to the state for the *current* thread.
522 * If not equal, then it doesn't matter that the actual
523 value may change immediately after comparison, as it can't
524 possibly change to the current thread's state.
525 * If equal, then the current thread holds the lock, so the value can't
526 change until we yield the lock.
528 static int
529 PyThreadState_IsCurrent(PyThreadState *tstate)
531 /* Must be the tstate for this thread */
532 assert(PyGILState_GetThisThreadState()==tstate);
533 /* On Windows at least, simple reads and writes to 32 bit values
534 are atomic.
536 return tstate == _PyThreadState_Current;
539 /* Internal initialization/finalization functions called by
540 Py_Initialize/Py_Finalize
542 void
543 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
545 assert(i && t); /* must init with valid states */
546 autoTLSkey = PyThread_create_key();
547 autoInterpreterState = i;
548 assert(PyThread_get_key_value(autoTLSkey) == NULL);
549 assert(t->gilstate_counter == 0);
551 _PyGILState_NoteThreadState(t);
554 void
555 _PyGILState_Fini(void)
557 PyThread_delete_key(autoTLSkey);
558 autoTLSkey = 0;
559 autoInterpreterState = NULL;
562 /* When a thread state is created for a thread by some mechanism other than
563 PyGILState_Ensure, it's important that the GILState machinery knows about
564 it so it doesn't try to create another thread state for the thread (this is
565 a better fix for SF bug #1010677 than the first one attempted).
567 static void
568 _PyGILState_NoteThreadState(PyThreadState* tstate)
570 /* If autoTLSkey is 0, this must be the very first threadstate created
571 in Py_Initialize(). Don't do anything for now (we'll be back here
572 when _PyGILState_Init is called). */
573 if (!autoTLSkey)
574 return;
576 /* Stick the thread state for this thread in thread local storage.
578 The only situation where you can legitimately have more than one
579 thread state for an OS level thread is when there are multiple
580 interpreters, when:
582 a) You shouldn't really be using the PyGILState_ APIs anyway,
583 and:
585 b) The slightly odd way PyThread_set_key_value works (see
586 comments by its implementation) means that the first thread
587 state created for that given OS level thread will "win",
588 which seems reasonable behaviour.
590 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
591 Py_FatalError("Couldn't create autoTLSkey mapping");
593 /* PyGILState_Release must not try to delete this thread state. */
594 tstate->gilstate_counter = 1;
597 /* The public functions */
598 PyThreadState *
599 PyGILState_GetThisThreadState(void)
601 if (autoInterpreterState == NULL || autoTLSkey == 0)
602 return NULL;
603 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
606 PyGILState_STATE
607 PyGILState_Ensure(void)
609 int current;
610 PyThreadState *tcur;
611 /* Note that we do not auto-init Python here - apart from
612 potential races with 2 threads auto-initializing, pep-311
613 spells out other issues. Embedders are expected to have
614 called Py_Initialize() and usually PyEval_InitThreads().
616 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
617 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
618 if (tcur == NULL) {
619 /* Create a new thread state for this thread */
620 tcur = PyThreadState_New(autoInterpreterState);
621 if (tcur == NULL)
622 Py_FatalError("Couldn't create thread-state for new thread");
623 /* This is our thread state! We'll need to delete it in the
624 matching call to PyGILState_Release(). */
625 tcur->gilstate_counter = 0;
626 current = 0; /* new thread state is never current */
628 else
629 current = PyThreadState_IsCurrent(tcur);
630 if (current == 0)
631 PyEval_RestoreThread(tcur);
632 /* Update our counter in the thread-state - no need for locks:
633 - tcur will remain valid as we hold the GIL.
634 - the counter is safe as we are the only thread "allowed"
635 to modify this value
637 ++tcur->gilstate_counter;
638 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
641 void
642 PyGILState_Release(PyGILState_STATE oldstate)
644 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
645 autoTLSkey);
646 if (tcur == NULL)
647 Py_FatalError("auto-releasing thread-state, "
648 "but no thread-state for this thread");
649 /* We must hold the GIL and have our thread state current */
650 /* XXX - remove the check - the assert should be fine,
651 but while this is very new (April 2003), the extra check
652 by release-only users can't hurt.
654 if (! PyThreadState_IsCurrent(tcur))
655 Py_FatalError("This thread state must be current when releasing");
656 assert(PyThreadState_IsCurrent(tcur));
657 --tcur->gilstate_counter;
658 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
660 /* If we're going to destroy this thread-state, we must
661 * clear it while the GIL is held, as destructors may run.
663 if (tcur->gilstate_counter == 0) {
664 /* can't have been locked when we created it */
665 assert(oldstate == PyGILState_UNLOCKED);
666 PyThreadState_Clear(tcur);
667 /* Delete the thread-state. Note this releases the GIL too!
668 * It's vital that the GIL be held here, to avoid shutdown
669 * races; see bugs 225673 and 1061968 (that nasty bug has a
670 * habit of coming back).
672 PyThreadState_DeleteCurrent();
674 /* Release the lock if necessary */
675 else if (oldstate == PyGILState_UNLOCKED)
676 PyEval_SaveThread();
679 #ifdef __cplusplus
681 #endif
683 #endif /* WITH_THREAD */