blob: b347c41c54a5ebfe82d40c6991273a0445862908 [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
Jeffrey Yasskin39370832010-05-03 19:29:34 +000050/* Assuming the current thread holds the GIL, this is the
51 PyThreadState for the current thread. */
52_Py_atomic_address _PyThreadState_Current = {NULL};
Guido van Rossum6297a7a2003-02-19 15:53:17 +000053PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000054
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000055#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000056static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000057#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000058
Guido van Rossuma027efa1997-05-05 20:56:21 +000059
60PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000061PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyInterpreterState *interp = (PyInterpreterState *)
64 malloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 if (interp != NULL) {
67 HEAD_INIT();
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 if (head_mutex == NULL)
70 Py_FatalError("Can't initialize threads for interpreter");
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 interp->modules = NULL;
73 interp->modules_reloading = NULL;
74 interp->modules_by_index = NULL;
75 interp->sysdict = NULL;
76 interp->builtins = NULL;
77 interp->tstate_head = NULL;
78 interp->codec_search_path = NULL;
79 interp->codec_search_cache = NULL;
80 interp->codec_error_registry = NULL;
81 interp->codecs_initialized = 0;
Victor Stinner3cbf14b2011-04-27 00:24:21 +020082 interp->fscodec_initialized = 0;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000083#ifdef HAVE_DLOPEN
84#ifdef RTLD_NOW
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000086#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000088#endif
89#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000090#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 interp->tscdump = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 HEAD_LOCK();
95 interp->next = interp_head;
96 interp_head = interp;
97 HEAD_UNLOCK();
98 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000101}
102
103
104void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000105PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 PyThreadState *p;
108 HEAD_LOCK();
109 for (p = interp->tstate_head; p != NULL; p = p->next)
110 PyThreadState_Clear(p);
111 HEAD_UNLOCK();
112 Py_CLEAR(interp->codec_search_path);
113 Py_CLEAR(interp->codec_search_cache);
114 Py_CLEAR(interp->codec_error_registry);
115 Py_CLEAR(interp->modules);
116 Py_CLEAR(interp->modules_by_index);
117 Py_CLEAR(interp->modules_reloading);
118 Py_CLEAR(interp->sysdict);
119 Py_CLEAR(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000120}
121
122
123static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 PyThreadState *p;
127 /* No need to lock the mutex here because this should only happen
128 when the threads are all really dead (XXX famous last words). */
129 while ((p = interp->tstate_head) != NULL) {
130 PyThreadState_Delete(p);
131 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132}
133
134
135void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000136PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 PyInterpreterState **p;
139 zapthreads(interp);
140 HEAD_LOCK();
141 for (p = &interp_head; ; p = &(*p)->next) {
142 if (*p == NULL)
143 Py_FatalError(
144 "PyInterpreterState_Delete: invalid interp");
145 if (*p == interp)
146 break;
147 }
148 if (interp->tstate_head != NULL)
149 Py_FatalError("PyInterpreterState_Delete: remaining threads");
150 *p = interp->next;
151 HEAD_UNLOCK();
152 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000153}
154
155
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000156/* Default implementation for _PyThreadState_GetFrame */
157static struct _frame *
158threadstate_getframe(PyThreadState *self)
159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000161}
162
Victor Stinner45b9be52010-03-03 23:28:07 +0000163static PyThreadState *
164new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
Tim Peters84705582004-10-10 02:47:33 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (_PyThreadState_GetFrame == NULL)
169 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (tstate != NULL) {
172 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 tstate->frame = NULL;
175 tstate->recursion_depth = 0;
176 tstate->overflowed = 0;
177 tstate->recursion_critical = 0;
178 tstate->tracing = 0;
179 tstate->use_tracing = 0;
180 tstate->tick_counter = 0;
181 tstate->gilstate_counter = 0;
182 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000183#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000185#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 tstate->thread_id = 0;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000187#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 tstate->dict = NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 tstate->curexc_type = NULL;
192 tstate->curexc_value = NULL;
193 tstate->curexc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 tstate->exc_type = NULL;
196 tstate->exc_value = NULL;
197 tstate->exc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 tstate->c_profilefunc = NULL;
200 tstate->c_tracefunc = NULL;
201 tstate->c_profileobj = NULL;
202 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (init)
205 _PyThreadState_Init(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 HEAD_LOCK();
208 tstate->next = interp->tstate_head;
209 interp->tstate_head = tstate;
210 HEAD_UNLOCK();
211 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000214}
215
Victor Stinner45b9be52010-03-03 23:28:07 +0000216PyThreadState *
217PyThreadState_New(PyInterpreterState *interp)
218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000220}
221
222PyThreadState *
223_PyThreadState_Prealloc(PyInterpreterState *interp)
224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000226}
227
228void
229_PyThreadState_Init(PyThreadState *tstate)
230{
231#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 _PyGILState_NoteThreadState(tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000233#endif
234}
235
Martin v. Löwis1a214512008-06-11 05:26:20 +0000236PyObject*
237PyState_FindModule(struct PyModuleDef* m)
238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 Py_ssize_t index = m->m_base.m_index;
240 PyInterpreterState *state = PyThreadState_GET()->interp;
241 PyObject *res;
242 if (index == 0)
243 return NULL;
244 if (state->modules_by_index == NULL)
245 return NULL;
246 if (index > PyList_GET_SIZE(state->modules_by_index))
247 return NULL;
248 res = PyList_GET_ITEM(state->modules_by_index, index);
249 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000250}
251
252int
253_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 PyInterpreterState *state = PyThreadState_GET()->interp;
256 if (!def)
257 return -1;
258 if (!state->modules_by_index) {
259 state->modules_by_index = PyList_New(0);
260 if (!state->modules_by_index)
261 return -1;
262 }
263 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
264 if (PyList_Append(state->modules_by_index, Py_None) < 0)
265 return -1;
266 Py_INCREF(module);
267 return PyList_SetItem(state->modules_by_index,
268 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000269}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000270
271void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000272PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 if (Py_VerboseFlag && tstate->frame != NULL)
275 fprintf(stderr,
276 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_CLEAR(tstate->dict);
281 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_CLEAR(tstate->curexc_type);
284 Py_CLEAR(tstate->curexc_value);
285 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_CLEAR(tstate->exc_type);
288 Py_CLEAR(tstate->exc_value);
289 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 tstate->c_profilefunc = NULL;
292 tstate->c_tracefunc = NULL;
293 Py_CLEAR(tstate->c_profileobj);
294 Py_CLEAR(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000295}
296
297
Guido van Rossum29757862001-01-23 01:46:06 +0000298/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
299static void
300tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 PyInterpreterState *interp;
303 PyThreadState **p;
304 PyThreadState *prev_p = NULL;
305 if (tstate == NULL)
306 Py_FatalError("PyThreadState_Delete: NULL tstate");
307 interp = tstate->interp;
308 if (interp == NULL)
309 Py_FatalError("PyThreadState_Delete: NULL interp");
310 HEAD_LOCK();
311 for (p = &interp->tstate_head; ; p = &(*p)->next) {
312 if (*p == NULL)
313 Py_FatalError(
314 "PyThreadState_Delete: invalid tstate");
315 if (*p == tstate)
316 break;
317 /* Sanity check. These states should never happen but if
318 * they do we must abort. Otherwise we'll end up spinning in
319 * in a tight loop with the lock held. A similar check is done
320 * in thread.c find_key(). */
321 if (*p == prev_p)
322 Py_FatalError(
323 "PyThreadState_Delete: small circular list(!)"
324 " and tstate not found.");
325 prev_p = *p;
326 if ((*p)->next == interp->tstate_head)
327 Py_FatalError(
328 "PyThreadState_Delete: circular list(!) and"
329 " tstate not found.");
330 }
331 *p = tstate->next;
332 HEAD_UNLOCK();
333 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000334}
335
336
Guido van Rossum29757862001-01-23 01:46:06 +0000337void
338PyThreadState_Delete(PyThreadState *tstate)
339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current))
341 Py_FatalError("PyThreadState_Delete: tstate is still current");
342 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000343#ifdef WITH_THREAD
Antoine Pitrou079ce542010-09-08 12:37:10 +0000344 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 PyThread_delete_key_value(autoTLSkey);
Tim Petersf4e69282006-02-27 17:15:31 +0000346#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000347}
348
349
350#ifdef WITH_THREAD
351void
352PyThreadState_DeleteCurrent()
353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
355 &_PyThreadState_Current);
356 if (tstate == NULL)
357 Py_FatalError(
358 "PyThreadState_DeleteCurrent: no current tstate");
359 _Py_atomic_store_relaxed(&_PyThreadState_Current, NULL);
360 tstate_delete_common(tstate);
Antoine Pitrou079ce542010-09-08 12:37:10 +0000361 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 PyThread_delete_key_value(autoTLSkey);
363 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000364}
365#endif /* WITH_THREAD */
366
367
Guido van Rossuma027efa1997-05-05 20:56:21 +0000368PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000369PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
372 &_PyThreadState_Current);
373 if (tstate == NULL)
374 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000377}
378
379
380PyThreadState *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 PyThreadState *oldts = (PyThreadState*)_Py_atomic_load_relaxed(
384 &_PyThreadState_Current);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 _Py_atomic_store_relaxed(&_PyThreadState_Current, newts);
387 /* It should not be possible for more than one thread state
388 to be used for a thread. Check this the best we can in debug
389 builds.
390 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000391#if defined(Py_DEBUG) && defined(WITH_THREAD)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (newts) {
393 /* This can be called from PyEval_RestoreThread(). Similar
394 to it, we need to ensure errno doesn't change.
395 */
396 int err = errno;
397 PyThreadState *check = PyGILState_GetThisThreadState();
398 if (check && check->interp == newts->interp && check != newts)
399 Py_FatalError("Invalid thread state for this thread");
400 errno = err;
401 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000402#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000404}
Guido van Rossumede04391998-04-10 20:18:25 +0000405
406/* An extension mechanism to store arbitrary additional per-thread state.
407 PyThreadState_GetDict() returns a dictionary that can be used to hold such
408 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000409 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
410 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000411
412PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000413PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
416 &_PyThreadState_Current);
417 if (tstate == NULL)
418 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (tstate->dict == NULL) {
421 PyObject *d;
422 tstate->dict = d = PyDict_New();
423 if (d == NULL)
424 PyErr_Clear();
425 }
426 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000427}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000428
429
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000430/* Asynchronously raise an exception in a thread.
431 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000432 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000433 to call this, or use ctypes. Must be called with the GIL held.
434 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
435 match any known thread id). Can be called with exc=NULL to clear an
436 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000437
438int
439PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 PyThreadState *tstate = PyThreadState_GET();
441 PyInterpreterState *interp = tstate->interp;
442 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 /* Although the GIL is held, a few C API functions can be called
445 * without the GIL held, and in particular some that create and
446 * destroy thread and interpreter states. Those can mutate the
447 * list of thread states we're traversing, so to prevent that we lock
448 * head_mutex for the duration.
449 */
450 HEAD_LOCK();
451 for (p = interp->tstate_head; p != NULL; p = p->next) {
452 if (p->thread_id == id) {
453 /* Tricky: we need to decref the current value
454 * (if any) in p->async_exc, but that can in turn
455 * allow arbitrary Python code to run, including
456 * perhaps calls to this function. To prevent
457 * deadlock, we need to release head_mutex before
458 * the decref.
459 */
460 PyObject *old_exc = p->async_exc;
461 Py_XINCREF(exc);
462 p->async_exc = exc;
463 HEAD_UNLOCK();
464 Py_XDECREF(old_exc);
465 _PyEval_SignalAsyncExc();
466 return 1;
467 }
468 }
469 HEAD_UNLOCK();
470 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000471}
472
473
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000474/* Routines for advanced debuggers, requested by David Beazley.
475 Don't use unless you know what you are doing! */
476
477PyInterpreterState *
478PyInterpreterState_Head(void)
479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 return interp_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000481}
482
483PyInterpreterState *
484PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000486}
487
488PyThreadState *
489PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000491}
492
493PyThreadState *
494PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000496}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000497
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000498/* The implementation of sys._current_frames(). This is intended to be
499 called with the GIL held, as it will be when called via
500 sys._current_frames(). It's possible it would work fine even without
501 the GIL held, but haven't thought enough about that.
502*/
503PyObject *
504_PyThread_CurrentFrames(void)
505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PyObject *result;
507 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 result = PyDict_New();
510 if (result == NULL)
511 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 /* for i in all interpreters:
514 * for t in all of i's thread states:
515 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +0200516 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 * need to grab head_mutex for the duration.
518 */
519 HEAD_LOCK();
520 for (i = interp_head; i != NULL; i = i->next) {
521 PyThreadState *t;
522 for (t = i->tstate_head; t != NULL; t = t->next) {
523 PyObject *id;
524 int stat;
525 struct _frame *frame = t->frame;
526 if (frame == NULL)
527 continue;
528 id = PyLong_FromLong(t->thread_id);
529 if (id == NULL)
530 goto Fail;
531 stat = PyDict_SetItem(result, id, (PyObject *)frame);
532 Py_DECREF(id);
533 if (stat < 0)
534 goto Fail;
535 }
536 }
537 HEAD_UNLOCK();
538 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000539
540 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 HEAD_UNLOCK();
542 Py_DECREF(result);
543 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000544}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000545
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000546/* Python "auto thread state" API. */
547#ifdef WITH_THREAD
548
549/* Keep this as a static, as it is not reliable! It can only
550 ever be compared to the state for the *current* thread.
551 * If not equal, then it doesn't matter that the actual
552 value may change immediately after comparison, as it can't
553 possibly change to the current thread's state.
554 * If equal, then the current thread holds the lock, so the value can't
555 change until we yield the lock.
556*/
557static int
558PyThreadState_IsCurrent(PyThreadState *tstate)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* Must be the tstate for this thread */
561 assert(PyGILState_GetThisThreadState()==tstate);
562 return tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000563}
564
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000565/* Internal initialization/finalization functions called by
566 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000567*/
Tim Peters19717fa2004-10-09 17:38:29 +0000568void
569_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 assert(i && t); /* must init with valid states */
572 autoTLSkey = PyThread_create_key();
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000573 if (autoTLSkey == -1)
574 Py_FatalError("Could not allocate TLS entry");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 autoInterpreterState = i;
576 assert(PyThread_get_key_value(autoTLSkey) == NULL);
577 assert(t->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000580}
581
Tim Peters19717fa2004-10-09 17:38:29 +0000582void
583_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 PyThread_delete_key(autoTLSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000587}
588
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200589/* Reset the TLS key - called by PyOS_AfterFork.
590 * This should not be necessary, but some - buggy - pthread implementations
591 * don't flush TLS on fork, see issue #10517.
592 */
593void
594_PyGILState_Reinit(void)
595{
596 PyThreadState *tstate = PyGILState_GetThisThreadState();
597 PyThread_delete_key(autoTLSkey);
598 if ((autoTLSkey = PyThread_create_key()) == -1)
599 Py_FatalError("Could not allocate TLS entry");
600
601 /* re-associate the current thread state with the new key */
602 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
603 Py_FatalError("Couldn't create autoTLSkey mapping");
604}
605
Michael W. Hudson188d4362005-06-20 16:52:57 +0000606/* When a thread state is created for a thread by some mechanism other than
607 PyGILState_Ensure, it's important that the GILState machinery knows about
608 it so it doesn't try to create another thread state for the thread (this is
609 a better fix for SF bug #1010677 than the first one attempted).
610*/
Thomas Wouters89f507f2006-12-13 04:49:30 +0000611static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000612_PyGILState_NoteThreadState(PyThreadState* tstate)
613{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000614 /* If autoTLSkey isn't initialized, this must be the very first
615 threadstate created in Py_Initialize(). Don't do anything for now
616 (we'll be back here when _PyGILState_Init is called). */
617 if (!autoInterpreterState)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 return;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 /* Stick the thread state for this thread in thread local storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 The only situation where you can legitimately have more than one
623 thread state for an OS level thread is when there are multiple
624 interpreters, when:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 a) You shouldn't really be using the PyGILState_ APIs anyway,
627 and:
Michael W. Hudson188d4362005-06-20 16:52:57 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 b) The slightly odd way PyThread_set_key_value works (see
630 comments by its implementation) means that the first thread
631 state created for that given OS level thread will "win",
632 which seems reasonable behaviour.
633 */
634 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
635 Py_FatalError("Couldn't create autoTLSkey mapping");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 /* PyGILState_Release must not try to delete this thread state. */
638 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000639}
640
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000641/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000642PyThreadState *
643PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000644{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000645 if (autoInterpreterState == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 return NULL;
647 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000648}
649
Tim Peters19717fa2004-10-09 17:38:29 +0000650PyGILState_STATE
651PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 int current;
654 PyThreadState *tcur;
655 /* Note that we do not auto-init Python here - apart from
656 potential races with 2 threads auto-initializing, pep-311
657 spells out other issues. Embedders are expected to have
658 called Py_Initialize() and usually PyEval_InitThreads().
659 */
660 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
661 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
662 if (tcur == NULL) {
663 /* Create a new thread state for this thread */
664 tcur = PyThreadState_New(autoInterpreterState);
665 if (tcur == NULL)
666 Py_FatalError("Couldn't create thread-state for new thread");
667 /* This is our thread state! We'll need to delete it in the
668 matching call to PyGILState_Release(). */
669 tcur->gilstate_counter = 0;
670 current = 0; /* new thread state is never current */
671 }
672 else
673 current = PyThreadState_IsCurrent(tcur);
674 if (current == 0)
675 PyEval_RestoreThread(tcur);
676 /* Update our counter in the thread-state - no need for locks:
677 - tcur will remain valid as we hold the GIL.
678 - the counter is safe as we are the only thread "allowed"
679 to modify this value
680 */
681 ++tcur->gilstate_counter;
682 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000683}
684
Tim Peters19717fa2004-10-09 17:38:29 +0000685void
686PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
689 autoTLSkey);
690 if (tcur == NULL)
691 Py_FatalError("auto-releasing thread-state, "
692 "but no thread-state for this thread");
693 /* We must hold the GIL and have our thread state current */
694 /* XXX - remove the check - the assert should be fine,
695 but while this is very new (April 2003), the extra check
696 by release-only users can't hurt.
697 */
698 if (! PyThreadState_IsCurrent(tcur))
699 Py_FatalError("This thread state must be current when releasing");
700 assert(PyThreadState_IsCurrent(tcur));
701 --tcur->gilstate_counter;
702 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 /* If we're going to destroy this thread-state, we must
705 * clear it while the GIL is held, as destructors may run.
706 */
707 if (tcur->gilstate_counter == 0) {
708 /* can't have been locked when we created it */
709 assert(oldstate == PyGILState_UNLOCKED);
710 PyThreadState_Clear(tcur);
711 /* Delete the thread-state. Note this releases the GIL too!
712 * It's vital that the GIL be held here, to avoid shutdown
713 * races; see bugs 225673 and 1061968 (that nasty bug has a
714 * habit of coming back).
715 */
716 PyThreadState_DeleteCurrent();
717 }
718 /* Release the lock if necessary */
719 else if (oldstate == PyGILState_UNLOCKED)
720 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000721}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722
723#ifdef __cplusplus
724}
725#endif
726
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000727#endif /* WITH_THREAD */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000728
729