blob: 6584cda775a8b0a6be27cad4c3af0c3d0da1325d [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);
265}
266
267
268#ifdef WITH_THREAD
269void
270PyThreadState_DeleteCurrent()
271{
272 PyThreadState *tstate = _PyThreadState_Current;
273 if (tstate == NULL)
274 Py_FatalError(
275 "PyThreadState_DeleteCurrent: no current tstate");
276 _PyThreadState_Current = NULL;
277 tstate_delete_common(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000278 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
279 PyThread_delete_key_value(autoTLSkey);
Guido van Rossum29757862001-01-23 01:46:06 +0000280 PyEval_ReleaseLock();
281}
282#endif /* WITH_THREAD */
283
284
Guido van Rossuma027efa1997-05-05 20:56:21 +0000285PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000286PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000287{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000288 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000289 Py_FatalError("PyThreadState_Get: no current thread");
290
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000291 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000292}
293
294
295PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000296PyThreadState_Swap(PyThreadState *new)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000297{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000298 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000299
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000300 _PyThreadState_Current = new;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000301 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000302 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000303 builds.
304 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000305#if defined(Py_DEBUG) && defined(WITH_THREAD)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000306 if (new) {
307 PyThreadState *check = PyGILState_GetThisThreadState();
Michael W. Hudson867f2d42005-06-16 11:35:00 +0000308 if (check && check->interp == new->interp && check != new)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000309 Py_FatalError("Invalid thread state for this thread");
310 }
311#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000312 return old;
313}
Guido van Rossumede04391998-04-10 20:18:25 +0000314
315/* An extension mechanism to store arbitrary additional per-thread state.
316 PyThreadState_GetDict() returns a dictionary that can be used to hold such
317 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000318 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
319 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000320
321PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000323{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000324 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000325 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000326
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000327 if (_PyThreadState_Current->dict == NULL) {
328 PyObject *d;
329 _PyThreadState_Current->dict = d = PyDict_New();
330 if (d == NULL)
331 PyErr_Clear();
332 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000333 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000334}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000335
336
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000337/* Asynchronously raise an exception in a thread.
338 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000339 To prevent naive misuse, you must write your own extension
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000340 to call this. Must be called with the GIL held.
341 Returns the number of tstates modified; if it returns a number
342 greater than one, you're in trouble, and you should call it again
343 with exc=NULL to revert the effect. This raises no exceptions. */
344
345int
346PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000347 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000348 PyInterpreterState *interp = tstate->interp;
349 PyThreadState *p;
350 int count = 0;
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000351 HEAD_LOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000352 for (p = interp->tstate_head; p != NULL; p = p->next) {
353 if (p->thread_id != id)
354 continue;
355 ZAP(p->async_exc);
356 Py_XINCREF(exc);
357 p->async_exc = exc;
358 count += 1;
359 }
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000360 HEAD_UNLOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000361 return count;
362}
363
364
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000365/* Routines for advanced debuggers, requested by David Beazley.
366 Don't use unless you know what you are doing! */
367
368PyInterpreterState *
369PyInterpreterState_Head(void)
370{
371 return interp_head;
372}
373
374PyInterpreterState *
375PyInterpreterState_Next(PyInterpreterState *interp) {
376 return interp->next;
377}
378
379PyThreadState *
380PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
381 return interp->tstate_head;
382}
383
384PyThreadState *
385PyThreadState_Next(PyThreadState *tstate) {
386 return tstate->next;
387}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000388
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000389
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000390/* Python "auto thread state" API. */
391#ifdef WITH_THREAD
392
393/* Keep this as a static, as it is not reliable! It can only
394 ever be compared to the state for the *current* thread.
395 * If not equal, then it doesn't matter that the actual
396 value may change immediately after comparison, as it can't
397 possibly change to the current thread's state.
398 * If equal, then the current thread holds the lock, so the value can't
399 change until we yield the lock.
400*/
401static int
402PyThreadState_IsCurrent(PyThreadState *tstate)
403{
404 /* Must be the tstate for this thread */
405 assert(PyGILState_GetThisThreadState()==tstate);
406 /* On Windows at least, simple reads and writes to 32 bit values
407 are atomic.
408 */
409 return tstate == _PyThreadState_Current;
410}
411
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000412/* Internal initialization/finalization functions called by
413 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000414*/
Tim Peters19717fa2004-10-09 17:38:29 +0000415void
416_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000417{
Tim Peters19717fa2004-10-09 17:38:29 +0000418 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000419 autoTLSkey = PyThread_create_key();
420 autoInterpreterState = i;
Tim Petersf9becec2004-10-09 22:47:13 +0000421 assert(PyThread_get_key_value(autoTLSkey) == NULL);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000422 assert(t->gilstate_counter == 0);
423
424 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000425}
426
Tim Peters19717fa2004-10-09 17:38:29 +0000427void
428_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000429{
430 PyThread_delete_key(autoTLSkey);
431 autoTLSkey = 0;
432 autoInterpreterState = NULL;;
433}
434
Michael W. Hudson188d4362005-06-20 16:52:57 +0000435/* When a thread state is created for a thread by some mechanism other than
436 PyGILState_Ensure, it's important that the GILState machinery knows about
437 it so it doesn't try to create another thread state for the thread (this is
438 a better fix for SF bug #1010677 than the first one attempted).
439*/
440void
441_PyGILState_NoteThreadState(PyThreadState* tstate)
442{
443 /* If autoTLSkey is 0, this must be the very first threadstate created
444 in Py_Initialize(). Don't do anything for now (we'll be back here
445 when _PyGILState_Init is called). */
446 if (!autoTLSkey)
447 return;
448
449 /* Stick the thread state for this thread in thread local storage.
450
451 The only situation where you can legitimately have more than one
452 thread state for an OS level thread is when there are multiple
453 interpreters, when:
454
455 a) You shouldn't really be using the PyGILState_ APIs anyway,
456 and:
457
458 b) The slightly odd way PyThread_set_key_value works (see
459 comments by its implementation) means that the first thread
460 state created for that given OS level thread will "win",
461 which seems reasonable behaviour.
462 */
463 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
464 Py_FatalError("Couldn't create autoTLSkey mapping");
465
466 /* PyGILState_Release must not try to delete this thread state. */
467 tstate->gilstate_counter = 1;
468}
469
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000470/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000471PyThreadState *
472PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000473{
Tim Peters19717fa2004-10-09 17:38:29 +0000474 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000475 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000476 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000477}
478
Tim Peters19717fa2004-10-09 17:38:29 +0000479PyGILState_STATE
480PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000481{
482 int current;
483 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000484 /* Note that we do not auto-init Python here - apart from
485 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000486 spells out other issues. Embedders are expected to have
487 called Py_Initialize() and usually PyEval_InitThreads().
488 */
489 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
490 tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000491 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000492 /* Create a new thread state for this thread */
493 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000494 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000495 Py_FatalError("Couldn't create thread-state for new thread");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000496 /* This is our thread state! We'll need to delete it in the
497 matching call to PyGILState_Release(). */
498 tcur->gilstate_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000499 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000500 }
501 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000502 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000503 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000504 PyEval_RestoreThread(tcur);
505 /* Update our counter in the thread-state - no need for locks:
506 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000507 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000508 to modify this value
509 */
Tim Peters19717fa2004-10-09 17:38:29 +0000510 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000511 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
512}
513
Tim Peters19717fa2004-10-09 17:38:29 +0000514void
515PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000516{
517 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000518 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000519 Py_FatalError("auto-releasing thread-state, "
520 "but no thread-state for this thread");
521 /* We must hold the GIL and have our thread state current */
522 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000523 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000524 by release-only users can't hurt.
525 */
Tim Peters19717fa2004-10-09 17:38:29 +0000526 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000527 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000528 assert(PyThreadState_IsCurrent(tcur));
529 --tcur->gilstate_counter;
530 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000531
Tim Petersfb1ffb02004-11-08 04:30:21 +0000532 /* If we're going to destroy this thread-state, we must
533 * clear it while the GIL is held, as destructors may run.
534 */
Tim Peters19717fa2004-10-09 17:38:29 +0000535 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000536 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000537 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000538 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000539 /* Delete the thread-state. Note this releases the GIL too!
540 * It's vital that the GIL be held here, to avoid shutdown
541 * races; see bugs 225673 and 1061968 (that nasty bug has a
542 * habit of coming back).
543 */
544 PyThreadState_DeleteCurrent();
Tim Peters89c0ec92004-10-10 05:30:40 +0000545 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000546 /* Release the lock if necessary */
547 else if (oldstate == PyGILState_UNLOCKED)
Michael W. Hudson774479c2005-04-18 08:46:17 +0000548 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000549}
550#endif /* WITH_THREAD */