blob: 36b06d67af7c2bcb7a2623403102ed456ff96e65 [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
Tim Peters84705582004-10-10 02:47:33 +00006/* --------------------------------------------------------------------------
7CAUTION
8
9Always use malloc() and free() directly in this file. A number of these
10functions are advertised as safe to call when the GIL isn't held, and in
11a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging
12obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid
13the expense of doing their own locking).
14-------------------------------------------------------------------------- */
15
Martin v. Löwisf0473d52001-07-18 16:17:16 +000016#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
24
25
Guido van Rossum1d5ad901999-06-18 14:22:24 +000026#ifdef WITH_THREAD
27#include "pythread.h"
28static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000029#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000030#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
31#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
Michael W. Hudson188d4362005-06-20 16:52:57 +000032
Anthony Baxterac6bd462006-04-13 02:06:09 +000033#ifdef __cplusplus
34extern "C" {
35#endif
36
Michael W. Hudson188d4362005-06-20 16:52:57 +000037/* The single PyInterpreterState used by this process'
38 GILState implementation
39*/
40static PyInterpreterState *autoInterpreterState = NULL;
41static int autoTLSkey = 0;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000042#else
43#define HEAD_INIT() /* Nothing */
44#define HEAD_LOCK() /* Nothing */
45#define HEAD_UNLOCK() /* Nothing */
46#endif
47
Guido van Rossum25ce5661997-08-02 03:10:38 +000048static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000049
Guido van Rossum18bc7c21998-12-21 18:27:28 +000050PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossum6297a7a2003-02-19 15:53:17 +000051PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000052
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000053#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000054static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000055#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000056
Guido van Rossuma027efa1997-05-05 20:56:21 +000057
58PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000059PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000060{
Tim Peters84705582004-10-10 02:47:33 +000061 PyInterpreterState *interp = (PyInterpreterState *)
62 malloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000063
Guido van Rossuma027efa1997-05-05 20:56:21 +000064 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000065 HEAD_INIT();
Neal Norwitze1fdb322006-07-21 05:32:28 +000066#ifdef WITH_THREAD
67 if (head_mutex == NULL)
68 Py_FatalError("Can't initialize threads for interpreter");
69#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000070 interp->modules = NULL;
Collin Winter276887b2007-03-12 16:11:39 +000071 interp->modules_reloading = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000072 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000073 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000074 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000075 interp->codec_search_path = NULL;
76 interp->codec_search_cache = NULL;
77 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000078#ifdef HAVE_DLOPEN
79#ifdef RTLD_NOW
80 interp->dlopenflags = RTLD_NOW;
81#else
82 interp->dlopenflags = RTLD_LAZY;
83#endif
84#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000085#ifdef WITH_TSC
86 interp->tscdump = 0;
87#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000088
Tim Peters412f2462000-09-02 09:16:15 +000089 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000090 interp->next = interp_head;
91 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000092 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000093 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000094
Guido van Rossuma027efa1997-05-05 20:56:21 +000095 return interp;
96}
97
98
99void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000100PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000101{
102 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000103 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000104 for (p = interp->tstate_head; p != NULL; p = p->next)
105 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000106 HEAD_UNLOCK();
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000107 Py_CLEAR(interp->codec_search_path);
108 Py_CLEAR(interp->codec_search_cache);
109 Py_CLEAR(interp->codec_error_registry);
110 Py_CLEAR(interp->modules);
Collin Winter276887b2007-03-12 16:11:39 +0000111 Py_CLEAR(interp->modules_reloading);
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000112 Py_CLEAR(interp->sysdict);
113 Py_CLEAR(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000114}
115
116
117static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000118zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000119{
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000120 PyThreadState *p;
121 /* No need to lock the mutex here because this should only happen
122 when the threads are all really dead (XXX famous last words). */
123 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000124 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000125 }
126}
127
128
129void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000130PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132 PyInterpreterState **p;
133 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000134 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000135 for (p = &interp_head; ; p = &(*p)->next) {
136 if (*p == NULL)
137 Py_FatalError(
138 "PyInterpreterState_Delete: invalid interp");
139 if (*p == interp)
140 break;
141 }
142 if (interp->tstate_head != NULL)
143 Py_FatalError("PyInterpreterState_Delete: remaining threads");
144 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000145 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000146 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000147}
148
149
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000150/* Default implementation for _PyThreadState_GetFrame */
151static struct _frame *
152threadstate_getframe(PyThreadState *self)
153{
154 return self->frame;
155}
156
Guido van Rossuma027efa1997-05-05 20:56:21 +0000157PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000158PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000159{
Tim Peters84705582004-10-10 02:47:33 +0000160 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
161
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000162 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000163 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000164
Guido van Rossuma027efa1997-05-05 20:56:21 +0000165 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000166 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167
168 tstate->frame = NULL;
169 tstate->recursion_depth = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000171 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000172 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000173 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000174 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000175#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000176 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000177#else
178 tstate->thread_id = 0;
179#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180
Guido van Rossumede04391998-04-10 20:18:25 +0000181 tstate->dict = NULL;
182
Guido van Rossuma027efa1997-05-05 20:56:21 +0000183 tstate->curexc_type = NULL;
184 tstate->curexc_value = NULL;
185 tstate->curexc_traceback = NULL;
186
187 tstate->exc_type = NULL;
188 tstate->exc_value = NULL;
189 tstate->exc_traceback = NULL;
190
Fred Drake5755ce62001-06-27 19:19:46 +0000191 tstate->c_profilefunc = NULL;
192 tstate->c_tracefunc = NULL;
193 tstate->c_profileobj = NULL;
194 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000195
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000196#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +0000197 _PyGILState_NoteThreadState(tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000198#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +0000199
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000200 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000201 tstate->next = interp->tstate_head;
202 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000203 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000204 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205
Guido van Rossuma027efa1997-05-05 20:56:21 +0000206 return tstate;
207}
208
209
210void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000211PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000213 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000214 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000215 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000216
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000217 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000218
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000219 Py_CLEAR(tstate->dict);
220 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000221
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000222 Py_CLEAR(tstate->curexc_type);
223 Py_CLEAR(tstate->curexc_value);
224 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000225
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000226 Py_CLEAR(tstate->exc_type);
227 Py_CLEAR(tstate->exc_value);
228 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229
Fred Drake5755ce62001-06-27 19:19:46 +0000230 tstate->c_profilefunc = NULL;
231 tstate->c_tracefunc = NULL;
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000232 Py_CLEAR(tstate->c_profileobj);
233 Py_CLEAR(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234}
235
236
Guido van Rossum29757862001-01-23 01:46:06 +0000237/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
238static void
239tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000240{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000241 PyInterpreterState *interp;
242 PyThreadState **p;
Gregory P. Smith2778c992008-01-21 07:11:11 +0000243 PyThreadState *prev_p = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000244 if (tstate == NULL)
245 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000246 interp = tstate->interp;
247 if (interp == NULL)
248 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000249 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250 for (p = &interp->tstate_head; ; p = &(*p)->next) {
251 if (*p == NULL)
252 Py_FatalError(
253 "PyThreadState_Delete: invalid tstate");
254 if (*p == tstate)
255 break;
Gregory P. Smith2778c992008-01-21 07:11:11 +0000256 if (*p == prev_p)
257 Py_FatalError(
258 "PyThreadState_Delete: small circular list(!)"
259 " and tstate not found.");
260 prev_p = *p;
261 if ((*p)->next == interp->tstate_head)
262 Py_FatalError(
263 "PyThreadState_Delete: circular list(!) and"
264 " tstate not found.");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000265 }
266 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000267 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000268 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000269}
270
271
Guido van Rossum29757862001-01-23 01:46:06 +0000272void
273PyThreadState_Delete(PyThreadState *tstate)
274{
275 if (tstate == _PyThreadState_Current)
276 Py_FatalError("PyThreadState_Delete: tstate is still current");
277 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000278#ifdef WITH_THREAD
279 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
280 PyThread_delete_key_value(autoTLSkey);
281#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000282}
283
284
285#ifdef WITH_THREAD
286void
287PyThreadState_DeleteCurrent()
288{
289 PyThreadState *tstate = _PyThreadState_Current;
290 if (tstate == NULL)
291 Py_FatalError(
292 "PyThreadState_DeleteCurrent: no current tstate");
293 _PyThreadState_Current = NULL;
294 tstate_delete_common(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000295 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
296 PyThread_delete_key_value(autoTLSkey);
Guido van Rossum29757862001-01-23 01:46:06 +0000297 PyEval_ReleaseLock();
298}
299#endif /* WITH_THREAD */
300
301
Guido van Rossuma027efa1997-05-05 20:56:21 +0000302PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000304{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000305 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000306 Py_FatalError("PyThreadState_Get: no current thread");
307
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000308 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000309}
310
311
312PyThreadState *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000313PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000314{
Anthony Baxter7b782b62006-04-11 12:01:56 +0000315 PyThreadState *oldts = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000316
Anthony Baxter7b782b62006-04-11 12:01:56 +0000317 _PyThreadState_Current = newts;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000318 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000319 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000320 builds.
321 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000322#if defined(Py_DEBUG) && defined(WITH_THREAD)
Anthony Baxter7b782b62006-04-11 12:01:56 +0000323 if (newts) {
Georg Brandlec6c2df2006-09-11 09:38:35 +0000324 /* This can be called from PyEval_RestoreThread(). Similar
325 to it, we need to ensure errno doesn't change.
326 */
327 int err = errno;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000328 PyThreadState *check = PyGILState_GetThisThreadState();
Anthony Baxter7b782b62006-04-11 12:01:56 +0000329 if (check && check->interp == newts->interp && check != newts)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000330 Py_FatalError("Invalid thread state for this thread");
Georg Brandlec6c2df2006-09-11 09:38:35 +0000331 errno = err;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000332 }
333#endif
Anthony Baxter7b782b62006-04-11 12:01:56 +0000334 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000335}
Guido van Rossumede04391998-04-10 20:18:25 +0000336
337/* An extension mechanism to store arbitrary additional per-thread state.
338 PyThreadState_GetDict() returns a dictionary that can be used to hold such
339 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000340 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
341 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000342
343PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000344PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000345{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000346 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000347 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000348
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000349 if (_PyThreadState_Current->dict == NULL) {
350 PyObject *d;
351 _PyThreadState_Current->dict = d = PyDict_New();
352 if (d == NULL)
353 PyErr_Clear();
354 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000355 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000356}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000357
358
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000359/* Asynchronously raise an exception in a thread.
360 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000361 To prevent naive misuse, you must write your own extension
Tim Peters4643c2f2006-08-10 22:45:34 +0000362 to call this, or use ctypes. Must be called with the GIL held.
363 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
364 match any known thread id). Can be called with exc=NULL to clear an
365 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000366
367int
368PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000369 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000370 PyInterpreterState *interp = tstate->interp;
371 PyThreadState *p;
Tim Peters4643c2f2006-08-10 22:45:34 +0000372
373 /* Although the GIL is held, a few C API functions can be called
374 * without the GIL held, and in particular some that create and
375 * destroy thread and interpreter states. Those can mutate the
376 * list of thread states we're traversing, so to prevent that we lock
377 * head_mutex for the duration.
378 */
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000379 HEAD_LOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000380 for (p = interp->tstate_head; p != NULL; p = p->next) {
Tim Peters4643c2f2006-08-10 22:45:34 +0000381 if (p->thread_id == id) {
382 /* Tricky: we need to decref the current value
383 * (if any) in p->async_exc, but that can in turn
384 * allow arbitrary Python code to run, including
385 * perhaps calls to this function. To prevent
386 * deadlock, we need to release head_mutex before
387 * the decref.
388 */
389 PyObject *old_exc = p->async_exc;
390 Py_XINCREF(exc);
391 p->async_exc = exc;
392 HEAD_UNLOCK();
393 Py_XDECREF(old_exc);
394 return 1;
395 }
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000396 }
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000397 HEAD_UNLOCK();
Tim Peters4643c2f2006-08-10 22:45:34 +0000398 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000399}
400
401
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000402/* Routines for advanced debuggers, requested by David Beazley.
403 Don't use unless you know what you are doing! */
404
405PyInterpreterState *
406PyInterpreterState_Head(void)
407{
408 return interp_head;
409}
410
411PyInterpreterState *
412PyInterpreterState_Next(PyInterpreterState *interp) {
413 return interp->next;
414}
415
416PyThreadState *
417PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
418 return interp->tstate_head;
419}
420
421PyThreadState *
422PyThreadState_Next(PyThreadState *tstate) {
423 return tstate->next;
424}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000425
Tim Peters112aad32006-07-19 00:03:19 +0000426/* The implementation of sys._current_frames(). This is intended to be
427 called with the GIL held, as it will be when called via
428 sys._current_frames(). It's possible it would work fine even without
429 the GIL held, but haven't thought enough about that.
430*/
431PyObject *
432_PyThread_CurrentFrames(void)
433{
434 PyObject *result;
435 PyInterpreterState *i;
436
437 result = PyDict_New();
438 if (result == NULL)
439 return NULL;
440
441 /* for i in all interpreters:
442 * for t in all of i's thread states:
443 * if t's frame isn't NULL, map t's id to its frame
444 * Because these lists can mutute even when the GIL is held, we
445 * need to grab head_mutex for the duration.
446 */
447 HEAD_LOCK();
448 for (i = interp_head; i != NULL; i = i->next) {
449 PyThreadState *t;
450 for (t = i->tstate_head; t != NULL; t = t->next) {
451 PyObject *id;
452 int stat;
453 struct _frame *frame = t->frame;
454 if (frame == NULL)
455 continue;
456 id = PyInt_FromLong(t->thread_id);
457 if (id == NULL)
458 goto Fail;
459 stat = PyDict_SetItem(result, id, (PyObject *)frame);
460 Py_DECREF(id);
461 if (stat < 0)
462 goto Fail;
463 }
464 }
465 HEAD_UNLOCK();
466 return result;
467
468 Fail:
469 HEAD_UNLOCK();
470 Py_DECREF(result);
471 return NULL;
472}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000473
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000474/* Python "auto thread state" API. */
475#ifdef WITH_THREAD
476
477/* Keep this as a static, as it is not reliable! It can only
478 ever be compared to the state for the *current* thread.
479 * If not equal, then it doesn't matter that the actual
480 value may change immediately after comparison, as it can't
481 possibly change to the current thread's state.
482 * If equal, then the current thread holds the lock, so the value can't
483 change until we yield the lock.
484*/
485static int
486PyThreadState_IsCurrent(PyThreadState *tstate)
487{
488 /* Must be the tstate for this thread */
489 assert(PyGILState_GetThisThreadState()==tstate);
490 /* On Windows at least, simple reads and writes to 32 bit values
491 are atomic.
492 */
493 return tstate == _PyThreadState_Current;
494}
495
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000496/* Internal initialization/finalization functions called by
497 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000498*/
Tim Peters19717fa2004-10-09 17:38:29 +0000499void
500_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000501{
Tim Peters19717fa2004-10-09 17:38:29 +0000502 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000503 autoTLSkey = PyThread_create_key();
504 autoInterpreterState = i;
Tim Petersf9becec2004-10-09 22:47:13 +0000505 assert(PyThread_get_key_value(autoTLSkey) == NULL);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000506 assert(t->gilstate_counter == 0);
507
508 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000509}
510
Tim Peters19717fa2004-10-09 17:38:29 +0000511void
512_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000513{
514 PyThread_delete_key(autoTLSkey);
515 autoTLSkey = 0;
Neal Norwitzd3f91902006-09-23 04:11:38 +0000516 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000517}
518
Michael W. Hudson188d4362005-06-20 16:52:57 +0000519/* When a thread state is created for a thread by some mechanism other than
520 PyGILState_Ensure, it's important that the GILState machinery knows about
521 it so it doesn't try to create another thread state for the thread (this is
522 a better fix for SF bug #1010677 than the first one attempted).
523*/
Georg Brandlec6c2df2006-09-11 09:38:35 +0000524static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000525_PyGILState_NoteThreadState(PyThreadState* tstate)
526{
527 /* If autoTLSkey is 0, this must be the very first threadstate created
528 in Py_Initialize(). Don't do anything for now (we'll be back here
529 when _PyGILState_Init is called). */
Tim Peters32a83612006-07-10 21:08:24 +0000530 if (!autoTLSkey)
Michael W. Hudson188d4362005-06-20 16:52:57 +0000531 return;
Tim Peters32a83612006-07-10 21:08:24 +0000532
Michael W. Hudson188d4362005-06-20 16:52:57 +0000533 /* Stick the thread state for this thread in thread local storage.
534
535 The only situation where you can legitimately have more than one
536 thread state for an OS level thread is when there are multiple
537 interpreters, when:
Tim Peters32a83612006-07-10 21:08:24 +0000538
Michael W. Hudson188d4362005-06-20 16:52:57 +0000539 a) You shouldn't really be using the PyGILState_ APIs anyway,
540 and:
541
542 b) The slightly odd way PyThread_set_key_value works (see
543 comments by its implementation) means that the first thread
544 state created for that given OS level thread will "win",
545 which seems reasonable behaviour.
546 */
547 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
548 Py_FatalError("Couldn't create autoTLSkey mapping");
549
550 /* PyGILState_Release must not try to delete this thread state. */
551 tstate->gilstate_counter = 1;
552}
553
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000554/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000555PyThreadState *
556PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000557{
Tim Peters19717fa2004-10-09 17:38:29 +0000558 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000559 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000560 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000561}
562
Tim Peters19717fa2004-10-09 17:38:29 +0000563PyGILState_STATE
564PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000565{
566 int current;
567 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000568 /* Note that we do not auto-init Python here - apart from
569 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000570 spells out other issues. Embedders are expected to have
571 called Py_Initialize() and usually PyEval_InitThreads().
572 */
573 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
Anthony Baxter7b782b62006-04-11 12:01:56 +0000574 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000575 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000576 /* Create a new thread state for this thread */
577 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000578 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000579 Py_FatalError("Couldn't create thread-state for new thread");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000580 /* This is our thread state! We'll need to delete it in the
581 matching call to PyGILState_Release(). */
582 tcur->gilstate_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000583 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000584 }
585 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000586 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000587 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000588 PyEval_RestoreThread(tcur);
589 /* Update our counter in the thread-state - no need for locks:
590 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000591 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000592 to modify this value
593 */
Tim Peters19717fa2004-10-09 17:38:29 +0000594 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000595 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
596}
597
Tim Peters19717fa2004-10-09 17:38:29 +0000598void
599PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000600{
Anthony Baxter7b782b62006-04-11 12:01:56 +0000601 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
602 autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000603 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000604 Py_FatalError("auto-releasing thread-state, "
605 "but no thread-state for this thread");
606 /* We must hold the GIL and have our thread state current */
607 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000608 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000609 by release-only users can't hurt.
610 */
Tim Peters19717fa2004-10-09 17:38:29 +0000611 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000612 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000613 assert(PyThreadState_IsCurrent(tcur));
614 --tcur->gilstate_counter;
615 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000616
Tim Petersfb1ffb02004-11-08 04:30:21 +0000617 /* If we're going to destroy this thread-state, we must
618 * clear it while the GIL is held, as destructors may run.
619 */
Tim Peters19717fa2004-10-09 17:38:29 +0000620 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000621 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000622 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000623 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000624 /* Delete the thread-state. Note this releases the GIL too!
625 * It's vital that the GIL be held here, to avoid shutdown
626 * races; see bugs 225673 and 1061968 (that nasty bug has a
627 * habit of coming back).
628 */
629 PyThreadState_DeleteCurrent();
Tim Peters89c0ec92004-10-10 05:30:40 +0000630 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000631 /* Release the lock if necessary */
632 else if (oldstate == PyGILState_UNLOCKED)
Michael W. Hudson774479c2005-04-18 08:46:17 +0000633 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000634}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000635
636#ifdef __cplusplus
637}
638#endif
639
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000640#endif /* WITH_THREAD */
Anthony Baxterac6bd462006-04-13 02:06:09 +0000641
642