blob: 3fae85be88e661ca3a851c38304b1ba86fb25653 [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001
2/* Thread and interpreter state structures and their interfaces */
3
4#include "Python.h"
5
Tim Peters84705582004-10-10 02:47:33 +00006/* --------------------------------------------------------------------------
7CAUTION
8
9Always use malloc() and free() directly in this file. A number of these
10functions are advertised as safe to call when the GIL isn't held, and in
11a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging
12obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid
13the expense of doing their own locking).
14-------------------------------------------------------------------------- */
15
Martin v. Löwisf0473d52001-07-18 16:17:16 +000016#ifdef HAVE_DLOPEN
17#ifdef HAVE_DLFCN_H
18#include <dlfcn.h>
19#endif
20#ifndef RTLD_LAZY
21#define RTLD_LAZY 1
22#endif
23#endif
24
25
Guido van Rossum1d5ad901999-06-18 14:22:24 +000026#ifdef WITH_THREAD
27#include "pythread.h"
28static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000029#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000030#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
31#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
Michael W. Hudson188d4362005-06-20 16:52:57 +000032
Anthony Baxterac6bd462006-04-13 02:06:09 +000033#ifdef __cplusplus
34extern "C" {
35#endif
36
Michael W. Hudson188d4362005-06-20 16:52:57 +000037/* The single PyInterpreterState used by this process'
38 GILState implementation
39*/
40static PyInterpreterState *autoInterpreterState = NULL;
41static int autoTLSkey = 0;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000042#else
43#define HEAD_INIT() /* Nothing */
44#define HEAD_LOCK() /* Nothing */
45#define HEAD_UNLOCK() /* Nothing */
46#endif
47
Guido van Rossum25ce5661997-08-02 03:10:38 +000048static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000049
Guido van Rossum18bc7c21998-12-21 18:27:28 +000050PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossum6297a7a2003-02-19 15:53:17 +000051PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000052
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000053#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000054static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000055#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000056
Guido van Rossuma027efa1997-05-05 20:56:21 +000057
58PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000059PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000060{
Tim Peters84705582004-10-10 02:47:33 +000061 PyInterpreterState *interp = (PyInterpreterState *)
62 malloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000063
Guido van Rossuma027efa1997-05-05 20:56:21 +000064 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000065 HEAD_INIT();
Neal Norwitze1fdb322006-07-21 05:32:28 +000066#ifdef WITH_THREAD
67 if (head_mutex == NULL)
68 Py_FatalError("Can't initialize threads for interpreter");
69#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000070 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000071 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000072 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000073 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000074 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
79 interp->dlopenflags = RTLD_NOW;
80#else
81 interp->dlopenflags = RTLD_LAZY;
82#endif
83#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000084#ifdef WITH_TSC
85 interp->tscdump = 0;
86#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000087
Tim Peters412f2462000-09-02 09:16:15 +000088 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000089 interp->next = interp_head;
90 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000091 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000092 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000093
Guido van Rossuma027efa1997-05-05 20:56:21 +000094 return interp;
95}
96
97
98void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000099PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100{
101 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000102 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000103 for (p = interp->tstate_head; p != NULL; p = p->next)
104 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000105 HEAD_UNLOCK();
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000106 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->sysdict);
111 Py_CLEAR(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112}
113
114
115static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000116zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000117{
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000118 PyThreadState *p;
119 /* No need to lock the mutex here because this should only happen
120 when the threads are all really dead (XXX famous last words). */
121 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000123 }
124}
125
126
127void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000128PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000129{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000130 PyInterpreterState **p;
131 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000132 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133 for (p = &interp_head; ; p = &(*p)->next) {
134 if (*p == NULL)
135 Py_FatalError(
136 "PyInterpreterState_Delete: invalid interp");
137 if (*p == interp)
138 break;
139 }
140 if (interp->tstate_head != NULL)
141 Py_FatalError("PyInterpreterState_Delete: remaining threads");
142 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000143 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000144 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000145}
146
147
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000148/* Default implementation for _PyThreadState_GetFrame */
149static struct _frame *
150threadstate_getframe(PyThreadState *self)
151{
152 return self->frame;
153}
154
Guido van Rossuma027efa1997-05-05 20:56:21 +0000155PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000156PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000157{
Tim Peters84705582004-10-10 02:47:33 +0000158 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
159
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000160 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000161 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162
Guido van Rossuma027efa1997-05-05 20:56:21 +0000163 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000164 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000165
166 tstate->frame = NULL;
167 tstate->recursion_depth = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000169 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000170 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000171 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000172 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000173#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000174 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000175#else
176 tstate->thread_id = 0;
177#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000178
Guido van Rossumede04391998-04-10 20:18:25 +0000179 tstate->dict = NULL;
180
Guido van Rossuma027efa1997-05-05 20:56:21 +0000181 tstate->curexc_type = NULL;
182 tstate->curexc_value = NULL;
183 tstate->curexc_traceback = NULL;
184
185 tstate->exc_type = NULL;
186 tstate->exc_value = NULL;
187 tstate->exc_traceback = NULL;
188
Fred Drake5755ce62001-06-27 19:19:46 +0000189 tstate->c_profilefunc = NULL;
190 tstate->c_tracefunc = NULL;
191 tstate->c_profileobj = NULL;
192 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000193
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000194#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +0000195 _PyGILState_NoteThreadState(tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000196#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +0000197
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000198 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199 tstate->next = interp->tstate_head;
200 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000201 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000202 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203
Guido van Rossuma027efa1997-05-05 20:56:21 +0000204 return tstate;
205}
206
207
208void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000209PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000211 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000213 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000214
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000215 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000216
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000217 Py_CLEAR(tstate->dict);
218 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000219
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000220 Py_CLEAR(tstate->curexc_type);
221 Py_CLEAR(tstate->curexc_value);
222 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000223
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000224 Py_CLEAR(tstate->exc_type);
225 Py_CLEAR(tstate->exc_value);
226 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000227
Fred Drake5755ce62001-06-27 19:19:46 +0000228 tstate->c_profilefunc = NULL;
229 tstate->c_tracefunc = NULL;
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000230 Py_CLEAR(tstate->c_profileobj);
231 Py_CLEAR(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232}
233
234
Guido van Rossum29757862001-01-23 01:46:06 +0000235/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
236static void
237tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000238{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239 PyInterpreterState *interp;
240 PyThreadState **p;
241 if (tstate == NULL)
242 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000243 interp = tstate->interp;
244 if (interp == NULL)
245 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000246 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000247 for (p = &interp->tstate_head; ; p = &(*p)->next) {
248 if (*p == NULL)
249 Py_FatalError(
250 "PyThreadState_Delete: invalid tstate");
251 if (*p == tstate)
252 break;
253 }
254 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000255 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000256 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000257}
258
259
Guido van Rossum29757862001-01-23 01:46:06 +0000260void
261PyThreadState_Delete(PyThreadState *tstate)
262{
263 if (tstate == _PyThreadState_Current)
264 Py_FatalError("PyThreadState_Delete: tstate is still current");
265 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000266#ifdef WITH_THREAD
267 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
268 PyThread_delete_key_value(autoTLSkey);
269#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000270}
271
272
273#ifdef WITH_THREAD
274void
275PyThreadState_DeleteCurrent()
276{
277 PyThreadState *tstate = _PyThreadState_Current;
278 if (tstate == NULL)
279 Py_FatalError(
280 "PyThreadState_DeleteCurrent: no current tstate");
281 _PyThreadState_Current = NULL;
282 tstate_delete_common(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000283 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
284 PyThread_delete_key_value(autoTLSkey);
Guido van Rossum29757862001-01-23 01:46:06 +0000285 PyEval_ReleaseLock();
286}
287#endif /* WITH_THREAD */
288
289
Guido van Rossuma027efa1997-05-05 20:56:21 +0000290PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000291PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000292{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000293 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000294 Py_FatalError("PyThreadState_Get: no current thread");
295
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000296 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000297}
298
299
300PyThreadState *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000301PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000302{
Anthony Baxter7b782b62006-04-11 12:01:56 +0000303 PyThreadState *oldts = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000304
Anthony Baxter7b782b62006-04-11 12:01:56 +0000305 _PyThreadState_Current = newts;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000306 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000307 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000308 builds.
309 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000310#if defined(Py_DEBUG) && defined(WITH_THREAD)
Anthony Baxter7b782b62006-04-11 12:01:56 +0000311 if (newts) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000312 PyThreadState *check = PyGILState_GetThisThreadState();
Anthony Baxter7b782b62006-04-11 12:01:56 +0000313 if (check && check->interp == newts->interp && check != newts)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000314 Py_FatalError("Invalid thread state for this thread");
315 }
316#endif
Anthony Baxter7b782b62006-04-11 12:01:56 +0000317 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000318}
Guido van Rossumede04391998-04-10 20:18:25 +0000319
320/* An extension mechanism to store arbitrary additional per-thread state.
321 PyThreadState_GetDict() returns a dictionary that can be used to hold such
322 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000323 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
324 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000325
326PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000328{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000329 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000330 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000331
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000332 if (_PyThreadState_Current->dict == NULL) {
333 PyObject *d;
334 _PyThreadState_Current->dict = d = PyDict_New();
335 if (d == NULL)
336 PyErr_Clear();
337 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000338 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000339}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000340
341
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000342/* Asynchronously raise an exception in a thread.
343 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000344 To prevent naive misuse, you must write your own extension
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000345 to call this. Must be called with the GIL held.
346 Returns the number of tstates modified; if it returns a number
347 greater than one, you're in trouble, and you should call it again
348 with exc=NULL to revert the effect. This raises no exceptions. */
349
350int
351PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000352 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000353 PyInterpreterState *interp = tstate->interp;
354 PyThreadState *p;
355 int count = 0;
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000356 HEAD_LOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000357 for (p = interp->tstate_head; p != NULL; p = p->next) {
358 if (p->thread_id != id)
359 continue;
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000360 Py_CLEAR(p->async_exc);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000361 Py_XINCREF(exc);
362 p->async_exc = exc;
363 count += 1;
364 }
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000365 HEAD_UNLOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000366 return count;
367}
368
369
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000370/* Routines for advanced debuggers, requested by David Beazley.
371 Don't use unless you know what you are doing! */
372
373PyInterpreterState *
374PyInterpreterState_Head(void)
375{
376 return interp_head;
377}
378
379PyInterpreterState *
380PyInterpreterState_Next(PyInterpreterState *interp) {
381 return interp->next;
382}
383
384PyThreadState *
385PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
386 return interp->tstate_head;
387}
388
389PyThreadState *
390PyThreadState_Next(PyThreadState *tstate) {
391 return tstate->next;
392}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000393
Tim Peters112aad32006-07-19 00:03:19 +0000394/* The implementation of sys._current_frames(). This is intended to be
395 called with the GIL held, as it will be when called via
396 sys._current_frames(). It's possible it would work fine even without
397 the GIL held, but haven't thought enough about that.
398*/
399PyObject *
400_PyThread_CurrentFrames(void)
401{
402 PyObject *result;
403 PyInterpreterState *i;
404
405 result = PyDict_New();
406 if (result == NULL)
407 return NULL;
408
409 /* for i in all interpreters:
410 * for t in all of i's thread states:
411 * if t's frame isn't NULL, map t's id to its frame
412 * Because these lists can mutute even when the GIL is held, we
413 * need to grab head_mutex for the duration.
414 */
415 HEAD_LOCK();
416 for (i = interp_head; i != NULL; i = i->next) {
417 PyThreadState *t;
418 for (t = i->tstate_head; t != NULL; t = t->next) {
419 PyObject *id;
420 int stat;
421 struct _frame *frame = t->frame;
422 if (frame == NULL)
423 continue;
424 id = PyInt_FromLong(t->thread_id);
425 if (id == NULL)
426 goto Fail;
427 stat = PyDict_SetItem(result, id, (PyObject *)frame);
428 Py_DECREF(id);
429 if (stat < 0)
430 goto Fail;
431 }
432 }
433 HEAD_UNLOCK();
434 return result;
435
436 Fail:
437 HEAD_UNLOCK();
438 Py_DECREF(result);
439 return NULL;
440}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000441
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000442/* Python "auto thread state" API. */
443#ifdef WITH_THREAD
444
445/* Keep this as a static, as it is not reliable! It can only
446 ever be compared to the state for the *current* thread.
447 * If not equal, then it doesn't matter that the actual
448 value may change immediately after comparison, as it can't
449 possibly change to the current thread's state.
450 * If equal, then the current thread holds the lock, so the value can't
451 change until we yield the lock.
452*/
453static int
454PyThreadState_IsCurrent(PyThreadState *tstate)
455{
456 /* Must be the tstate for this thread */
457 assert(PyGILState_GetThisThreadState()==tstate);
458 /* On Windows at least, simple reads and writes to 32 bit values
459 are atomic.
460 */
461 return tstate == _PyThreadState_Current;
462}
463
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000464/* Internal initialization/finalization functions called by
465 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000466*/
Tim Peters19717fa2004-10-09 17:38:29 +0000467void
468_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000469{
Tim Peters19717fa2004-10-09 17:38:29 +0000470 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000471 autoTLSkey = PyThread_create_key();
472 autoInterpreterState = i;
Tim Petersf9becec2004-10-09 22:47:13 +0000473 assert(PyThread_get_key_value(autoTLSkey) == NULL);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000474 assert(t->gilstate_counter == 0);
475
476 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000477}
478
Tim Peters19717fa2004-10-09 17:38:29 +0000479void
480_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000481{
482 PyThread_delete_key(autoTLSkey);
483 autoTLSkey = 0;
484 autoInterpreterState = NULL;;
485}
486
Michael W. Hudson188d4362005-06-20 16:52:57 +0000487/* When a thread state is created for a thread by some mechanism other than
488 PyGILState_Ensure, it's important that the GILState machinery knows about
489 it so it doesn't try to create another thread state for the thread (this is
490 a better fix for SF bug #1010677 than the first one attempted).
491*/
492void
493_PyGILState_NoteThreadState(PyThreadState* tstate)
494{
495 /* If autoTLSkey is 0, this must be the very first threadstate created
496 in Py_Initialize(). Don't do anything for now (we'll be back here
497 when _PyGILState_Init is called). */
Tim Peters32a83612006-07-10 21:08:24 +0000498 if (!autoTLSkey)
Michael W. Hudson188d4362005-06-20 16:52:57 +0000499 return;
Tim Peters32a83612006-07-10 21:08:24 +0000500
Michael W. Hudson188d4362005-06-20 16:52:57 +0000501 /* Stick the thread state for this thread in thread local storage.
502
503 The only situation where you can legitimately have more than one
504 thread state for an OS level thread is when there are multiple
505 interpreters, when:
Tim Peters32a83612006-07-10 21:08:24 +0000506
Michael W. Hudson188d4362005-06-20 16:52:57 +0000507 a) You shouldn't really be using the PyGILState_ APIs anyway,
508 and:
509
510 b) The slightly odd way PyThread_set_key_value works (see
511 comments by its implementation) means that the first thread
512 state created for that given OS level thread will "win",
513 which seems reasonable behaviour.
514 */
515 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
516 Py_FatalError("Couldn't create autoTLSkey mapping");
517
518 /* PyGILState_Release must not try to delete this thread state. */
519 tstate->gilstate_counter = 1;
520}
521
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000522/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000523PyThreadState *
524PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000525{
Tim Peters19717fa2004-10-09 17:38:29 +0000526 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000527 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000528 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000529}
530
Tim Peters19717fa2004-10-09 17:38:29 +0000531PyGILState_STATE
532PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000533{
534 int current;
535 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000536 /* Note that we do not auto-init Python here - apart from
537 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000538 spells out other issues. Embedders are expected to have
539 called Py_Initialize() and usually PyEval_InitThreads().
540 */
541 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
Anthony Baxter7b782b62006-04-11 12:01:56 +0000542 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000543 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000544 /* Create a new thread state for this thread */
545 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000546 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000547 Py_FatalError("Couldn't create thread-state for new thread");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000548 /* This is our thread state! We'll need to delete it in the
549 matching call to PyGILState_Release(). */
550 tcur->gilstate_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000551 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000552 }
553 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000554 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000555 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000556 PyEval_RestoreThread(tcur);
557 /* Update our counter in the thread-state - no need for locks:
558 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000559 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000560 to modify this value
561 */
Tim Peters19717fa2004-10-09 17:38:29 +0000562 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000563 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
564}
565
Tim Peters19717fa2004-10-09 17:38:29 +0000566void
567PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000568{
Anthony Baxter7b782b62006-04-11 12:01:56 +0000569 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
570 autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000571 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000572 Py_FatalError("auto-releasing thread-state, "
573 "but no thread-state for this thread");
574 /* We must hold the GIL and have our thread state current */
575 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000576 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000577 by release-only users can't hurt.
578 */
Tim Peters19717fa2004-10-09 17:38:29 +0000579 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000580 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000581 assert(PyThreadState_IsCurrent(tcur));
582 --tcur->gilstate_counter;
583 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000584
Tim Petersfb1ffb02004-11-08 04:30:21 +0000585 /* If we're going to destroy this thread-state, we must
586 * clear it while the GIL is held, as destructors may run.
587 */
Tim Peters19717fa2004-10-09 17:38:29 +0000588 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000589 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000590 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000591 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000592 /* Delete the thread-state. Note this releases the GIL too!
593 * It's vital that the GIL be held here, to avoid shutdown
594 * races; see bugs 225673 and 1061968 (that nasty bug has a
595 * habit of coming back).
596 */
597 PyThreadState_DeleteCurrent();
Tim Peters89c0ec92004-10-10 05:30:40 +0000598 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000599 /* Release the lock if necessary */
600 else if (oldstate == PyGILState_UNLOCKED)
Michael W. Hudson774479c2005-04-18 08:46:17 +0000601 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000602}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000603
604#ifdef __cplusplus
605}
606#endif
607
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000608#endif /* WITH_THREAD */
Anthony Baxterac6bd462006-04-13 02:06:09 +0000609
610