blob: 5793612b51a0596082cb702700e326bbe1da22f2 [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;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000147#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000148 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000149#else
150 tstate->thread_id = 0;
151#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000152
Guido van Rossumede04391998-04-10 20:18:25 +0000153 tstate->dict = NULL;
154
Guido van Rossuma027efa1997-05-05 20:56:21 +0000155 tstate->curexc_type = NULL;
156 tstate->curexc_value = NULL;
157 tstate->curexc_traceback = NULL;
158
159 tstate->exc_type = NULL;
160 tstate->exc_value = NULL;
161 tstate->exc_traceback = NULL;
162
Fred Drake5755ce62001-06-27 19:19:46 +0000163 tstate->c_profilefunc = NULL;
164 tstate->c_tracefunc = NULL;
165 tstate->c_profileobj = NULL;
166 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000168 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000169 tstate->next = interp->tstate_head;
170 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000171 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000172 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000173
Guido van Rossuma027efa1997-05-05 20:56:21 +0000174 return tstate;
175}
176
177
178void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000179PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000180{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000181 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000182 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000183 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000184
185 ZAP(tstate->frame);
186
Guido van Rossumede04391998-04-10 20:18:25 +0000187 ZAP(tstate->dict);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000188 ZAP(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000189
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190 ZAP(tstate->curexc_type);
191 ZAP(tstate->curexc_value);
192 ZAP(tstate->curexc_traceback);
193
194 ZAP(tstate->exc_type);
195 ZAP(tstate->exc_value);
196 ZAP(tstate->exc_traceback);
197
Fred Drake5755ce62001-06-27 19:19:46 +0000198 tstate->c_profilefunc = NULL;
199 tstate->c_tracefunc = NULL;
200 ZAP(tstate->c_profileobj);
201 ZAP(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202}
203
204
Guido van Rossum29757862001-01-23 01:46:06 +0000205/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
206static void
207tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209 PyInterpreterState *interp;
210 PyThreadState **p;
211 if (tstate == NULL)
212 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213 interp = tstate->interp;
214 if (interp == NULL)
215 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000216 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000217 for (p = &interp->tstate_head; ; p = &(*p)->next) {
218 if (*p == NULL)
219 Py_FatalError(
220 "PyThreadState_Delete: invalid tstate");
221 if (*p == tstate)
222 break;
223 }
224 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000225 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000226 PyMem_DEL(tstate);
227}
228
229
Guido van Rossum29757862001-01-23 01:46:06 +0000230void
231PyThreadState_Delete(PyThreadState *tstate)
232{
233 if (tstate == _PyThreadState_Current)
234 Py_FatalError("PyThreadState_Delete: tstate is still current");
235 tstate_delete_common(tstate);
236}
237
238
239#ifdef WITH_THREAD
240void
241PyThreadState_DeleteCurrent()
242{
243 PyThreadState *tstate = _PyThreadState_Current;
244 if (tstate == NULL)
245 Py_FatalError(
246 "PyThreadState_DeleteCurrent: no current tstate");
247 _PyThreadState_Current = NULL;
248 tstate_delete_common(tstate);
249 PyEval_ReleaseLock();
250}
251#endif /* WITH_THREAD */
252
253
Guido van Rossuma027efa1997-05-05 20:56:21 +0000254PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000255PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000256{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000257 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000258 Py_FatalError("PyThreadState_Get: no current thread");
259
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000260 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000261}
262
263
264PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000265PyThreadState_Swap(PyThreadState *new)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000266{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000267 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000268
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000269 _PyThreadState_Current = new;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000270 /* It should not be possible for more than one thread state
271 to be used for a thread. Check this the best we can in debug
272 builds.
273 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000274#if defined(Py_DEBUG) && defined(WITH_THREAD)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000275 if (new) {
276 PyThreadState *check = PyGILState_GetThisThreadState();
277 if (check && check != new)
278 Py_FatalError("Invalid thread state for this thread");
279 }
280#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000281 return old;
282}
Guido van Rossumede04391998-04-10 20:18:25 +0000283
284/* An extension mechanism to store arbitrary additional per-thread state.
285 PyThreadState_GetDict() returns a dictionary that can be used to hold such
286 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000287 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
288 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000289
290PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000291PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000292{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000293 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000294 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000295
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000296 if (_PyThreadState_Current->dict == NULL) {
297 PyObject *d;
298 _PyThreadState_Current->dict = d = PyDict_New();
299 if (d == NULL)
300 PyErr_Clear();
301 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000302 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000303}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000304
305
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000306/* Asynchronously raise an exception in a thread.
307 Requested by Just van Rossum and Alex Martelli.
308 To prevent naive misuse, you must write your own exception
309 to call this. Must be called with the GIL held.
310 Returns the number of tstates modified; if it returns a number
311 greater than one, you're in trouble, and you should call it again
312 with exc=NULL to revert the effect. This raises no exceptions. */
313
314int
315PyThreadState_SetAsyncExc(long id, PyObject *exc) {
316 PyThreadState *tstate = PyThreadState_Get();
317 PyInterpreterState *interp = tstate->interp;
318 PyThreadState *p;
319 int count = 0;
320 for (p = interp->tstate_head; p != NULL; p = p->next) {
321 if (p->thread_id != id)
322 continue;
323 ZAP(p->async_exc);
324 Py_XINCREF(exc);
325 p->async_exc = exc;
326 count += 1;
327 }
328 return count;
329}
330
331
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000332/* Routines for advanced debuggers, requested by David Beazley.
333 Don't use unless you know what you are doing! */
334
335PyInterpreterState *
336PyInterpreterState_Head(void)
337{
338 return interp_head;
339}
340
341PyInterpreterState *
342PyInterpreterState_Next(PyInterpreterState *interp) {
343 return interp->next;
344}
345
346PyThreadState *
347PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
348 return interp->tstate_head;
349}
350
351PyThreadState *
352PyThreadState_Next(PyThreadState *tstate) {
353 return tstate->next;
354}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000355
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000356
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000357/* Python "auto thread state" API. */
358#ifdef WITH_THREAD
359
360/* Keep this as a static, as it is not reliable! It can only
361 ever be compared to the state for the *current* thread.
362 * If not equal, then it doesn't matter that the actual
363 value may change immediately after comparison, as it can't
364 possibly change to the current thread's state.
365 * If equal, then the current thread holds the lock, so the value can't
366 change until we yield the lock.
367*/
368static int
369PyThreadState_IsCurrent(PyThreadState *tstate)
370{
371 /* Must be the tstate for this thread */
372 assert(PyGILState_GetThisThreadState()==tstate);
373 /* On Windows at least, simple reads and writes to 32 bit values
374 are atomic.
375 */
376 return tstate == _PyThreadState_Current;
377}
378
379/* The single PyInterpreterState used by this process'
380 GILState implementation
381*/
382static PyInterpreterState *autoInterpreterState = NULL;
383static int autoTLSkey = 0;
384
385/* Internal initialization/finalization functions called by
386 Py_Initialize/Py_Finalize
387*/
388void _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
389{
390 assert(i && t); /* must init with a valid states */
391 autoTLSkey = PyThread_create_key();
392 autoInterpreterState = i;
393 /* Now stash the thread state for this thread in TLS */
394 PyThread_set_key_value(autoTLSkey, (void *)t);
395 assert(t->gilstate_counter==0); /* must be a new thread state */
396 t->gilstate_counter = 1;
397}
398
399void _PyGILState_Fini(void)
400{
401 PyThread_delete_key(autoTLSkey);
402 autoTLSkey = 0;
403 autoInterpreterState = NULL;;
404}
405
406/* The public functions */
407PyThreadState *PyGILState_GetThisThreadState(void)
408{
409 if (autoInterpreterState==NULL || autoTLSkey==0)
410 return NULL;
411 return (PyThreadState *) PyThread_get_key_value(autoTLSkey);
412}
413
414PyGILState_STATE PyGILState_Ensure(void)
415{
416 int current;
417 PyThreadState *tcur;
418 /* Note that we do not auto-init Python here - apart from
419 potential races with 2 threads auto-initializing, pep-311
420 spells out other issues. Embedders are expected to have
421 called Py_Initialize() and usually PyEval_InitThreads().
422 */
423 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
424 tcur = PyThread_get_key_value(autoTLSkey);
425 if (tcur==NULL) {
426 /* Create a new thread state for this thread */
427 tcur = PyThreadState_New(autoInterpreterState);
428 if (tcur==NULL)
429 Py_FatalError("Couldn't create thread-state for new thread");
430 PyThread_set_key_value(autoTLSkey, (void *)tcur);
431 current = 0; /* new thread state is never current */
432 } else
433 current = PyThreadState_IsCurrent(tcur);
434 if (!current)
435 PyEval_RestoreThread(tcur);
436 /* Update our counter in the thread-state - no need for locks:
437 - tcur will remain valid as we hold the GIL.
438 - the counter is safe as we are the only thread "allowed"
439 to modify this value
440 */
441 tcur->gilstate_counter++;
442 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
443}
444
445void PyGILState_Release(PyGILState_STATE oldstate)
446{
447 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
448 if (tcur==NULL)
449 Py_FatalError("auto-releasing thread-state, "
450 "but no thread-state for this thread");
451 /* We must hold the GIL and have our thread state current */
452 /* XXX - remove the check - the assert should be fine,
453 but while this is very new (April 2003), the extra check
454 by release-only users can't hurt.
455 */
456 if (!PyThreadState_IsCurrent(tcur))
457 Py_FatalError("This thread state must be current when releasing");
458 assert (PyThreadState_IsCurrent(tcur));
459 tcur->gilstate_counter -= 1;
460 assert (tcur->gilstate_counter >= 0); /* illegal counter value */
461
462 /* If we are about to destroy this thread-state, we must
463 clear it while the lock is held, as destructors may run
464 */
465 if (tcur->gilstate_counter==0) {
466 /* can't have been locked when we created it */
467 assert(oldstate==PyGILState_UNLOCKED);
468 PyThreadState_Clear(tcur);
469 }
470
471 /* Release the lock if necessary */
472 if (oldstate==PyGILState_UNLOCKED)
473 PyEval_ReleaseThread(tcur);
474
475 /* Now complete destruction of the thread if necessary */
476 if (tcur->gilstate_counter==0) {
477 /* Delete this thread from our TLS */
478 PyThread_delete_key_value(autoTLSkey);
479 /* Delete the thread-state */
480 PyThreadState_Delete(tcur);
481 }
482}
483#endif /* WITH_THREAD */