blob: b8f460ff8a841faec72aac2bc6d3e9000e24c486 [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();
Guido van Rossum25ce5661997-08-02 03:10:38 +000066 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000067 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000068 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000069 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000070 interp->codec_search_path = NULL;
71 interp->codec_search_cache = NULL;
72 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000073#ifdef HAVE_DLOPEN
74#ifdef RTLD_NOW
75 interp->dlopenflags = RTLD_NOW;
76#else
77 interp->dlopenflags = RTLD_LAZY;
78#endif
79#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000080#ifdef WITH_TSC
81 interp->tscdump = 0;
82#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000083
Tim Peters412f2462000-09-02 09:16:15 +000084 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000085 interp->next = interp_head;
86 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000087 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000088 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000089
Guido van Rossuma027efa1997-05-05 20:56:21 +000090 return interp;
91}
92
93
94void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000095PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000096{
97 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000098 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000099 for (p = interp->tstate_head; p != NULL; p = p->next)
100 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000101 HEAD_UNLOCK();
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000102 Py_CLEAR(interp->codec_search_path);
103 Py_CLEAR(interp->codec_search_cache);
104 Py_CLEAR(interp->codec_error_registry);
105 Py_CLEAR(interp->modules);
106 Py_CLEAR(interp->sysdict);
107 Py_CLEAR(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000108}
109
110
111static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000113{
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000114 PyThreadState *p;
115 /* No need to lock the mutex here because this should only happen
116 when the threads are all really dead (XXX famous last words). */
117 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000118 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000119 }
120}
121
122
123void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000125{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000126 PyInterpreterState **p;
127 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000128 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000129 for (p = &interp_head; ; p = &(*p)->next) {
130 if (*p == NULL)
131 Py_FatalError(
132 "PyInterpreterState_Delete: invalid interp");
133 if (*p == interp)
134 break;
135 }
136 if (interp->tstate_head != NULL)
137 Py_FatalError("PyInterpreterState_Delete: remaining threads");
138 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000139 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000140 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000141}
142
143
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000144/* Default implementation for _PyThreadState_GetFrame */
145static struct _frame *
146threadstate_getframe(PyThreadState *self)
147{
148 return self->frame;
149}
150
Guido van Rossuma027efa1997-05-05 20:56:21 +0000151PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000152PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000153{
Tim Peters84705582004-10-10 02:47:33 +0000154 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
155
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000156 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000157 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000158
Guido van Rossuma027efa1997-05-05 20:56:21 +0000159 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000160 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000161
162 tstate->frame = NULL;
163 tstate->recursion_depth = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000164 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000165 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000166 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000167 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000168 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000169#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000170 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000171#else
172 tstate->thread_id = 0;
173#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000174
Guido van Rossumede04391998-04-10 20:18:25 +0000175 tstate->dict = NULL;
176
Guido van Rossuma027efa1997-05-05 20:56:21 +0000177 tstate->curexc_type = NULL;
178 tstate->curexc_value = NULL;
179 tstate->curexc_traceback = NULL;
180
181 tstate->exc_type = NULL;
182 tstate->exc_value = NULL;
183 tstate->exc_traceback = NULL;
184
Fred Drake5755ce62001-06-27 19:19:46 +0000185 tstate->c_profilefunc = NULL;
186 tstate->c_tracefunc = NULL;
187 tstate->c_profileobj = NULL;
188 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000189
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000190#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +0000191 _PyGILState_NoteThreadState(tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000192#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +0000193
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000194 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195 tstate->next = interp->tstate_head;
196 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000197 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000198 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199
Guido van Rossuma027efa1997-05-05 20:56:21 +0000200 return tstate;
201}
202
203
204void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000205PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000207 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000208 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000209 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000211 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000213 Py_CLEAR(tstate->dict);
214 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000215
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000216 Py_CLEAR(tstate->curexc_type);
217 Py_CLEAR(tstate->curexc_value);
218 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000219
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000220 Py_CLEAR(tstate->exc_type);
221 Py_CLEAR(tstate->exc_value);
222 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000223
Fred Drake5755ce62001-06-27 19:19:46 +0000224 tstate->c_profilefunc = NULL;
225 tstate->c_tracefunc = NULL;
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000226 Py_CLEAR(tstate->c_profileobj);
227 Py_CLEAR(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228}
229
230
Guido van Rossum29757862001-01-23 01:46:06 +0000231/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
232static void
233tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000234{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235 PyInterpreterState *interp;
236 PyThreadState **p;
237 if (tstate == NULL)
238 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239 interp = tstate->interp;
240 if (interp == NULL)
241 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000242 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000243 for (p = &interp->tstate_head; ; p = &(*p)->next) {
244 if (*p == NULL)
245 Py_FatalError(
246 "PyThreadState_Delete: invalid tstate");
247 if (*p == tstate)
248 break;
249 }
250 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000251 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000252 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000253}
254
255
Guido van Rossum29757862001-01-23 01:46:06 +0000256void
257PyThreadState_Delete(PyThreadState *tstate)
258{
259 if (tstate == _PyThreadState_Current)
260 Py_FatalError("PyThreadState_Delete: tstate is still current");
261 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000262#ifdef WITH_THREAD
263 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
264 PyThread_delete_key_value(autoTLSkey);
265#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000266}
267
268
269#ifdef WITH_THREAD
270void
271PyThreadState_DeleteCurrent()
272{
273 PyThreadState *tstate = _PyThreadState_Current;
274 if (tstate == NULL)
275 Py_FatalError(
276 "PyThreadState_DeleteCurrent: no current tstate");
277 _PyThreadState_Current = NULL;
278 tstate_delete_common(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000279 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
280 PyThread_delete_key_value(autoTLSkey);
Guido van Rossum29757862001-01-23 01:46:06 +0000281 PyEval_ReleaseLock();
282}
283#endif /* WITH_THREAD */
284
285
Guido van Rossuma027efa1997-05-05 20:56:21 +0000286PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000288{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000289 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000290 Py_FatalError("PyThreadState_Get: no current thread");
291
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000292 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000293}
294
295
296PyThreadState *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000297PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000298{
Anthony Baxter7b782b62006-04-11 12:01:56 +0000299 PyThreadState *oldts = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000300
Anthony Baxter7b782b62006-04-11 12:01:56 +0000301 _PyThreadState_Current = newts;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000302 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000303 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000304 builds.
305 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000306#if defined(Py_DEBUG) && defined(WITH_THREAD)
Anthony Baxter7b782b62006-04-11 12:01:56 +0000307 if (newts) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000308 PyThreadState *check = PyGILState_GetThisThreadState();
Anthony Baxter7b782b62006-04-11 12:01:56 +0000309 if (check && check->interp == newts->interp && check != newts)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000310 Py_FatalError("Invalid thread state for this thread");
311 }
312#endif
Anthony Baxter7b782b62006-04-11 12:01:56 +0000313 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000314}
Guido van Rossumede04391998-04-10 20:18:25 +0000315
316/* An extension mechanism to store arbitrary additional per-thread state.
317 PyThreadState_GetDict() returns a dictionary that can be used to hold such
318 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000319 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
320 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000321
322PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000323PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000324{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000325 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000326 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000327
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000328 if (_PyThreadState_Current->dict == NULL) {
329 PyObject *d;
330 _PyThreadState_Current->dict = d = PyDict_New();
331 if (d == NULL)
332 PyErr_Clear();
333 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000334 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000335}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000336
337
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000338/* Asynchronously raise an exception in a thread.
339 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000340 To prevent naive misuse, you must write your own extension
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000341 to call this. Must be called with the GIL held.
342 Returns the number of tstates modified; if it returns a number
343 greater than one, you're in trouble, and you should call it again
344 with exc=NULL to revert the effect. This raises no exceptions. */
345
346int
347PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000348 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000349 PyInterpreterState *interp = tstate->interp;
350 PyThreadState *p;
351 int count = 0;
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000352 HEAD_LOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000353 for (p = interp->tstate_head; p != NULL; p = p->next) {
354 if (p->thread_id != id)
355 continue;
Martin v. Löwisab0e2842006-04-15 18:14:21 +0000356 Py_CLEAR(p->async_exc);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000357 Py_XINCREF(exc);
358 p->async_exc = exc;
359 count += 1;
360 }
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000361 HEAD_UNLOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000362 return count;
363}
364
365
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000366/* Routines for advanced debuggers, requested by David Beazley.
367 Don't use unless you know what you are doing! */
368
369PyInterpreterState *
370PyInterpreterState_Head(void)
371{
372 return interp_head;
373}
374
375PyInterpreterState *
376PyInterpreterState_Next(PyInterpreterState *interp) {
377 return interp->next;
378}
379
380PyThreadState *
381PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
382 return interp->tstate_head;
383}
384
385PyThreadState *
386PyThreadState_Next(PyThreadState *tstate) {
387 return tstate->next;
388}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000389
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000390
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000391/* Python "auto thread state" API. */
392#ifdef WITH_THREAD
393
394/* Keep this as a static, as it is not reliable! It can only
395 ever be compared to the state for the *current* thread.
396 * If not equal, then it doesn't matter that the actual
397 value may change immediately after comparison, as it can't
398 possibly change to the current thread's state.
399 * If equal, then the current thread holds the lock, so the value can't
400 change until we yield the lock.
401*/
402static int
403PyThreadState_IsCurrent(PyThreadState *tstate)
404{
405 /* Must be the tstate for this thread */
406 assert(PyGILState_GetThisThreadState()==tstate);
407 /* On Windows at least, simple reads and writes to 32 bit values
408 are atomic.
409 */
410 return tstate == _PyThreadState_Current;
411}
412
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000413/* Internal initialization/finalization functions called by
414 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000415*/
Tim Peters19717fa2004-10-09 17:38:29 +0000416void
417_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000418{
Tim Peters19717fa2004-10-09 17:38:29 +0000419 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000420 autoTLSkey = PyThread_create_key();
421 autoInterpreterState = i;
Tim Petersf9becec2004-10-09 22:47:13 +0000422 assert(PyThread_get_key_value(autoTLSkey) == NULL);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000423 assert(t->gilstate_counter == 0);
424
425 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000426}
427
Tim Peters19717fa2004-10-09 17:38:29 +0000428void
429_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000430{
431 PyThread_delete_key(autoTLSkey);
432 autoTLSkey = 0;
433 autoInterpreterState = NULL;;
434}
435
Michael W. Hudson188d4362005-06-20 16:52:57 +0000436/* When a thread state is created for a thread by some mechanism other than
437 PyGILState_Ensure, it's important that the GILState machinery knows about
438 it so it doesn't try to create another thread state for the thread (this is
439 a better fix for SF bug #1010677 than the first one attempted).
440*/
441void
442_PyGILState_NoteThreadState(PyThreadState* tstate)
443{
444 /* If autoTLSkey is 0, this must be the very first threadstate created
445 in Py_Initialize(). Don't do anything for now (we'll be back here
446 when _PyGILState_Init is called). */
447 if (!autoTLSkey)
448 return;
449
450 /* Stick the thread state for this thread in thread local storage.
451
452 The only situation where you can legitimately have more than one
453 thread state for an OS level thread is when there are multiple
454 interpreters, when:
455
456 a) You shouldn't really be using the PyGILState_ APIs anyway,
457 and:
458
459 b) The slightly odd way PyThread_set_key_value works (see
460 comments by its implementation) means that the first thread
461 state created for that given OS level thread will "win",
462 which seems reasonable behaviour.
463 */
464 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
465 Py_FatalError("Couldn't create autoTLSkey mapping");
466
467 /* PyGILState_Release must not try to delete this thread state. */
468 tstate->gilstate_counter = 1;
469}
470
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000471/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000472PyThreadState *
473PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000474{
Tim Peters19717fa2004-10-09 17:38:29 +0000475 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000476 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000477 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000478}
479
Tim Peters19717fa2004-10-09 17:38:29 +0000480PyGILState_STATE
481PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000482{
483 int current;
484 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000485 /* Note that we do not auto-init Python here - apart from
486 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000487 spells out other issues. Embedders are expected to have
488 called Py_Initialize() and usually PyEval_InitThreads().
489 */
490 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
Anthony Baxter7b782b62006-04-11 12:01:56 +0000491 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000492 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000493 /* Create a new thread state for this thread */
494 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000495 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000496 Py_FatalError("Couldn't create thread-state for new thread");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000497 /* This is our thread state! We'll need to delete it in the
498 matching call to PyGILState_Release(). */
499 tcur->gilstate_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000500 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000501 }
502 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000503 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000504 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000505 PyEval_RestoreThread(tcur);
506 /* Update our counter in the thread-state - no need for locks:
507 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000508 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000509 to modify this value
510 */
Tim Peters19717fa2004-10-09 17:38:29 +0000511 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000512 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
513}
514
Tim Peters19717fa2004-10-09 17:38:29 +0000515void
516PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000517{
Anthony Baxter7b782b62006-04-11 12:01:56 +0000518 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
519 autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000520 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000521 Py_FatalError("auto-releasing thread-state, "
522 "but no thread-state for this thread");
523 /* We must hold the GIL and have our thread state current */
524 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000525 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000526 by release-only users can't hurt.
527 */
Tim Peters19717fa2004-10-09 17:38:29 +0000528 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000529 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000530 assert(PyThreadState_IsCurrent(tcur));
531 --tcur->gilstate_counter;
532 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000533
Tim Petersfb1ffb02004-11-08 04:30:21 +0000534 /* If we're going to destroy this thread-state, we must
535 * clear it while the GIL is held, as destructors may run.
536 */
Tim Peters19717fa2004-10-09 17:38:29 +0000537 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000538 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000539 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000540 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000541 /* Delete the thread-state. Note this releases the GIL too!
542 * It's vital that the GIL be held here, to avoid shutdown
543 * races; see bugs 225673 and 1061968 (that nasty bug has a
544 * habit of coming back).
545 */
546 PyThreadState_DeleteCurrent();
Tim Peters89c0ec92004-10-10 05:30:40 +0000547 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000548 /* Release the lock if necessary */
549 else if (oldstate == PyGILState_UNLOCKED)
Michael W. Hudson774479c2005-04-18 08:46:17 +0000550 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000551}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000552
553#ifdef __cplusplus
554}
555#endif
556
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000557#endif /* WITH_THREAD */
Anthony Baxterac6bd462006-04-13 02:06:09 +0000558
559