Added duplicate call to fileConfig() to ensure that it cleans up after itself correctly.
[python.git] / Python / pystate.c
blobb8f460ff8a841faec72aac2bc6d3e9000e24c486
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 interp->modules = NULL;
67 interp->sysdict = NULL;
68 interp->builtins = NULL;
69 interp->tstate_head = NULL;
70 interp->codec_search_path = NULL;
71 interp->codec_search_cache = NULL;
72 interp->codec_error_registry = NULL;
73 #ifdef HAVE_DLOPEN
74 #ifdef RTLD_NOW
75 interp->dlopenflags = RTLD_NOW;
76 #else
77 interp->dlopenflags = RTLD_LAZY;
78 #endif
79 #endif
80 #ifdef WITH_TSC
81 interp->tscdump = 0;
82 #endif
84 HEAD_LOCK();
85 interp->next = interp_head;
86 interp_head = interp;
87 HEAD_UNLOCK();
90 return interp;
94 void
95 PyInterpreterState_Clear(PyInterpreterState *interp)
97 PyThreadState *p;
98 HEAD_LOCK();
99 for (p = interp->tstate_head; p != NULL; p = p->next)
100 PyThreadState_Clear(p);
101 HEAD_UNLOCK();
102 Py_CLEAR(interp->codec_search_path);
103 Py_CLEAR(interp->codec_search_cache);
104 Py_CLEAR(interp->codec_error_registry);
105 Py_CLEAR(interp->modules);
106 Py_CLEAR(interp->sysdict);
107 Py_CLEAR(interp->builtins);
111 static void
112 zapthreads(PyInterpreterState *interp)
114 PyThreadState *p;
115 /* No need to lock the mutex here because this should only happen
116 when the threads are all really dead (XXX famous last words). */
117 while ((p = interp->tstate_head) != NULL) {
118 PyThreadState_Delete(p);
123 void
124 PyInterpreterState_Delete(PyInterpreterState *interp)
126 PyInterpreterState **p;
127 zapthreads(interp);
128 HEAD_LOCK();
129 for (p = &interp_head; ; p = &(*p)->next) {
130 if (*p == NULL)
131 Py_FatalError(
132 "PyInterpreterState_Delete: invalid interp");
133 if (*p == interp)
134 break;
136 if (interp->tstate_head != NULL)
137 Py_FatalError("PyInterpreterState_Delete: remaining threads");
138 *p = interp->next;
139 HEAD_UNLOCK();
140 free(interp);
144 /* Default implementation for _PyThreadState_GetFrame */
145 static struct _frame *
146 threadstate_getframe(PyThreadState *self)
148 return self->frame;
151 PyThreadState *
152 PyThreadState_New(PyInterpreterState *interp)
154 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
156 if (_PyThreadState_GetFrame == NULL)
157 _PyThreadState_GetFrame = threadstate_getframe;
159 if (tstate != NULL) {
160 tstate->interp = interp;
162 tstate->frame = NULL;
163 tstate->recursion_depth = 0;
164 tstate->tracing = 0;
165 tstate->use_tracing = 0;
166 tstate->tick_counter = 0;
167 tstate->gilstate_counter = 0;
168 tstate->async_exc = NULL;
169 #ifdef WITH_THREAD
170 tstate->thread_id = PyThread_get_thread_ident();
171 #else
172 tstate->thread_id = 0;
173 #endif
175 tstate->dict = NULL;
177 tstate->curexc_type = NULL;
178 tstate->curexc_value = NULL;
179 tstate->curexc_traceback = NULL;
181 tstate->exc_type = NULL;
182 tstate->exc_value = NULL;
183 tstate->exc_traceback = NULL;
185 tstate->c_profilefunc = NULL;
186 tstate->c_tracefunc = NULL;
187 tstate->c_profileobj = NULL;
188 tstate->c_traceobj = NULL;
190 #ifdef WITH_THREAD
191 _PyGILState_NoteThreadState(tstate);
192 #endif
194 HEAD_LOCK();
195 tstate->next = interp->tstate_head;
196 interp->tstate_head = tstate;
197 HEAD_UNLOCK();
200 return tstate;
204 void
205 PyThreadState_Clear(PyThreadState *tstate)
207 if (Py_VerboseFlag && tstate->frame != NULL)
208 fprintf(stderr,
209 "PyThreadState_Clear: warning: thread still has a frame\n");
211 Py_CLEAR(tstate->frame);
213 Py_CLEAR(tstate->dict);
214 Py_CLEAR(tstate->async_exc);
216 Py_CLEAR(tstate->curexc_type);
217 Py_CLEAR(tstate->curexc_value);
218 Py_CLEAR(tstate->curexc_traceback);
220 Py_CLEAR(tstate->exc_type);
221 Py_CLEAR(tstate->exc_value);
222 Py_CLEAR(tstate->exc_traceback);
224 tstate->c_profilefunc = NULL;
225 tstate->c_tracefunc = NULL;
226 Py_CLEAR(tstate->c_profileobj);
227 Py_CLEAR(tstate->c_traceobj);
231 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
232 static void
233 tstate_delete_common(PyThreadState *tstate)
235 PyInterpreterState *interp;
236 PyThreadState **p;
237 if (tstate == NULL)
238 Py_FatalError("PyThreadState_Delete: NULL tstate");
239 interp = tstate->interp;
240 if (interp == NULL)
241 Py_FatalError("PyThreadState_Delete: NULL interp");
242 HEAD_LOCK();
243 for (p = &interp->tstate_head; ; p = &(*p)->next) {
244 if (*p == NULL)
245 Py_FatalError(
246 "PyThreadState_Delete: invalid tstate");
247 if (*p == tstate)
248 break;
250 *p = tstate->next;
251 HEAD_UNLOCK();
252 free(tstate);
256 void
257 PyThreadState_Delete(PyThreadState *tstate)
259 if (tstate == _PyThreadState_Current)
260 Py_FatalError("PyThreadState_Delete: tstate is still current");
261 tstate_delete_common(tstate);
262 #ifdef WITH_THREAD
263 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
264 PyThread_delete_key_value(autoTLSkey);
265 #endif /* WITH_THREAD */
269 #ifdef WITH_THREAD
270 void
271 PyThreadState_DeleteCurrent()
273 PyThreadState *tstate = _PyThreadState_Current;
274 if (tstate == NULL)
275 Py_FatalError(
276 "PyThreadState_DeleteCurrent: no current tstate");
277 _PyThreadState_Current = NULL;
278 tstate_delete_common(tstate);
279 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
280 PyThread_delete_key_value(autoTLSkey);
281 PyEval_ReleaseLock();
283 #endif /* WITH_THREAD */
286 PyThreadState *
287 PyThreadState_Get(void)
289 if (_PyThreadState_Current == NULL)
290 Py_FatalError("PyThreadState_Get: no current thread");
292 return _PyThreadState_Current;
296 PyThreadState *
297 PyThreadState_Swap(PyThreadState *newts)
299 PyThreadState *oldts = _PyThreadState_Current;
301 _PyThreadState_Current = newts;
302 /* It should not be possible for more than one thread state
303 to be used for a thread. Check this the best we can in debug
304 builds.
306 #if defined(Py_DEBUG) && defined(WITH_THREAD)
307 if (newts) {
308 PyThreadState *check = PyGILState_GetThisThreadState();
309 if (check && check->interp == newts->interp && check != newts)
310 Py_FatalError("Invalid thread state for this thread");
312 #endif
313 return oldts;
316 /* An extension mechanism to store arbitrary additional per-thread state.
317 PyThreadState_GetDict() returns a dictionary that can be used to hold such
318 state; the caller should pick a unique key and store its state there. If
319 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
320 and the caller should assume no per-thread state is available. */
322 PyObject *
323 PyThreadState_GetDict(void)
325 if (_PyThreadState_Current == NULL)
326 return NULL;
328 if (_PyThreadState_Current->dict == NULL) {
329 PyObject *d;
330 _PyThreadState_Current->dict = d = PyDict_New();
331 if (d == NULL)
332 PyErr_Clear();
334 return _PyThreadState_Current->dict;
338 /* Asynchronously raise an exception in a thread.
339 Requested by Just van Rossum and Alex Martelli.
340 To prevent naive misuse, you must write your own extension
341 to call this. Must be called with the GIL held.
342 Returns the number of tstates modified; if it returns a number
343 greater than one, you're in trouble, and you should call it again
344 with exc=NULL to revert the effect. This raises no exceptions. */
347 PyThreadState_SetAsyncExc(long id, PyObject *exc) {
348 PyThreadState *tstate = PyThreadState_GET();
349 PyInterpreterState *interp = tstate->interp;
350 PyThreadState *p;
351 int count = 0;
352 HEAD_LOCK();
353 for (p = interp->tstate_head; p != NULL; p = p->next) {
354 if (p->thread_id != id)
355 continue;
356 Py_CLEAR(p->async_exc);
357 Py_XINCREF(exc);
358 p->async_exc = exc;
359 count += 1;
361 HEAD_UNLOCK();
362 return count;
366 /* Routines for advanced debuggers, requested by David Beazley.
367 Don't use unless you know what you are doing! */
369 PyInterpreterState *
370 PyInterpreterState_Head(void)
372 return interp_head;
375 PyInterpreterState *
376 PyInterpreterState_Next(PyInterpreterState *interp) {
377 return interp->next;
380 PyThreadState *
381 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
382 return interp->tstate_head;
385 PyThreadState *
386 PyThreadState_Next(PyThreadState *tstate) {
387 return tstate->next;
391 /* Python "auto thread state" API. */
392 #ifdef WITH_THREAD
394 /* Keep this as a static, as it is not reliable! It can only
395 ever be compared to the state for the *current* thread.
396 * If not equal, then it doesn't matter that the actual
397 value may change immediately after comparison, as it can't
398 possibly change to the current thread's state.
399 * If equal, then the current thread holds the lock, so the value can't
400 change until we yield the lock.
402 static int
403 PyThreadState_IsCurrent(PyThreadState *tstate)
405 /* Must be the tstate for this thread */
406 assert(PyGILState_GetThisThreadState()==tstate);
407 /* On Windows at least, simple reads and writes to 32 bit values
408 are atomic.
410 return tstate == _PyThreadState_Current;
413 /* Internal initialization/finalization functions called by
414 Py_Initialize/Py_Finalize
416 void
417 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
419 assert(i && t); /* must init with valid states */
420 autoTLSkey = PyThread_create_key();
421 autoInterpreterState = i;
422 assert(PyThread_get_key_value(autoTLSkey) == NULL);
423 assert(t->gilstate_counter == 0);
425 _PyGILState_NoteThreadState(t);
428 void
429 _PyGILState_Fini(void)
431 PyThread_delete_key(autoTLSkey);
432 autoTLSkey = 0;
433 autoInterpreterState = NULL;;
436 /* When a thread state is created for a thread by some mechanism other than
437 PyGILState_Ensure, it's important that the GILState machinery knows about
438 it so it doesn't try to create another thread state for the thread (this is
439 a better fix for SF bug #1010677 than the first one attempted).
441 void
442 _PyGILState_NoteThreadState(PyThreadState* tstate)
444 /* If autoTLSkey is 0, this must be the very first threadstate created
445 in Py_Initialize(). Don't do anything for now (we'll be back here
446 when _PyGILState_Init is called). */
447 if (!autoTLSkey)
448 return;
450 /* Stick the thread state for this thread in thread local storage.
452 The only situation where you can legitimately have more than one
453 thread state for an OS level thread is when there are multiple
454 interpreters, when:
456 a) You shouldn't really be using the PyGILState_ APIs anyway,
457 and:
459 b) The slightly odd way PyThread_set_key_value works (see
460 comments by its implementation) means that the first thread
461 state created for that given OS level thread will "win",
462 which seems reasonable behaviour.
464 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
465 Py_FatalError("Couldn't create autoTLSkey mapping");
467 /* PyGILState_Release must not try to delete this thread state. */
468 tstate->gilstate_counter = 1;
471 /* The public functions */
472 PyThreadState *
473 PyGILState_GetThisThreadState(void)
475 if (autoInterpreterState == NULL || autoTLSkey == 0)
476 return NULL;
477 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
480 PyGILState_STATE
481 PyGILState_Ensure(void)
483 int current;
484 PyThreadState *tcur;
485 /* Note that we do not auto-init Python here - apart from
486 potential races with 2 threads auto-initializing, pep-311
487 spells out other issues. Embedders are expected to have
488 called Py_Initialize() and usually PyEval_InitThreads().
490 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
491 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
492 if (tcur == NULL) {
493 /* Create a new thread state for this thread */
494 tcur = PyThreadState_New(autoInterpreterState);
495 if (tcur == NULL)
496 Py_FatalError("Couldn't create thread-state for new thread");
497 /* This is our thread state! We'll need to delete it in the
498 matching call to PyGILState_Release(). */
499 tcur->gilstate_counter = 0;
500 current = 0; /* new thread state is never current */
502 else
503 current = PyThreadState_IsCurrent(tcur);
504 if (current == 0)
505 PyEval_RestoreThread(tcur);
506 /* Update our counter in the thread-state - no need for locks:
507 - tcur will remain valid as we hold the GIL.
508 - the counter is safe as we are the only thread "allowed"
509 to modify this value
511 ++tcur->gilstate_counter;
512 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
515 void
516 PyGILState_Release(PyGILState_STATE oldstate)
518 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
519 autoTLSkey);
520 if (tcur == NULL)
521 Py_FatalError("auto-releasing thread-state, "
522 "but no thread-state for this thread");
523 /* We must hold the GIL and have our thread state current */
524 /* XXX - remove the check - the assert should be fine,
525 but while this is very new (April 2003), the extra check
526 by release-only users can't hurt.
528 if (! PyThreadState_IsCurrent(tcur))
529 Py_FatalError("This thread state must be current when releasing");
530 assert(PyThreadState_IsCurrent(tcur));
531 --tcur->gilstate_counter;
532 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
534 /* If we're going to destroy this thread-state, we must
535 * clear it while the GIL is held, as destructors may run.
537 if (tcur->gilstate_counter == 0) {
538 /* can't have been locked when we created it */
539 assert(oldstate == PyGILState_UNLOCKED);
540 PyThreadState_Clear(tcur);
541 /* Delete the thread-state. Note this releases the GIL too!
542 * It's vital that the GIL be held here, to avoid shutdown
543 * races; see bugs 225673 and 1061968 (that nasty bug has a
544 * habit of coming back).
546 PyThreadState_DeleteCurrent();
548 /* Release the lock if necessary */
549 else if (oldstate == PyGILState_UNLOCKED)
550 PyEval_SaveThread();
553 #ifdef __cplusplus
555 #endif
557 #endif /* WITH_THREAD */