blob: 9c85b5c8cde41a6fdb5da9a810d317608deca6ac [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
Anthony Baxterac6bd462006-04-13 02:06:09 +000040#ifdef __cplusplus
41extern "C" {
42#endif
43
Michael W. Hudson188d4362005-06-20 16:52:57 +000044/* The single PyInterpreterState used by this process'
45 GILState implementation
46*/
47static PyInterpreterState *autoInterpreterState = NULL;
48static int autoTLSkey = 0;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000049#else
50#define HEAD_INIT() /* Nothing */
51#define HEAD_LOCK() /* Nothing */
52#define HEAD_UNLOCK() /* Nothing */
53#endif
54
Guido van Rossum25ce5661997-08-02 03:10:38 +000055static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000056
Guido van Rossum18bc7c21998-12-21 18:27:28 +000057PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossum6297a7a2003-02-19 15:53:17 +000058PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000059
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000060#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000061static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000062#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000063
Guido van Rossuma027efa1997-05-05 20:56:21 +000064
65PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000066PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000067{
Tim Peters84705582004-10-10 02:47:33 +000068 PyInterpreterState *interp = (PyInterpreterState *)
69 malloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000070
Guido van Rossuma027efa1997-05-05 20:56:21 +000071 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000072 HEAD_INIT();
Guido van Rossum25ce5661997-08-02 03:10:38 +000073 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000074 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000075 interp->builtins = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000076 interp->tstate_head = NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000077 interp->codec_search_path = NULL;
78 interp->codec_search_cache = NULL;
79 interp->codec_error_registry = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000080#ifdef HAVE_DLOPEN
81#ifdef RTLD_NOW
82 interp->dlopenflags = RTLD_NOW;
83#else
84 interp->dlopenflags = RTLD_LAZY;
85#endif
86#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000087#ifdef WITH_TSC
88 interp->tscdump = 0;
89#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000090
Tim Peters412f2462000-09-02 09:16:15 +000091 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000092 interp->next = interp_head;
93 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000094 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000095 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000096
Guido van Rossuma027efa1997-05-05 20:56:21 +000097 return interp;
98}
99
100
101void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000102PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000103{
104 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000105 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000106 for (p = interp->tstate_head; p != NULL; p = p->next)
107 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000108 HEAD_UNLOCK();
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000109 ZAP(interp->codec_search_path);
110 ZAP(interp->codec_search_cache);
111 ZAP(interp->codec_error_registry);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112 ZAP(interp->modules);
113 ZAP(interp->sysdict);
114 ZAP(interp->builtins);
115}
116
117
118static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000119zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000120{
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000121 PyThreadState *p;
122 /* No need to lock the mutex here because this should only happen
123 when the threads are all really dead (XXX famous last words). */
124 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000125 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000126 }
127}
128
129
130void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000131PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133 PyInterpreterState **p;
134 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000135 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136 for (p = &interp_head; ; p = &(*p)->next) {
137 if (*p == NULL)
138 Py_FatalError(
139 "PyInterpreterState_Delete: invalid interp");
140 if (*p == interp)
141 break;
142 }
143 if (interp->tstate_head != NULL)
144 Py_FatalError("PyInterpreterState_Delete: remaining threads");
145 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000146 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000147 free(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000148}
149
150
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000151/* Default implementation for _PyThreadState_GetFrame */
152static struct _frame *
153threadstate_getframe(PyThreadState *self)
154{
155 return self->frame;
156}
157
Guido van Rossuma027efa1997-05-05 20:56:21 +0000158PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000159PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000160{
Tim Peters84705582004-10-10 02:47:33 +0000161 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
162
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000163 if (_PyThreadState_GetFrame == NULL)
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000164 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000165
Guido van Rossuma027efa1997-05-05 20:56:21 +0000166 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000167 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168
169 tstate->frame = NULL;
170 tstate->recursion_depth = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000171 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000172 tstate->use_tracing = 0;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000173 tstate->tick_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000174 tstate->gilstate_counter = 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000175 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000176#ifdef WITH_THREAD
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000177 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000178#else
179 tstate->thread_id = 0;
180#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000181
Guido van Rossumede04391998-04-10 20:18:25 +0000182 tstate->dict = NULL;
183
Guido van Rossuma027efa1997-05-05 20:56:21 +0000184 tstate->curexc_type = NULL;
185 tstate->curexc_value = NULL;
186 tstate->curexc_traceback = NULL;
187
188 tstate->exc_type = NULL;
189 tstate->exc_value = NULL;
190 tstate->exc_traceback = NULL;
191
Fred Drake5755ce62001-06-27 19:19:46 +0000192 tstate->c_profilefunc = NULL;
193 tstate->c_tracefunc = NULL;
194 tstate->c_profileobj = NULL;
195 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000196
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000197#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +0000198 _PyGILState_NoteThreadState(tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +0000199#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +0000200
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000201 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202 tstate->next = interp->tstate_head;
203 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000204 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000205 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206
Guido van Rossuma027efa1997-05-05 20:56:21 +0000207 return tstate;
208}
209
210
211void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000212PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000214 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000215 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000216 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000217
218 ZAP(tstate->frame);
219
Guido van Rossumede04391998-04-10 20:18:25 +0000220 ZAP(tstate->dict);
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000221 ZAP(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000222
Guido van Rossum25ce5661997-08-02 03:10:38 +0000223 ZAP(tstate->curexc_type);
224 ZAP(tstate->curexc_value);
225 ZAP(tstate->curexc_traceback);
226
227 ZAP(tstate->exc_type);
228 ZAP(tstate->exc_value);
229 ZAP(tstate->exc_traceback);
230
Fred Drake5755ce62001-06-27 19:19:46 +0000231 tstate->c_profilefunc = NULL;
232 tstate->c_tracefunc = NULL;
233 ZAP(tstate->c_profileobj);
234 ZAP(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235}
236
237
Guido van Rossum29757862001-01-23 01:46:06 +0000238/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
239static void
240tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000241{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242 PyInterpreterState *interp;
243 PyThreadState **p;
244 if (tstate == NULL)
245 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000246 interp = tstate->interp;
247 if (interp == NULL)
248 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000249 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250 for (p = &interp->tstate_head; ; p = &(*p)->next) {
251 if (*p == NULL)
252 Py_FatalError(
253 "PyThreadState_Delete: invalid tstate");
254 if (*p == tstate)
255 break;
256 }
257 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000258 HEAD_UNLOCK();
Tim Peters84705582004-10-10 02:47:33 +0000259 free(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000260}
261
262
Guido van Rossum29757862001-01-23 01:46:06 +0000263void
264PyThreadState_Delete(PyThreadState *tstate)
265{
266 if (tstate == _PyThreadState_Current)
267 Py_FatalError("PyThreadState_Delete: tstate is still current");
268 tstate_delete_common(tstate);
Tim Petersf4e69282006-02-27 17:15:31 +0000269#ifdef WITH_THREAD
270 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
271 PyThread_delete_key_value(autoTLSkey);
272#endif /* WITH_THREAD */
Guido van Rossum29757862001-01-23 01:46:06 +0000273}
274
275
276#ifdef WITH_THREAD
277void
278PyThreadState_DeleteCurrent()
279{
280 PyThreadState *tstate = _PyThreadState_Current;
281 if (tstate == NULL)
282 Py_FatalError(
283 "PyThreadState_DeleteCurrent: no current tstate");
284 _PyThreadState_Current = NULL;
285 tstate_delete_common(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000286 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
287 PyThread_delete_key_value(autoTLSkey);
Guido van Rossum29757862001-01-23 01:46:06 +0000288 PyEval_ReleaseLock();
289}
290#endif /* WITH_THREAD */
291
292
Guido van Rossuma027efa1997-05-05 20:56:21 +0000293PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000294PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000295{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000296 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000297 Py_FatalError("PyThreadState_Get: no current thread");
298
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000299 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000300}
301
302
303PyThreadState *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000304PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000305{
Anthony Baxter7b782b62006-04-11 12:01:56 +0000306 PyThreadState *oldts = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307
Anthony Baxter7b782b62006-04-11 12:01:56 +0000308 _PyThreadState_Current = newts;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000309 /* It should not be possible for more than one thread state
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000310 to be used for a thread. Check this the best we can in debug
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000311 builds.
312 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000313#if defined(Py_DEBUG) && defined(WITH_THREAD)
Anthony Baxter7b782b62006-04-11 12:01:56 +0000314 if (newts) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000315 PyThreadState *check = PyGILState_GetThisThreadState();
Anthony Baxter7b782b62006-04-11 12:01:56 +0000316 if (check && check->interp == newts->interp && check != newts)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000317 Py_FatalError("Invalid thread state for this thread");
318 }
319#endif
Anthony Baxter7b782b62006-04-11 12:01:56 +0000320 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000321}
Guido van Rossumede04391998-04-10 20:18:25 +0000322
323/* An extension mechanism to store arbitrary additional per-thread state.
324 PyThreadState_GetDict() returns a dictionary that can be used to hold such
325 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000326 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
327 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000328
329PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000330PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000331{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000332 if (_PyThreadState_Current == NULL)
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000333 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000334
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000335 if (_PyThreadState_Current->dict == NULL) {
336 PyObject *d;
337 _PyThreadState_Current->dict = d = PyDict_New();
338 if (d == NULL)
339 PyErr_Clear();
340 }
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000341 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000342}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000343
344
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000345/* Asynchronously raise an exception in a thread.
346 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000347 To prevent naive misuse, you must write your own extension
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000348 to call this. Must be called with the GIL held.
349 Returns the number of tstates modified; if it returns a number
350 greater than one, you're in trouble, and you should call it again
351 with exc=NULL to revert the effect. This raises no exceptions. */
352
353int
354PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000355 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000356 PyInterpreterState *interp = tstate->interp;
357 PyThreadState *p;
358 int count = 0;
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000359 HEAD_LOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000360 for (p = interp->tstate_head; p != NULL; p = p->next) {
361 if (p->thread_id != id)
362 continue;
363 ZAP(p->async_exc);
364 Py_XINCREF(exc);
365 p->async_exc = exc;
366 count += 1;
367 }
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000368 HEAD_UNLOCK();
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000369 return count;
370}
371
372
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000373/* Routines for advanced debuggers, requested by David Beazley.
374 Don't use unless you know what you are doing! */
375
376PyInterpreterState *
377PyInterpreterState_Head(void)
378{
379 return interp_head;
380}
381
382PyInterpreterState *
383PyInterpreterState_Next(PyInterpreterState *interp) {
384 return interp->next;
385}
386
387PyThreadState *
388PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
389 return interp->tstate_head;
390}
391
392PyThreadState *
393PyThreadState_Next(PyThreadState *tstate) {
394 return tstate->next;
395}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000396
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000397
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000398/* Python "auto thread state" API. */
399#ifdef WITH_THREAD
400
401/* Keep this as a static, as it is not reliable! It can only
402 ever be compared to the state for the *current* thread.
403 * If not equal, then it doesn't matter that the actual
404 value may change immediately after comparison, as it can't
405 possibly change to the current thread's state.
406 * If equal, then the current thread holds the lock, so the value can't
407 change until we yield the lock.
408*/
409static int
410PyThreadState_IsCurrent(PyThreadState *tstate)
411{
412 /* Must be the tstate for this thread */
413 assert(PyGILState_GetThisThreadState()==tstate);
414 /* On Windows at least, simple reads and writes to 32 bit values
415 are atomic.
416 */
417 return tstate == _PyThreadState_Current;
418}
419
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000420/* Internal initialization/finalization functions called by
421 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000422*/
Tim Peters19717fa2004-10-09 17:38:29 +0000423void
424_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000425{
Tim Peters19717fa2004-10-09 17:38:29 +0000426 assert(i && t); /* must init with valid states */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000427 autoTLSkey = PyThread_create_key();
428 autoInterpreterState = i;
Tim Petersf9becec2004-10-09 22:47:13 +0000429 assert(PyThread_get_key_value(autoTLSkey) == NULL);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000430 assert(t->gilstate_counter == 0);
431
432 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000433}
434
Tim Peters19717fa2004-10-09 17:38:29 +0000435void
436_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000437{
438 PyThread_delete_key(autoTLSkey);
439 autoTLSkey = 0;
440 autoInterpreterState = NULL;;
441}
442
Michael W. Hudson188d4362005-06-20 16:52:57 +0000443/* When a thread state is created for a thread by some mechanism other than
444 PyGILState_Ensure, it's important that the GILState machinery knows about
445 it so it doesn't try to create another thread state for the thread (this is
446 a better fix for SF bug #1010677 than the first one attempted).
447*/
448void
449_PyGILState_NoteThreadState(PyThreadState* tstate)
450{
451 /* If autoTLSkey is 0, this must be the very first threadstate created
452 in Py_Initialize(). Don't do anything for now (we'll be back here
453 when _PyGILState_Init is called). */
454 if (!autoTLSkey)
455 return;
456
457 /* Stick the thread state for this thread in thread local storage.
458
459 The only situation where you can legitimately have more than one
460 thread state for an OS level thread is when there are multiple
461 interpreters, when:
462
463 a) You shouldn't really be using the PyGILState_ APIs anyway,
464 and:
465
466 b) The slightly odd way PyThread_set_key_value works (see
467 comments by its implementation) means that the first thread
468 state created for that given OS level thread will "win",
469 which seems reasonable behaviour.
470 */
471 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
472 Py_FatalError("Couldn't create autoTLSkey mapping");
473
474 /* PyGILState_Release must not try to delete this thread state. */
475 tstate->gilstate_counter = 1;
476}
477
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000478/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000479PyThreadState *
480PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000481{
Tim Peters19717fa2004-10-09 17:38:29 +0000482 if (autoInterpreterState == NULL || autoTLSkey == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000483 return NULL;
Tim Peters19717fa2004-10-09 17:38:29 +0000484 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000485}
486
Tim Peters19717fa2004-10-09 17:38:29 +0000487PyGILState_STATE
488PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000489{
490 int current;
491 PyThreadState *tcur;
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000492 /* Note that we do not auto-init Python here - apart from
493 potential races with 2 threads auto-initializing, pep-311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000494 spells out other issues. Embedders are expected to have
495 called Py_Initialize() and usually PyEval_InitThreads().
496 */
497 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
Anthony Baxter7b782b62006-04-11 12:01:56 +0000498 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000499 if (tcur == NULL) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000500 /* Create a new thread state for this thread */
501 tcur = PyThreadState_New(autoInterpreterState);
Tim Peters19717fa2004-10-09 17:38:29 +0000502 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000503 Py_FatalError("Couldn't create thread-state for new thread");
Michael W. Hudson188d4362005-06-20 16:52:57 +0000504 /* This is our thread state! We'll need to delete it in the
505 matching call to PyGILState_Release(). */
506 tcur->gilstate_counter = 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000507 current = 0; /* new thread state is never current */
Tim Peters19717fa2004-10-09 17:38:29 +0000508 }
509 else
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000510 current = PyThreadState_IsCurrent(tcur);
Tim Peters19717fa2004-10-09 17:38:29 +0000511 if (current == 0)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000512 PyEval_RestoreThread(tcur);
513 /* Update our counter in the thread-state - no need for locks:
514 - tcur will remain valid as we hold the GIL.
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000515 - the counter is safe as we are the only thread "allowed"
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000516 to modify this value
517 */
Tim Peters19717fa2004-10-09 17:38:29 +0000518 ++tcur->gilstate_counter;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000519 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
520}
521
Tim Peters19717fa2004-10-09 17:38:29 +0000522void
523PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000524{
Anthony Baxter7b782b62006-04-11 12:01:56 +0000525 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
526 autoTLSkey);
Tim Peters19717fa2004-10-09 17:38:29 +0000527 if (tcur == NULL)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000528 Py_FatalError("auto-releasing thread-state, "
529 "but no thread-state for this thread");
530 /* We must hold the GIL and have our thread state current */
531 /* XXX - remove the check - the assert should be fine,
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000532 but while this is very new (April 2003), the extra check
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000533 by release-only users can't hurt.
534 */
Tim Peters19717fa2004-10-09 17:38:29 +0000535 if (! PyThreadState_IsCurrent(tcur))
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000536 Py_FatalError("This thread state must be current when releasing");
Tim Peters19717fa2004-10-09 17:38:29 +0000537 assert(PyThreadState_IsCurrent(tcur));
538 --tcur->gilstate_counter;
539 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000540
Tim Petersfb1ffb02004-11-08 04:30:21 +0000541 /* If we're going to destroy this thread-state, we must
542 * clear it while the GIL is held, as destructors may run.
543 */
Tim Peters19717fa2004-10-09 17:38:29 +0000544 if (tcur->gilstate_counter == 0) {
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000545 /* can't have been locked when we created it */
Tim Peters19717fa2004-10-09 17:38:29 +0000546 assert(oldstate == PyGILState_UNLOCKED);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000547 PyThreadState_Clear(tcur);
Tim Petersfb1ffb02004-11-08 04:30:21 +0000548 /* Delete the thread-state. Note this releases the GIL too!
549 * It's vital that the GIL be held here, to avoid shutdown
550 * races; see bugs 225673 and 1061968 (that nasty bug has a
551 * habit of coming back).
552 */
553 PyThreadState_DeleteCurrent();
Tim Peters89c0ec92004-10-10 05:30:40 +0000554 }
Tim Petersfb1ffb02004-11-08 04:30:21 +0000555 /* Release the lock if necessary */
556 else if (oldstate == PyGILState_UNLOCKED)
Michael W. Hudson774479c2005-04-18 08:46:17 +0000557 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000558}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000559
560#ifdef __cplusplus
561}
562#endif
563
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000564#endif /* WITH_THREAD */
Anthony Baxterac6bd462006-04-13 02:06:09 +0000565
566