blob: 7584ea0d832f4259d36438d25916f395e1cfeb9f [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
Benjamin Peterson6688eb52012-04-13 11:58:27 -040025#ifdef __cplusplus
26extern "C" {
27#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +000028
Guido van Rossum1d5ad901999-06-18 14:22:24 +000029#ifdef WITH_THREAD
30#include "pythread.h"
31static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000032#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000033#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
34#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
Michael W. Hudson188d4362005-06-20 16:52:57 +000035
36/* The single PyInterpreterState used by this process'
37 GILState implementation
38*/
39static PyInterpreterState *autoInterpreterState = NULL;
40static int autoTLSkey = 0;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000041#else
42#define HEAD_INIT() /* Nothing */
43#define HEAD_LOCK() /* Nothing */
44#define HEAD_UNLOCK() /* Nothing */
45#endif
46
Guido van Rossum25ce5661997-08-02 03:10:38 +000047static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000048
Guido van Rossum18bc7c21998-12-21 18:27:28 +000049PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossum6297a7a2003-02-19 15:53:17 +000050PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000051
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000052#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000053static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000054#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000055
Guido van Rossuma027efa1997-05-05 20:56:21 +000056
57PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000058PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000059{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000060 PyInterpreterState *interp = (PyInterpreterState *)
61 malloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000062
Antoine Pitrouc83ea132010-05-09 14:46:46 +000063 if (interp != NULL) {
64 HEAD_INIT();
Neal Norwitze1fdb322006-07-21 05:32:28 +000065#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +000066 if (head_mutex == NULL)
67 Py_FatalError("Can't initialize threads for interpreter");
Neal Norwitze1fdb322006-07-21 05:32:28 +000068#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000069 interp->modules = NULL;
70 interp->modules_reloading = NULL;
71 interp->sysdict = NULL;
72 interp->builtins = NULL;
73 interp->tstate_head = NULL;
74 interp->codec_search_path = NULL;
75 interp->codec_search_cache = NULL;
76 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000077#ifdef HAVE_DLOPEN
78#ifdef RTLD_NOW
Antoine Pitrouc83ea132010-05-09 14:46:46 +000079 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000080#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000082#endif
83#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000084#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +000085 interp->tscdump = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000086#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000087
Antoine Pitrouc83ea132010-05-09 14:46:46 +000088 HEAD_LOCK();
89 interp->next = interp_head;
90 interp_head = interp;
91 HEAD_UNLOCK();
92 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000093
Antoine Pitrouc83ea132010-05-09 14:46:46 +000094 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +000095}
96
97
98void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000099PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000101 PyThreadState *p;
102 HEAD_LOCK();
103 for (p = interp->tstate_head; p != NULL; p = p->next)
104 PyThreadState_Clear(p);
105 HEAD_UNLOCK();
106 Py_CLEAR(interp->codec_search_path);
107 Py_CLEAR(interp->codec_search_cache);
108 Py_CLEAR(interp->codec_error_registry);
109 Py_CLEAR(interp->modules);
110 Py_CLEAR(interp->modules_reloading);
111 Py_CLEAR(interp->sysdict);
112 Py_CLEAR(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000113}
114
115
116static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000117zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000118{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 PyThreadState *p;
120 /* No need to lock the mutex here because this should only happen
121 when the threads are all really dead (XXX famous last words). */
122 while ((p = interp->tstate_head) != NULL) {
123 PyThreadState_Delete(p);
124 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000125}
126
127
128void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000129PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131 PyInterpreterState **p;
132 zapthreads(interp);
133 HEAD_LOCK();
134 for (p = &interp_head; ; p = &(*p)->next) {
135 if (*p == NULL)
136 Py_FatalError(
137 "PyInterpreterState_Delete: invalid interp");
138 if (*p == interp)
139 break;
140 }
141 if (interp->tstate_head != NULL)
142 Py_FatalError("PyInterpreterState_Delete: remaining threads");
143 *p = interp->next;
144 HEAD_UNLOCK();
145 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000146}
147
148
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000149/* Default implementation for _PyThreadState_GetFrame */
150static struct _frame *
151threadstate_getframe(PyThreadState *self)
152{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000153 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000154}
155
Victor Stinner71fb87e2010-03-03 23:20:25 +0000156static PyThreadState *
157new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000158{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
Tim Peters84705582004-10-10 02:47:33 +0000160
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 if (_PyThreadState_GetFrame == NULL)
162 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000164 if (tstate != NULL) {
165 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000166
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 tstate->frame = NULL;
168 tstate->recursion_depth = 0;
169 tstate->tracing = 0;
170 tstate->use_tracing = 0;
171 tstate->tick_counter = 0;
172 tstate->gilstate_counter = 0;
173 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000174#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000175 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000176#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000177 tstate->thread_id = 0;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000178#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000179
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000180 tstate->dict = NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000181
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000182 tstate->curexc_type = NULL;
183 tstate->curexc_value = NULL;
184 tstate->curexc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000185
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000186 tstate->exc_type = NULL;
187 tstate->exc_value = NULL;
188 tstate->exc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000189
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 tstate->c_profilefunc = NULL;
191 tstate->c_tracefunc = NULL;
192 tstate->c_profileobj = NULL;
193 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194
Antoine Pitrou58098a72012-09-06 00:59:49 +0200195 tstate->trash_delete_nesting = 0;
196 tstate->trash_delete_later = NULL;
197
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000198 if (init)
199 _PyThreadState_Init(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000200
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 HEAD_LOCK();
202 tstate->next = interp->tstate_head;
203 interp->tstate_head = tstate;
204 HEAD_UNLOCK();
205 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000207 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208}
209
Victor Stinner71fb87e2010-03-03 23:20:25 +0000210PyThreadState *
211PyThreadState_New(PyInterpreterState *interp)
212{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000213 return new_threadstate(interp, 1);
Victor Stinner71fb87e2010-03-03 23:20:25 +0000214}
215
216PyThreadState *
217_PyThreadState_Prealloc(PyInterpreterState *interp)
218{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000219 return new_threadstate(interp, 0);
Victor Stinner71fb87e2010-03-03 23:20:25 +0000220}
221
222void
223_PyThreadState_Init(PyThreadState *tstate)
224{
225#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000226 _PyGILState_NoteThreadState(tstate);
Victor Stinner71fb87e2010-03-03 23:20:25 +0000227#endif
228}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000229
230void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000231PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000233 if (Py_VerboseFlag && tstate->frame != NULL)
234 fprintf(stderr,
235 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000237 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 Py_CLEAR(tstate->dict);
240 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000241
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000242 Py_CLEAR(tstate->curexc_type);
243 Py_CLEAR(tstate->curexc_value);
244 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000245
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000246 Py_CLEAR(tstate->exc_type);
247 Py_CLEAR(tstate->exc_value);
248 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000249
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000250 tstate->c_profilefunc = NULL;
251 tstate->c_tracefunc = NULL;
252 Py_CLEAR(tstate->c_profileobj);
253 Py_CLEAR(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254}
255
256
Guido van Rossum29757862001-01-23 01:46:06 +0000257/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
258static void
259tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000260{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000261 PyInterpreterState *interp;
262 PyThreadState **p;
263 PyThreadState *prev_p = NULL;
264 if (tstate == NULL)
265 Py_FatalError("PyThreadState_Delete: NULL tstate");
266 interp = tstate->interp;
267 if (interp == NULL)
268 Py_FatalError("PyThreadState_Delete: NULL interp");
269 HEAD_LOCK();
270 for (p = &interp->tstate_head; ; p = &(*p)->next) {
271 if (*p == NULL)
272 Py_FatalError(
273 "PyThreadState_Delete: invalid tstate");
274 if (*p == tstate)
275 break;
276 /* Sanity check. These states should never happen but if
277 * they do we must abort. Otherwise we'll end up spinning in
278 * in a tight loop with the lock held. A similar check is done
279 * in thread.c find_key(). */
280 if (*p == prev_p)
281 Py_FatalError(
282 "PyThreadState_Delete: small circular list(!)"
283 " and tstate not found.");
284 prev_p = *p;
285 if ((*p)->next == interp->tstate_head)
286 Py_FatalError(
287 "PyThreadState_Delete: circular list(!) and"
288 " tstate not found.");
289 }
290 *p = tstate->next;
291 HEAD_UNLOCK();
292 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000293}
294
295
Guido van Rossum29757862001-01-23 01:46:06 +0000296void
297PyThreadState_Delete(PyThreadState *tstate)
298{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000299 if (tstate == _PyThreadState_Current)
300 Py_FatalError("PyThreadState_Delete: tstate is still current");
301 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000302#ifdef WITH_THREAD
Antoine Pitrou0a7b65b2010-09-08 12:40:49 +0000303 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 PyThread_delete_key_value(autoTLSkey);
Tim Petersf4e69282006-02-27 17:15:31 +0000305#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000306}
307
308
309#ifdef WITH_THREAD
310void
311PyThreadState_DeleteCurrent()
312{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000313 PyThreadState *tstate = _PyThreadState_Current;
314 if (tstate == NULL)
315 Py_FatalError(
316 "PyThreadState_DeleteCurrent: no current tstate");
317 _PyThreadState_Current = NULL;
Benjamin Peterson81669692014-06-16 23:07:49 -0700318 /*
319 Only call tstate_delete_common to have the tstate if we're not finalizing
320 or we're the main thread. The main thread will do this for us. Not calling
321 tstate_delete_common means we won't lock the interpreter head lock,
322 avoiding a possible deadlock with the GIL.
323 */
324 if (!_Py_Finalizing || _Py_Finalizing == tstate)
325 tstate_delete_common(tstate);
Antoine Pitrou0a7b65b2010-09-08 12:40:49 +0000326 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000327 PyThread_delete_key_value(autoTLSkey);
328 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000329}
330#endif /* WITH_THREAD */
331
332
Guido van Rossuma027efa1997-05-05 20:56:21 +0000333PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000334PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000335{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000336 if (_PyThreadState_Current == NULL)
337 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000338
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000339 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000340}
341
342
343PyThreadState *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000344PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000345{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 PyThreadState *oldts = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000347
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000348 _PyThreadState_Current = newts;
349 /* It should not be possible for more than one thread state
350 to be used for a thread. Check this the best we can in debug
351 builds.
352 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000353#if defined(Py_DEBUG) && defined(WITH_THREAD)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354 if (newts) {
355 /* This can be called from PyEval_RestoreThread(). Similar
356 to it, we need to ensure errno doesn't change.
357 */
358 int err = errno;
359 PyThreadState *check = PyGILState_GetThisThreadState();
360 if (check && check->interp == newts->interp && check != newts)
361 Py_FatalError("Invalid thread state for this thread");
362 errno = err;
363 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000364#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000366}
Guido van Rossumede04391998-04-10 20:18:25 +0000367
368/* An extension mechanism to store arbitrary additional per-thread state.
369 PyThreadState_GetDict() returns a dictionary that can be used to hold such
370 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000371 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
372 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000373
374PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000375PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000376{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000377 if (_PyThreadState_Current == NULL)
378 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000379
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000380 if (_PyThreadState_Current->dict == NULL) {
381 PyObject *d;
382 _PyThreadState_Current->dict = d = PyDict_New();
383 if (d == NULL)
384 PyErr_Clear();
385 }
386 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000387}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000388
389
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000390/* Asynchronously raise an exception in a thread.
391 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000392 To prevent naive misuse, you must write your own extension
Tim Peters4643c2f2006-08-10 22:45:34 +0000393 to call this, or use ctypes. Must be called with the GIL held.
394 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
395 match any known thread id). Can be called with exc=NULL to clear an
396 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000397
398int
399PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000400 PyThreadState *tstate = PyThreadState_GET();
401 PyInterpreterState *interp = tstate->interp;
402 PyThreadState *p;
Tim Peters4643c2f2006-08-10 22:45:34 +0000403
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 /* Although the GIL is held, a few C API functions can be called
405 * without the GIL held, and in particular some that create and
406 * destroy thread and interpreter states. Those can mutate the
407 * list of thread states we're traversing, so to prevent that we lock
408 * head_mutex for the duration.
409 */
410 HEAD_LOCK();
411 for (p = interp->tstate_head; p != NULL; p = p->next) {
412 if (p->thread_id == id) {
413 /* Tricky: we need to decref the current value
414 * (if any) in p->async_exc, but that can in turn
415 * allow arbitrary Python code to run, including
416 * perhaps calls to this function. To prevent
417 * deadlock, we need to release head_mutex before
418 * the decref.
419 */
420 PyObject *old_exc = p->async_exc;
421 Py_XINCREF(exc);
422 p->async_exc = exc;
423 HEAD_UNLOCK();
424 Py_XDECREF(old_exc);
425 return 1;
426 }
427 }
428 HEAD_UNLOCK();
429 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000430}
431
432
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000433/* Routines for advanced debuggers, requested by David Beazley.
434 Don't use unless you know what you are doing! */
435
436PyInterpreterState *
437PyInterpreterState_Head(void)
438{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000439 return interp_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000440}
441
442PyInterpreterState *
443PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000444 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000445}
446
447PyThreadState *
448PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000449 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000450}
451
452PyThreadState *
453PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000454 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000455}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000456
Tim Peters112aad32006-07-19 00:03:19 +0000457/* The implementation of sys._current_frames(). This is intended to be
458 called with the GIL held, as it will be when called via
459 sys._current_frames(). It's possible it would work fine even without
460 the GIL held, but haven't thought enough about that.
461*/
462PyObject *
463_PyThread_CurrentFrames(void)
464{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000465 PyObject *result;
466 PyInterpreterState *i;
Tim Peters112aad32006-07-19 00:03:19 +0000467
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 result = PyDict_New();
469 if (result == NULL)
470 return NULL;
Tim Peters112aad32006-07-19 00:03:19 +0000471
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000472 /* for i in all interpreters:
473 * for t in all of i's thread states:
474 * if t's frame isn't NULL, map t's id to its frame
Ezio Melottic2077b02011-03-16 12:34:31 +0200475 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000476 * need to grab head_mutex for the duration.
477 */
478 HEAD_LOCK();
479 for (i = interp_head; i != NULL; i = i->next) {
480 PyThreadState *t;
481 for (t = i->tstate_head; t != NULL; t = t->next) {
482 PyObject *id;
483 int stat;
484 struct _frame *frame = t->frame;
485 if (frame == NULL)
486 continue;
487 id = PyInt_FromLong(t->thread_id);
488 if (id == NULL)
489 goto Fail;
490 stat = PyDict_SetItem(result, id, (PyObject *)frame);
491 Py_DECREF(id);
492 if (stat < 0)
493 goto Fail;
494 }
495 }
496 HEAD_UNLOCK();
497 return result;
Tim Peters112aad32006-07-19 00:03:19 +0000498
499 Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000500 HEAD_UNLOCK();
501 Py_DECREF(result);
502 return NULL;
Tim Peters112aad32006-07-19 00:03:19 +0000503}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000504
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000505/* Python "auto thread state" API. */
506#ifdef WITH_THREAD
507
508/* Keep this as a static, as it is not reliable! It can only
509 ever be compared to the state for the *current* thread.
510 * If not equal, then it doesn't matter that the actual
511 value may change immediately after comparison, as it can't
512 possibly change to the current thread's state.
513 * If equal, then the current thread holds the lock, so the value can't
514 change until we yield the lock.
515*/
516static int
517PyThreadState_IsCurrent(PyThreadState *tstate)
518{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000519 /* Must be the tstate for this thread */
520 assert(PyGILState_GetThisThreadState()==tstate);
521 /* On Windows at least, simple reads and writes to 32 bit values
522 are atomic.
523 */
524 return tstate == _PyThreadState_Current;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000525}
526
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000527/* Internal initialization/finalization functions called by
528 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000529*/
Tim Peters19717fa2004-10-09 17:38:29 +0000530void
531_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000532{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000533 assert(i && t); /* must init with valid states */
534 autoTLSkey = PyThread_create_key();
535 autoInterpreterState = i;
536 assert(PyThread_get_key_value(autoTLSkey) == NULL);
537 assert(t->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000538
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000539 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000540}
541
Tim Peters19717fa2004-10-09 17:38:29 +0000542void
543_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000544{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000545 PyThread_delete_key(autoTLSkey);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000546 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000547}
548
Michael W. Hudson188d4362005-06-20 16:52:57 +0000549/* When a thread state is created for a thread by some mechanism other than
550 PyGILState_Ensure, it's important that the GILState machinery knows about
551 it so it doesn't try to create another thread state for the thread (this is
552 a better fix for SF bug #1010677 than the first one attempted).
553*/
Georg Brandlec6c2df2006-09-11 09:38:35 +0000554static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000555_PyGILState_NoteThreadState(PyThreadState* tstate)
556{
Antoine Pitrou0a7b65b2010-09-08 12:40:49 +0000557 /* If autoTLSkey isn't initialized, this must be the very first
558 threadstate created in Py_Initialize(). Don't do anything for now
559 (we'll be back here when _PyGILState_Init is called). */
560 if (!autoInterpreterState)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000561 return;
Tim Peters32a83612006-07-10 21:08:24 +0000562
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000563 /* Stick the thread state for this thread in thread local storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +0000564
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000565 The only situation where you can legitimately have more than one
566 thread state for an OS level thread is when there are multiple
567 interpreters, when:
Tim Peters32a83612006-07-10 21:08:24 +0000568
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000569 a) You shouldn't really be using the PyGILState_ APIs anyway,
570 and:
Michael W. Hudson188d4362005-06-20 16:52:57 +0000571
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000572 b) The slightly odd way PyThread_set_key_value works (see
573 comments by its implementation) means that the first thread
574 state created for that given OS level thread will "win",
575 which seems reasonable behaviour.
576 */
577 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
578 Py_FatalError("Couldn't create autoTLSkey mapping");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000579
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000580 /* PyGILState_Release must not try to delete this thread state. */
581 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000582}
583
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000584/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000585PyThreadState *
586PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000587{
Antoine Pitrou0a7b65b2010-09-08 12:40:49 +0000588 if (autoInterpreterState == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000589 return NULL;
590 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000591}
592
Tim Peters19717fa2004-10-09 17:38:29 +0000593PyGILState_STATE
594PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000595{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000596 int current;
597 PyThreadState *tcur;
598 /* Note that we do not auto-init Python here - apart from
599 potential races with 2 threads auto-initializing, pep-311
600 spells out other issues. Embedders are expected to have
601 called Py_Initialize() and usually PyEval_InitThreads().
602 */
603 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
604 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
605 if (tcur == NULL) {
606 /* Create a new thread state for this thread */
607 tcur = PyThreadState_New(autoInterpreterState);
608 if (tcur == NULL)
609 Py_FatalError("Couldn't create thread-state for new thread");
610 /* This is our thread state! We'll need to delete it in the
611 matching call to PyGILState_Release(). */
612 tcur->gilstate_counter = 0;
613 current = 0; /* new thread state is never current */
614 }
615 else
616 current = PyThreadState_IsCurrent(tcur);
617 if (current == 0)
618 PyEval_RestoreThread(tcur);
619 /* Update our counter in the thread-state - no need for locks:
620 - tcur will remain valid as we hold the GIL.
621 - the counter is safe as we are the only thread "allowed"
622 to modify this value
623 */
624 ++tcur->gilstate_counter;
625 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000626}
627
Tim Peters19717fa2004-10-09 17:38:29 +0000628void
629PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000630{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000631 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
632 autoTLSkey);
633 if (tcur == NULL)
634 Py_FatalError("auto-releasing thread-state, "
635 "but no thread-state for this thread");
636 /* We must hold the GIL and have our thread state current */
637 /* XXX - remove the check - the assert should be fine,
638 but while this is very new (April 2003), the extra check
639 by release-only users can't hurt.
640 */
641 if (! PyThreadState_IsCurrent(tcur))
642 Py_FatalError("This thread state must be current when releasing");
643 assert(PyThreadState_IsCurrent(tcur));
644 --tcur->gilstate_counter;
645 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000646
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000647 /* If we're going to destroy this thread-state, we must
648 * clear it while the GIL is held, as destructors may run.
649 */
650 if (tcur->gilstate_counter == 0) {
651 /* can't have been locked when we created it */
652 assert(oldstate == PyGILState_UNLOCKED);
653 PyThreadState_Clear(tcur);
654 /* Delete the thread-state. Note this releases the GIL too!
655 * It's vital that the GIL be held here, to avoid shutdown
656 * races; see bugs 225673 and 1061968 (that nasty bug has a
657 * habit of coming back).
658 */
659 PyThreadState_DeleteCurrent();
660 }
661 /* Release the lock if necessary */
662 else if (oldstate == PyGILState_UNLOCKED)
663 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000664}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000665
Benjamin Peterson7c0b44e2012-04-13 18:06:36 -0400666#endif /* WITH_THREAD */
667
Anthony Baxterac6bd462006-04-13 02:06:09 +0000668#ifdef __cplusplus
669}
670#endif
671
Anthony Baxterac6bd462006-04-13 02:06:09 +0000672