blob: b083f8cb944b2c37535d48191ec65d815ebaa148 [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 Rossuma027efa1997-05-05 20:56:21 +0000146
Guido van Rossumede04391998-04-10 20:18:25 +0000147 tstate->dict = NULL;
148
Guido van Rossuma027efa1997-05-05 20:56:21 +0000149 tstate->curexc_type = NULL;
150 tstate->curexc_value = NULL;
151 tstate->curexc_traceback = NULL;
152
153 tstate->exc_type = NULL;
154 tstate->exc_value = NULL;
155 tstate->exc_traceback = NULL;
156
Fred Drake5755ce62001-06-27 19:19:46 +0000157 tstate->c_profilefunc = NULL;
158 tstate->c_tracefunc = NULL;
159 tstate->c_profileobj = NULL;
160 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000161
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000162 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163 tstate->next = interp->tstate_head;
164 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000165 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000166 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000167
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168 return tstate;
169}
170
171
172void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000173PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000174{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000175 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000176 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000177 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000178
179 ZAP(tstate->frame);
180
Guido van Rossumede04391998-04-10 20:18:25 +0000181 ZAP(tstate->dict);
182
Guido van Rossum25ce5661997-08-02 03:10:38 +0000183 ZAP(tstate->curexc_type);
184 ZAP(tstate->curexc_value);
185 ZAP(tstate->curexc_traceback);
186
187 ZAP(tstate->exc_type);
188 ZAP(tstate->exc_value);
189 ZAP(tstate->exc_traceback);
190
Fred Drake5755ce62001-06-27 19:19:46 +0000191 tstate->c_profilefunc = NULL;
192 tstate->c_tracefunc = NULL;
193 ZAP(tstate->c_profileobj);
194 ZAP(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195}
196
197
Guido van Rossum29757862001-01-23 01:46:06 +0000198/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
199static void
200tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000201{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202 PyInterpreterState *interp;
203 PyThreadState **p;
204 if (tstate == NULL)
205 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206 interp = tstate->interp;
207 if (interp == NULL)
208 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000209 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210 for (p = &interp->tstate_head; ; p = &(*p)->next) {
211 if (*p == NULL)
212 Py_FatalError(
213 "PyThreadState_Delete: invalid tstate");
214 if (*p == tstate)
215 break;
216 }
217 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000218 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000219 PyMem_DEL(tstate);
220}
221
222
Guido van Rossum29757862001-01-23 01:46:06 +0000223void
224PyThreadState_Delete(PyThreadState *tstate)
225{
226 if (tstate == _PyThreadState_Current)
227 Py_FatalError("PyThreadState_Delete: tstate is still current");
228 tstate_delete_common(tstate);
229}
230
231
232#ifdef WITH_THREAD
233void
234PyThreadState_DeleteCurrent()
235{
236 PyThreadState *tstate = _PyThreadState_Current;
237 if (tstate == NULL)
238 Py_FatalError(
239 "PyThreadState_DeleteCurrent: no current tstate");
240 _PyThreadState_Current = NULL;
241 tstate_delete_common(tstate);
242 PyEval_ReleaseLock();
243}
244#endif /* WITH_THREAD */
245
246
Guido van Rossuma027efa1997-05-05 20:56:21 +0000247PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000249{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000250 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000251 Py_FatalError("PyThreadState_Get: no current thread");
252
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000253 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000254}
255
256
257PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258PyThreadState_Swap(PyThreadState *new)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000259{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000260 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000261
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000262 _PyThreadState_Current = new;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000263 /* It should not be possible for more than one thread state
264 to be used for a thread. Check this the best we can in debug
265 builds.
266 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000267#if defined(Py_DEBUG) && defined(WITH_THREAD)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000268 if (new) {
269 PyThreadState *check = PyGILState_GetThisThreadState();
270 if (check && check != new)
271 Py_FatalError("Invalid thread state for this thread");
272 }
273#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000274 return old;
275}
Guido van Rossumede04391998-04-10 20:18:25 +0000276
277/* An extension mechanism to store arbitrary additional per-thread state.
278 PyThreadState_GetDict() returns a dictionary that can be used to hold such
279 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000280 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
281 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000282
283PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000284PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000285{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000286 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000287 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000288
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000289 if (_PyThreadState_Current->dict == NULL) {
290 PyObject *d;
291 _PyThreadState_Current->dict = d = PyDict_New();
292 if (d == NULL)
293 PyErr_Clear();
294 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000295 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000296}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000297
298
299/* Routines for advanced debuggers, requested by David Beazley.
300 Don't use unless you know what you are doing! */
301
302PyInterpreterState *
303PyInterpreterState_Head(void)
304{
305 return interp_head;
306}
307
308PyInterpreterState *
309PyInterpreterState_Next(PyInterpreterState *interp) {
310 return interp->next;
311}
312
313PyThreadState *
314PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
315 return interp->tstate_head;
316}
317
318PyThreadState *
319PyThreadState_Next(PyThreadState *tstate) {
320 return tstate->next;
321}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000322
323/* Python "auto thread state" API. */
324#ifdef WITH_THREAD
325
326/* Keep this as a static, as it is not reliable! It can only
327 ever be compared to the state for the *current* thread.
328 * If not equal, then it doesn't matter that the actual
329 value may change immediately after comparison, as it can't
330 possibly change to the current thread's state.
331 * If equal, then the current thread holds the lock, so the value can't
332 change until we yield the lock.
333*/
334static int
335PyThreadState_IsCurrent(PyThreadState *tstate)
336{
337 /* Must be the tstate for this thread */
338 assert(PyGILState_GetThisThreadState()==tstate);
339 /* On Windows at least, simple reads and writes to 32 bit values
340 are atomic.
341 */
342 return tstate == _PyThreadState_Current;
343}
344
345/* The single PyInterpreterState used by this process'
346 GILState implementation
347*/
348static PyInterpreterState *autoInterpreterState = NULL;
349static int autoTLSkey = 0;
350
351/* Internal initialization/finalization functions called by
352 Py_Initialize/Py_Finalize
353*/
354void _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
355{
356 assert(i && t); /* must init with a valid states */
357 autoTLSkey = PyThread_create_key();
358 autoInterpreterState = i;
359 /* Now stash the thread state for this thread in TLS */
360 PyThread_set_key_value(autoTLSkey, (void *)t);
361 assert(t->gilstate_counter==0); /* must be a new thread state */
362 t->gilstate_counter = 1;
363}
364
365void _PyGILState_Fini(void)
366{
367 PyThread_delete_key(autoTLSkey);
368 autoTLSkey = 0;
369 autoInterpreterState = NULL;;
370}
371
372/* The public functions */
373PyThreadState *PyGILState_GetThisThreadState(void)
374{
375 if (autoInterpreterState==NULL || autoTLSkey==0)
376 return NULL;
377 return (PyThreadState *) PyThread_get_key_value(autoTLSkey);
378}
379
380PyGILState_STATE PyGILState_Ensure(void)
381{
382 int current;
383 PyThreadState *tcur;
384 /* Note that we do not auto-init Python here - apart from
385 potential races with 2 threads auto-initializing, pep-311
386 spells out other issues. Embedders are expected to have
387 called Py_Initialize() and usually PyEval_InitThreads().
388 */
389 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
390 tcur = PyThread_get_key_value(autoTLSkey);
391 if (tcur==NULL) {
392 /* Create a new thread state for this thread */
393 tcur = PyThreadState_New(autoInterpreterState);
394 if (tcur==NULL)
395 Py_FatalError("Couldn't create thread-state for new thread");
396 PyThread_set_key_value(autoTLSkey, (void *)tcur);
397 current = 0; /* new thread state is never current */
398 } else
399 current = PyThreadState_IsCurrent(tcur);
400 if (!current)
401 PyEval_RestoreThread(tcur);
402 /* Update our counter in the thread-state - no need for locks:
403 - tcur will remain valid as we hold the GIL.
404 - the counter is safe as we are the only thread "allowed"
405 to modify this value
406 */
407 tcur->gilstate_counter++;
408 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
409}
410
411void PyGILState_Release(PyGILState_STATE oldstate)
412{
413 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
414 if (tcur==NULL)
415 Py_FatalError("auto-releasing thread-state, "
416 "but no thread-state for this thread");
417 /* We must hold the GIL and have our thread state current */
418 /* XXX - remove the check - the assert should be fine,
419 but while this is very new (April 2003), the extra check
420 by release-only users can't hurt.
421 */
422 if (!PyThreadState_IsCurrent(tcur))
423 Py_FatalError("This thread state must be current when releasing");
424 assert (PyThreadState_IsCurrent(tcur));
425 tcur->gilstate_counter -= 1;
426 assert (tcur->gilstate_counter >= 0); /* illegal counter value */
427
428 /* If we are about to destroy this thread-state, we must
429 clear it while the lock is held, as destructors may run
430 */
431 if (tcur->gilstate_counter==0) {
432 /* can't have been locked when we created it */
433 assert(oldstate==PyGILState_UNLOCKED);
434 PyThreadState_Clear(tcur);
435 }
436
437 /* Release the lock if necessary */
438 if (oldstate==PyGILState_UNLOCKED)
439 PyEval_ReleaseThread(tcur);
440
441 /* Now complete destruction of the thread if necessary */
442 if (tcur->gilstate_counter==0) {
443 /* Delete this thread from our TLS */
444 PyThread_delete_key_value(autoTLSkey);
445 /* Delete the thread-state */
446 PyThreadState_Delete(tcur);
447 }
448}
449#endif /* WITH_THREAD */