blob: 94792567187338ef05d31a5c0d22f08f0d41fbf3 [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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();
Thomas Wouters0e3f5912006-08-11 14:57:12 +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;
Guido van Rossumd8faa362007-04-27 19:54:29 +000071 interp->modules_reloading = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +000072 interp->modules_by_index = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000073 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000074 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000075 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000076 interp->codec_search_path = NULL;
77 interp->codec_search_cache = NULL;
78 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000079#ifdef HAVE_DLOPEN
80#ifdef RTLD_NOW
81 interp->dlopenflags = RTLD_NOW;
82#else
83 interp->dlopenflags = RTLD_LAZY;
84#endif
85#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000086#ifdef WITH_TSC
87 interp->tscdump = 0;
88#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000089
Tim Peters412f2462000-09-02 09:16:15 +000090 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000091 interp->next = interp_head;
92 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000093 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000094 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000095
Guido van Rossuma027efa1997-05-05 20:56:21 +000096 return interp;
97}
98
99
100void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000101PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000102{
103 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000104 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105 for (p = interp->tstate_head; p != NULL; p = p->next)
106 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000107 HEAD_UNLOCK();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108 Py_CLEAR(interp->codec_search_path);
109 Py_CLEAR(interp->codec_search_cache);
110 Py_CLEAR(interp->codec_error_registry);
111 Py_CLEAR(interp->modules);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000112 Py_CLEAR(interp->modules_by_index);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000113 Py_CLEAR(interp->modules_reloading);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114 Py_CLEAR(interp->sysdict);
115 Py_CLEAR(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000116}
117
118
119static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000120zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000121{
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000122 PyThreadState *p;
123 /* No need to lock the mutex here because this should only happen
124 when the threads are all really dead (XXX famous last words). */
125 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000126 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127 }
128}
129
130
131void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000132PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000133{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000134 PyInterpreterState **p;
135 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000136 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000137 for (p = &interp_head; ; p = &(*p)->next) {
138 if (*p == NULL)
139 Py_FatalError(
140 "PyInterpreterState_Delete: invalid interp");
141 if (*p == interp)
142 break;
143 }
144 if (interp->tstate_head != NULL)
145 Py_FatalError("PyInterpreterState_Delete: remaining threads");
146 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000147 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000148 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000149}
150
151
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000152/* Default implementation for _PyThreadState_GetFrame */
153static struct _frame *
154threadstate_getframe(PyThreadState *self)
155{
156 return self->frame;
157}
158
Guido van Rossuma027efa1997-05-05 20:56:21 +0000159PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000160PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000161{
Tim Peters84705582004-10-10 02:47:33 +0000162 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
163
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000164 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000165 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000166
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000168 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000169
170 tstate->frame = NULL;
171 tstate->recursion_depth = 0;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000172 tstate->overflowed = 0;
173 tstate->recursion_critical = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000174 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000175 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000176 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000177 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000178 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000179#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000180 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000181#else
182 tstate->thread_id = 0;
183#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000184
Guido van Rossumede04391998-04-10 20:18:25 +0000185 tstate->dict = NULL;
186
Guido van Rossuma027efa1997-05-05 20:56:21 +0000187 tstate->curexc_type = NULL;
188 tstate->curexc_value = NULL;
189 tstate->curexc_traceback = NULL;
190
191 tstate->exc_type = NULL;
192 tstate->exc_value = NULL;
193 tstate->exc_traceback = NULL;
194
Fred Drake5755ce62001-06-27 19:19:46 +0000195 tstate->c_profilefunc = NULL;
196 tstate->c_tracefunc = NULL;
197 tstate->c_profileobj = NULL;
198 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000199
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000200#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +0000201 _PyGILState_NoteThreadState(tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000202#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +0000203
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000204 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205 tstate->next = interp->tstate_head;
206 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000207 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209
Guido van Rossuma027efa1997-05-05 20:56:21 +0000210 return tstate;
211}
212
Martin v. Löwis1a214512008-06-11 05:26:20 +0000213PyObject*
214PyState_FindModule(struct PyModuleDef* m)
215{
216 Py_ssize_t index = m->m_base.m_index;
217 PyInterpreterState *state = PyThreadState_GET()->interp;
218 PyObject *res;
219 if (index == 0)
220 return NULL;
221 if (state->modules_by_index == NULL)
222 return NULL;
223 if (index > PyList_GET_SIZE(state->modules_by_index))
224 return NULL;
225 res = PyList_GET_ITEM(state->modules_by_index, index);
226 return res==Py_None ? NULL : res;
227}
228
229int
230_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
231{
232 PyInterpreterState *state = PyThreadState_GET()->interp;
233 if (!def)
234 return -1;
235 if (!state->modules_by_index) {
236 state->modules_by_index = PyList_New(20);
237 if (!state->modules_by_index)
238 return -1;
239 }
240 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
241 if (PyList_Append(state->modules_by_index, Py_None) < 0)
242 return -1;
243 Py_INCREF(module);
244 return PyList_SetItem(state->modules_by_index,
245 def->m_base.m_index, module);
246}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000247
248void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000251 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000252 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000253 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000256
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257 Py_CLEAR(tstate->dict);
258 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000259
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260 Py_CLEAR(tstate->curexc_type);
261 Py_CLEAR(tstate->curexc_value);
262 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000263
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264 Py_CLEAR(tstate->exc_type);
265 Py_CLEAR(tstate->exc_value);
266 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000267
Fred Drake5755ce62001-06-27 19:19:46 +0000268 tstate->c_profilefunc = NULL;
269 tstate->c_tracefunc = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000270 Py_CLEAR(tstate->c_profileobj);
271 Py_CLEAR(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000272}
273
274
Guido van Rossum29757862001-01-23 01:46:06 +0000275/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
276static void
277tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000278{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000279 PyInterpreterState *interp;
280 PyThreadState **p;
Christian Heimese1c98112008-01-21 11:20:28 +0000281 PyThreadState *prev_p = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000282 if (tstate == NULL)
283 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000284 interp = tstate->interp;
285 if (interp == NULL)
286 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000287 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000288 for (p = &interp->tstate_head; ; p = &(*p)->next) {
289 if (*p == NULL)
290 Py_FatalError(
291 "PyThreadState_Delete: invalid tstate");
292 if (*p == tstate)
293 break;
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000294 /* Sanity check. These states should never happen but if
295 * they do we must abort. Otherwise we'll end up spinning in
296 * in a tight loop with the lock held. A similar check is done
297 * in thread.c find_key(). */
Christian Heimese1c98112008-01-21 11:20:28 +0000298 if (*p == prev_p)
299 Py_FatalError(
300 "PyThreadState_Delete: small circular list(!)"
301 " and tstate not found.");
302 prev_p = *p;
303 if ((*p)->next == interp->tstate_head)
304 Py_FatalError(
305 "PyThreadState_Delete: circular list(!) and"
306 " tstate not found.");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307 }
308 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000309 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000310 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000311}
312
313
Guido van Rossum29757862001-01-23 01:46:06 +0000314void
315PyThreadState_Delete(PyThreadState *tstate)
316{
317 if (tstate == _PyThreadState_Current)
318 Py_FatalError("PyThreadState_Delete: tstate is still current");
319 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000320#ifdef WITH_THREAD
321 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
322 PyThread_delete_key_value(autoTLSkey);
323#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000324}
325
326
327#ifdef WITH_THREAD
328void
329PyThreadState_DeleteCurrent()
330{
331 PyThreadState *tstate = _PyThreadState_Current;
332 if (tstate == NULL)
333 Py_FatalError(
334 "PyThreadState_DeleteCurrent: no current tstate");
335 _PyThreadState_Current = NULL;
336 tstate_delete_common(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000337 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
338 PyThread_delete_key_value(autoTLSkey);
Guido van Rossum29757862001-01-23 01:46:06 +0000339 PyEval_ReleaseLock();
340}
341#endif /* WITH_THREAD */
342
343
Guido van Rossuma027efa1997-05-05 20:56:21 +0000344PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000346{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000347 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000348 Py_FatalError("PyThreadState_Get: no current thread");
349
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000350 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000351}
352
353
354PyThreadState *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000356{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000357 PyThreadState *oldts = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000358
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359 _PyThreadState_Current = newts;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000360 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000361 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000362 builds.
363 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000364#if defined(Py_DEBUG) && defined(WITH_THREAD)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000365 if (newts) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 /* This can be called from PyEval_RestoreThread(). Similar
367 to it, we need to ensure errno doesn't change.
368 */
369 int err = errno;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000370 PyThreadState *check = PyGILState_GetThisThreadState();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000371 if (check && check->interp == newts->interp && check != newts)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000372 Py_FatalError("Invalid thread state for this thread");
Thomas Wouters89f507f2006-12-13 04:49:30 +0000373 errno = err;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000374 }
375#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000376 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000377}
Guido van Rossumede04391998-04-10 20:18:25 +0000378
379/* An extension mechanism to store arbitrary additional per-thread state.
380 PyThreadState_GetDict() returns a dictionary that can be used to hold such
381 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000382 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
383 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000384
385PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000386PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000387{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000388 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000389 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000390
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000391 if (_PyThreadState_Current->dict == NULL) {
392 PyObject *d;
393 _PyThreadState_Current->dict = d = PyDict_New();
394 if (d == NULL)
395 PyErr_Clear();
396 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000397 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000398}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000399
400
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000401/* Asynchronously raise an exception in a thread.
402 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000403 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000404 to call this, or use ctypes. Must be called with the GIL held.
405 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
406 match any known thread id). Can be called with exc=NULL to clear an
407 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000408
409int
410PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000411 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000412 PyInterpreterState *interp = tstate->interp;
413 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000414
415 /* Although the GIL is held, a few C API functions can be called
416 * without the GIL held, and in particular some that create and
417 * destroy thread and interpreter states. Those can mutate the
418 * list of thread states we're traversing, so to prevent that we lock
419 * head_mutex for the duration.
420 */
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000421 HEAD_LOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000422 for (p = interp->tstate_head; p != NULL; p = p->next) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000423 if (p->thread_id == id) {
424 /* Tricky: we need to decref the current value
425 * (if any) in p->async_exc, but that can in turn
426 * allow arbitrary Python code to run, including
427 * perhaps calls to this function. To prevent
428 * deadlock, we need to release head_mutex before
429 * the decref.
430 */
431 PyObject *old_exc = p->async_exc;
432 Py_XINCREF(exc);
433 p->async_exc = exc;
434 HEAD_UNLOCK();
435 Py_XDECREF(old_exc);
436 return 1;
437 }
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000438 }
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000439 HEAD_UNLOCK();
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000440 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000441}
442
443
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000444/* Routines for advanced debuggers, requested by David Beazley.
445 Don't use unless you know what you are doing! */
446
447PyInterpreterState *
448PyInterpreterState_Head(void)
449{
450 return interp_head;
451}
452
453PyInterpreterState *
454PyInterpreterState_Next(PyInterpreterState *interp) {
455 return interp->next;
456}
457
458PyThreadState *
459PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
460 return interp->tstate_head;
461}
462
463PyThreadState *
464PyThreadState_Next(PyThreadState *tstate) {
465 return tstate->next;
466}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000467
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468/* The implementation of sys._current_frames(). This is intended to be
469 called with the GIL held, as it will be when called via
470 sys._current_frames(). It's possible it would work fine even without
471 the GIL held, but haven't thought enough about that.
472*/
473PyObject *
474_PyThread_CurrentFrames(void)
475{
476 PyObject *result;
477 PyInterpreterState *i;
478
479 result = PyDict_New();
480 if (result == NULL)
481 return NULL;
482
483 /* for i in all interpreters:
484 * for t in all of i's thread states:
485 * if t's frame isn't NULL, map t's id to its frame
486 * Because these lists can mutute even when the GIL is held, we
487 * need to grab head_mutex for the duration.
488 */
489 HEAD_LOCK();
490 for (i = interp_head; i != NULL; i = i->next) {
491 PyThreadState *t;
492 for (t = i->tstate_head; t != NULL; t = t->next) {
493 PyObject *id;
494 int stat;
495 struct _frame *frame = t->frame;
496 if (frame == NULL)
497 continue;
Christian Heimes217cfd12007-12-02 14:31:20 +0000498 id = PyLong_FromLong(t->thread_id);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000499 if (id == NULL)
500 goto Fail;
501 stat = PyDict_SetItem(result, id, (PyObject *)frame);
502 Py_DECREF(id);
503 if (stat < 0)
504 goto Fail;
505 }
506 }
507 HEAD_UNLOCK();
508 return result;
509
510 Fail:
511 HEAD_UNLOCK();
512 Py_DECREF(result);
513 return NULL;
514}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000515
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000516/* Python "auto thread state" API. */
517#ifdef WITH_THREAD
518
519/* Keep this as a static, as it is not reliable! It can only
520 ever be compared to the state for the *current* thread.
521 * If not equal, then it doesn't matter that the actual
522 value may change immediately after comparison, as it can't
523 possibly change to the current thread's state.
524 * If equal, then the current thread holds the lock, so the value can't
525 change until we yield the lock.
526*/
527static int
528PyThreadState_IsCurrent(PyThreadState *tstate)
529{
530 /* Must be the tstate for this thread */
531 assert(PyGILState_GetThisThreadState()==tstate);
532 /* On Windows at least, simple reads and writes to 32 bit values
533 are atomic.
534 */
535 return tstate == _PyThreadState_Current;
536}
537
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000538/* Internal initialization/finalization functions called by
539 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000540*/
Tim Peters19717fa2004-10-09 17:38:29 +0000541void
542_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000543{
Tim Peters19717fa2004-10-09 17:38:29 +0000544 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000545 autoTLSkey = PyThread_create_key();
546 autoInterpreterState = i;
Tim Petersf9becec2004-10-09 22:47:13 +0000547 assert(PyThread_get_key_value(autoTLSkey) == NULL);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000548 assert(t->gilstate_counter == 0);
549
550 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000551}
552
Tim Peters19717fa2004-10-09 17:38:29 +0000553void
554_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000555{
556 PyThread_delete_key(autoTLSkey);
557 autoTLSkey = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000558 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000559}
560
Michael W. Hudson188d4362005-06-20 16:52:57 +0000561/* When a thread state is created for a thread by some mechanism other than
562 PyGILState_Ensure, it's important that the GILState machinery knows about
563 it so it doesn't try to create another thread state for the thread (this is
564 a better fix for SF bug #1010677 than the first one attempted).
565*/
Thomas Wouters89f507f2006-12-13 04:49:30 +0000566static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000567_PyGILState_NoteThreadState(PyThreadState* tstate)
568{
569 /* If autoTLSkey is 0, this must be the very first threadstate created
570 in Py_Initialize(). Don't do anything for now (we'll be back here
571 when _PyGILState_Init is called). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000572 if (!autoTLSkey)
Michael W. Hudson188d4362005-06-20 16:52:57 +0000573 return;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000574
Michael W. Hudson188d4362005-06-20 16:52:57 +0000575 /* Stick the thread state for this thread in thread local storage.
576
577 The only situation where you can legitimately have more than one
578 thread state for an OS level thread is when there are multiple
579 interpreters, when:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000580
Michael W. Hudson188d4362005-06-20 16:52:57 +0000581 a) You shouldn't really be using the PyGILState_ APIs anyway,
582 and:
583
584 b) The slightly odd way PyThread_set_key_value works (see
585 comments by its implementation) means that the first thread
586 state created for that given OS level thread will "win",
587 which seems reasonable behaviour.
588 */
589 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
590 Py_FatalError("Couldn't create autoTLSkey mapping");
591
592 /* PyGILState_Release must not try to delete this thread state. */
593 tstate->gilstate_counter = 1;
594}
595
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000596/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000597PyThreadState *
598PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000599{
Tim Peters19717fa2004-10-09 17:38:29 +0000600 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000601 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000602 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000603}
604
Tim Peters19717fa2004-10-09 17:38:29 +0000605PyGILState_STATE
606PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000607{
608 int current;
609 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000610 /* Note that we do not auto-init Python here - apart from
611 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000612 spells out other issues. Embedders are expected to have
613 called Py_Initialize() and usually PyEval_InitThreads().
614 */
615 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000617 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000618 /* Create a new thread state for this thread */
619 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000620 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000621 Py_FatalError("Couldn't create thread-state for new thread");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000622 /* This is our thread state! We'll need to delete it in the
623 matching call to PyGILState_Release(). */
624 tcur->gilstate_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000625 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000626 }
627 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000628 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000629 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000630 PyEval_RestoreThread(tcur);
631 /* Update our counter in the thread-state - no need for locks:
632 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000633 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000634 to modify this value
635 */
Tim Peters19717fa2004-10-09 17:38:29 +0000636 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000637 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
638}
639
Tim Peters19717fa2004-10-09 17:38:29 +0000640void
641PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000642{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
644 autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000645 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000646 Py_FatalError("auto-releasing thread-state, "
647 "but no thread-state for this thread");
648 /* We must hold the GIL and have our thread state current */
649 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000650 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000651 by release-only users can't hurt.
652 */
Tim Peters19717fa2004-10-09 17:38:29 +0000653 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000654 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000655 assert(PyThreadState_IsCurrent(tcur));
656 --tcur->gilstate_counter;
657 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000658
Tim Petersfb1ffb02004-11-08 04:30:21 +0000659 /* If we're going to destroy this thread-state, we must
660 * clear it while the GIL is held, as destructors may run.
661 */
Tim Peters19717fa2004-10-09 17:38:29 +0000662 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000663 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000664 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000665 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000666 /* Delete the thread-state. Note this releases the GIL too!
667 * It's vital that the GIL be held here, to avoid shutdown
668 * races; see bugs 225673 and 1061968 (that nasty bug has a
669 * habit of coming back).
670 */
671 PyThreadState_DeleteCurrent();
Tim Peters89c0ec92004-10-10 05:30:40 +0000672 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000673 /* Release the lock if necessary */
674 else if (oldstate == PyGILState_UNLOCKED)
Michael W. Hudson774479c2005-04-18 08:46:17 +0000675 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000676}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677
678#ifdef __cplusplus
679}
680#endif
681
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000682#endif /* WITH_THREAD */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683
684