Fix issue number in comment.
[python.git] / Python / pystate.c
blobda417c103202f792fefd42ea3fcb617ceb36585c
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->sysdict = NULL;
73 interp->builtins = NULL;
74 interp->tstate_head = NULL;
75 interp->codec_search_path = NULL;
76 interp->codec_search_cache = NULL;
77 interp->codec_error_registry = NULL;
78 #ifdef HAVE_DLOPEN
79 #ifdef RTLD_NOW
80 interp->dlopenflags = RTLD_NOW;
81 #else
82 interp->dlopenflags = RTLD_LAZY;
83 #endif
84 #endif
85 #ifdef WITH_TSC
86 interp->tscdump = 0;
87 #endif
89 HEAD_LOCK();
90 interp->next = interp_head;
91 interp_head = interp;
92 HEAD_UNLOCK();
95 return interp;
99 void
100 PyInterpreterState_Clear(PyInterpreterState *interp)
102 PyThreadState *p;
103 HEAD_LOCK();
104 for (p = interp->tstate_head; p != NULL; p = p->next)
105 PyThreadState_Clear(p);
106 HEAD_UNLOCK();
107 Py_CLEAR(interp->codec_search_path);
108 Py_CLEAR(interp->codec_search_cache);
109 Py_CLEAR(interp->codec_error_registry);
110 Py_CLEAR(interp->modules);
111 Py_CLEAR(interp->modules_reloading);
112 Py_CLEAR(interp->sysdict);
113 Py_CLEAR(interp->builtins);
117 static void
118 zapthreads(PyInterpreterState *interp)
120 PyThreadState *p;
121 /* No need to lock the mutex here because this should only happen
122 when the threads are all really dead (XXX famous last words). */
123 while ((p = interp->tstate_head) != NULL) {
124 PyThreadState_Delete(p);
129 void
130 PyInterpreterState_Delete(PyInterpreterState *interp)
132 PyInterpreterState **p;
133 zapthreads(interp);
134 HEAD_LOCK();
135 for (p = &interp_head; ; p = &(*p)->next) {
136 if (*p == NULL)
137 Py_FatalError(
138 "PyInterpreterState_Delete: invalid interp");
139 if (*p == interp)
140 break;
142 if (interp->tstate_head != NULL)
143 Py_FatalError("PyInterpreterState_Delete: remaining threads");
144 *p = interp->next;
145 HEAD_UNLOCK();
146 free(interp);
150 /* Default implementation for _PyThreadState_GetFrame */
151 static struct _frame *
152 threadstate_getframe(PyThreadState *self)
154 return self->frame;
157 PyThreadState *
158 PyThreadState_New(PyInterpreterState *interp)
160 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
162 if (_PyThreadState_GetFrame == NULL)
163 _PyThreadState_GetFrame = threadstate_getframe;
165 if (tstate != NULL) {
166 tstate->interp = interp;
168 tstate->frame = NULL;
169 tstate->recursion_depth = 0;
170 tstate->tracing = 0;
171 tstate->use_tracing = 0;
172 tstate->tick_counter = 0;
173 tstate->gilstate_counter = 0;
174 tstate->async_exc = NULL;
175 #ifdef WITH_THREAD
176 tstate->thread_id = PyThread_get_thread_ident();
177 #else
178 tstate->thread_id = 0;
179 #endif
181 tstate->dict = NULL;
183 tstate->curexc_type = NULL;
184 tstate->curexc_value = NULL;
185 tstate->curexc_traceback = NULL;
187 tstate->exc_type = NULL;
188 tstate->exc_value = NULL;
189 tstate->exc_traceback = NULL;
191 tstate->c_profilefunc = NULL;
192 tstate->c_tracefunc = NULL;
193 tstate->c_profileobj = NULL;
194 tstate->c_traceobj = NULL;
196 #ifdef WITH_THREAD
197 _PyGILState_NoteThreadState(tstate);
198 #endif
200 HEAD_LOCK();
201 tstate->next = interp->tstate_head;
202 interp->tstate_head = tstate;
203 HEAD_UNLOCK();
206 return tstate;
210 void
211 PyThreadState_Clear(PyThreadState *tstate)
213 if (Py_VerboseFlag && tstate->frame != NULL)
214 fprintf(stderr,
215 "PyThreadState_Clear: warning: thread still has a frame\n");
217 Py_CLEAR(tstate->frame);
219 Py_CLEAR(tstate->dict);
220 Py_CLEAR(tstate->async_exc);
222 Py_CLEAR(tstate->curexc_type);
223 Py_CLEAR(tstate->curexc_value);
224 Py_CLEAR(tstate->curexc_traceback);
226 Py_CLEAR(tstate->exc_type);
227 Py_CLEAR(tstate->exc_value);
228 Py_CLEAR(tstate->exc_traceback);
230 tstate->c_profilefunc = NULL;
231 tstate->c_tracefunc = NULL;
232 Py_CLEAR(tstate->c_profileobj);
233 Py_CLEAR(tstate->c_traceobj);
237 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
238 static void
239 tstate_delete_common(PyThreadState *tstate)
241 PyInterpreterState *interp;
242 PyThreadState **p;
243 PyThreadState *prev_p = NULL;
244 if (tstate == NULL)
245 Py_FatalError("PyThreadState_Delete: NULL tstate");
246 interp = tstate->interp;
247 if (interp == NULL)
248 Py_FatalError("PyThreadState_Delete: NULL interp");
249 HEAD_LOCK();
250 for (p = &interp->tstate_head; ; p = &(*p)->next) {
251 if (*p == NULL)
252 Py_FatalError(
253 "PyThreadState_Delete: invalid tstate");
254 if (*p == tstate)
255 break;
256 /* Sanity check. These states should never happen but if
257 * they do we must abort. Otherwise we'll end up spinning in
258 * in a tight loop with the lock held. A similar check is done
259 * in thread.c find_key(). */
260 if (*p == prev_p)
261 Py_FatalError(
262 "PyThreadState_Delete: small circular list(!)"
263 " and tstate not found.");
264 prev_p = *p;
265 if ((*p)->next == interp->tstate_head)
266 Py_FatalError(
267 "PyThreadState_Delete: circular list(!) and"
268 " tstate not found.");
270 *p = tstate->next;
271 HEAD_UNLOCK();
272 free(tstate);
276 void
277 PyThreadState_Delete(PyThreadState *tstate)
279 if (tstate == _PyThreadState_Current)
280 Py_FatalError("PyThreadState_Delete: tstate is still current");
281 tstate_delete_common(tstate);
282 #ifdef WITH_THREAD
283 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
284 PyThread_delete_key_value(autoTLSkey);
285 #endif /* WITH_THREAD */
289 #ifdef WITH_THREAD
290 void
291 PyThreadState_DeleteCurrent()
293 PyThreadState *tstate = _PyThreadState_Current;
294 if (tstate == NULL)
295 Py_FatalError(
296 "PyThreadState_DeleteCurrent: no current tstate");
297 _PyThreadState_Current = NULL;
298 tstate_delete_common(tstate);
299 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
300 PyThread_delete_key_value(autoTLSkey);
301 PyEval_ReleaseLock();
303 #endif /* WITH_THREAD */
306 PyThreadState *
307 PyThreadState_Get(void)
309 if (_PyThreadState_Current == NULL)
310 Py_FatalError("PyThreadState_Get: no current thread");
312 return _PyThreadState_Current;
316 PyThreadState *
317 PyThreadState_Swap(PyThreadState *newts)
319 PyThreadState *oldts = _PyThreadState_Current;
321 _PyThreadState_Current = newts;
322 /* It should not be possible for more than one thread state
323 to be used for a thread. Check this the best we can in debug
324 builds.
326 #if defined(Py_DEBUG) && defined(WITH_THREAD)
327 if (newts) {
328 /* This can be called from PyEval_RestoreThread(). Similar
329 to it, we need to ensure errno doesn't change.
331 int err = errno;
332 PyThreadState *check = PyGILState_GetThisThreadState();
333 if (check && check->interp == newts->interp && check != newts)
334 Py_FatalError("Invalid thread state for this thread");
335 errno = err;
337 #endif
338 return oldts;
341 /* An extension mechanism to store arbitrary additional per-thread state.
342 PyThreadState_GetDict() returns a dictionary that can be used to hold such
343 state; the caller should pick a unique key and store its state there. If
344 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
345 and the caller should assume no per-thread state is available. */
347 PyObject *
348 PyThreadState_GetDict(void)
350 if (_PyThreadState_Current == NULL)
351 return NULL;
353 if (_PyThreadState_Current->dict == NULL) {
354 PyObject *d;
355 _PyThreadState_Current->dict = d = PyDict_New();
356 if (d == NULL)
357 PyErr_Clear();
359 return _PyThreadState_Current->dict;
363 /* Asynchronously raise an exception in a thread.
364 Requested by Just van Rossum and Alex Martelli.
365 To prevent naive misuse, you must write your own extension
366 to call this, or use ctypes. Must be called with the GIL held.
367 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
368 match any known thread id). Can be called with exc=NULL to clear an
369 existing async exception. This raises no exceptions. */
372 PyThreadState_SetAsyncExc(long id, PyObject *exc) {
373 PyThreadState *tstate = PyThreadState_GET();
374 PyInterpreterState *interp = tstate->interp;
375 PyThreadState *p;
377 /* Although the GIL is held, a few C API functions can be called
378 * without the GIL held, and in particular some that create and
379 * destroy thread and interpreter states. Those can mutate the
380 * list of thread states we're traversing, so to prevent that we lock
381 * head_mutex for the duration.
383 HEAD_LOCK();
384 for (p = interp->tstate_head; p != NULL; p = p->next) {
385 if (p->thread_id == id) {
386 /* Tricky: we need to decref the current value
387 * (if any) in p->async_exc, but that can in turn
388 * allow arbitrary Python code to run, including
389 * perhaps calls to this function. To prevent
390 * deadlock, we need to release head_mutex before
391 * the decref.
393 PyObject *old_exc = p->async_exc;
394 Py_XINCREF(exc);
395 p->async_exc = exc;
396 HEAD_UNLOCK();
397 Py_XDECREF(old_exc);
398 return 1;
401 HEAD_UNLOCK();
402 return 0;
406 /* Routines for advanced debuggers, requested by David Beazley.
407 Don't use unless you know what you are doing! */
409 PyInterpreterState *
410 PyInterpreterState_Head(void)
412 return interp_head;
415 PyInterpreterState *
416 PyInterpreterState_Next(PyInterpreterState *interp) {
417 return interp->next;
420 PyThreadState *
421 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
422 return interp->tstate_head;
425 PyThreadState *
426 PyThreadState_Next(PyThreadState *tstate) {
427 return tstate->next;
430 /* The implementation of sys._current_frames(). This is intended to be
431 called with the GIL held, as it will be when called via
432 sys._current_frames(). It's possible it would work fine even without
433 the GIL held, but haven't thought enough about that.
435 PyObject *
436 _PyThread_CurrentFrames(void)
438 PyObject *result;
439 PyInterpreterState *i;
441 result = PyDict_New();
442 if (result == NULL)
443 return NULL;
445 /* for i in all interpreters:
446 * for t in all of i's thread states:
447 * if t's frame isn't NULL, map t's id to its frame
448 * Because these lists can mutute even when the GIL is held, we
449 * need to grab head_mutex for the duration.
451 HEAD_LOCK();
452 for (i = interp_head; i != NULL; i = i->next) {
453 PyThreadState *t;
454 for (t = i->tstate_head; t != NULL; t = t->next) {
455 PyObject *id;
456 int stat;
457 struct _frame *frame = t->frame;
458 if (frame == NULL)
459 continue;
460 id = PyInt_FromLong(t->thread_id);
461 if (id == NULL)
462 goto Fail;
463 stat = PyDict_SetItem(result, id, (PyObject *)frame);
464 Py_DECREF(id);
465 if (stat < 0)
466 goto Fail;
469 HEAD_UNLOCK();
470 return result;
472 Fail:
473 HEAD_UNLOCK();
474 Py_DECREF(result);
475 return NULL;
478 /* Python "auto thread state" API. */
479 #ifdef WITH_THREAD
481 /* Keep this as a static, as it is not reliable! It can only
482 ever be compared to the state for the *current* thread.
483 * If not equal, then it doesn't matter that the actual
484 value may change immediately after comparison, as it can't
485 possibly change to the current thread's state.
486 * If equal, then the current thread holds the lock, so the value can't
487 change until we yield the lock.
489 static int
490 PyThreadState_IsCurrent(PyThreadState *tstate)
492 /* Must be the tstate for this thread */
493 assert(PyGILState_GetThisThreadState()==tstate);
494 /* On Windows at least, simple reads and writes to 32 bit values
495 are atomic.
497 return tstate == _PyThreadState_Current;
500 /* Internal initialization/finalization functions called by
501 Py_Initialize/Py_Finalize
503 void
504 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
506 assert(i && t); /* must init with valid states */
507 autoTLSkey = PyThread_create_key();
508 autoInterpreterState = i;
509 assert(PyThread_get_key_value(autoTLSkey) == NULL);
510 assert(t->gilstate_counter == 0);
512 _PyGILState_NoteThreadState(t);
515 void
516 _PyGILState_Fini(void)
518 PyThread_delete_key(autoTLSkey);
519 autoTLSkey = 0;
520 autoInterpreterState = NULL;
523 /* When a thread state is created for a thread by some mechanism other than
524 PyGILState_Ensure, it's important that the GILState machinery knows about
525 it so it doesn't try to create another thread state for the thread (this is
526 a better fix for SF bug #1010677 than the first one attempted).
528 static void
529 _PyGILState_NoteThreadState(PyThreadState* tstate)
531 /* If autoTLSkey is 0, this must be the very first threadstate created
532 in Py_Initialize(). Don't do anything for now (we'll be back here
533 when _PyGILState_Init is called). */
534 if (!autoTLSkey)
535 return;
537 /* Stick the thread state for this thread in thread local storage.
539 The only situation where you can legitimately have more than one
540 thread state for an OS level thread is when there are multiple
541 interpreters, when:
543 a) You shouldn't really be using the PyGILState_ APIs anyway,
544 and:
546 b) The slightly odd way PyThread_set_key_value works (see
547 comments by its implementation) means that the first thread
548 state created for that given OS level thread will "win",
549 which seems reasonable behaviour.
551 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
552 Py_FatalError("Couldn't create autoTLSkey mapping");
554 /* PyGILState_Release must not try to delete this thread state. */
555 tstate->gilstate_counter = 1;
558 /* The public functions */
559 PyThreadState *
560 PyGILState_GetThisThreadState(void)
562 if (autoInterpreterState == NULL || autoTLSkey == 0)
563 return NULL;
564 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
567 PyGILState_STATE
568 PyGILState_Ensure(void)
570 int current;
571 PyThreadState *tcur;
572 /* Note that we do not auto-init Python here - apart from
573 potential races with 2 threads auto-initializing, pep-311
574 spells out other issues. Embedders are expected to have
575 called Py_Initialize() and usually PyEval_InitThreads().
577 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
578 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
579 if (tcur == NULL) {
580 /* Create a new thread state for this thread */
581 tcur = PyThreadState_New(autoInterpreterState);
582 if (tcur == NULL)
583 Py_FatalError("Couldn't create thread-state for new thread");
584 /* This is our thread state! We'll need to delete it in the
585 matching call to PyGILState_Release(). */
586 tcur->gilstate_counter = 0;
587 current = 0; /* new thread state is never current */
589 else
590 current = PyThreadState_IsCurrent(tcur);
591 if (current == 0)
592 PyEval_RestoreThread(tcur);
593 /* Update our counter in the thread-state - no need for locks:
594 - tcur will remain valid as we hold the GIL.
595 - the counter is safe as we are the only thread "allowed"
596 to modify this value
598 ++tcur->gilstate_counter;
599 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
602 void
603 PyGILState_Release(PyGILState_STATE oldstate)
605 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
606 autoTLSkey);
607 if (tcur == NULL)
608 Py_FatalError("auto-releasing thread-state, "
609 "but no thread-state for this thread");
610 /* We must hold the GIL and have our thread state current */
611 /* XXX - remove the check - the assert should be fine,
612 but while this is very new (April 2003), the extra check
613 by release-only users can't hurt.
615 if (! PyThreadState_IsCurrent(tcur))
616 Py_FatalError("This thread state must be current when releasing");
617 assert(PyThreadState_IsCurrent(tcur));
618 --tcur->gilstate_counter;
619 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
621 /* If we're going to destroy this thread-state, we must
622 * clear it while the GIL is held, as destructors may run.
624 if (tcur->gilstate_counter == 0) {
625 /* can't have been locked when we created it */
626 assert(oldstate == PyGILState_UNLOCKED);
627 PyThreadState_Clear(tcur);
628 /* Delete the thread-state. Note this releases the GIL too!
629 * It's vital that the GIL be held here, to avoid shutdown
630 * races; see bugs 225673 and 1061968 (that nasty bug has a
631 * habit of coming back).
633 PyThreadState_DeleteCurrent();
635 /* Release the lock if necessary */
636 else if (oldstate == PyGILState_UNLOCKED)
637 PyEval_SaveThread();
640 #ifdef __cplusplus
642 #endif
644 #endif /* WITH_THREAD */