Changes due to added test for fileConfig contributed by Shane Hathaway.
[python.git] / Python / pystate.c
blob6584cda775a8b0a6be27cad4c3af0c3d0da1325d
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 #define ZAP(x) { \
27 PyObject *tmp = (PyObject *)(x); \
28 (x) = NULL; \
29 Py_XDECREF(tmp); \
33 #ifdef WITH_THREAD
34 #include "pythread.h"
35 static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
36 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
37 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
38 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
40 /* The single PyInterpreterState used by this process'
41 GILState implementation
43 static PyInterpreterState *autoInterpreterState = NULL;
44 static int autoTLSkey = 0;
45 #else
46 #define HEAD_INIT() /* Nothing */
47 #define HEAD_LOCK() /* Nothing */
48 #define HEAD_UNLOCK() /* Nothing */
49 #endif
51 static PyInterpreterState *interp_head = NULL;
53 PyThreadState *_PyThreadState_Current = NULL;
54 PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
56 #ifdef WITH_THREAD
57 static void _PyGILState_NoteThreadState(PyThreadState* tstate);
58 #endif
61 PyInterpreterState *
62 PyInterpreterState_New(void)
64 PyInterpreterState *interp = (PyInterpreterState *)
65 malloc(sizeof(PyInterpreterState));
67 if (interp != NULL) {
68 HEAD_INIT();
69 interp->modules = NULL;
70 interp->sysdict = NULL;
71 interp->builtins = NULL;
72 interp->tstate_head = NULL;
73 interp->codec_search_path = NULL;
74 interp->codec_search_cache = NULL;
75 interp->codec_error_registry = NULL;
76 #ifdef HAVE_DLOPEN
77 #ifdef RTLD_NOW
78 interp->dlopenflags = RTLD_NOW;
79 #else
80 interp->dlopenflags = RTLD_LAZY;
81 #endif
82 #endif
83 #ifdef WITH_TSC
84 interp->tscdump = 0;
85 #endif
87 HEAD_LOCK();
88 interp->next = interp_head;
89 interp_head = interp;
90 HEAD_UNLOCK();
93 return interp;
97 void
98 PyInterpreterState_Clear(PyInterpreterState *interp)
100 PyThreadState *p;
101 HEAD_LOCK();
102 for (p = interp->tstate_head; p != NULL; p = p->next)
103 PyThreadState_Clear(p);
104 HEAD_UNLOCK();
105 ZAP(interp->codec_search_path);
106 ZAP(interp->codec_search_cache);
107 ZAP(interp->codec_error_registry);
108 ZAP(interp->modules);
109 ZAP(interp->sysdict);
110 ZAP(interp->builtins);
114 static void
115 zapthreads(PyInterpreterState *interp)
117 PyThreadState *p;
118 /* No need to lock the mutex here because this should only happen
119 when the threads are all really dead (XXX famous last words). */
120 while ((p = interp->tstate_head) != NULL) {
121 PyThreadState_Delete(p);
126 void
127 PyInterpreterState_Delete(PyInterpreterState *interp)
129 PyInterpreterState **p;
130 zapthreads(interp);
131 HEAD_LOCK();
132 for (p = &interp_head; ; p = &(*p)->next) {
133 if (*p == NULL)
134 Py_FatalError(
135 "PyInterpreterState_Delete: invalid interp");
136 if (*p == interp)
137 break;
139 if (interp->tstate_head != NULL)
140 Py_FatalError("PyInterpreterState_Delete: remaining threads");
141 *p = interp->next;
142 HEAD_UNLOCK();
143 free(interp);
147 /* Default implementation for _PyThreadState_GetFrame */
148 static struct _frame *
149 threadstate_getframe(PyThreadState *self)
151 return self->frame;
154 PyThreadState *
155 PyThreadState_New(PyInterpreterState *interp)
157 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
159 if (_PyThreadState_GetFrame == NULL)
160 _PyThreadState_GetFrame = threadstate_getframe;
162 if (tstate != NULL) {
163 tstate->interp = interp;
165 tstate->frame = NULL;
166 tstate->recursion_depth = 0;
167 tstate->tracing = 0;
168 tstate->use_tracing = 0;
169 tstate->tick_counter = 0;
170 tstate->gilstate_counter = 0;
171 tstate->async_exc = NULL;
172 #ifdef WITH_THREAD
173 tstate->thread_id = PyThread_get_thread_ident();
174 #else
175 tstate->thread_id = 0;
176 #endif
178 tstate->dict = NULL;
180 tstate->curexc_type = NULL;
181 tstate->curexc_value = NULL;
182 tstate->curexc_traceback = NULL;
184 tstate->exc_type = NULL;
185 tstate->exc_value = NULL;
186 tstate->exc_traceback = NULL;
188 tstate->c_profilefunc = NULL;
189 tstate->c_tracefunc = NULL;
190 tstate->c_profileobj = NULL;
191 tstate->c_traceobj = NULL;
193 #ifdef WITH_THREAD
194 _PyGILState_NoteThreadState(tstate);
195 #endif
197 HEAD_LOCK();
198 tstate->next = interp->tstate_head;
199 interp->tstate_head = tstate;
200 HEAD_UNLOCK();
203 return tstate;
207 void
208 PyThreadState_Clear(PyThreadState *tstate)
210 if (Py_VerboseFlag && tstate->frame != NULL)
211 fprintf(stderr,
212 "PyThreadState_Clear: warning: thread still has a frame\n");
214 ZAP(tstate->frame);
216 ZAP(tstate->dict);
217 ZAP(tstate->async_exc);
219 ZAP(tstate->curexc_type);
220 ZAP(tstate->curexc_value);
221 ZAP(tstate->curexc_traceback);
223 ZAP(tstate->exc_type);
224 ZAP(tstate->exc_value);
225 ZAP(tstate->exc_traceback);
227 tstate->c_profilefunc = NULL;
228 tstate->c_tracefunc = NULL;
229 ZAP(tstate->c_profileobj);
230 ZAP(tstate->c_traceobj);
234 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
235 static void
236 tstate_delete_common(PyThreadState *tstate)
238 PyInterpreterState *interp;
239 PyThreadState **p;
240 if (tstate == NULL)
241 Py_FatalError("PyThreadState_Delete: NULL tstate");
242 interp = tstate->interp;
243 if (interp == NULL)
244 Py_FatalError("PyThreadState_Delete: NULL interp");
245 HEAD_LOCK();
246 for (p = &interp->tstate_head; ; p = &(*p)->next) {
247 if (*p == NULL)
248 Py_FatalError(
249 "PyThreadState_Delete: invalid tstate");
250 if (*p == tstate)
251 break;
253 *p = tstate->next;
254 HEAD_UNLOCK();
255 free(tstate);
259 void
260 PyThreadState_Delete(PyThreadState *tstate)
262 if (tstate == _PyThreadState_Current)
263 Py_FatalError("PyThreadState_Delete: tstate is still current");
264 tstate_delete_common(tstate);
268 #ifdef WITH_THREAD
269 void
270 PyThreadState_DeleteCurrent()
272 PyThreadState *tstate = _PyThreadState_Current;
273 if (tstate == NULL)
274 Py_FatalError(
275 "PyThreadState_DeleteCurrent: no current tstate");
276 _PyThreadState_Current = NULL;
277 tstate_delete_common(tstate);
278 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
279 PyThread_delete_key_value(autoTLSkey);
280 PyEval_ReleaseLock();
282 #endif /* WITH_THREAD */
285 PyThreadState *
286 PyThreadState_Get(void)
288 if (_PyThreadState_Current == NULL)
289 Py_FatalError("PyThreadState_Get: no current thread");
291 return _PyThreadState_Current;
295 PyThreadState *
296 PyThreadState_Swap(PyThreadState *new)
298 PyThreadState *old = _PyThreadState_Current;
300 _PyThreadState_Current = new;
301 /* It should not be possible for more than one thread state
302 to be used for a thread. Check this the best we can in debug
303 builds.
305 #if defined(Py_DEBUG) && defined(WITH_THREAD)
306 if (new) {
307 PyThreadState *check = PyGILState_GetThisThreadState();
308 if (check && check->interp == new->interp && check != new)
309 Py_FatalError("Invalid thread state for this thread");
311 #endif
312 return old;
315 /* An extension mechanism to store arbitrary additional per-thread state.
316 PyThreadState_GetDict() returns a dictionary that can be used to hold such
317 state; the caller should pick a unique key and store its state there. If
318 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
319 and the caller should assume no per-thread state is available. */
321 PyObject *
322 PyThreadState_GetDict(void)
324 if (_PyThreadState_Current == NULL)
325 return NULL;
327 if (_PyThreadState_Current->dict == NULL) {
328 PyObject *d;
329 _PyThreadState_Current->dict = d = PyDict_New();
330 if (d == NULL)
331 PyErr_Clear();
333 return _PyThreadState_Current->dict;
337 /* Asynchronously raise an exception in a thread.
338 Requested by Just van Rossum and Alex Martelli.
339 To prevent naive misuse, you must write your own extension
340 to call this. Must be called with the GIL held.
341 Returns the number of tstates modified; if it returns a number
342 greater than one, you're in trouble, and you should call it again
343 with exc=NULL to revert the effect. This raises no exceptions. */
346 PyThreadState_SetAsyncExc(long id, PyObject *exc) {
347 PyThreadState *tstate = PyThreadState_GET();
348 PyInterpreterState *interp = tstate->interp;
349 PyThreadState *p;
350 int count = 0;
351 HEAD_LOCK();
352 for (p = interp->tstate_head; p != NULL; p = p->next) {
353 if (p->thread_id != id)
354 continue;
355 ZAP(p->async_exc);
356 Py_XINCREF(exc);
357 p->async_exc = exc;
358 count += 1;
360 HEAD_UNLOCK();
361 return count;
365 /* Routines for advanced debuggers, requested by David Beazley.
366 Don't use unless you know what you are doing! */
368 PyInterpreterState *
369 PyInterpreterState_Head(void)
371 return interp_head;
374 PyInterpreterState *
375 PyInterpreterState_Next(PyInterpreterState *interp) {
376 return interp->next;
379 PyThreadState *
380 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
381 return interp->tstate_head;
384 PyThreadState *
385 PyThreadState_Next(PyThreadState *tstate) {
386 return tstate->next;
390 /* Python "auto thread state" API. */
391 #ifdef WITH_THREAD
393 /* Keep this as a static, as it is not reliable! It can only
394 ever be compared to the state for the *current* thread.
395 * If not equal, then it doesn't matter that the actual
396 value may change immediately after comparison, as it can't
397 possibly change to the current thread's state.
398 * If equal, then the current thread holds the lock, so the value can't
399 change until we yield the lock.
401 static int
402 PyThreadState_IsCurrent(PyThreadState *tstate)
404 /* Must be the tstate for this thread */
405 assert(PyGILState_GetThisThreadState()==tstate);
406 /* On Windows at least, simple reads and writes to 32 bit values
407 are atomic.
409 return tstate == _PyThreadState_Current;
412 /* Internal initialization/finalization functions called by
413 Py_Initialize/Py_Finalize
415 void
416 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
418 assert(i && t); /* must init with valid states */
419 autoTLSkey = PyThread_create_key();
420 autoInterpreterState = i;
421 assert(PyThread_get_key_value(autoTLSkey) == NULL);
422 assert(t->gilstate_counter == 0);
424 _PyGILState_NoteThreadState(t);
427 void
428 _PyGILState_Fini(void)
430 PyThread_delete_key(autoTLSkey);
431 autoTLSkey = 0;
432 autoInterpreterState = NULL;;
435 /* When a thread state is created for a thread by some mechanism other than
436 PyGILState_Ensure, it's important that the GILState machinery knows about
437 it so it doesn't try to create another thread state for the thread (this is
438 a better fix for SF bug #1010677 than the first one attempted).
440 void
441 _PyGILState_NoteThreadState(PyThreadState* tstate)
443 /* If autoTLSkey is 0, this must be the very first threadstate created
444 in Py_Initialize(). Don't do anything for now (we'll be back here
445 when _PyGILState_Init is called). */
446 if (!autoTLSkey)
447 return;
449 /* Stick the thread state for this thread in thread local storage.
451 The only situation where you can legitimately have more than one
452 thread state for an OS level thread is when there are multiple
453 interpreters, when:
455 a) You shouldn't really be using the PyGILState_ APIs anyway,
456 and:
458 b) The slightly odd way PyThread_set_key_value works (see
459 comments by its implementation) means that the first thread
460 state created for that given OS level thread will "win",
461 which seems reasonable behaviour.
463 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
464 Py_FatalError("Couldn't create autoTLSkey mapping");
466 /* PyGILState_Release must not try to delete this thread state. */
467 tstate->gilstate_counter = 1;
470 /* The public functions */
471 PyThreadState *
472 PyGILState_GetThisThreadState(void)
474 if (autoInterpreterState == NULL || autoTLSkey == 0)
475 return NULL;
476 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
479 PyGILState_STATE
480 PyGILState_Ensure(void)
482 int current;
483 PyThreadState *tcur;
484 /* Note that we do not auto-init Python here - apart from
485 potential races with 2 threads auto-initializing, pep-311
486 spells out other issues. Embedders are expected to have
487 called Py_Initialize() and usually PyEval_InitThreads().
489 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
490 tcur = PyThread_get_key_value(autoTLSkey);
491 if (tcur == NULL) {
492 /* Create a new thread state for this thread */
493 tcur = PyThreadState_New(autoInterpreterState);
494 if (tcur == NULL)
495 Py_FatalError("Couldn't create thread-state for new thread");
496 /* This is our thread state! We'll need to delete it in the
497 matching call to PyGILState_Release(). */
498 tcur->gilstate_counter = 0;
499 current = 0; /* new thread state is never current */
501 else
502 current = PyThreadState_IsCurrent(tcur);
503 if (current == 0)
504 PyEval_RestoreThread(tcur);
505 /* Update our counter in the thread-state - no need for locks:
506 - tcur will remain valid as we hold the GIL.
507 - the counter is safe as we are the only thread "allowed"
508 to modify this value
510 ++tcur->gilstate_counter;
511 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
514 void
515 PyGILState_Release(PyGILState_STATE oldstate)
517 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
518 if (tcur == NULL)
519 Py_FatalError("auto-releasing thread-state, "
520 "but no thread-state for this thread");
521 /* We must hold the GIL and have our thread state current */
522 /* XXX - remove the check - the assert should be fine,
523 but while this is very new (April 2003), the extra check
524 by release-only users can't hurt.
526 if (! PyThreadState_IsCurrent(tcur))
527 Py_FatalError("This thread state must be current when releasing");
528 assert(PyThreadState_IsCurrent(tcur));
529 --tcur->gilstate_counter;
530 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
532 /* If we're going to destroy this thread-state, we must
533 * clear it while the GIL is held, as destructors may run.
535 if (tcur->gilstate_counter == 0) {
536 /* can't have been locked when we created it */
537 assert(oldstate == PyGILState_UNLOCKED);
538 PyThreadState_Clear(tcur);
539 /* Delete the thread-state. Note this releases the GIL too!
540 * It's vital that the GIL be held here, to avoid shutdown
541 * races; see bugs 225673 and 1061968 (that nasty bug has a
542 * habit of coming back).
544 PyThreadState_DeleteCurrent();
546 /* Release the lock if necessary */
547 else if (oldstate == PyGILState_UNLOCKED)
548 PyEval_SaveThread();
550 #endif /* WITH_THREAD */