blob: 3ac799ce8862560720f10d375e855e136bdb6719 [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. Hudson188d4362005-06-20 16:52:57 +000056static void _PyGILState_NoteThreadState(PyThreadState* tstate);
57
Guido van Rossuma027efa1997-05-05 20:56:21 +000058
59PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000060PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000061{
Tim Peters84705582004-10-10 02:47:33 +000062 PyInterpreterState *interp = (PyInterpreterState *)
63 malloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000064
Guido van Rossuma027efa1997-05-05 20:56:21 +000065 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000066 HEAD_INIT();
Guido van Rossum25ce5661997-08-02 03:10:38 +000067 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000068 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000069 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000070 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000071 interp->codec_search_path = NULL;
72 interp->codec_search_cache = NULL;
73 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000074#ifdef HAVE_DLOPEN
75#ifdef RTLD_NOW
76 interp->dlopenflags = RTLD_NOW;
77#else
78 interp->dlopenflags = RTLD_LAZY;
79#endif
80#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081#ifdef WITH_TSC
82 interp->tscdump = 0;
83#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000084
Tim Peters412f2462000-09-02 09:16:15 +000085 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000086 interp->next = interp_head;
87 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000088 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000089 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000090
Guido van Rossuma027efa1997-05-05 20:56:21 +000091 return interp;
92}
93
94
95void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000096PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000097{
98 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000099 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100 for (p = interp->tstate_head; p != NULL; p = p->next)
101 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000102 HEAD_UNLOCK();
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000103 ZAP(interp->codec_search_path);
104 ZAP(interp->codec_search_cache);
105 ZAP(interp->codec_error_registry);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000106 ZAP(interp->modules);
107 ZAP(interp->sysdict);
108 ZAP(interp->builtins);
109}
110
111
112static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000113zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000114{
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000115 PyThreadState *p;
116 /* No need to lock the mutex here because this should only happen
117 when the threads are all really dead (XXX famous last words). */
118 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000119 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000120 }
121}
122
123
124void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000126{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127 PyInterpreterState **p;
128 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000129 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000130 for (p = &interp_head; ; p = &(*p)->next) {
131 if (*p == NULL)
132 Py_FatalError(
133 "PyInterpreterState_Delete: invalid interp");
134 if (*p == interp)
135 break;
136 }
137 if (interp->tstate_head != NULL)
138 Py_FatalError("PyInterpreterState_Delete: remaining threads");
139 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000140 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000141 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000142}
143
144
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000145/* Default implementation for _PyThreadState_GetFrame */
146static struct _frame *
147threadstate_getframe(PyThreadState *self)
148{
149 return self->frame;
150}
151
Guido van Rossuma027efa1997-05-05 20:56:21 +0000152PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000153PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000154{
Tim Peters84705582004-10-10 02:47:33 +0000155 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
156
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000157 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000158 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000159
Guido van Rossuma027efa1997-05-05 20:56:21 +0000160 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000161 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000162
163 tstate->frame = NULL;
164 tstate->recursion_depth = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000165 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000166 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000167 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000168 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000169 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000170#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000171 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000172#else
173 tstate->thread_id = 0;
174#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000175
Guido van Rossumede04391998-04-10 20:18:25 +0000176 tstate->dict = NULL;
177
Guido van Rossuma027efa1997-05-05 20:56:21 +0000178 tstate->curexc_type = NULL;
179 tstate->curexc_value = NULL;
180 tstate->curexc_traceback = NULL;
181
182 tstate->exc_type = NULL;
183 tstate->exc_value = NULL;
184 tstate->exc_traceback = NULL;
185
Fred Drake5755ce62001-06-27 19:19:46 +0000186 tstate->c_profilefunc = NULL;
187 tstate->c_tracefunc = NULL;
188 tstate->c_profileobj = NULL;
189 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000190
Michael W. Hudson188d4362005-06-20 16:52:57 +0000191 _PyGILState_NoteThreadState(tstate);
192
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000193 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194 tstate->next = interp->tstate_head;
195 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000196 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000197 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000198
Guido van Rossuma027efa1997-05-05 20:56:21 +0000199 return tstate;
200}
201
202
203void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000204PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000206 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000207 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000208 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209
210 ZAP(tstate->frame);
211
Guido van Rossumede04391998-04-10 20:18:25 +0000212 ZAP(tstate->dict);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000213 ZAP(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000214
Guido van Rossum25ce5661997-08-02 03:10:38 +0000215 ZAP(tstate->curexc_type);
216 ZAP(tstate->curexc_value);
217 ZAP(tstate->curexc_traceback);
218
219 ZAP(tstate->exc_type);
220 ZAP(tstate->exc_value);
221 ZAP(tstate->exc_traceback);
222
Fred Drake5755ce62001-06-27 19:19:46 +0000223 tstate->c_profilefunc = NULL;
224 tstate->c_tracefunc = NULL;
225 ZAP(tstate->c_profileobj);
226 ZAP(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000227}
228
229
Guido van Rossum29757862001-01-23 01:46:06 +0000230/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
231static void
232tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000233{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234 PyInterpreterState *interp;
235 PyThreadState **p;
236 if (tstate == NULL)
237 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238 interp = tstate->interp;
239 if (interp == NULL)
240 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000241 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242 for (p = &interp->tstate_head; ; p = &(*p)->next) {
243 if (*p == NULL)
244 Py_FatalError(
245 "PyThreadState_Delete: invalid tstate");
246 if (*p == tstate)
247 break;
248 }
249 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000250 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000251 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000252}
253
254
Guido van Rossum29757862001-01-23 01:46:06 +0000255void
256PyThreadState_Delete(PyThreadState *tstate)
257{
258 if (tstate == _PyThreadState_Current)
259 Py_FatalError("PyThreadState_Delete: tstate is still current");
260 tstate_delete_common(tstate);
261}
262
263
264#ifdef WITH_THREAD
265void
266PyThreadState_DeleteCurrent()
267{
268 PyThreadState *tstate = _PyThreadState_Current;
269 if (tstate == NULL)
270 Py_FatalError(
271 "PyThreadState_DeleteCurrent: no current tstate");
272 _PyThreadState_Current = NULL;
273 tstate_delete_common(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000274 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
275 PyThread_delete_key_value(autoTLSkey);
Guido van Rossum29757862001-01-23 01:46:06 +0000276 PyEval_ReleaseLock();
277}
278#endif /* WITH_THREAD */
279
280
Guido van Rossuma027efa1997-05-05 20:56:21 +0000281PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000282PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000283{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000284 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000285 Py_FatalError("PyThreadState_Get: no current thread");
286
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000287 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000288}
289
290
291PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000292PyThreadState_Swap(PyThreadState *new)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000293{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000294 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000295
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000296 _PyThreadState_Current = new;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000297 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000298 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000299 builds.
300 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000301#if defined(Py_DEBUG) && defined(WITH_THREAD)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000302 if (new) {
303 PyThreadState *check = PyGILState_GetThisThreadState();
Michael W. Hudson867f2d42005-06-16 11:35:00 +0000304 if (check && check->interp == new->interp && check != new)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000305 Py_FatalError("Invalid thread state for this thread");
306 }
307#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000308 return old;
309}
Guido van Rossumede04391998-04-10 20:18:25 +0000310
311/* An extension mechanism to store arbitrary additional per-thread state.
312 PyThreadState_GetDict() returns a dictionary that can be used to hold such
313 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000314 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
315 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000316
317PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000318PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000319{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000320 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000321 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000322
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000323 if (_PyThreadState_Current->dict == NULL) {
324 PyObject *d;
325 _PyThreadState_Current->dict = d = PyDict_New();
326 if (d == NULL)
327 PyErr_Clear();
328 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000329 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000330}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000331
332
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000333/* Asynchronously raise an exception in a thread.
334 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000335 To prevent naive misuse, you must write your own extension
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000336 to call this. Must be called with the GIL held.
337 Returns the number of tstates modified; if it returns a number
338 greater than one, you're in trouble, and you should call it again
339 with exc=NULL to revert the effect. This raises no exceptions. */
340
341int
342PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000343 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000344 PyInterpreterState *interp = tstate->interp;
345 PyThreadState *p;
346 int count = 0;
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000347 HEAD_LOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000348 for (p = interp->tstate_head; p != NULL; p = p->next) {
349 if (p->thread_id != id)
350 continue;
351 ZAP(p->async_exc);
352 Py_XINCREF(exc);
353 p->async_exc = exc;
354 count += 1;
355 }
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000356 HEAD_UNLOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000357 return count;
358}
359
360
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000361/* Routines for advanced debuggers, requested by David Beazley.
362 Don't use unless you know what you are doing! */
363
364PyInterpreterState *
365PyInterpreterState_Head(void)
366{
367 return interp_head;
368}
369
370PyInterpreterState *
371PyInterpreterState_Next(PyInterpreterState *interp) {
372 return interp->next;
373}
374
375PyThreadState *
376PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
377 return interp->tstate_head;
378}
379
380PyThreadState *
381PyThreadState_Next(PyThreadState *tstate) {
382 return tstate->next;
383}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000384
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000385
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000386/* Python "auto thread state" API. */
387#ifdef WITH_THREAD
388
389/* Keep this as a static, as it is not reliable! It can only
390 ever be compared to the state for the *current* thread.
391 * If not equal, then it doesn't matter that the actual
392 value may change immediately after comparison, as it can't
393 possibly change to the current thread's state.
394 * If equal, then the current thread holds the lock, so the value can't
395 change until we yield the lock.
396*/
397static int
398PyThreadState_IsCurrent(PyThreadState *tstate)
399{
400 /* Must be the tstate for this thread */
401 assert(PyGILState_GetThisThreadState()==tstate);
402 /* On Windows at least, simple reads and writes to 32 bit values
403 are atomic.
404 */
405 return tstate == _PyThreadState_Current;
406}
407
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000408/* Internal initialization/finalization functions called by
409 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000410*/
Tim Peters19717fa2004-10-09 17:38:29 +0000411void
412_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000413{
Tim Peters19717fa2004-10-09 17:38:29 +0000414 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000415 autoTLSkey = PyThread_create_key();
416 autoInterpreterState = i;
Tim Petersf9becec2004-10-09 22:47:13 +0000417 assert(PyThread_get_key_value(autoTLSkey) == NULL);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000418 assert(t->gilstate_counter == 0);
419
420 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000421}
422
Tim Peters19717fa2004-10-09 17:38:29 +0000423void
424_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000425{
426 PyThread_delete_key(autoTLSkey);
427 autoTLSkey = 0;
428 autoInterpreterState = NULL;;
429}
430
Michael W. Hudson188d4362005-06-20 16:52:57 +0000431/* When a thread state is created for a thread by some mechanism other than
432 PyGILState_Ensure, it's important that the GILState machinery knows about
433 it so it doesn't try to create another thread state for the thread (this is
434 a better fix for SF bug #1010677 than the first one attempted).
435*/
436void
437_PyGILState_NoteThreadState(PyThreadState* tstate)
438{
439 /* If autoTLSkey is 0, this must be the very first threadstate created
440 in Py_Initialize(). Don't do anything for now (we'll be back here
441 when _PyGILState_Init is called). */
442 if (!autoTLSkey)
443 return;
444
445 /* Stick the thread state for this thread in thread local storage.
446
447 The only situation where you can legitimately have more than one
448 thread state for an OS level thread is when there are multiple
449 interpreters, when:
450
451 a) You shouldn't really be using the PyGILState_ APIs anyway,
452 and:
453
454 b) The slightly odd way PyThread_set_key_value works (see
455 comments by its implementation) means that the first thread
456 state created for that given OS level thread will "win",
457 which seems reasonable behaviour.
458 */
459 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
460 Py_FatalError("Couldn't create autoTLSkey mapping");
461
462 /* PyGILState_Release must not try to delete this thread state. */
463 tstate->gilstate_counter = 1;
464}
465
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000466/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000467PyThreadState *
468PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000469{
Tim Peters19717fa2004-10-09 17:38:29 +0000470 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000471 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000472 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000473}
474
Tim Peters19717fa2004-10-09 17:38:29 +0000475PyGILState_STATE
476PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000477{
478 int current;
479 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000480 /* Note that we do not auto-init Python here - apart from
481 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000482 spells out other issues. Embedders are expected to have
483 called Py_Initialize() and usually PyEval_InitThreads().
484 */
485 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
486 tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000487 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000488 /* Create a new thread state for this thread */
489 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000490 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000491 Py_FatalError("Couldn't create thread-state for new thread");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000492 /* This is our thread state! We'll need to delete it in the
493 matching call to PyGILState_Release(). */
494 tcur->gilstate_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000495 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000496 }
497 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000498 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000499 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000500 PyEval_RestoreThread(tcur);
501 /* Update our counter in the thread-state - no need for locks:
502 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000503 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000504 to modify this value
505 */
Tim Peters19717fa2004-10-09 17:38:29 +0000506 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000507 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
508}
509
Tim Peters19717fa2004-10-09 17:38:29 +0000510void
511PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000512{
513 PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000514 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000515 Py_FatalError("auto-releasing thread-state, "
516 "but no thread-state for this thread");
517 /* We must hold the GIL and have our thread state current */
518 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000519 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000520 by release-only users can't hurt.
521 */
Tim Peters19717fa2004-10-09 17:38:29 +0000522 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000523 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000524 assert(PyThreadState_IsCurrent(tcur));
525 --tcur->gilstate_counter;
526 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000527
Tim Petersfb1ffb02004-11-08 04:30:21 +0000528 /* If we're going to destroy this thread-state, we must
529 * clear it while the GIL is held, as destructors may run.
530 */
Tim Peters19717fa2004-10-09 17:38:29 +0000531 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000532 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000533 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000534 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000535 /* Delete the thread-state. Note this releases the GIL too!
536 * It's vital that the GIL be held here, to avoid shutdown
537 * races; see bugs 225673 and 1061968 (that nasty bug has a
538 * habit of coming back).
539 */
540 PyThreadState_DeleteCurrent();
Tim Peters89c0ec92004-10-10 05:30:40 +0000541 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000542 /* Release the lock if necessary */
543 else if (oldstate == PyGILState_UNLOCKED)
Michael W. Hudson774479c2005-04-18 08:46:17 +0000544 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000545}
546#endif /* WITH_THREAD */