blob: e8cb54715612bdaffb0166d1f1cedae7646d1631 [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001
2/* Thread and interpreter state structures and their interfaces */
3
4#include "Python.h"
5
Martin v. Löwisf0473d52001-07-18 16:17:16 +00006#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
14
15
Guido van Rossum25ce5661997-08-02 03:10:38 +000016#define ZAP(x) { \
17 PyObject *tmp = (PyObject *)(x); \
18 (x) = NULL; \
19 Py_XDECREF(tmp); \
20}
21
22
Guido van Rossum1d5ad901999-06-18 14:22:24 +000023#ifdef WITH_THREAD
24#include "pythread.h"
25static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000026#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000027#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
34
Guido van Rossum25ce5661997-08-02 03:10:38 +000035static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000036
Guido van Rossum18bc7c21998-12-21 18:27:28 +000037PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossum6297a7a2003-02-19 15:53:17 +000038PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000039
40
41PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000042PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000043{
44 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +000045
Guido van Rossuma027efa1997-05-05 20:56:21 +000046 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000047 HEAD_INIT();
Guido van Rossum25ce5661997-08-02 03:10:38 +000048 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000049 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000050 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000051 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000052 interp->codec_search_path = NULL;
53 interp->codec_search_cache = NULL;
54 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000055#ifdef HAVE_DLOPEN
56#ifdef RTLD_NOW
57 interp->dlopenflags = RTLD_NOW;
58#else
59 interp->dlopenflags = RTLD_LAZY;
60#endif
61#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000062
Tim Peters412f2462000-09-02 09:16:15 +000063 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000064 interp->next = interp_head;
65 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000066 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000067 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000068
Guido van Rossuma027efa1997-05-05 20:56:21 +000069 return interp;
70}
71
72
73void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000074PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000075{
76 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000077 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000078 for (p = interp->tstate_head; p != NULL; p = p->next)
79 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +000080 HEAD_UNLOCK();
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000081 ZAP(interp->codec_search_path);
82 ZAP(interp->codec_search_cache);
83 ZAP(interp->codec_error_registry);
Guido van Rossum25ce5661997-08-02 03:10:38 +000084 ZAP(interp->modules);
85 ZAP(interp->sysdict);
86 ZAP(interp->builtins);
87}
88
89
90static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000091zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000092{
Guido van Rossum1d5ad901999-06-18 14:22:24 +000093 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) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000097 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +000098 }
99}
100
101
102void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000103PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000104{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105 PyInterpreterState **p;
106 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000107 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000108 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;
114 }
115 if (interp->tstate_head != NULL)
116 Py_FatalError("PyInterpreterState_Delete: remaining threads");
117 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000118 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000119 PyMem_DEL(interp);
120}
121
122
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000123/* Default implementation for _PyThreadState_GetFrame */
124static struct _frame *
125threadstate_getframe(PyThreadState *self)
126{
127 return self->frame;
128}
129
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000131PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132{
133 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000134 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000135 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136
Guido van Rossuma027efa1997-05-05 20:56:21 +0000137 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000138 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000139
140 tstate->frame = NULL;
141 tstate->recursion_depth = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000142 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000143 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000144 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000145 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000146 tstate->async_exc = NULL;
147 tstate->thread_id = PyThread_get_thread_ident();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000148
Guido van Rossumede04391998-04-10 20:18:25 +0000149 tstate->dict = NULL;
150
Guido van Rossuma027efa1997-05-05 20:56:21 +0000151 tstate->curexc_type = NULL;
152 tstate->curexc_value = NULL;
153 tstate->curexc_traceback = NULL;
154
155 tstate->exc_type = NULL;
156 tstate->exc_value = NULL;
157 tstate->exc_traceback = NULL;
158
Fred Drake5755ce62001-06-27 19:19:46 +0000159 tstate->c_profilefunc = NULL;
160 tstate->c_tracefunc = NULL;
161 tstate->c_profileobj = NULL;
162 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000163
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000164 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000165 tstate->next = interp->tstate_head;
166 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000167 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000169
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170 return tstate;
171}
172
173
174void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000175PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000176{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000177 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000178 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000179 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000180
181 ZAP(tstate->frame);
182
Guido van Rossumede04391998-04-10 20:18:25 +0000183 ZAP(tstate->dict);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000184 ZAP(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000185
Guido van Rossum25ce5661997-08-02 03:10:38 +0000186 ZAP(tstate->curexc_type);
187 ZAP(tstate->curexc_value);
188 ZAP(tstate->curexc_traceback);
189
190 ZAP(tstate->exc_type);
191 ZAP(tstate->exc_value);
192 ZAP(tstate->exc_traceback);
193
Fred Drake5755ce62001-06-27 19:19:46 +0000194 tstate->c_profilefunc = NULL;
195 tstate->c_tracefunc = NULL;
196 ZAP(tstate->c_profileobj);
197 ZAP(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000198}
199
200
Guido van Rossum29757862001-01-23 01:46:06 +0000201/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
202static void
203tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000204{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205 PyInterpreterState *interp;
206 PyThreadState **p;
207 if (tstate == NULL)
208 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209 interp = tstate->interp;
210 if (interp == NULL)
211 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000212 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213 for (p = &interp->tstate_head; ; p = &(*p)->next) {
214 if (*p == NULL)
215 Py_FatalError(
216 "PyThreadState_Delete: invalid tstate");
217 if (*p == tstate)
218 break;
219 }
220 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000221 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000222 PyMem_DEL(tstate);
223}
224
225
Guido van Rossum29757862001-01-23 01:46:06 +0000226void
227PyThreadState_Delete(PyThreadState *tstate)
228{
229 if (tstate == _PyThreadState_Current)
230 Py_FatalError("PyThreadState_Delete: tstate is still current");
231 tstate_delete_common(tstate);
232}
233
234
235#ifdef WITH_THREAD
236void
237PyThreadState_DeleteCurrent()
238{
239 PyThreadState *tstate = _PyThreadState_Current;
240 if (tstate == NULL)
241 Py_FatalError(
242 "PyThreadState_DeleteCurrent: no current tstate");
243 _PyThreadState_Current = NULL;
244 tstate_delete_common(tstate);
245 PyEval_ReleaseLock();
246}
247#endif /* WITH_THREAD */
248
249
Guido van Rossuma027efa1997-05-05 20:56:21 +0000250PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000251PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000252{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000253 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254 Py_FatalError("PyThreadState_Get: no current thread");
255
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000256 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000257}
258
259
260PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000261PyThreadState_Swap(PyThreadState *new)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000262{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000263 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000264
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000265 _PyThreadState_Current = new;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000266 /* It should not be possible for more than one thread state
267 to be used for a thread. Check this the best we can in debug
268 builds.
269 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000270#if defined(Py_DEBUG) && defined(WITH_THREAD)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000271 if (new) {
272 PyThreadState *check = PyGILState_GetThisThreadState();
273 if (check && check != new)
274 Py_FatalError("Invalid thread state for this thread");
275 }
276#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000277 return old;
278}
Guido van Rossumede04391998-04-10 20:18:25 +0000279
280/* An extension mechanism to store arbitrary additional per-thread state.
281 PyThreadState_GetDict() returns a dictionary that can be used to hold such
282 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000283 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
284 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000285
286PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000288{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000289 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000290 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000291
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000292 if (_PyThreadState_Current->dict == NULL) {
293 PyObject *d;
294 _PyThreadState_Current->dict = d = PyDict_New();
295 if (d == NULL)
296 PyErr_Clear();
297 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000298 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000299}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000300
301
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000302/* Asynchronously raise an exception in a thread.
303 Requested by Just van Rossum and Alex Martelli.
304 To prevent naive misuse, you must write your own exception
305 to call this. Must be called with the GIL held.
306 Returns the number of tstates modified; if it returns a number
307 greater than one, you're in trouble, and you should call it again
308 with exc=NULL to revert the effect. This raises no exceptions. */
309
310int
311PyThreadState_SetAsyncExc(long id, PyObject *exc) {
312 PyThreadState *tstate = PyThreadState_Get();
313 PyInterpreterState *interp = tstate->interp;
314 PyThreadState *p;
315 int count = 0;
316 for (p = interp->tstate_head; p != NULL; p = p->next) {
317 if (p->thread_id != id)
318 continue;
319 ZAP(p->async_exc);
320 Py_XINCREF(exc);
321 p->async_exc = exc;
322 count += 1;
323 }
324 return count;
325}
326
327
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000328/* Routines for advanced debuggers, requested by David Beazley.
329 Don't use unless you know what you are doing! */
330
331PyInterpreterState *
332PyInterpreterState_Head(void)
333{
334 return interp_head;
335}
336
337PyInterpreterState *
338PyInterpreterState_Next(PyInterpreterState *interp) {
339 return interp->next;
340}
341
342PyThreadState *
343PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
344 return interp->tstate_head;
345}
346
347PyThreadState *
348PyThreadState_Next(PyThreadState *tstate) {
349 return tstate->next;
350}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000351
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000352
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000353/* Python "auto thread state" API. */
354#ifdef WITH_THREAD
355
356/* Keep this as a static, as it is not reliable! It can only
357 ever be compared to the state for the *current* thread.
358 * If not equal, then it doesn't matter that the actual
359 value may change immediately after comparison, as it can't
360 possibly change to the current thread's state.
361 * If equal, then the current thread holds the lock, so the value can't
362 change until we yield the lock.
363*/
364static int
365PyThreadState_IsCurrent(PyThreadState *tstate)
366{
367 /* Must be the tstate for this thread */
368 assert(PyGILState_GetThisThreadState()==tstate);
369 /* On Windows at least, simple reads and writes to 32 bit values
370 are atomic.
371 */
372 return tstate == _PyThreadState_Current;
373}
374
375/* The single PyInterpreterState used by this process'
376 GILState implementation
377*/
378static PyInterpreterState *autoInterpreterState = NULL;
379static int autoTLSkey = 0;
380
381/* Internal initialization/finalization functions called by
382 Py_Initialize/Py_Finalize
383*/
384void _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
385{
386 assert(i && t); /* must init with a valid states */
387 autoTLSkey = PyThread_create_key();
388 autoInterpreterState = i;
389 /* Now stash the thread state for this thread in TLS */
390 PyThread_set_key_value(autoTLSkey, (void *)t);
391 assert(t->gilstate_counter==0); /* must be a new thread state */
392 t->gilstate_counter = 1;
393}
394
395void _PyGILState_Fini(void)
396{
397 PyThread_delete_key(autoTLSkey);
398 autoTLSkey = 0;
399 autoInterpreterState = NULL;;
400}
401
402/* The public functions */
403PyThreadState *PyGILState_GetThisThreadState(void)
404{
405 if (autoInterpreterState==NULL || autoTLSkey==0)
406 return NULL;
407 return (PyThreadState *) PyThread_get_key_value(autoTLSkey);
408}
409
410PyGILState_STATE PyGILState_Ensure(void)
411{
412 int current;
413 PyThreadState *tcur;
414 /* Note that we do not auto-init Python here - apart from
415 potential races with 2 threads auto-initializing, pep-311
416 spells out other issues. Embedders are expected to have
417 called Py_Initialize() and usually PyEval_InitThreads().
418 */
419 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
420 tcur = PyThread_get_key_value(autoTLSkey);
421 if (tcur==NULL) {
422 /* Create a new thread state for this thread */
423 tcur = PyThreadState_New(autoInterpreterState);
424 if (tcur==NULL)
425 Py_FatalError("Couldn't create thread-state for new thread");
426 PyThread_set_key_value(autoTLSkey, (void *)tcur);
427 current = 0; /* new thread state is never current */
428 } else
429 current = PyThreadState_IsCurrent(tcur);
430 if (!current)
431 PyEval_RestoreThread(tcur);
432 /* Update our counter in the thread-state - no need for locks:
433 - tcur will remain valid as we hold the GIL.
434 - the counter is safe as we are the only thread "allowed"
435 to modify this value
436 */
437 tcur->gilstate_counter++;
438 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
439}
440
441void PyGILState_Release(PyGILState_STATE oldstate)
442{
443 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
444 if (tcur==NULL)
445 Py_FatalError("auto-releasing thread-state, "
446 "but no thread-state for this thread");
447 /* We must hold the GIL and have our thread state current */
448 /* XXX - remove the check - the assert should be fine,
449 but while this is very new (April 2003), the extra check
450 by release-only users can't hurt.
451 */
452 if (!PyThreadState_IsCurrent(tcur))
453 Py_FatalError("This thread state must be current when releasing");
454 assert (PyThreadState_IsCurrent(tcur));
455 tcur->gilstate_counter -= 1;
456 assert (tcur->gilstate_counter >= 0); /* illegal counter value */
457
458 /* If we are about to destroy this thread-state, we must
459 clear it while the lock is held, as destructors may run
460 */
461 if (tcur->gilstate_counter==0) {
462 /* can't have been locked when we created it */
463 assert(oldstate==PyGILState_UNLOCKED);
464 PyThreadState_Clear(tcur);
465 }
466
467 /* Release the lock if necessary */
468 if (oldstate==PyGILState_UNLOCKED)
469 PyEval_ReleaseThread(tcur);
470
471 /* Now complete destruction of the thread if necessary */
472 if (tcur->gilstate_counter==0) {
473 /* Delete this thread from our TLS */
474 PyThread_delete_key_value(autoTLSkey);
475 /* Delete the thread-state */
476 PyThreadState_Delete(tcur);
477 }
478}
479#endif /* WITH_THREAD */