blob: d88d2d116e8289680381ef6cf2619435a8c9bca0 [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
Martin v. Löwisf0473d52001-07-18 16:17:16 +00006#ifdef HAVE_DLOPEN
7#ifdef HAVE_DLFCN_H
8#include <dlfcn.h>
9#endif
10#ifndef RTLD_LAZY
11#define RTLD_LAZY 1
12#endif
13#endif
14
15
Guido van Rossum25ce5661997-08-02 03:10:38 +000016#define ZAP(x) { \
17 PyObject *tmp = (PyObject *)(x); \
18 (x) = NULL; \
19 Py_XDECREF(tmp); \
20}
21
22
Guido van Rossum1d5ad901999-06-18 14:22:24 +000023#ifdef WITH_THREAD
24#include "pythread.h"
25static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000026#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000027#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
28#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
29#else
30#define HEAD_INIT() /* Nothing */
31#define HEAD_LOCK() /* Nothing */
32#define HEAD_UNLOCK() /* Nothing */
33#endif
34
Guido van Rossum25ce5661997-08-02 03:10:38 +000035static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000036
Guido van Rossum18bc7c21998-12-21 18:27:28 +000037PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossum6297a7a2003-02-19 15:53:17 +000038PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000039
40
41PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000042PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000043{
44 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +000045
Guido van Rossuma027efa1997-05-05 20:56:21 +000046 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000047 HEAD_INIT();
Guido van Rossum25ce5661997-08-02 03:10:38 +000048 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000049 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000050 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000051 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000052 interp->codec_search_path = NULL;
53 interp->codec_search_cache = NULL;
54 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000055#ifdef HAVE_DLOPEN
56#ifdef RTLD_NOW
57 interp->dlopenflags = RTLD_NOW;
58#else
59 interp->dlopenflags = RTLD_LAZY;
60#endif
61#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062#ifdef WITH_TSC
63 interp->tscdump = 0;
64#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000065
Tim Peters412f2462000-09-02 09:16:15 +000066 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000067 interp->next = interp_head;
68 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000069 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000070 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000071
Guido van Rossuma027efa1997-05-05 20:56:21 +000072 return interp;
73}
74
75
76void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000077PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000078{
79 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000080 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000081 for (p = interp->tstate_head; p != NULL; p = p->next)
82 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +000083 HEAD_UNLOCK();
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000084 ZAP(interp->codec_search_path);
85 ZAP(interp->codec_search_cache);
86 ZAP(interp->codec_error_registry);
Guido van Rossum25ce5661997-08-02 03:10:38 +000087 ZAP(interp->modules);
88 ZAP(interp->sysdict);
89 ZAP(interp->builtins);
90}
91
92
93static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000094zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000095{
Guido van Rossum1d5ad901999-06-18 14:22:24 +000096 PyThreadState *p;
97 /* No need to lock the mutex here because this should only happen
98 when the threads are all really dead (XXX famous last words). */
99 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000101 }
102}
103
104
105void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000106PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000107{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000108 PyInterpreterState **p;
109 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000110 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111 for (p = &interp_head; ; p = &(*p)->next) {
112 if (*p == NULL)
113 Py_FatalError(
114 "PyInterpreterState_Delete: invalid interp");
115 if (*p == interp)
116 break;
117 }
118 if (interp->tstate_head != NULL)
119 Py_FatalError("PyInterpreterState_Delete: remaining threads");
120 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000121 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000122 PyMem_DEL(interp);
123}
124
125
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000126/* Default implementation for _PyThreadState_GetFrame */
127static struct _frame *
128threadstate_getframe(PyThreadState *self)
129{
130 return self->frame;
131}
132
Guido van Rossuma027efa1997-05-05 20:56:21 +0000133PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000134PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000135{
136 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000137 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000138 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000139
Guido van Rossuma027efa1997-05-05 20:56:21 +0000140 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000141 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000142
143 tstate->frame = NULL;
144 tstate->recursion_depth = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000145 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000146 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000147 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000148 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000149 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000150#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000151 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000152#else
153 tstate->thread_id = 0;
154#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000155
Guido van Rossumede04391998-04-10 20:18:25 +0000156 tstate->dict = NULL;
157
Guido van Rossuma027efa1997-05-05 20:56:21 +0000158 tstate->curexc_type = NULL;
159 tstate->curexc_value = NULL;
160 tstate->curexc_traceback = NULL;
161
162 tstate->exc_type = NULL;
163 tstate->exc_value = NULL;
164 tstate->exc_traceback = NULL;
165
Fred Drake5755ce62001-06-27 19:19:46 +0000166 tstate->c_profilefunc = NULL;
167 tstate->c_tracefunc = NULL;
168 tstate->c_profileobj = NULL;
169 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000171 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000172 tstate->next = interp->tstate_head;
173 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000174 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000175 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000176
Guido van Rossuma027efa1997-05-05 20:56:21 +0000177 return tstate;
178}
179
180
181void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000182PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000183{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000184 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000185 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000186 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000187
188 ZAP(tstate->frame);
189
Guido van Rossumede04391998-04-10 20:18:25 +0000190 ZAP(tstate->dict);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000191 ZAP(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000192
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193 ZAP(tstate->curexc_type);
194 ZAP(tstate->curexc_value);
195 ZAP(tstate->curexc_traceback);
196
197 ZAP(tstate->exc_type);
198 ZAP(tstate->exc_value);
199 ZAP(tstate->exc_traceback);
200
Fred Drake5755ce62001-06-27 19:19:46 +0000201 tstate->c_profilefunc = NULL;
202 tstate->c_tracefunc = NULL;
203 ZAP(tstate->c_profileobj);
204 ZAP(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205}
206
207
Guido van Rossum29757862001-01-23 01:46:06 +0000208/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
209static void
210tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000211{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212 PyInterpreterState *interp;
213 PyThreadState **p;
214 if (tstate == NULL)
215 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000216 interp = tstate->interp;
217 if (interp == NULL)
218 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000219 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000220 for (p = &interp->tstate_head; ; p = &(*p)->next) {
221 if (*p == NULL)
222 Py_FatalError(
223 "PyThreadState_Delete: invalid tstate");
224 if (*p == tstate)
225 break;
226 }
227 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000228 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000229 PyMem_DEL(tstate);
230}
231
232
Guido van Rossum29757862001-01-23 01:46:06 +0000233void
234PyThreadState_Delete(PyThreadState *tstate)
235{
236 if (tstate == _PyThreadState_Current)
237 Py_FatalError("PyThreadState_Delete: tstate is still current");
238 tstate_delete_common(tstate);
239}
240
241
242#ifdef WITH_THREAD
243void
244PyThreadState_DeleteCurrent()
245{
246 PyThreadState *tstate = _PyThreadState_Current;
247 if (tstate == NULL)
248 Py_FatalError(
249 "PyThreadState_DeleteCurrent: no current tstate");
250 _PyThreadState_Current = NULL;
251 tstate_delete_common(tstate);
252 PyEval_ReleaseLock();
253}
254#endif /* WITH_THREAD */
255
256
Guido van Rossuma027efa1997-05-05 20:56:21 +0000257PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000259{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000260 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000261 Py_FatalError("PyThreadState_Get: no current thread");
262
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000263 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000264}
265
266
267PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000268PyThreadState_Swap(PyThreadState *new)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000269{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000270 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000271
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000272 _PyThreadState_Current = new;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000273 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000274 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000275 builds.
276 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000277#if defined(Py_DEBUG) && defined(WITH_THREAD)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000278 if (new) {
279 PyThreadState *check = PyGILState_GetThisThreadState();
280 if (check && check != new)
281 Py_FatalError("Invalid thread state for this thread");
282 }
283#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000284 return old;
285}
Guido van Rossumede04391998-04-10 20:18:25 +0000286
287/* An extension mechanism to store arbitrary additional per-thread state.
288 PyThreadState_GetDict() returns a dictionary that can be used to hold such
289 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000290 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
291 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000292
293PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000294PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000295{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000296 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000297 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000298
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000299 if (_PyThreadState_Current->dict == NULL) {
300 PyObject *d;
301 _PyThreadState_Current->dict = d = PyDict_New();
302 if (d == NULL)
303 PyErr_Clear();
304 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000305 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000306}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000307
308
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000309/* Asynchronously raise an exception in a thread.
310 Requested by Just van Rossum and Alex Martelli.
311 To prevent naive misuse, you must write your own exception
312 to call this. Must be called with the GIL held.
313 Returns the number of tstates modified; if it returns a number
314 greater than one, you're in trouble, and you should call it again
315 with exc=NULL to revert the effect. This raises no exceptions. */
316
317int
318PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000319 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000320 PyInterpreterState *interp = tstate->interp;
321 PyThreadState *p;
322 int count = 0;
323 for (p = interp->tstate_head; p != NULL; p = p->next) {
324 if (p->thread_id != id)
325 continue;
326 ZAP(p->async_exc);
327 Py_XINCREF(exc);
328 p->async_exc = exc;
329 count += 1;
330 }
331 return count;
332}
333
334
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000335/* Routines for advanced debuggers, requested by David Beazley.
336 Don't use unless you know what you are doing! */
337
338PyInterpreterState *
339PyInterpreterState_Head(void)
340{
341 return interp_head;
342}
343
344PyInterpreterState *
345PyInterpreterState_Next(PyInterpreterState *interp) {
346 return interp->next;
347}
348
349PyThreadState *
350PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
351 return interp->tstate_head;
352}
353
354PyThreadState *
355PyThreadState_Next(PyThreadState *tstate) {
356 return tstate->next;
357}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000358
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000359
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000360/* Python "auto thread state" API. */
361#ifdef WITH_THREAD
362
363/* Keep this as a static, as it is not reliable! It can only
364 ever be compared to the state for the *current* thread.
365 * If not equal, then it doesn't matter that the actual
366 value may change immediately after comparison, as it can't
367 possibly change to the current thread's state.
368 * If equal, then the current thread holds the lock, so the value can't
369 change until we yield the lock.
370*/
371static int
372PyThreadState_IsCurrent(PyThreadState *tstate)
373{
374 /* Must be the tstate for this thread */
375 assert(PyGILState_GetThisThreadState()==tstate);
376 /* On Windows at least, simple reads and writes to 32 bit values
377 are atomic.
378 */
379 return tstate == _PyThreadState_Current;
380}
381
382/* The single PyInterpreterState used by this process'
383 GILState implementation
384*/
385static PyInterpreterState *autoInterpreterState = NULL;
386static int autoTLSkey = 0;
387
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000388/* Internal initialization/finalization functions called by
389 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000390*/
Tim Peters19717fa2004-10-09 17:38:29 +0000391void
392_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000393{
Tim Peters19717fa2004-10-09 17:38:29 +0000394 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000395 autoTLSkey = PyThread_create_key();
396 autoInterpreterState = i;
397 /* Now stash the thread state for this thread in TLS */
398 PyThread_set_key_value(autoTLSkey, (void *)t);
Tim Peters19717fa2004-10-09 17:38:29 +0000399 assert(t->gilstate_counter == 0); /* must be a new thread state */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000400 t->gilstate_counter = 1;
401}
402
Tim Peters19717fa2004-10-09 17:38:29 +0000403void
404_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000405{
406 PyThread_delete_key(autoTLSkey);
407 autoTLSkey = 0;
408 autoInterpreterState = NULL;;
409}
410
411/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000412PyThreadState *
413PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000414{
Tim Peters19717fa2004-10-09 17:38:29 +0000415 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000416 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000417 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000418}
419
Tim Peters19717fa2004-10-09 17:38:29 +0000420PyGILState_STATE
421PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000422{
423 int current;
424 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000425 /* Note that we do not auto-init Python here - apart from
426 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000427 spells out other issues. Embedders are expected to have
428 called Py_Initialize() and usually PyEval_InitThreads().
429 */
430 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
431 tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000432 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000433 /* Create a new thread state for this thread */
434 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000435 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000436 Py_FatalError("Couldn't create thread-state for new thread");
437 PyThread_set_key_value(autoTLSkey, (void *)tcur);
438 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000439 }
440 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000441 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000442 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000443 PyEval_RestoreThread(tcur);
444 /* Update our counter in the thread-state - no need for locks:
445 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000446 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000447 to modify this value
448 */
Tim Peters19717fa2004-10-09 17:38:29 +0000449 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000450 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
451}
452
Tim Peters19717fa2004-10-09 17:38:29 +0000453void
454PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000455{
456 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000457 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000458 Py_FatalError("auto-releasing thread-state, "
459 "but no thread-state for this thread");
460 /* We must hold the GIL and have our thread state current */
461 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000462 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000463 by release-only users can't hurt.
464 */
Tim Peters19717fa2004-10-09 17:38:29 +0000465 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000466 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000467 assert(PyThreadState_IsCurrent(tcur));
468 --tcur->gilstate_counter;
469 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000470
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000471 /* If we are about to destroy this thread-state, we must
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000472 clear it while the lock is held, as destructors may run
473 */
Tim Peters19717fa2004-10-09 17:38:29 +0000474 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000475 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000476 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000477 PyThreadState_Clear(tcur);
478 }
479
480 /* Release the lock if necessary */
Tim Peters19717fa2004-10-09 17:38:29 +0000481 if (oldstate == PyGILState_UNLOCKED)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000482 PyEval_ReleaseThread(tcur);
483
484 /* Now complete destruction of the thread if necessary */
Tim Peters19717fa2004-10-09 17:38:29 +0000485 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000486 /* Delete this thread from our TLS */
487 PyThread_delete_key_value(autoTLSkey);
488 /* Delete the thread-state */
489 PyThreadState_Delete(tcur);
490 }
491}
492#endif /* WITH_THREAD */