blob: 867334e81e032de60023788292aeb7339138f4be [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 Rossum25ce5661997-08-02 03:10:38 +000026#define ZAP(x) { \
27 PyObject *tmp = (PyObject *)(x); \
28 (x) = NULL; \
29 Py_XDECREF(tmp); \
30}
31
32
Guido van Rossum1d5ad901999-06-18 14:22:24 +000033#ifdef WITH_THREAD
34#include "pythread.h"
35static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000036#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000037#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
38#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
Michael W. Hudson188d4362005-06-20 16:52:57 +000039
40/* The single PyInterpreterState used by this process'
41 GILState implementation
42*/
43static PyInterpreterState *autoInterpreterState = NULL;
44static int autoTLSkey = 0;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000045#else
46#define HEAD_INIT() /* Nothing */
47#define HEAD_LOCK() /* Nothing */
48#define HEAD_UNLOCK() /* Nothing */
49#endif
50
Guido van Rossum25ce5661997-08-02 03:10:38 +000051static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000052
Guido van Rossum18bc7c21998-12-21 18:27:28 +000053PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossum6297a7a2003-02-19 15:53:17 +000054PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000055
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000056#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000057static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000058#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000059
Guido van Rossuma027efa1997-05-05 20:56:21 +000060
61PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000062PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000063{
Tim Peters84705582004-10-10 02:47:33 +000064 PyInterpreterState *interp = (PyInterpreterState *)
65 malloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000066
Guido van Rossuma027efa1997-05-05 20:56:21 +000067 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000068 HEAD_INIT();
Guido van Rossum25ce5661997-08-02 03:10:38 +000069 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000070 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000071 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000072 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000073 interp->codec_search_path = NULL;
74 interp->codec_search_cache = NULL;
75 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000076#ifdef HAVE_DLOPEN
77#ifdef RTLD_NOW
78 interp->dlopenflags = RTLD_NOW;
79#else
80 interp->dlopenflags = RTLD_LAZY;
81#endif
82#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000083#ifdef WITH_TSC
84 interp->tscdump = 0;
85#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000086
Tim Peters412f2462000-09-02 09:16:15 +000087 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000088 interp->next = interp_head;
89 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000090 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000091 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000092
Guido van Rossuma027efa1997-05-05 20:56:21 +000093 return interp;
94}
95
96
97void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000098PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000099{
100 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000101 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000102 for (p = interp->tstate_head; p != NULL; p = p->next)
103 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000104 HEAD_UNLOCK();
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000105 ZAP(interp->codec_search_path);
106 ZAP(interp->codec_search_cache);
107 ZAP(interp->codec_error_registry);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000108 ZAP(interp->modules);
109 ZAP(interp->sysdict);
110 ZAP(interp->builtins);
111}
112
113
114static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000115zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000116{
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000117 PyThreadState *p;
118 /* No need to lock the mutex here because this should only happen
119 when the threads are all really dead (XXX famous last words). */
120 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000121 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122 }
123}
124
125
126void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000128{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000129 PyInterpreterState **p;
130 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000131 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132 for (p = &interp_head; ; p = &(*p)->next) {
133 if (*p == NULL)
134 Py_FatalError(
135 "PyInterpreterState_Delete: invalid interp");
136 if (*p == interp)
137 break;
138 }
139 if (interp->tstate_head != NULL)
140 Py_FatalError("PyInterpreterState_Delete: remaining threads");
141 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000142 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000143 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000144}
145
146
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000147/* Default implementation for _PyThreadState_GetFrame */
148static struct _frame *
149threadstate_getframe(PyThreadState *self)
150{
151 return self->frame;
152}
153
Guido van Rossuma027efa1997-05-05 20:56:21 +0000154PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000155PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000156{
Tim Peters84705582004-10-10 02:47:33 +0000157 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
158
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000159 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000160 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000161
Guido van Rossuma027efa1997-05-05 20:56:21 +0000162 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000164
165 tstate->frame = NULL;
166 tstate->recursion_depth = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000168 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000169 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000170 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000171 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000172#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000173 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000174#else
175 tstate->thread_id = 0;
176#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000177
Guido van Rossumede04391998-04-10 20:18:25 +0000178 tstate->dict = NULL;
179
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180 tstate->curexc_type = NULL;
181 tstate->curexc_value = NULL;
182 tstate->curexc_traceback = NULL;
183
184 tstate->exc_type = NULL;
185 tstate->exc_value = NULL;
186 tstate->exc_traceback = NULL;
187
Fred Drake5755ce62001-06-27 19:19:46 +0000188 tstate->c_profilefunc = NULL;
189 tstate->c_tracefunc = NULL;
190 tstate->c_profileobj = NULL;
191 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000192
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000193#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +0000194 _PyGILState_NoteThreadState(tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000195#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +0000196
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000197 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000198 tstate->next = interp->tstate_head;
199 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000200 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000201 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202
Guido van Rossuma027efa1997-05-05 20:56:21 +0000203 return tstate;
204}
205
206
207void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000208PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000210 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000211 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000212 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213
214 ZAP(tstate->frame);
215
Guido van Rossumede04391998-04-10 20:18:25 +0000216 ZAP(tstate->dict);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000217 ZAP(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000218
Guido van Rossum25ce5661997-08-02 03:10:38 +0000219 ZAP(tstate->curexc_type);
220 ZAP(tstate->curexc_value);
221 ZAP(tstate->curexc_traceback);
222
223 ZAP(tstate->exc_type);
224 ZAP(tstate->exc_value);
225 ZAP(tstate->exc_traceback);
226
Fred Drake5755ce62001-06-27 19:19:46 +0000227 tstate->c_profilefunc = NULL;
228 tstate->c_tracefunc = NULL;
229 ZAP(tstate->c_profileobj);
230 ZAP(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231}
232
233
Guido van Rossum29757862001-01-23 01:46:06 +0000234/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
235static void
236tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000237{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238 PyInterpreterState *interp;
239 PyThreadState **p;
240 if (tstate == NULL)
241 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242 interp = tstate->interp;
243 if (interp == NULL)
244 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000245 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000246 for (p = &interp->tstate_head; ; p = &(*p)->next) {
247 if (*p == NULL)
248 Py_FatalError(
249 "PyThreadState_Delete: invalid tstate");
250 if (*p == tstate)
251 break;
252 }
253 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000254 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000255 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000256}
257
258
Guido van Rossum29757862001-01-23 01:46:06 +0000259void
260PyThreadState_Delete(PyThreadState *tstate)
261{
262 if (tstate == _PyThreadState_Current)
263 Py_FatalError("PyThreadState_Delete: tstate is still current");
264 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000265#ifdef WITH_THREAD
266 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
267 PyThread_delete_key_value(autoTLSkey);
268#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000269}
270
271
272#ifdef WITH_THREAD
273void
274PyThreadState_DeleteCurrent()
275{
276 PyThreadState *tstate = _PyThreadState_Current;
277 if (tstate == NULL)
278 Py_FatalError(
279 "PyThreadState_DeleteCurrent: no current tstate");
280 _PyThreadState_Current = NULL;
281 tstate_delete_common(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000282 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
283 PyThread_delete_key_value(autoTLSkey);
Guido van Rossum29757862001-01-23 01:46:06 +0000284 PyEval_ReleaseLock();
285}
286#endif /* WITH_THREAD */
287
288
Guido van Rossuma027efa1997-05-05 20:56:21 +0000289PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000290PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000291{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000292 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000293 Py_FatalError("PyThreadState_Get: no current thread");
294
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000295 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000296}
297
298
299PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000300PyThreadState_Swap(PyThreadState *new)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000301{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000302 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000303
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000304 _PyThreadState_Current = new;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000305 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000306 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000307 builds.
308 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000309#if defined(Py_DEBUG) && defined(WITH_THREAD)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000310 if (new) {
311 PyThreadState *check = PyGILState_GetThisThreadState();
Michael W. Hudson867f2d42005-06-16 11:35:00 +0000312 if (check && check->interp == new->interp && check != new)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000313 Py_FatalError("Invalid thread state for this thread");
314 }
315#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000316 return old;
317}
Guido van Rossumede04391998-04-10 20:18:25 +0000318
319/* An extension mechanism to store arbitrary additional per-thread state.
320 PyThreadState_GetDict() returns a dictionary that can be used to hold such
321 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000322 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
323 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000324
325PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000327{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000328 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000329 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000330
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000331 if (_PyThreadState_Current->dict == NULL) {
332 PyObject *d;
333 _PyThreadState_Current->dict = d = PyDict_New();
334 if (d == NULL)
335 PyErr_Clear();
336 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000337 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000338}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000339
340
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000341/* Asynchronously raise an exception in a thread.
342 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000343 To prevent naive misuse, you must write your own extension
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000344 to call this. Must be called with the GIL held.
345 Returns the number of tstates modified; if it returns a number
346 greater than one, you're in trouble, and you should call it again
347 with exc=NULL to revert the effect. This raises no exceptions. */
348
349int
350PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000351 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000352 PyInterpreterState *interp = tstate->interp;
353 PyThreadState *p;
354 int count = 0;
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000355 HEAD_LOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000356 for (p = interp->tstate_head; p != NULL; p = p->next) {
357 if (p->thread_id != id)
358 continue;
359 ZAP(p->async_exc);
360 Py_XINCREF(exc);
361 p->async_exc = exc;
362 count += 1;
363 }
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000364 HEAD_UNLOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000365 return count;
366}
367
368
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000369/* Routines for advanced debuggers, requested by David Beazley.
370 Don't use unless you know what you are doing! */
371
372PyInterpreterState *
373PyInterpreterState_Head(void)
374{
375 return interp_head;
376}
377
378PyInterpreterState *
379PyInterpreterState_Next(PyInterpreterState *interp) {
380 return interp->next;
381}
382
383PyThreadState *
384PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
385 return interp->tstate_head;
386}
387
388PyThreadState *
389PyThreadState_Next(PyThreadState *tstate) {
390 return tstate->next;
391}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000392
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000393
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000394/* Python "auto thread state" API. */
395#ifdef WITH_THREAD
396
397/* Keep this as a static, as it is not reliable! It can only
398 ever be compared to the state for the *current* thread.
399 * If not equal, then it doesn't matter that the actual
400 value may change immediately after comparison, as it can't
401 possibly change to the current thread's state.
402 * If equal, then the current thread holds the lock, so the value can't
403 change until we yield the lock.
404*/
405static int
406PyThreadState_IsCurrent(PyThreadState *tstate)
407{
408 /* Must be the tstate for this thread */
409 assert(PyGILState_GetThisThreadState()==tstate);
410 /* On Windows at least, simple reads and writes to 32 bit values
411 are atomic.
412 */
413 return tstate == _PyThreadState_Current;
414}
415
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000416/* Internal initialization/finalization functions called by
417 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000418*/
Tim Peters19717fa2004-10-09 17:38:29 +0000419void
420_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000421{
Tim Peters19717fa2004-10-09 17:38:29 +0000422 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000423 autoTLSkey = PyThread_create_key();
424 autoInterpreterState = i;
Tim Petersf9becec2004-10-09 22:47:13 +0000425 assert(PyThread_get_key_value(autoTLSkey) == NULL);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000426 assert(t->gilstate_counter == 0);
427
428 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000429}
430
Tim Peters19717fa2004-10-09 17:38:29 +0000431void
432_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000433{
434 PyThread_delete_key(autoTLSkey);
435 autoTLSkey = 0;
436 autoInterpreterState = NULL;;
437}
438
Michael W. Hudson188d4362005-06-20 16:52:57 +0000439/* When a thread state is created for a thread by some mechanism other than
440 PyGILState_Ensure, it's important that the GILState machinery knows about
441 it so it doesn't try to create another thread state for the thread (this is
442 a better fix for SF bug #1010677 than the first one attempted).
443*/
444void
445_PyGILState_NoteThreadState(PyThreadState* tstate)
446{
447 /* If autoTLSkey is 0, this must be the very first threadstate created
448 in Py_Initialize(). Don't do anything for now (we'll be back here
449 when _PyGILState_Init is called). */
450 if (!autoTLSkey)
451 return;
452
453 /* Stick the thread state for this thread in thread local storage.
454
455 The only situation where you can legitimately have more than one
456 thread state for an OS level thread is when there are multiple
457 interpreters, when:
458
459 a) You shouldn't really be using the PyGILState_ APIs anyway,
460 and:
461
462 b) The slightly odd way PyThread_set_key_value works (see
463 comments by its implementation) means that the first thread
464 state created for that given OS level thread will "win",
465 which seems reasonable behaviour.
466 */
467 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
468 Py_FatalError("Couldn't create autoTLSkey mapping");
469
470 /* PyGILState_Release must not try to delete this thread state. */
471 tstate->gilstate_counter = 1;
472}
473
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000474/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000475PyThreadState *
476PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000477{
Tim Peters19717fa2004-10-09 17:38:29 +0000478 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000479 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000480 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000481}
482
Tim Peters19717fa2004-10-09 17:38:29 +0000483PyGILState_STATE
484PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000485{
486 int current;
487 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000488 /* Note that we do not auto-init Python here - apart from
489 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000490 spells out other issues. Embedders are expected to have
491 called Py_Initialize() and usually PyEval_InitThreads().
492 */
493 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
494 tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000495 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000496 /* Create a new thread state for this thread */
497 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000498 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000499 Py_FatalError("Couldn't create thread-state for new thread");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000500 /* This is our thread state! We'll need to delete it in the
501 matching call to PyGILState_Release(). */
502 tcur->gilstate_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000503 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000504 }
505 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000506 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000507 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000508 PyEval_RestoreThread(tcur);
509 /* Update our counter in the thread-state - no need for locks:
510 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000511 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000512 to modify this value
513 */
Tim Peters19717fa2004-10-09 17:38:29 +0000514 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000515 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
516}
517
Tim Peters19717fa2004-10-09 17:38:29 +0000518void
519PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000520{
521 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000522 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000523 Py_FatalError("auto-releasing thread-state, "
524 "but no thread-state for this thread");
525 /* We must hold the GIL and have our thread state current */
526 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000527 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000528 by release-only users can't hurt.
529 */
Tim Peters19717fa2004-10-09 17:38:29 +0000530 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000531 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000532 assert(PyThreadState_IsCurrent(tcur));
533 --tcur->gilstate_counter;
534 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000535
Tim Petersfb1ffb02004-11-08 04:30:21 +0000536 /* If we're going to destroy this thread-state, we must
537 * clear it while the GIL is held, as destructors may run.
538 */
Tim Peters19717fa2004-10-09 17:38:29 +0000539 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000540 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000541 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000542 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000543 /* Delete the thread-state. Note this releases the GIL too!
544 * It's vital that the GIL be held here, to avoid shutdown
545 * races; see bugs 225673 and 1061968 (that nasty bug has a
546 * habit of coming back).
547 */
548 PyThreadState_DeleteCurrent();
Tim Peters89c0ec92004-10-10 05:30:40 +0000549 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000550 /* Release the lock if necessary */
551 else if (oldstate == PyGILState_UNLOCKED)
Michael W. Hudson774479c2005-04-18 08:46:17 +0000552 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000553}
554#endif /* WITH_THREAD */