blob: d7d127b2559e895136499b5672892311e67a9e28 [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 Pitrouc83ea132010-05-09 14:46:46 +0000195 if (init)
196 _PyThreadState_Init(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000197
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000198 HEAD_LOCK();
199 tstate->next = interp->tstate_head;
200 interp->tstate_head = tstate;
201 HEAD_UNLOCK();
202 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000204 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000205}
206
Victor Stinner71fb87e2010-03-03 23:20:25 +0000207PyThreadState *
208PyThreadState_New(PyInterpreterState *interp)
209{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000210 return new_threadstate(interp, 1);
Victor Stinner71fb87e2010-03-03 23:20:25 +0000211}
212
213PyThreadState *
214_PyThreadState_Prealloc(PyInterpreterState *interp)
215{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000216 return new_threadstate(interp, 0);
Victor Stinner71fb87e2010-03-03 23:20:25 +0000217}
218
219void
220_PyThreadState_Init(PyThreadState *tstate)
221{
222#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 _PyGILState_NoteThreadState(tstate);
Victor Stinner71fb87e2010-03-03 23:20:25 +0000224#endif
225}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000226
227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000230 if (Py_VerboseFlag && tstate->frame != NULL)
231 fprintf(stderr,
232 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000233
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000234 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000236 Py_CLEAR(tstate->dict);
237 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000238
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 Py_CLEAR(tstate->curexc_type);
240 Py_CLEAR(tstate->curexc_value);
241 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 Py_CLEAR(tstate->exc_type);
244 Py_CLEAR(tstate->exc_value);
245 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000246
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 tstate->c_profilefunc = NULL;
248 tstate->c_tracefunc = NULL;
249 Py_CLEAR(tstate->c_profileobj);
250 Py_CLEAR(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000251}
252
253
Guido van Rossum29757862001-01-23 01:46:06 +0000254/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
255static void
256tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000257{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000258 PyInterpreterState *interp;
259 PyThreadState **p;
260 PyThreadState *prev_p = NULL;
261 if (tstate == NULL)
262 Py_FatalError("PyThreadState_Delete: NULL tstate");
263 interp = tstate->interp;
264 if (interp == NULL)
265 Py_FatalError("PyThreadState_Delete: NULL interp");
266 HEAD_LOCK();
267 for (p = &interp->tstate_head; ; p = &(*p)->next) {
268 if (*p == NULL)
269 Py_FatalError(
270 "PyThreadState_Delete: invalid tstate");
271 if (*p == tstate)
272 break;
273 /* Sanity check. These states should never happen but if
274 * they do we must abort. Otherwise we'll end up spinning in
275 * in a tight loop with the lock held. A similar check is done
276 * in thread.c find_key(). */
277 if (*p == prev_p)
278 Py_FatalError(
279 "PyThreadState_Delete: small circular list(!)"
280 " and tstate not found.");
281 prev_p = *p;
282 if ((*p)->next == interp->tstate_head)
283 Py_FatalError(
284 "PyThreadState_Delete: circular list(!) and"
285 " tstate not found.");
286 }
287 *p = tstate->next;
288 HEAD_UNLOCK();
289 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000290}
291
292
Guido van Rossum29757862001-01-23 01:46:06 +0000293void
294PyThreadState_Delete(PyThreadState *tstate)
295{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000296 if (tstate == _PyThreadState_Current)
297 Py_FatalError("PyThreadState_Delete: tstate is still current");
298 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000299#ifdef WITH_THREAD
Antoine Pitrou0a7b65b2010-09-08 12:40:49 +0000300 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 PyThread_delete_key_value(autoTLSkey);
Tim Petersf4e69282006-02-27 17:15:31 +0000302#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000303}
304
305
306#ifdef WITH_THREAD
307void
308PyThreadState_DeleteCurrent()
309{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000310 PyThreadState *tstate = _PyThreadState_Current;
311 if (tstate == NULL)
312 Py_FatalError(
313 "PyThreadState_DeleteCurrent: no current tstate");
314 _PyThreadState_Current = NULL;
315 tstate_delete_common(tstate);
Antoine Pitrou0a7b65b2010-09-08 12:40:49 +0000316 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000317 PyThread_delete_key_value(autoTLSkey);
318 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000319}
320#endif /* WITH_THREAD */
321
322
Guido van Rossuma027efa1997-05-05 20:56:21 +0000323PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000324PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000325{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000326 if (_PyThreadState_Current == NULL)
327 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000328
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000329 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000330}
331
332
333PyThreadState *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000334PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000335{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000336 PyThreadState *oldts = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000337
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000338 _PyThreadState_Current = newts;
339 /* It should not be possible for more than one thread state
340 to be used for a thread. Check this the best we can in debug
341 builds.
342 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000343#if defined(Py_DEBUG) && defined(WITH_THREAD)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 if (newts) {
345 /* This can be called from PyEval_RestoreThread(). Similar
346 to it, we need to ensure errno doesn't change.
347 */
348 int err = errno;
349 PyThreadState *check = PyGILState_GetThisThreadState();
350 if (check && check->interp == newts->interp && check != newts)
351 Py_FatalError("Invalid thread state for this thread");
352 errno = err;
353 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000354#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000355 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000356}
Guido van Rossumede04391998-04-10 20:18:25 +0000357
358/* An extension mechanism to store arbitrary additional per-thread state.
359 PyThreadState_GetDict() returns a dictionary that can be used to hold such
360 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000361 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
362 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000363
364PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000365PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000366{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000367 if (_PyThreadState_Current == NULL)
368 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000369
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 if (_PyThreadState_Current->dict == NULL) {
371 PyObject *d;
372 _PyThreadState_Current->dict = d = PyDict_New();
373 if (d == NULL)
374 PyErr_Clear();
375 }
376 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000377}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000378
379
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000380/* Asynchronously raise an exception in a thread.
381 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000382 To prevent naive misuse, you must write your own extension
Tim Peters4643c2f2006-08-10 22:45:34 +0000383 to call this, or use ctypes. Must be called with the GIL held.
384 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
385 match any known thread id). Can be called with exc=NULL to clear an
386 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000387
388int
389PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000390 PyThreadState *tstate = PyThreadState_GET();
391 PyInterpreterState *interp = tstate->interp;
392 PyThreadState *p;
Tim Peters4643c2f2006-08-10 22:45:34 +0000393
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000394 /* Although the GIL is held, a few C API functions can be called
395 * without the GIL held, and in particular some that create and
396 * destroy thread and interpreter states. Those can mutate the
397 * list of thread states we're traversing, so to prevent that we lock
398 * head_mutex for the duration.
399 */
400 HEAD_LOCK();
401 for (p = interp->tstate_head; p != NULL; p = p->next) {
402 if (p->thread_id == id) {
403 /* Tricky: we need to decref the current value
404 * (if any) in p->async_exc, but that can in turn
405 * allow arbitrary Python code to run, including
406 * perhaps calls to this function. To prevent
407 * deadlock, we need to release head_mutex before
408 * the decref.
409 */
410 PyObject *old_exc = p->async_exc;
411 Py_XINCREF(exc);
412 p->async_exc = exc;
413 HEAD_UNLOCK();
414 Py_XDECREF(old_exc);
415 return 1;
416 }
417 }
418 HEAD_UNLOCK();
419 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000420}
421
422
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000423/* Routines for advanced debuggers, requested by David Beazley.
424 Don't use unless you know what you are doing! */
425
426PyInterpreterState *
427PyInterpreterState_Head(void)
428{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000429 return interp_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000430}
431
432PyInterpreterState *
433PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000434 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000435}
436
437PyThreadState *
438PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000439 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000440}
441
442PyThreadState *
443PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000444 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000445}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000446
Tim Peters112aad32006-07-19 00:03:19 +0000447/* The implementation of sys._current_frames(). This is intended to be
448 called with the GIL held, as it will be when called via
449 sys._current_frames(). It's possible it would work fine even without
450 the GIL held, but haven't thought enough about that.
451*/
452PyObject *
453_PyThread_CurrentFrames(void)
454{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000455 PyObject *result;
456 PyInterpreterState *i;
Tim Peters112aad32006-07-19 00:03:19 +0000457
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000458 result = PyDict_New();
459 if (result == NULL)
460 return NULL;
Tim Peters112aad32006-07-19 00:03:19 +0000461
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000462 /* for i in all interpreters:
463 * for t in all of i's thread states:
464 * if t's frame isn't NULL, map t's id to its frame
Ezio Melottic2077b02011-03-16 12:34:31 +0200465 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000466 * need to grab head_mutex for the duration.
467 */
468 HEAD_LOCK();
469 for (i = interp_head; i != NULL; i = i->next) {
470 PyThreadState *t;
471 for (t = i->tstate_head; t != NULL; t = t->next) {
472 PyObject *id;
473 int stat;
474 struct _frame *frame = t->frame;
475 if (frame == NULL)
476 continue;
477 id = PyInt_FromLong(t->thread_id);
478 if (id == NULL)
479 goto Fail;
480 stat = PyDict_SetItem(result, id, (PyObject *)frame);
481 Py_DECREF(id);
482 if (stat < 0)
483 goto Fail;
484 }
485 }
486 HEAD_UNLOCK();
487 return result;
Tim Peters112aad32006-07-19 00:03:19 +0000488
489 Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000490 HEAD_UNLOCK();
491 Py_DECREF(result);
492 return NULL;
Tim Peters112aad32006-07-19 00:03:19 +0000493}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000494
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000495/* Python "auto thread state" API. */
496#ifdef WITH_THREAD
497
498/* Keep this as a static, as it is not reliable! It can only
499 ever be compared to the state for the *current* thread.
500 * If not equal, then it doesn't matter that the actual
501 value may change immediately after comparison, as it can't
502 possibly change to the current thread's state.
503 * If equal, then the current thread holds the lock, so the value can't
504 change until we yield the lock.
505*/
506static int
507PyThreadState_IsCurrent(PyThreadState *tstate)
508{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000509 /* Must be the tstate for this thread */
510 assert(PyGILState_GetThisThreadState()==tstate);
511 /* On Windows at least, simple reads and writes to 32 bit values
512 are atomic.
513 */
514 return tstate == _PyThreadState_Current;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000515}
516
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000517/* Internal initialization/finalization functions called by
518 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000519*/
Tim Peters19717fa2004-10-09 17:38:29 +0000520void
521_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000522{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000523 assert(i && t); /* must init with valid states */
524 autoTLSkey = PyThread_create_key();
525 autoInterpreterState = i;
526 assert(PyThread_get_key_value(autoTLSkey) == NULL);
527 assert(t->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000528
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000529 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000530}
531
Tim Peters19717fa2004-10-09 17:38:29 +0000532void
533_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000534{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000535 PyThread_delete_key(autoTLSkey);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000536 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000537}
538
Michael W. Hudson188d4362005-06-20 16:52:57 +0000539/* When a thread state is created for a thread by some mechanism other than
540 PyGILState_Ensure, it's important that the GILState machinery knows about
541 it so it doesn't try to create another thread state for the thread (this is
542 a better fix for SF bug #1010677 than the first one attempted).
543*/
Georg Brandlec6c2df2006-09-11 09:38:35 +0000544static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000545_PyGILState_NoteThreadState(PyThreadState* tstate)
546{
Antoine Pitrou0a7b65b2010-09-08 12:40:49 +0000547 /* If autoTLSkey isn't initialized, this must be the very first
548 threadstate created in Py_Initialize(). Don't do anything for now
549 (we'll be back here when _PyGILState_Init is called). */
550 if (!autoInterpreterState)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000551 return;
Tim Peters32a83612006-07-10 21:08:24 +0000552
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000553 /* Stick the thread state for this thread in thread local storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +0000554
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000555 The only situation where you can legitimately have more than one
556 thread state for an OS level thread is when there are multiple
557 interpreters, when:
Tim Peters32a83612006-07-10 21:08:24 +0000558
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000559 a) You shouldn't really be using the PyGILState_ APIs anyway,
560 and:
Michael W. Hudson188d4362005-06-20 16:52:57 +0000561
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000562 b) The slightly odd way PyThread_set_key_value works (see
563 comments by its implementation) means that the first thread
564 state created for that given OS level thread will "win",
565 which seems reasonable behaviour.
566 */
567 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
568 Py_FatalError("Couldn't create autoTLSkey mapping");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000569
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000570 /* PyGILState_Release must not try to delete this thread state. */
571 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000572}
573
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000574/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000575PyThreadState *
576PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000577{
Antoine Pitrou0a7b65b2010-09-08 12:40:49 +0000578 if (autoInterpreterState == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000579 return NULL;
580 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000581}
582
Tim Peters19717fa2004-10-09 17:38:29 +0000583PyGILState_STATE
584PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000585{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000586 int current;
587 PyThreadState *tcur;
588 /* Note that we do not auto-init Python here - apart from
589 potential races with 2 threads auto-initializing, pep-311
590 spells out other issues. Embedders are expected to have
591 called Py_Initialize() and usually PyEval_InitThreads().
592 */
593 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
594 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
595 if (tcur == NULL) {
596 /* Create a new thread state for this thread */
597 tcur = PyThreadState_New(autoInterpreterState);
598 if (tcur == NULL)
599 Py_FatalError("Couldn't create thread-state for new thread");
600 /* This is our thread state! We'll need to delete it in the
601 matching call to PyGILState_Release(). */
602 tcur->gilstate_counter = 0;
603 current = 0; /* new thread state is never current */
604 }
605 else
606 current = PyThreadState_IsCurrent(tcur);
607 if (current == 0)
608 PyEval_RestoreThread(tcur);
609 /* Update our counter in the thread-state - no need for locks:
610 - tcur will remain valid as we hold the GIL.
611 - the counter is safe as we are the only thread "allowed"
612 to modify this value
613 */
614 ++tcur->gilstate_counter;
615 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000616}
617
Tim Peters19717fa2004-10-09 17:38:29 +0000618void
619PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000620{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000621 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
622 autoTLSkey);
623 if (tcur == NULL)
624 Py_FatalError("auto-releasing thread-state, "
625 "but no thread-state for this thread");
626 /* We must hold the GIL and have our thread state current */
627 /* XXX - remove the check - the assert should be fine,
628 but while this is very new (April 2003), the extra check
629 by release-only users can't hurt.
630 */
631 if (! PyThreadState_IsCurrent(tcur))
632 Py_FatalError("This thread state must be current when releasing");
633 assert(PyThreadState_IsCurrent(tcur));
634 --tcur->gilstate_counter;
635 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000636
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000637 /* If we're going to destroy this thread-state, we must
638 * clear it while the GIL is held, as destructors may run.
639 */
640 if (tcur->gilstate_counter == 0) {
641 /* can't have been locked when we created it */
642 assert(oldstate == PyGILState_UNLOCKED);
643 PyThreadState_Clear(tcur);
644 /* Delete the thread-state. Note this releases the GIL too!
645 * It's vital that the GIL be held here, to avoid shutdown
646 * races; see bugs 225673 and 1061968 (that nasty bug has a
647 * habit of coming back).
648 */
649 PyThreadState_DeleteCurrent();
650 }
651 /* Release the lock if necessary */
652 else if (oldstate == PyGILState_UNLOCKED)
653 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000654}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000655
Benjamin Peterson7c0b44e2012-04-13 18:06:36 -0400656#endif /* WITH_THREAD */
657
Anthony Baxterac6bd462006-04-13 02:06:09 +0000658#ifdef __cplusplus
659}
660#endif
661
Anthony Baxterac6bd462006-04-13 02:06:09 +0000662