blob: f1eb6d97fac54ad31b99f078ac7b8568600bed6e [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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000061 PyInterpreterState *interp = (PyInterpreterState *)
62 malloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000063
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000064 if (interp != NULL) {
65 HEAD_INIT();
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000067 if (head_mutex == NULL)
68 Py_FatalError("Can't initialize threads for interpreter");
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000070 interp->modules = NULL;
71 interp->modules_reloading = NULL;
72 interp->modules_by_index = NULL;
73 interp->sysdict = NULL;
74 interp->builtins = NULL;
75 interp->tstate_head = NULL;
76 interp->codec_search_path = NULL;
77 interp->codec_search_cache = NULL;
78 interp->codec_error_registry = NULL;
79 interp->codecs_initialized = 0;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000080#ifdef HAVE_DLOPEN
81#ifdef RTLD_NOW
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000082 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000083#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000084 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000085#endif
86#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000087#ifdef WITH_TSC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000088 interp->tscdump = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000090
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000091 HEAD_LOCK();
92 interp->next = interp_head;
93 interp_head = interp;
94 HEAD_UNLOCK();
95 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000096
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +000098}
99
100
101void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000102PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000103{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000104 PyThreadState *p;
105 HEAD_LOCK();
106 for (p = interp->tstate_head; p != NULL; p = p->next)
107 PyThreadState_Clear(p);
108 HEAD_UNLOCK();
109 Py_CLEAR(interp->codec_search_path);
110 Py_CLEAR(interp->codec_search_cache);
111 Py_CLEAR(interp->codec_error_registry);
112 Py_CLEAR(interp->modules);
113 Py_CLEAR(interp->modules_by_index);
114 Py_CLEAR(interp->modules_reloading);
115 Py_CLEAR(interp->sysdict);
116 Py_CLEAR(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000117}
118
119
120static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000121zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000123 PyThreadState *p;
124 /* No need to lock the mutex here because this should only happen
125 when the threads are all really dead (XXX famous last words). */
126 while ((p = interp->tstate_head) != NULL) {
127 PyThreadState_Delete(p);
128 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000129}
130
131
132void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000133PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000134{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000135 PyInterpreterState **p;
136 zapthreads(interp);
137 HEAD_LOCK();
138 for (p = &interp_head; ; p = &(*p)->next) {
139 if (*p == NULL)
140 Py_FatalError(
141 "PyInterpreterState_Delete: invalid interp");
142 if (*p == interp)
143 break;
144 }
145 if (interp->tstate_head != NULL)
146 Py_FatalError("PyInterpreterState_Delete: remaining threads");
147 *p = interp->next;
148 HEAD_UNLOCK();
149 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000150}
151
152
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000153/* Default implementation for _PyThreadState_GetFrame */
154static struct _frame *
155threadstate_getframe(PyThreadState *self)
156{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000157 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000158}
159
Victor Stinneref32bc82010-03-03 23:32:07 +0000160static PyThreadState *
161new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000162{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
Tim Peters84705582004-10-10 02:47:33 +0000164
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000165 if (_PyThreadState_GetFrame == NULL)
166 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000167
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168 if (tstate != NULL) {
169 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 tstate->frame = NULL;
172 tstate->recursion_depth = 0;
173 tstate->overflowed = 0;
174 tstate->recursion_critical = 0;
175 tstate->tracing = 0;
176 tstate->use_tracing = 0;
177 tstate->tick_counter = 0;
178 tstate->gilstate_counter = 0;
179 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000180#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000181 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000182#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000183 tstate->thread_id = 0;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000184#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000185
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000186 tstate->dict = NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000187
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000188 tstate->curexc_type = NULL;
189 tstate->curexc_value = NULL;
190 tstate->curexc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000191
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000192 tstate->exc_type = NULL;
193 tstate->exc_value = NULL;
194 tstate->exc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000195
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000196 tstate->c_profilefunc = NULL;
197 tstate->c_tracefunc = NULL;
198 tstate->c_profileobj = NULL;
199 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000200
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000201 if (init)
202 _PyThreadState_Init(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000203
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000204 HEAD_LOCK();
205 tstate->next = interp->tstate_head;
206 interp->tstate_head = tstate;
207 HEAD_UNLOCK();
208 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000210 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000211}
212
Victor Stinneref32bc82010-03-03 23:32:07 +0000213PyThreadState *
214PyThreadState_New(PyInterpreterState *interp)
215{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 return new_threadstate(interp, 1);
Victor Stinneref32bc82010-03-03 23:32:07 +0000217}
218
219PyThreadState *
220_PyThreadState_Prealloc(PyInterpreterState *interp)
221{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000222 return new_threadstate(interp, 0);
Victor Stinneref32bc82010-03-03 23:32:07 +0000223}
224
225void
226_PyThreadState_Init(PyThreadState *tstate)
227{
228#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000229 _PyGILState_NoteThreadState(tstate);
Victor Stinneref32bc82010-03-03 23:32:07 +0000230#endif
231}
232
Martin v. Löwis1a214512008-06-11 05:26:20 +0000233PyObject*
234PyState_FindModule(struct PyModuleDef* m)
235{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 Py_ssize_t index = m->m_base.m_index;
237 PyInterpreterState *state = PyThreadState_GET()->interp;
238 PyObject *res;
239 if (index == 0)
240 return NULL;
241 if (state->modules_by_index == NULL)
242 return NULL;
243 if (index > PyList_GET_SIZE(state->modules_by_index))
244 return NULL;
245 res = PyList_GET_ITEM(state->modules_by_index, index);
246 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000247}
248
249int
250_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
251{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000252 PyInterpreterState *state = PyThreadState_GET()->interp;
253 if (!def)
254 return -1;
255 if (!state->modules_by_index) {
256 state->modules_by_index = PyList_New(0);
257 if (!state->modules_by_index)
258 return -1;
259 }
260 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
261 if (PyList_Append(state->modules_by_index, Py_None) < 0)
262 return -1;
263 Py_INCREF(module);
264 return PyList_SetItem(state->modules_by_index,
265 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000266}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000267
268void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000270{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000271 if (Py_VerboseFlag && tstate->frame != NULL)
272 fprintf(stderr,
273 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000274
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000275 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000276
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000277 Py_CLEAR(tstate->dict);
278 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000279
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000280 Py_CLEAR(tstate->curexc_type);
281 Py_CLEAR(tstate->curexc_value);
282 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000283
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000284 Py_CLEAR(tstate->exc_type);
285 Py_CLEAR(tstate->exc_value);
286 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000287
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000288 tstate->c_profilefunc = NULL;
289 tstate->c_tracefunc = NULL;
290 Py_CLEAR(tstate->c_profileobj);
291 Py_CLEAR(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000292}
293
294
Guido van Rossum29757862001-01-23 01:46:06 +0000295/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
296static void
297tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000298{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000299 PyInterpreterState *interp;
300 PyThreadState **p;
301 PyThreadState *prev_p = NULL;
302 if (tstate == NULL)
303 Py_FatalError("PyThreadState_Delete: NULL tstate");
304 interp = tstate->interp;
305 if (interp == NULL)
306 Py_FatalError("PyThreadState_Delete: NULL interp");
307 HEAD_LOCK();
308 for (p = &interp->tstate_head; ; p = &(*p)->next) {
309 if (*p == NULL)
310 Py_FatalError(
311 "PyThreadState_Delete: invalid tstate");
312 if (*p == tstate)
313 break;
314 /* Sanity check. These states should never happen but if
315 * they do we must abort. Otherwise we'll end up spinning in
316 * in a tight loop with the lock held. A similar check is done
317 * in thread.c find_key(). */
318 if (*p == prev_p)
319 Py_FatalError(
320 "PyThreadState_Delete: small circular list(!)"
321 " and tstate not found.");
322 prev_p = *p;
323 if ((*p)->next == interp->tstate_head)
324 Py_FatalError(
325 "PyThreadState_Delete: circular list(!) and"
326 " tstate not found.");
327 }
328 *p = tstate->next;
329 HEAD_UNLOCK();
330 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000331}
332
333
Guido van Rossum29757862001-01-23 01:46:06 +0000334void
335PyThreadState_Delete(PyThreadState *tstate)
336{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000337 if (tstate == _PyThreadState_Current)
338 Py_FatalError("PyThreadState_Delete: tstate is still current");
339 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000340#ifdef WITH_THREAD
Antoine Pitrou3c6261a2010-09-08 12:48:12 +0000341 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000342 PyThread_delete_key_value(autoTLSkey);
Tim Petersf4e69282006-02-27 17:15:31 +0000343#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000344}
345
346
347#ifdef WITH_THREAD
348void
349PyThreadState_DeleteCurrent()
350{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000351 PyThreadState *tstate = _PyThreadState_Current;
352 if (tstate == NULL)
353 Py_FatalError(
354 "PyThreadState_DeleteCurrent: no current tstate");
355 _PyThreadState_Current = NULL;
356 tstate_delete_common(tstate);
Antoine Pitrou3c6261a2010-09-08 12:48:12 +0000357 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000358 PyThread_delete_key_value(autoTLSkey);
359 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000360}
361#endif /* WITH_THREAD */
362
363
Guido van Rossuma027efa1997-05-05 20:56:21 +0000364PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000365PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000366{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000367 if (_PyThreadState_Current == NULL)
368 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000369
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000370 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000371}
372
373
374PyThreadState *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000375PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000376{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000377 PyThreadState *oldts = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000378
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000379 _PyThreadState_Current = newts;
380 /* It should not be possible for more than one thread state
381 to be used for a thread. Check this the best we can in debug
382 builds.
383 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000384#if defined(Py_DEBUG) && defined(WITH_THREAD)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000385 if (newts) {
386 /* This can be called from PyEval_RestoreThread(). Similar
387 to it, we need to ensure errno doesn't change.
388 */
389 int err = errno;
390 PyThreadState *check = PyGILState_GetThisThreadState();
391 if (check && check->interp == newts->interp && check != newts)
392 Py_FatalError("Invalid thread state for this thread");
393 errno = err;
394 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000395#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000396 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000397}
Guido van Rossumede04391998-04-10 20:18:25 +0000398
399/* An extension mechanism to store arbitrary additional per-thread state.
400 PyThreadState_GetDict() returns a dictionary that can be used to hold such
401 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000402 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
403 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000404
405PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000406PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000407{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000408 if (_PyThreadState_Current == NULL)
409 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000410
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000411 if (_PyThreadState_Current->dict == NULL) {
412 PyObject *d;
413 _PyThreadState_Current->dict = d = PyDict_New();
414 if (d == NULL)
415 PyErr_Clear();
416 }
417 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000418}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000419
420
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000421/* Asynchronously raise an exception in a thread.
422 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000423 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000424 to call this, or use ctypes. Must be called with the GIL held.
425 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
426 match any known thread id). Can be called with exc=NULL to clear an
427 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000428
429int
430PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000431 PyThreadState *tstate = PyThreadState_GET();
432 PyInterpreterState *interp = tstate->interp;
433 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000434
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000435 /* Although the GIL is held, a few C API functions can be called
436 * without the GIL held, and in particular some that create and
437 * destroy thread and interpreter states. Those can mutate the
438 * list of thread states we're traversing, so to prevent that we lock
439 * head_mutex for the duration.
440 */
441 HEAD_LOCK();
442 for (p = interp->tstate_head; p != NULL; p = p->next) {
443 if (p->thread_id == id) {
444 /* Tricky: we need to decref the current value
445 * (if any) in p->async_exc, but that can in turn
446 * allow arbitrary Python code to run, including
447 * perhaps calls to this function. To prevent
448 * deadlock, we need to release head_mutex before
449 * the decref.
450 */
451 PyObject *old_exc = p->async_exc;
452 Py_XINCREF(exc);
453 p->async_exc = exc;
454 HEAD_UNLOCK();
455 Py_XDECREF(old_exc);
456 return 1;
457 }
458 }
459 HEAD_UNLOCK();
460 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000461}
462
463
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000464/* Routines for advanced debuggers, requested by David Beazley.
465 Don't use unless you know what you are doing! */
466
467PyInterpreterState *
468PyInterpreterState_Head(void)
469{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000470 return interp_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000471}
472
473PyInterpreterState *
474PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000475 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000476}
477
478PyThreadState *
479PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000480 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000481}
482
483PyThreadState *
484PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000485 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000486}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000487
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000488/* The implementation of sys._current_frames(). This is intended to be
489 called with the GIL held, as it will be when called via
490 sys._current_frames(). It's possible it would work fine even without
491 the GIL held, but haven't thought enough about that.
492*/
493PyObject *
494_PyThread_CurrentFrames(void)
495{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000496 PyObject *result;
497 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000498
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000499 result = PyDict_New();
500 if (result == NULL)
501 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000502
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000503 /* for i in all interpreters:
504 * for t in all of i's thread states:
505 * if t's frame isn't NULL, map t's id to its frame
506 * Because these lists can mutute even when the GIL is held, we
507 * need to grab head_mutex for the duration.
508 */
509 HEAD_LOCK();
510 for (i = interp_head; i != NULL; i = i->next) {
511 PyThreadState *t;
512 for (t = i->tstate_head; t != NULL; t = t->next) {
513 PyObject *id;
514 int stat;
515 struct _frame *frame = t->frame;
516 if (frame == NULL)
517 continue;
518 id = PyLong_FromLong(t->thread_id);
519 if (id == NULL)
520 goto Fail;
521 stat = PyDict_SetItem(result, id, (PyObject *)frame);
522 Py_DECREF(id);
523 if (stat < 0)
524 goto Fail;
525 }
526 }
527 HEAD_UNLOCK();
528 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000529
530 Fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000531 HEAD_UNLOCK();
532 Py_DECREF(result);
533 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000534}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000535
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000536/* Python "auto thread state" API. */
537#ifdef WITH_THREAD
538
539/* Keep this as a static, as it is not reliable! It can only
540 ever be compared to the state for the *current* thread.
541 * If not equal, then it doesn't matter that the actual
542 value may change immediately after comparison, as it can't
543 possibly change to the current thread's state.
544 * If equal, then the current thread holds the lock, so the value can't
545 change until we yield the lock.
546*/
547static int
548PyThreadState_IsCurrent(PyThreadState *tstate)
549{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000550 /* Must be the tstate for this thread */
551 assert(PyGILState_GetThisThreadState()==tstate);
552 /* On Windows at least, simple reads and writes to 32 bit values
553 are atomic.
554 */
555 return tstate == _PyThreadState_Current;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000556}
557
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000558/* Internal initialization/finalization functions called by
559 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000560*/
Tim Peters19717fa2004-10-09 17:38:29 +0000561void
562_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000563{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000564 assert(i && t); /* must init with valid states */
565 autoTLSkey = PyThread_create_key();
566 autoInterpreterState = i;
567 assert(PyThread_get_key_value(autoTLSkey) == NULL);
568 assert(t->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000569
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000570 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000571}
572
Tim Peters19717fa2004-10-09 17:38:29 +0000573void
574_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000575{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000576 PyThread_delete_key(autoTLSkey);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000577 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000578}
579
Michael W. Hudson188d4362005-06-20 16:52:57 +0000580/* When a thread state is created for a thread by some mechanism other than
581 PyGILState_Ensure, it's important that the GILState machinery knows about
582 it so it doesn't try to create another thread state for the thread (this is
583 a better fix for SF bug #1010677 than the first one attempted).
584*/
Thomas Wouters89f507f2006-12-13 04:49:30 +0000585static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000586_PyGILState_NoteThreadState(PyThreadState* tstate)
587{
Antoine Pitrou3c6261a2010-09-08 12:48:12 +0000588 /* If autoTLSkey isn't initialized, this must be the very first
589 threadstate created in Py_Initialize(). Don't do anything for now
590 (we'll be back here when _PyGILState_Init is called). */
591 if (!autoInterpreterState)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000592 return;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000594 /* Stick the thread state for this thread in thread local storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +0000595
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000596 The only situation where you can legitimately have more than one
597 thread state for an OS level thread is when there are multiple
598 interpreters, when:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000599
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000600 a) You shouldn't really be using the PyGILState_ APIs anyway,
601 and:
Michael W. Hudson188d4362005-06-20 16:52:57 +0000602
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000603 b) The slightly odd way PyThread_set_key_value works (see
604 comments by its implementation) means that the first thread
605 state created for that given OS level thread will "win",
606 which seems reasonable behaviour.
607 */
608 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
609 Py_FatalError("Couldn't create autoTLSkey mapping");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000610
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000611 /* PyGILState_Release must not try to delete this thread state. */
612 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000613}
614
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000615/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000616PyThreadState *
617PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000618{
Antoine Pitrou3c6261a2010-09-08 12:48:12 +0000619 if (autoInterpreterState == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000620 return NULL;
621 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000622}
623
Tim Peters19717fa2004-10-09 17:38:29 +0000624PyGILState_STATE
625PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000626{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000627 int current;
628 PyThreadState *tcur;
629 /* Note that we do not auto-init Python here - apart from
630 potential races with 2 threads auto-initializing, pep-311
631 spells out other issues. Embedders are expected to have
632 called Py_Initialize() and usually PyEval_InitThreads().
633 */
634 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
635 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
636 if (tcur == NULL) {
637 /* Create a new thread state for this thread */
638 tcur = PyThreadState_New(autoInterpreterState);
639 if (tcur == NULL)
640 Py_FatalError("Couldn't create thread-state for new thread");
641 /* This is our thread state! We'll need to delete it in the
642 matching call to PyGILState_Release(). */
643 tcur->gilstate_counter = 0;
644 current = 0; /* new thread state is never current */
645 }
646 else
647 current = PyThreadState_IsCurrent(tcur);
648 if (current == 0)
649 PyEval_RestoreThread(tcur);
650 /* Update our counter in the thread-state - no need for locks:
651 - tcur will remain valid as we hold the GIL.
652 - the counter is safe as we are the only thread "allowed"
653 to modify this value
654 */
655 ++tcur->gilstate_counter;
656 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000657}
658
Tim Peters19717fa2004-10-09 17:38:29 +0000659void
660PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000661{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
663 autoTLSkey);
664 if (tcur == NULL)
665 Py_FatalError("auto-releasing thread-state, "
666 "but no thread-state for this thread");
667 /* We must hold the GIL and have our thread state current */
668 /* XXX - remove the check - the assert should be fine,
669 but while this is very new (April 2003), the extra check
670 by release-only users can't hurt.
671 */
672 if (! PyThreadState_IsCurrent(tcur))
673 Py_FatalError("This thread state must be current when releasing");
674 assert(PyThreadState_IsCurrent(tcur));
675 --tcur->gilstate_counter;
676 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000677
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000678 /* If we're going to destroy this thread-state, we must
679 * clear it while the GIL is held, as destructors may run.
680 */
681 if (tcur->gilstate_counter == 0) {
682 /* can't have been locked when we created it */
683 assert(oldstate == PyGILState_UNLOCKED);
684 PyThreadState_Clear(tcur);
685 /* Delete the thread-state. Note this releases the GIL too!
686 * It's vital that the GIL be held here, to avoid shutdown
687 * races; see bugs 225673 and 1061968 (that nasty bug has a
688 * habit of coming back).
689 */
690 PyThreadState_DeleteCurrent();
691 }
692 /* Release the lock if necessary */
693 else if (oldstate == PyGILState_UNLOCKED)
694 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000695}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000696
697#ifdef __cplusplus
698}
699#endif
700
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000701#endif /* WITH_THREAD */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000702
703