| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Thread and interpreter state structures and their interfaces */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-10 02:47:33 +00:00
										 |  |  | /* --------------------------------------------------------------------------
 | 
					
						
							|  |  |  | CAUTION | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Always use malloc() and free() directly in this file.  A number of these | 
					
						
							|  |  |  | functions are advertised as safe to call when the GIL isn't held, and in | 
					
						
							|  |  |  | a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging | 
					
						
							|  |  |  | obmalloc functions.  Those aren't thread-safe (they rely on the GIL to avoid | 
					
						
							|  |  |  | the expense of doing their own locking). | 
					
						
							|  |  |  | -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-18 16:17:16 +00:00
										 |  |  | #ifdef HAVE_DLOPEN
 | 
					
						
							|  |  |  | #ifdef HAVE_DLFCN_H
 | 
					
						
							|  |  |  | #include <dlfcn.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #ifndef RTLD_LAZY
 | 
					
						
							|  |  |  | #define RTLD_LAZY 1
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | #define ZAP(x) { \
 | 
					
						
							|  |  |  | 	PyObject *tmp = (PyObject *)(x); \ | 
					
						
							|  |  |  | 	(x) = NULL; \ | 
					
						
							|  |  |  | 	Py_XDECREF(tmp); \ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  | #include "pythread.h"
 | 
					
						
							|  |  |  | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ | 
					
						
							| 
									
										
										
										
											2000-08-04 21:27:47 +00:00
										 |  |  | #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
 | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
 | 
					
						
							|  |  |  | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
 | 
					
						
							| 
									
										
										
										
											2005-06-20 16:52:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* The single PyInterpreterState used by this process'
 | 
					
						
							|  |  |  |    GILState implementation | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | static PyInterpreterState *autoInterpreterState = NULL; | 
					
						
							|  |  |  | static int autoTLSkey = 0; | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | #define HEAD_INIT() /* Nothing */
 | 
					
						
							|  |  |  | #define HEAD_LOCK() /* Nothing */
 | 
					
						
							|  |  |  | #define HEAD_UNLOCK() /* Nothing */
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | static PyInterpreterState *interp_head = NULL; | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:27:28 +00:00
										 |  |  | PyThreadState *_PyThreadState_Current = NULL; | 
					
						
							| 
									
										
										
										
											2003-02-19 15:53:17 +00:00
										 |  |  | PyThreadFrameGetter _PyThreadState_GetFrame = NULL; | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-30 08:20:24 +00:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							| 
									
										
										
										
											2005-06-20 16:52:57 +00:00
										 |  |  | static void _PyGILState_NoteThreadState(PyThreadState* tstate); | 
					
						
							| 
									
										
										
										
											2005-09-30 08:20:24 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-06-20 16:52:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | PyInterpreterState * | 
					
						
							| 
									
										
										
										
											2000-07-22 18:47:25 +00:00
										 |  |  | PyInterpreterState_New(void) | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-10-10 02:47:33 +00:00
										 |  |  | 	PyInterpreterState *interp = (PyInterpreterState *) | 
					
						
							|  |  |  | 				     malloc(sizeof(PyInterpreterState)); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	if (interp != NULL) { | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | 		HEAD_INIT(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 		interp->modules = NULL; | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 		interp->sysdict = NULL; | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 		interp->builtins = NULL; | 
					
						
							|  |  |  | 		interp->tstate_head = NULL; | 
					
						
							| 
									
										
										
										
											2003-03-19 00:35:36 +00:00
										 |  |  | 		interp->codec_search_path = NULL; | 
					
						
							|  |  |  | 		interp->codec_search_cache = NULL; | 
					
						
							|  |  |  | 		interp->codec_error_registry = NULL; | 
					
						
							| 
									
										
										
										
											2001-07-18 16:17:16 +00:00
										 |  |  | #ifdef HAVE_DLOPEN
 | 
					
						
							|  |  |  | #ifdef RTLD_NOW
 | 
					
						
							|  |  |  |                 interp->dlopenflags = RTLD_NOW; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 		interp->dlopenflags = RTLD_LAZY; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-06-08 08:17:44 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef WITH_TSC
 | 
					
						
							|  |  |  | 		interp->tscdump = 0; | 
					
						
							| 
									
										
										
										
											2001-07-18 16:17:16 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-02 09:16:15 +00:00
										 |  |  | 		HEAD_LOCK(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 		interp->next = interp_head; | 
					
						
							|  |  |  | 		interp_head = interp; | 
					
						
							| 
									
										
										
										
											2000-09-02 09:16:15 +00:00
										 |  |  | 		HEAD_UNLOCK(); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	return interp; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2000-07-22 18:47:25 +00:00
										 |  |  | PyInterpreterState_Clear(PyInterpreterState *interp) | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyThreadState *p; | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | 	HEAD_LOCK(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	for (p = interp->tstate_head; p != NULL; p = p->next) | 
					
						
							|  |  |  | 		PyThreadState_Clear(p); | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | 	HEAD_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2003-03-19 00:35:36 +00:00
										 |  |  | 	ZAP(interp->codec_search_path); | 
					
						
							|  |  |  | 	ZAP(interp->codec_search_cache); | 
					
						
							|  |  |  | 	ZAP(interp->codec_error_registry); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	ZAP(interp->modules); | 
					
						
							|  |  |  | 	ZAP(interp->sysdict); | 
					
						
							|  |  |  | 	ZAP(interp->builtins); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-22 18:47:25 +00:00
										 |  |  | zapthreads(PyInterpreterState *interp) | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | 	PyThreadState *p; | 
					
						
							|  |  |  | 	/* No need to lock the mutex here because this should only happen
 | 
					
						
							|  |  |  | 	   when the threads are all really dead (XXX famous last words). */ | 
					
						
							|  |  |  | 	while ((p = interp->tstate_head) != NULL) { | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 		PyThreadState_Delete(p); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2000-07-22 18:47:25 +00:00
										 |  |  | PyInterpreterState_Delete(PyInterpreterState *interp) | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyInterpreterState **p; | 
					
						
							|  |  |  | 	zapthreads(interp); | 
					
						
							| 
									
										
										
										
											2000-09-02 09:16:15 +00:00
										 |  |  | 	HEAD_LOCK(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	for (p = &interp_head; ; p = &(*p)->next) { | 
					
						
							|  |  |  | 		if (*p == NULL) | 
					
						
							|  |  |  | 			Py_FatalError( | 
					
						
							|  |  |  | 				"PyInterpreterState_Delete: invalid interp"); | 
					
						
							|  |  |  | 		if (*p == interp) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (interp->tstate_head != NULL) | 
					
						
							|  |  |  | 		Py_FatalError("PyInterpreterState_Delete: remaining threads"); | 
					
						
							|  |  |  | 	*p = interp->next; | 
					
						
							| 
									
										
										
										
											2000-09-02 09:16:15 +00:00
										 |  |  | 	HEAD_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2004-10-10 02:47:33 +00:00
										 |  |  | 	free(interp); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-08 12:53:11 +00:00
										 |  |  | /* Default implementation for _PyThreadState_GetFrame */ | 
					
						
							|  |  |  | static struct _frame * | 
					
						
							|  |  |  | threadstate_getframe(PyThreadState *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return self->frame; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | PyThreadState * | 
					
						
							| 
									
										
										
										
											2000-07-22 18:47:25 +00:00
										 |  |  | PyThreadState_New(PyInterpreterState *interp) | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-10-10 02:47:33 +00:00
										 |  |  | 	PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-08 12:53:11 +00:00
										 |  |  | 	if (_PyThreadState_GetFrame == NULL) | 
					
						
							| 
									
										
										
										
											2003-02-19 15:53:17 +00:00
										 |  |  | 		_PyThreadState_GetFrame = threadstate_getframe; | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	if (tstate != NULL) { | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 		tstate->interp = interp; | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		tstate->frame = NULL; | 
					
						
							|  |  |  | 		tstate->recursion_depth = 0; | 
					
						
							|  |  |  | 		tstate->tracing = 0; | 
					
						
							| 
									
										
										
										
											2001-07-03 23:39:52 +00:00
										 |  |  | 		tstate->use_tracing = 0; | 
					
						
							| 
									
										
										
										
											2002-11-08 12:53:11 +00:00
										 |  |  | 		tstate->tick_counter = 0; | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		tstate->gilstate_counter = 0; | 
					
						
							| 
									
										
										
										
											2003-06-28 21:53:52 +00:00
										 |  |  | 		tstate->async_exc = NULL; | 
					
						
							| 
									
										
										
										
											2003-07-13 10:41:53 +00:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							| 
									
										
										
										
											2003-06-28 21:53:52 +00:00
										 |  |  | 		tstate->thread_id = PyThread_get_thread_ident(); | 
					
						
							| 
									
										
										
										
											2003-07-13 10:41:53 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 		tstate->thread_id = 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-10 20:18:25 +00:00
										 |  |  | 		tstate->dict = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 		tstate->curexc_type = NULL; | 
					
						
							|  |  |  | 		tstate->curexc_value = NULL; | 
					
						
							|  |  |  | 		tstate->curexc_traceback = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		tstate->exc_type = NULL; | 
					
						
							|  |  |  | 		tstate->exc_value = NULL; | 
					
						
							|  |  |  | 		tstate->exc_traceback = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-06-27 19:19:46 +00:00
										 |  |  | 		tstate->c_profilefunc = NULL; | 
					
						
							|  |  |  | 		tstate->c_tracefunc = NULL; | 
					
						
							|  |  |  | 		tstate->c_profileobj = NULL; | 
					
						
							|  |  |  | 		tstate->c_traceobj = NULL; | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-30 08:20:24 +00:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							| 
									
										
										
										
											2005-06-20 16:52:57 +00:00
										 |  |  | 		_PyGILState_NoteThreadState(tstate); | 
					
						
							| 
									
										
										
										
											2005-09-30 08:20:24 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-06-20 16:52:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | 		HEAD_LOCK(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 		tstate->next = interp->tstate_head; | 
					
						
							|  |  |  | 		interp->tstate_head = tstate; | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | 		HEAD_UNLOCK(); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	return tstate; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2000-07-22 18:47:25 +00:00
										 |  |  | PyThreadState_Clear(PyThreadState *tstate) | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-11-03 22:08:36 +00:00
										 |  |  | 	if (Py_VerboseFlag && tstate->frame != NULL) | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 		fprintf(stderr, | 
					
						
							| 
									
										
										
										
											1997-08-21 02:28:19 +00:00
										 |  |  | 		  "PyThreadState_Clear: warning: thread still has a frame\n"); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ZAP(tstate->frame); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-10 20:18:25 +00:00
										 |  |  | 	ZAP(tstate->dict); | 
					
						
							| 
									
										
										
										
											2003-06-28 21:53:52 +00:00
										 |  |  | 	ZAP(tstate->async_exc); | 
					
						
							| 
									
										
										
										
											1998-04-10 20:18:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	ZAP(tstate->curexc_type); | 
					
						
							|  |  |  | 	ZAP(tstate->curexc_value); | 
					
						
							|  |  |  | 	ZAP(tstate->curexc_traceback); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	ZAP(tstate->exc_type); | 
					
						
							|  |  |  | 	ZAP(tstate->exc_value); | 
					
						
							|  |  |  | 	ZAP(tstate->exc_traceback); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-06-27 19:19:46 +00:00
										 |  |  | 	tstate->c_profilefunc = NULL; | 
					
						
							|  |  |  | 	tstate->c_tracefunc = NULL; | 
					
						
							|  |  |  | 	ZAP(tstate->c_profileobj); | 
					
						
							|  |  |  | 	ZAP(tstate->c_traceobj); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-23 01:46:06 +00:00
										 |  |  | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | tstate_delete_common(PyThreadState *tstate) | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyInterpreterState *interp; | 
					
						
							|  |  |  | 	PyThreadState **p; | 
					
						
							|  |  |  | 	if (tstate == NULL) | 
					
						
							|  |  |  | 		Py_FatalError("PyThreadState_Delete: NULL tstate"); | 
					
						
							|  |  |  | 	interp = tstate->interp; | 
					
						
							|  |  |  | 	if (interp == NULL) | 
					
						
							|  |  |  | 		Py_FatalError("PyThreadState_Delete: NULL interp"); | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | 	HEAD_LOCK(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	for (p = &interp->tstate_head; ; p = &(*p)->next) { | 
					
						
							|  |  |  | 		if (*p == NULL) | 
					
						
							|  |  |  | 			Py_FatalError( | 
					
						
							|  |  |  | 				"PyThreadState_Delete: invalid tstate"); | 
					
						
							|  |  |  | 		if (*p == tstate) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	*p = tstate->next; | 
					
						
							| 
									
										
										
										
											1999-06-18 14:22:24 +00:00
										 |  |  | 	HEAD_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2004-10-10 02:47:33 +00:00
										 |  |  | 	free(tstate); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-23 01:46:06 +00:00
										 |  |  | void | 
					
						
							|  |  |  | PyThreadState_Delete(PyThreadState *tstate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (tstate == _PyThreadState_Current) | 
					
						
							|  |  |  | 		Py_FatalError("PyThreadState_Delete: tstate is still current"); | 
					
						
							|  |  |  | 	tstate_delete_common(tstate); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | PyThreadState_DeleteCurrent() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyThreadState *tstate = _PyThreadState_Current; | 
					
						
							|  |  |  | 	if (tstate == NULL) | 
					
						
							|  |  |  | 		Py_FatalError( | 
					
						
							|  |  |  | 			"PyThreadState_DeleteCurrent: no current tstate"); | 
					
						
							|  |  |  | 	_PyThreadState_Current = NULL; | 
					
						
							|  |  |  | 	tstate_delete_common(tstate); | 
					
						
							| 
									
										
										
										
											2005-06-20 16:52:57 +00:00
										 |  |  | 	if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate) | 
					
						
							|  |  |  | 		PyThread_delete_key_value(autoTLSkey); | 
					
						
							| 
									
										
										
										
											2001-01-23 01:46:06 +00:00
										 |  |  | 	PyEval_ReleaseLock(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif /* WITH_THREAD */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | PyThreadState * | 
					
						
							| 
									
										
										
										
											2000-07-22 18:47:25 +00:00
										 |  |  | PyThreadState_Get(void) | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1998-12-21 18:27:28 +00:00
										 |  |  | 	if (_PyThreadState_Current == NULL) | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 		Py_FatalError("PyThreadState_Get: no current thread"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:27:28 +00:00
										 |  |  | 	return _PyThreadState_Current; | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyThreadState * | 
					
						
							| 
									
										
										
										
											2000-07-22 18:47:25 +00:00
										 |  |  | PyThreadState_Swap(PyThreadState *new) | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1998-12-21 18:27:28 +00:00
										 |  |  | 	PyThreadState *old = _PyThreadState_Current; | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:27:28 +00:00
										 |  |  | 	_PyThreadState_Current = new; | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 	/* It should not be possible for more than one thread state
 | 
					
						
							| 
									
										
										
										
											2004-10-09 17:25:05 +00:00
										 |  |  | 	   to be used for a thread.  Check this the best we can in debug | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 	   builds. | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2003-05-01 05:25:29 +00:00
										 |  |  | #if defined(Py_DEBUG) && defined(WITH_THREAD)
 | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 	if (new) { | 
					
						
							|  |  |  | 		PyThreadState *check = PyGILState_GetThisThreadState(); | 
					
						
							| 
									
										
										
										
											2005-06-16 11:35:00 +00:00
										 |  |  | 		if (check && check->interp == new->interp && check != new) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 			Py_FatalError("Invalid thread state for this thread"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	return old; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1998-04-10 20:18:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* An extension mechanism to store arbitrary additional per-thread state.
 | 
					
						
							|  |  |  |    PyThreadState_GetDict() returns a dictionary that can be used to hold such | 
					
						
							|  |  |  |    state; the caller should pick a unique key and store its state there.  If | 
					
						
							| 
									
										
										
										
											2003-04-15 15:12:39 +00:00
										 |  |  |    PyThreadState_GetDict() returns NULL, an exception has *not* been raised | 
					
						
							|  |  |  |    and the caller should assume no per-thread state is available. */ | 
					
						
							| 
									
										
										
										
											1998-04-10 20:18:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-22 18:47:25 +00:00
										 |  |  | PyThreadState_GetDict(void) | 
					
						
							| 
									
										
										
										
											1998-04-10 20:18:25 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1998-12-21 18:27:28 +00:00
										 |  |  | 	if (_PyThreadState_Current == NULL) | 
					
						
							| 
									
										
										
										
											2003-04-15 15:12:39 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1998-04-10 20:18:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-15 15:12:39 +00:00
										 |  |  | 	if (_PyThreadState_Current->dict == NULL) { | 
					
						
							|  |  |  | 		PyObject *d; | 
					
						
							|  |  |  | 		_PyThreadState_Current->dict = d = PyDict_New(); | 
					
						
							|  |  |  | 		if (d == NULL) | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1998-12-21 18:27:28 +00:00
										 |  |  | 	return _PyThreadState_Current->dict; | 
					
						
							| 
									
										
										
										
											1998-04-10 20:18:25 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2001-07-19 12:19:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-28 21:53:52 +00:00
										 |  |  | /* Asynchronously raise an exception in a thread.
 | 
					
						
							|  |  |  |    Requested by Just van Rossum and Alex Martelli. | 
					
						
							| 
									
										
										
										
											2005-02-08 02:07:57 +00:00
										 |  |  |    To prevent naive misuse, you must write your own extension | 
					
						
							| 
									
										
										
										
											2003-06-28 21:53:52 +00:00
										 |  |  |    to call this.  Must be called with the GIL held. | 
					
						
							|  |  |  |    Returns the number of tstates modified; if it returns a number | 
					
						
							|  |  |  |    greater than one, you're in trouble, and you should call it again | 
					
						
							|  |  |  |    with exc=NULL to revert the effect.  This raises no exceptions. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyThreadState_SetAsyncExc(long id, PyObject *exc) { | 
					
						
							| 
									
										
										
										
											2004-03-24 22:22:12 +00:00
										 |  |  | 	PyThreadState *tstate = PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2003-06-28 21:53:52 +00:00
										 |  |  | 	PyInterpreterState *interp = tstate->interp; | 
					
						
							|  |  |  | 	PyThreadState *p; | 
					
						
							|  |  |  | 	int count = 0; | 
					
						
							| 
									
										
										
										
											2005-02-08 02:07:57 +00:00
										 |  |  | 	HEAD_LOCK(); | 
					
						
							| 
									
										
										
										
											2003-06-28 21:53:52 +00:00
										 |  |  | 	for (p = interp->tstate_head; p != NULL; p = p->next) { | 
					
						
							|  |  |  | 		if (p->thread_id != id) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		ZAP(p->async_exc); | 
					
						
							|  |  |  | 		Py_XINCREF(exc); | 
					
						
							|  |  |  | 		p->async_exc = exc; | 
					
						
							|  |  |  | 		count += 1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-02-08 02:07:57 +00:00
										 |  |  | 	HEAD_UNLOCK(); | 
					
						
							| 
									
										
										
										
											2003-06-28 21:53:52 +00:00
										 |  |  | 	return count; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-19 12:19:27 +00:00
										 |  |  | /* Routines for advanced debuggers, requested by David Beazley.
 | 
					
						
							|  |  |  |    Don't use unless you know what you are doing! */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyInterpreterState * | 
					
						
							|  |  |  | PyInterpreterState_Head(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return interp_head; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyInterpreterState * | 
					
						
							|  |  |  | PyInterpreterState_Next(PyInterpreterState *interp) { | 
					
						
							|  |  |  | 	return interp->next; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyThreadState * | 
					
						
							|  |  |  | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { | 
					
						
							|  |  |  | 	return interp->tstate_head; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyThreadState * | 
					
						
							|  |  |  | PyThreadState_Next(PyThreadState *tstate) { | 
					
						
							|  |  |  | 	return tstate->next; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-28 21:53:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | /* Python "auto thread state" API. */ | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Keep this as a static, as it is not reliable!  It can only
 | 
					
						
							|  |  |  |    ever be compared to the state for the *current* thread. | 
					
						
							|  |  |  |    * If not equal, then it doesn't matter that the actual | 
					
						
							|  |  |  |      value may change immediately after comparison, as it can't | 
					
						
							|  |  |  |      possibly change to the current thread's state. | 
					
						
							|  |  |  |    * If equal, then the current thread holds the lock, so the value can't | 
					
						
							|  |  |  |      change until we yield the lock. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | PyThreadState_IsCurrent(PyThreadState *tstate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* Must be the tstate for this thread */ | 
					
						
							|  |  |  | 	assert(PyGILState_GetThisThreadState()==tstate); | 
					
						
							|  |  |  | 	/* On Windows at least, simple reads and writes to 32 bit values
 | 
					
						
							|  |  |  | 	   are atomic. | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  | 	return tstate == _PyThreadState_Current; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-09 17:25:05 +00:00
										 |  |  | /* Internal initialization/finalization functions called by
 | 
					
						
							|  |  |  |    Py_Initialize/Py_Finalize | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | void | 
					
						
							|  |  |  | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	assert(i && t); /* must init with valid states */ | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 	autoTLSkey = PyThread_create_key(); | 
					
						
							|  |  |  | 	autoInterpreterState = i; | 
					
						
							| 
									
										
										
										
											2004-10-09 22:47:13 +00:00
										 |  |  | 	assert(PyThread_get_key_value(autoTLSkey) == NULL); | 
					
						
							| 
									
										
										
										
											2005-06-20 16:52:57 +00:00
										 |  |  | 	assert(t->gilstate_counter == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_PyGILState_NoteThreadState(t); | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | void | 
					
						
							|  |  |  | _PyGILState_Fini(void) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyThread_delete_key(autoTLSkey); | 
					
						
							|  |  |  | 	autoTLSkey = 0; | 
					
						
							|  |  |  | 	autoInterpreterState = NULL;; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-20 16:52:57 +00:00
										 |  |  | /* When a thread state is created for a thread by some mechanism other than
 | 
					
						
							|  |  |  |    PyGILState_Ensure, it's important that the GILState machinery knows about | 
					
						
							|  |  |  |    it so it doesn't try to create another thread state for the thread (this is | 
					
						
							|  |  |  |    a better fix for SF bug #1010677 than the first one attempted). | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _PyGILState_NoteThreadState(PyThreadState* tstate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* If autoTLSkey is 0, this must be the very first threadstate created
 | 
					
						
							|  |  |  | 	   in Py_Initialize().  Don't do anything for now (we'll be back here | 
					
						
							|  |  |  | 	   when _PyGILState_Init is called). */ | 
					
						
							|  |  |  | 	if (!autoTLSkey)  | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* Stick the thread state for this thread in thread local storage.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	   The only situation where you can legitimately have more than one | 
					
						
							|  |  |  | 	   thread state for an OS level thread is when there are multiple | 
					
						
							|  |  |  | 	   interpreters, when: | 
					
						
							|  |  |  | 	        | 
					
						
							|  |  |  | 	       a) You shouldn't really be using the PyGILState_ APIs anyway, | 
					
						
							|  |  |  | 	          and: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	       b) The slightly odd way PyThread_set_key_value works (see | 
					
						
							|  |  |  | 	          comments by its implementation) means that the first thread | 
					
						
							|  |  |  | 	          state created for that given OS level thread will "win", | 
					
						
							|  |  |  | 	          which seems reasonable behaviour. | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  | 	if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) | 
					
						
							|  |  |  | 		Py_FatalError("Couldn't create autoTLSkey mapping"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* PyGILState_Release must not try to delete this thread state. */ | 
					
						
							|  |  |  | 	tstate->gilstate_counter = 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | /* The public functions */ | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | PyThreadState * | 
					
						
							|  |  |  | PyGILState_GetThisThreadState(void) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	if (autoInterpreterState == NULL || autoTLSkey == 0) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	return (PyThreadState *)PyThread_get_key_value(autoTLSkey); | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | PyGILState_STATE | 
					
						
							|  |  |  | PyGILState_Ensure(void) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int current; | 
					
						
							|  |  |  | 	PyThreadState *tcur; | 
					
						
							| 
									
										
										
										
											2004-10-09 17:25:05 +00:00
										 |  |  | 	/* Note that we do not auto-init Python here - apart from
 | 
					
						
							|  |  |  | 	   potential races with 2 threads auto-initializing, pep-311 | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 	   spells out other issues.  Embedders are expected to have | 
					
						
							|  |  |  | 	   called Py_Initialize() and usually PyEval_InitThreads(). | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  | 	assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ | 
					
						
							|  |  |  | 	tcur = PyThread_get_key_value(autoTLSkey); | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	if (tcur == NULL) { | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		/* Create a new thread state for this thread */ | 
					
						
							|  |  |  | 		tcur = PyThreadState_New(autoInterpreterState); | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 		if (tcur == NULL) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 			Py_FatalError("Couldn't create thread-state for new thread"); | 
					
						
							| 
									
										
										
										
											2005-06-20 16:52:57 +00:00
										 |  |  | 		/* This is our thread state!  We'll need to delete it in the
 | 
					
						
							|  |  |  | 		   matching call to PyGILState_Release(). */ | 
					
						
							|  |  |  | 		tcur->gilstate_counter = 0; | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		current = 0; /* new thread state is never current */ | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		current = PyThreadState_IsCurrent(tcur); | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	if (current == 0) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		PyEval_RestoreThread(tcur); | 
					
						
							|  |  |  | 	/* Update our counter in the thread-state - no need for locks:
 | 
					
						
							|  |  |  | 	   - tcur will remain valid as we hold the GIL. | 
					
						
							| 
									
										
										
										
											2004-10-09 17:25:05 +00:00
										 |  |  | 	   - the counter is safe as we are the only thread "allowed" | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 	     to modify this value | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	++tcur->gilstate_counter; | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 	return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | void | 
					
						
							|  |  |  | PyGILState_Release(PyGILState_STATE oldstate) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyThreadState *tcur = PyThread_get_key_value(autoTLSkey); | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	if (tcur == NULL) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		Py_FatalError("auto-releasing thread-state, " | 
					
						
							|  |  |  | 		              "but no thread-state for this thread"); | 
					
						
							|  |  |  | 	/* We must hold the GIL and have our thread state current */ | 
					
						
							|  |  |  | 	/* XXX - remove the check - the assert should be fine,
 | 
					
						
							| 
									
										
										
										
											2004-10-09 17:25:05 +00:00
										 |  |  | 	   but while this is very new (April 2003), the extra check | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 	   by release-only users can't hurt. | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	if (! PyThreadState_IsCurrent(tcur)) | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		Py_FatalError("This thread state must be current when releasing"); | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	assert(PyThreadState_IsCurrent(tcur)); | 
					
						
							|  |  |  | 	--tcur->gilstate_counter; | 
					
						
							|  |  |  | 	assert(tcur->gilstate_counter >= 0); /* illegal counter value */ | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-08 04:30:21 +00:00
										 |  |  | 	/* If we're going to destroy this thread-state, we must
 | 
					
						
							|  |  |  | 	 * clear it while the GIL is held, as destructors may run. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 	if (tcur->gilstate_counter == 0) { | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		/* can't have been locked when we created it */ | 
					
						
							| 
									
										
										
										
											2004-10-09 17:38:29 +00:00
										 |  |  | 		assert(oldstate == PyGILState_UNLOCKED); | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 		PyThreadState_Clear(tcur); | 
					
						
							| 
									
										
										
										
											2004-11-08 04:30:21 +00:00
										 |  |  | 		/* Delete the thread-state.  Note this releases the GIL too!
 | 
					
						
							|  |  |  | 		 * It's vital that the GIL be held here, to avoid shutdown | 
					
						
							|  |  |  | 		 * races; see bugs 225673 and 1061968 (that nasty bug has a | 
					
						
							|  |  |  | 		 * habit of coming back). | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		PyThreadState_DeleteCurrent(); | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	/* Release the lock if necessary */ | 
					
						
							| 
									
										
										
										
											2004-11-08 04:30:21 +00:00
										 |  |  | 	else if (oldstate == PyGILState_UNLOCKED) | 
					
						
							| 
									
										
										
										
											2005-04-18 08:46:17 +00:00
										 |  |  | 		PyEval_SaveThread(); | 
					
						
							| 
									
										
										
										
											2003-04-19 15:41:53 +00:00
										 |  |  | } | 
					
						
							|  |  |  | #endif /* WITH_THREAD */
 |