blob: 182560748fe432f74fb523429b77294075a86db9 [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)
39#else
40#define HEAD_INIT() /* Nothing */
41#define HEAD_LOCK() /* Nothing */
42#define HEAD_UNLOCK() /* Nothing */
43#endif
44
Guido van Rossum25ce5661997-08-02 03:10:38 +000045static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000046
Guido van Rossum18bc7c21998-12-21 18:27:28 +000047PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossum6297a7a2003-02-19 15:53:17 +000048PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000049
50
51PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000052PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000053{
Tim Peters84705582004-10-10 02:47:33 +000054 PyInterpreterState *interp = (PyInterpreterState *)
55 malloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000056
Guido van Rossuma027efa1997-05-05 20:56:21 +000057 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000058 HEAD_INIT();
Guido van Rossum25ce5661997-08-02 03:10:38 +000059 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000060 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000061 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000062 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000063 interp->codec_search_path = NULL;
64 interp->codec_search_cache = NULL;
65 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000066#ifdef HAVE_DLOPEN
67#ifdef RTLD_NOW
68 interp->dlopenflags = RTLD_NOW;
69#else
70 interp->dlopenflags = RTLD_LAZY;
71#endif
72#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000073#ifdef WITH_TSC
74 interp->tscdump = 0;
75#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000076
Tim Peters412f2462000-09-02 09:16:15 +000077 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000078 interp->next = interp_head;
79 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000080 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000081 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000082
Guido van Rossuma027efa1997-05-05 20:56:21 +000083 return interp;
84}
85
86
87void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000088PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000089{
90 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000091 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000092 for (p = interp->tstate_head; p != NULL; p = p->next)
93 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +000094 HEAD_UNLOCK();
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000095 ZAP(interp->codec_search_path);
96 ZAP(interp->codec_search_cache);
97 ZAP(interp->codec_error_registry);
Guido van Rossum25ce5661997-08-02 03:10:38 +000098 ZAP(interp->modules);
99 ZAP(interp->sysdict);
100 ZAP(interp->builtins);
101}
102
103
104static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000105zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000106{
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000107 PyThreadState *p;
108 /* No need to lock the mutex here because this should only happen
109 when the threads are all really dead (XXX famous last words). */
110 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112 }
113}
114
115
116void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000117PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000118{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000119 PyInterpreterState **p;
120 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000121 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122 for (p = &interp_head; ; p = &(*p)->next) {
123 if (*p == NULL)
124 Py_FatalError(
125 "PyInterpreterState_Delete: invalid interp");
126 if (*p == interp)
127 break;
128 }
129 if (interp->tstate_head != NULL)
130 Py_FatalError("PyInterpreterState_Delete: remaining threads");
131 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000132 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000133 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000134}
135
136
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000137/* Default implementation for _PyThreadState_GetFrame */
138static struct _frame *
139threadstate_getframe(PyThreadState *self)
140{
141 return self->frame;
142}
143
Guido van Rossuma027efa1997-05-05 20:56:21 +0000144PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000145PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000146{
Tim Peters84705582004-10-10 02:47:33 +0000147 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
148
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000149 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000150 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000151
Guido van Rossuma027efa1997-05-05 20:56:21 +0000152 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000153 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000154
155 tstate->frame = NULL;
156 tstate->recursion_depth = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000157 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000158 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000159 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000160 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000161 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000162#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000163 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000164#else
165 tstate->thread_id = 0;
166#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167
Guido van Rossumede04391998-04-10 20:18:25 +0000168 tstate->dict = NULL;
169
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170 tstate->curexc_type = NULL;
171 tstate->curexc_value = NULL;
172 tstate->curexc_traceback = NULL;
173
174 tstate->exc_type = NULL;
175 tstate->exc_value = NULL;
176 tstate->exc_traceback = NULL;
177
Fred Drake5755ce62001-06-27 19:19:46 +0000178 tstate->c_profilefunc = NULL;
179 tstate->c_tracefunc = NULL;
180 tstate->c_profileobj = NULL;
181 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000182
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000183 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000184 tstate->next = interp->tstate_head;
185 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000186 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000187 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000188
Guido van Rossuma027efa1997-05-05 20:56:21 +0000189 return tstate;
190}
191
192
193void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000194PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000196 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000198 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199
200 ZAP(tstate->frame);
201
Guido van Rossumede04391998-04-10 20:18:25 +0000202 ZAP(tstate->dict);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000203 ZAP(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000204
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205 ZAP(tstate->curexc_type);
206 ZAP(tstate->curexc_value);
207 ZAP(tstate->curexc_traceback);
208
209 ZAP(tstate->exc_type);
210 ZAP(tstate->exc_value);
211 ZAP(tstate->exc_traceback);
212
Fred Drake5755ce62001-06-27 19:19:46 +0000213 tstate->c_profilefunc = NULL;
214 tstate->c_tracefunc = NULL;
215 ZAP(tstate->c_profileobj);
216 ZAP(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000217}
218
219
Guido van Rossum29757862001-01-23 01:46:06 +0000220/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
221static void
222tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000223{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000224 PyInterpreterState *interp;
225 PyThreadState **p;
226 if (tstate == NULL)
227 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228 interp = tstate->interp;
229 if (interp == NULL)
230 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000231 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232 for (p = &interp->tstate_head; ; p = &(*p)->next) {
233 if (*p == NULL)
234 Py_FatalError(
235 "PyThreadState_Delete: invalid tstate");
236 if (*p == tstate)
237 break;
238 }
239 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000240 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000241 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000242}
243
244
Guido van Rossum29757862001-01-23 01:46:06 +0000245void
246PyThreadState_Delete(PyThreadState *tstate)
247{
248 if (tstate == _PyThreadState_Current)
249 Py_FatalError("PyThreadState_Delete: tstate is still current");
250 tstate_delete_common(tstate);
251}
252
253
254#ifdef WITH_THREAD
255void
256PyThreadState_DeleteCurrent()
257{
258 PyThreadState *tstate = _PyThreadState_Current;
259 if (tstate == NULL)
260 Py_FatalError(
261 "PyThreadState_DeleteCurrent: no current tstate");
262 _PyThreadState_Current = NULL;
263 tstate_delete_common(tstate);
264 PyEval_ReleaseLock();
265}
266#endif /* WITH_THREAD */
267
268
Guido van Rossuma027efa1997-05-05 20:56:21 +0000269PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000270PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000271{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000272 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000273 Py_FatalError("PyThreadState_Get: no current thread");
274
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000275 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000276}
277
278
279PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000280PyThreadState_Swap(PyThreadState *new)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000281{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000282 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000283
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000284 _PyThreadState_Current = new;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000285 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000286 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000287 builds.
288 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000289#if defined(Py_DEBUG) && defined(WITH_THREAD)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000290 if (new) {
291 PyThreadState *check = PyGILState_GetThisThreadState();
292 if (check && check != new)
293 Py_FatalError("Invalid thread state for this thread");
294 }
295#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000296 return old;
297}
Guido van Rossumede04391998-04-10 20:18:25 +0000298
299/* An extension mechanism to store arbitrary additional per-thread state.
300 PyThreadState_GetDict() returns a dictionary that can be used to hold such
301 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000302 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
303 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000304
305PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000306PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000307{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000308 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000309 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000310
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000311 if (_PyThreadState_Current->dict == NULL) {
312 PyObject *d;
313 _PyThreadState_Current->dict = d = PyDict_New();
314 if (d == NULL)
315 PyErr_Clear();
316 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000317 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000318}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000319
320
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000321/* Asynchronously raise an exception in a thread.
322 Requested by Just van Rossum and Alex Martelli.
323 To prevent naive misuse, you must write your own exception
324 to call this. Must be called with the GIL held.
325 Returns the number of tstates modified; if it returns a number
326 greater than one, you're in trouble, and you should call it again
327 with exc=NULL to revert the effect. This raises no exceptions. */
328
329int
330PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000331 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000332 PyInterpreterState *interp = tstate->interp;
333 PyThreadState *p;
334 int count = 0;
335 for (p = interp->tstate_head; p != NULL; p = p->next) {
336 if (p->thread_id != id)
337 continue;
338 ZAP(p->async_exc);
339 Py_XINCREF(exc);
340 p->async_exc = exc;
341 count += 1;
342 }
343 return count;
344}
345
346
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000347/* Routines for advanced debuggers, requested by David Beazley.
348 Don't use unless you know what you are doing! */
349
350PyInterpreterState *
351PyInterpreterState_Head(void)
352{
353 return interp_head;
354}
355
356PyInterpreterState *
357PyInterpreterState_Next(PyInterpreterState *interp) {
358 return interp->next;
359}
360
361PyThreadState *
362PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
363 return interp->tstate_head;
364}
365
366PyThreadState *
367PyThreadState_Next(PyThreadState *tstate) {
368 return tstate->next;
369}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000370
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000371
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000372/* Python "auto thread state" API. */
373#ifdef WITH_THREAD
374
375/* Keep this as a static, as it is not reliable! It can only
376 ever be compared to the state for the *current* thread.
377 * If not equal, then it doesn't matter that the actual
378 value may change immediately after comparison, as it can't
379 possibly change to the current thread's state.
380 * If equal, then the current thread holds the lock, so the value can't
381 change until we yield the lock.
382*/
383static int
384PyThreadState_IsCurrent(PyThreadState *tstate)
385{
386 /* Must be the tstate for this thread */
387 assert(PyGILState_GetThisThreadState()==tstate);
388 /* On Windows at least, simple reads and writes to 32 bit values
389 are atomic.
390 */
391 return tstate == _PyThreadState_Current;
392}
393
394/* The single PyInterpreterState used by this process'
395 GILState implementation
396*/
397static PyInterpreterState *autoInterpreterState = NULL;
398static int autoTLSkey = 0;
399
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000400/* Internal initialization/finalization functions called by
401 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000402*/
Tim Peters19717fa2004-10-09 17:38:29 +0000403void
404_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000405{
Tim Peters19717fa2004-10-09 17:38:29 +0000406 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000407 autoTLSkey = PyThread_create_key();
408 autoInterpreterState = i;
409 /* Now stash the thread state for this thread in TLS */
Tim Petersf9becec2004-10-09 22:47:13 +0000410 assert(PyThread_get_key_value(autoTLSkey) == NULL);
411 if (PyThread_set_key_value(autoTLSkey, (void *)t) < 0)
412 Py_FatalError("Couldn't create autoTLSkey mapping");
Tim Peters19717fa2004-10-09 17:38:29 +0000413 assert(t->gilstate_counter == 0); /* must be a new thread state */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000414 t->gilstate_counter = 1;
415}
416
Tim Peters19717fa2004-10-09 17:38:29 +0000417void
418_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000419{
420 PyThread_delete_key(autoTLSkey);
421 autoTLSkey = 0;
422 autoInterpreterState = NULL;;
423}
424
425/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000426PyThreadState *
427PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000428{
Tim Peters19717fa2004-10-09 17:38:29 +0000429 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000430 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000431 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000432}
433
Tim Peters19717fa2004-10-09 17:38:29 +0000434PyGILState_STATE
435PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000436{
437 int current;
438 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000439 /* Note that we do not auto-init Python here - apart from
440 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000441 spells out other issues. Embedders are expected to have
442 called Py_Initialize() and usually PyEval_InitThreads().
443 */
444 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
445 tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000446 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000447 /* Create a new thread state for this thread */
448 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000449 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000450 Py_FatalError("Couldn't create thread-state for new thread");
Tim Petersf9becec2004-10-09 22:47:13 +0000451 if (PyThread_set_key_value(autoTLSkey, (void *)tcur) < 0)
452 Py_FatalError("Couldn't create autoTLSkey mapping");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000453 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000454 }
455 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000456 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000457 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000458 PyEval_RestoreThread(tcur);
459 /* Update our counter in the thread-state - no need for locks:
460 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000461 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000462 to modify this value
463 */
Tim Peters19717fa2004-10-09 17:38:29 +0000464 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000465 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
466}
467
Tim Peters19717fa2004-10-09 17:38:29 +0000468void
469PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000470{
471 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000472 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000473 Py_FatalError("auto-releasing thread-state, "
474 "but no thread-state for this thread");
475 /* We must hold the GIL and have our thread state current */
476 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000477 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000478 by release-only users can't hurt.
479 */
Tim Peters19717fa2004-10-09 17:38:29 +0000480 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000481 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000482 assert(PyThreadState_IsCurrent(tcur));
483 --tcur->gilstate_counter;
484 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000485
Tim Petersfb1ffb02004-11-08 04:30:21 +0000486 /* If we're going to destroy this thread-state, we must
487 * clear it while the GIL is held, as destructors may run.
488 */
Tim Peters19717fa2004-10-09 17:38:29 +0000489 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000490 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000491 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000492 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000493 /* Delete the thread-state. Note this releases the GIL too!
494 * It's vital that the GIL be held here, to avoid shutdown
495 * races; see bugs 225673 and 1061968 (that nasty bug has a
496 * habit of coming back).
497 */
498 PyThreadState_DeleteCurrent();
499 /* Delete this thread from our TLS. */
Tim Peters89c0ec92004-10-10 05:30:40 +0000500 PyThread_delete_key_value(autoTLSkey);
Tim Peters89c0ec92004-10-10 05:30:40 +0000501 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000502 /* Release the lock if necessary */
503 else if (oldstate == PyGILState_UNLOCKED)
504 PyEval_ReleaseThread(tcur);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000505}
506#endif /* WITH_THREAD */