blob: abca8ddfdff1444f3ee9528084ee05c9e28ca302 [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.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000323 To prevent naive misuse, you must write your own extension
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000324 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;
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000335 HEAD_LOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000336 for (p = interp->tstate_head; p != NULL; p = p->next) {
337 if (p->thread_id != id)
338 continue;
339 ZAP(p->async_exc);
340 Py_XINCREF(exc);
341 p->async_exc = exc;
342 count += 1;
343 }
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000344 HEAD_UNLOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000345 return count;
346}
347
348
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000349/* Routines for advanced debuggers, requested by David Beazley.
350 Don't use unless you know what you are doing! */
351
352PyInterpreterState *
353PyInterpreterState_Head(void)
354{
355 return interp_head;
356}
357
358PyInterpreterState *
359PyInterpreterState_Next(PyInterpreterState *interp) {
360 return interp->next;
361}
362
363PyThreadState *
364PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
365 return interp->tstate_head;
366}
367
368PyThreadState *
369PyThreadState_Next(PyThreadState *tstate) {
370 return tstate->next;
371}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000372
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000373
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000374/* Python "auto thread state" API. */
375#ifdef WITH_THREAD
376
377/* Keep this as a static, as it is not reliable! It can only
378 ever be compared to the state for the *current* thread.
379 * If not equal, then it doesn't matter that the actual
380 value may change immediately after comparison, as it can't
381 possibly change to the current thread's state.
382 * If equal, then the current thread holds the lock, so the value can't
383 change until we yield the lock.
384*/
385static int
386PyThreadState_IsCurrent(PyThreadState *tstate)
387{
388 /* Must be the tstate for this thread */
389 assert(PyGILState_GetThisThreadState()==tstate);
390 /* On Windows at least, simple reads and writes to 32 bit values
391 are atomic.
392 */
393 return tstate == _PyThreadState_Current;
394}
395
396/* The single PyInterpreterState used by this process'
397 GILState implementation
398*/
399static PyInterpreterState *autoInterpreterState = NULL;
400static int autoTLSkey = 0;
401
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000402/* Internal initialization/finalization functions called by
403 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000404*/
Tim Peters19717fa2004-10-09 17:38:29 +0000405void
406_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000407{
Tim Peters19717fa2004-10-09 17:38:29 +0000408 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000409 autoTLSkey = PyThread_create_key();
410 autoInterpreterState = i;
411 /* Now stash the thread state for this thread in TLS */
Tim Petersf9becec2004-10-09 22:47:13 +0000412 assert(PyThread_get_key_value(autoTLSkey) == NULL);
413 if (PyThread_set_key_value(autoTLSkey, (void *)t) < 0)
414 Py_FatalError("Couldn't create autoTLSkey mapping");
Tim Peters19717fa2004-10-09 17:38:29 +0000415 assert(t->gilstate_counter == 0); /* must be a new thread state */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000416 t->gilstate_counter = 1;
417}
418
Tim Peters19717fa2004-10-09 17:38:29 +0000419void
420_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000421{
422 PyThread_delete_key(autoTLSkey);
423 autoTLSkey = 0;
424 autoInterpreterState = NULL;;
425}
426
427/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000428PyThreadState *
429PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000430{
Tim Peters19717fa2004-10-09 17:38:29 +0000431 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000432 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000433 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000434}
435
Tim Peters19717fa2004-10-09 17:38:29 +0000436PyGILState_STATE
437PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000438{
439 int current;
440 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000441 /* Note that we do not auto-init Python here - apart from
442 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000443 spells out other issues. Embedders are expected to have
444 called Py_Initialize() and usually PyEval_InitThreads().
445 */
446 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
447 tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000448 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000449 /* Create a new thread state for this thread */
450 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000451 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000452 Py_FatalError("Couldn't create thread-state for new thread");
Tim Petersf9becec2004-10-09 22:47:13 +0000453 if (PyThread_set_key_value(autoTLSkey, (void *)tcur) < 0)
454 Py_FatalError("Couldn't create autoTLSkey mapping");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000455 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000456 }
457 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000458 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000459 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000460 PyEval_RestoreThread(tcur);
461 /* Update our counter in the thread-state - no need for locks:
462 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000463 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000464 to modify this value
465 */
Tim Peters19717fa2004-10-09 17:38:29 +0000466 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000467 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
468}
469
Tim Peters19717fa2004-10-09 17:38:29 +0000470void
471PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000472{
473 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000474 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000475 Py_FatalError("auto-releasing thread-state, "
476 "but no thread-state for this thread");
477 /* We must hold the GIL and have our thread state current */
478 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000479 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000480 by release-only users can't hurt.
481 */
Tim Peters19717fa2004-10-09 17:38:29 +0000482 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000483 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000484 assert(PyThreadState_IsCurrent(tcur));
485 --tcur->gilstate_counter;
486 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000487
Tim Petersfb1ffb02004-11-08 04:30:21 +0000488 /* If we're going to destroy this thread-state, we must
489 * clear it while the GIL is held, as destructors may run.
490 */
Tim Peters19717fa2004-10-09 17:38:29 +0000491 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000492 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000493 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000494 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000495 /* Delete the thread-state. Note this releases the GIL too!
496 * It's vital that the GIL be held here, to avoid shutdown
497 * races; see bugs 225673 and 1061968 (that nasty bug has a
498 * habit of coming back).
499 */
500 PyThreadState_DeleteCurrent();
501 /* Delete this thread from our TLS. */
Tim Peters89c0ec92004-10-10 05:30:40 +0000502 PyThread_delete_key_value(autoTLSkey);
Tim Peters89c0ec92004-10-10 05:30:40 +0000503 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000504 /* Release the lock if necessary */
505 else if (oldstate == PyGILState_UNLOCKED)
Michael W. Hudson774479c2005-04-18 08:46:17 +0000506 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000507}
508#endif /* WITH_THREAD */