[Python-checkins] python/dist/src/Python ceval.c,2.358,2.359 pystate.c,2.25,2.26 pythonrun.c,2.190,2.191 thread.c,2.45,2.46 thread_sgi.h,2.16,2.17

mhammond@users.sourceforge.net mhammond@users.sourceforge.net
Sat, 19 Apr 2003 08:41:57 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv30725/Python

Modified Files:
	ceval.c pystate.c pythonrun.c thread.c thread_sgi.h 
Log Message:
New PyGILState_ API - implements pep 311, from patch 684256.


Index: ceval.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/ceval.c,v
retrieving revision 2.358
retrieving revision 2.359
diff -C2 -d -r2.358 -r2.359
*** ceval.c	9 Apr 2003 19:06:17 -0000	2.358
--- ceval.c	19 Apr 2003 15:41:48 -0000	2.359
***************
*** 322,325 ****
--- 322,327 ----
  	if (tstate == NULL)
  		Py_FatalError("PyEval_AcquireThread: NULL new thread state");
+ 	/* Check someone has called PyEval_InitThreads() to create the lock */
+ 	assert(interpreter_lock);
  	PyThread_acquire_lock(interpreter_lock, 1);
  	if (PyThreadState_Swap(tstate) != NULL)

Index: pystate.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/pystate.c,v
retrieving revision 2.25
retrieving revision 2.26
diff -C2 -d -r2.25 -r2.26
*** pystate.c	15 Apr 2003 15:12:39 -0000	2.25
--- pystate.c	19 Apr 2003 15:41:49 -0000	2.26
***************
*** 143,146 ****
--- 143,147 ----
  		tstate->use_tracing = 0;
  		tstate->tick_counter = 0;
+ 		tstate->gilstate_counter = 0;
  
  		tstate->dict = NULL;
***************
*** 260,264 ****
  
  	_PyThreadState_Current = new;
! 
  	return old;
  }
--- 261,275 ----
  
  	_PyThreadState_Current = new;
! 	/* It should not be possible for more than one thread state
! 	   to be used for a thread.  Check this the best we can in debug 
! 	   builds.
! 	*/
! #if defined(Py_DEBUG)
! 	if (new) {
! 		PyThreadState *check = PyGILState_GetThisThreadState();
! 		if (check && check != new)
! 			Py_FatalError("Invalid thread state for this thread");
! 	}
! #endif
  	return old;
  }
***************
*** 309,310 ****
--- 320,449 ----
  	return tstate->next;
  }
+ 
+ /* 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;
+ }
+ 
+ /* The single PyInterpreterState used by this process'
+    GILState implementation
+ */
+ static PyInterpreterState *autoInterpreterState = NULL;
+ static int autoTLSkey = 0;
+ 
+ /* Internal initialization/finalization functions called by 
+    Py_Initialize/Py_Finalize 
+ */
+ void _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
+ {
+ 	assert(i && t); /* must init with a valid states */
+ 	autoTLSkey = PyThread_create_key();
+ 	autoInterpreterState = i;
+ 	/* Now stash the thread state for this thread in TLS */
+ 	PyThread_set_key_value(autoTLSkey, (void *)t);
+ 	assert(t->gilstate_counter==0); /* must be a new thread state */
+ 	t->gilstate_counter = 1;
+ }
+ 
+ void _PyGILState_Fini(void)
+ {
+ 	PyThread_delete_key(autoTLSkey);
+ 	autoTLSkey = 0;
+ 	autoInterpreterState = NULL;;
+ }
+ 
+ /* The public functions */
+ PyThreadState *PyGILState_GetThisThreadState(void)
+ {
+ 	if (autoInterpreterState==NULL || autoTLSkey==0)
+ 		return NULL;
+ 	return (PyThreadState *) PyThread_get_key_value(autoTLSkey);
+ }
+ 
+ PyGILState_STATE PyGILState_Ensure(void)
+ {
+ 	int current;
+ 	PyThreadState *tcur;
+ 	/* Note that we do not auto-init Python here - apart from 
+ 	   potential races with 2 threads auto-initializing, pep-311 
+ 	   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);
+ 	if (tcur==NULL) {
+ 		/* Create a new thread state for this thread */
+ 		tcur = PyThreadState_New(autoInterpreterState);
+ 		if (tcur==NULL)
+ 			Py_FatalError("Couldn't create thread-state for new thread");
+ 		PyThread_set_key_value(autoTLSkey, (void *)tcur);
+ 		current = 0; /* new thread state is never current */
+ 	} else
+ 		current = PyThreadState_IsCurrent(tcur);
+ 	if (!current)
+ 		PyEval_RestoreThread(tcur);
+ 	/* Update our counter in the thread-state - no need for locks:
+ 	   - tcur will remain valid as we hold the GIL.
+ 	   - the counter is safe as we are the only thread "allowed" 
+ 	     to modify this value
+ 	*/
+ 	tcur->gilstate_counter++;
+ 	return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
+ }
+ 
+ void PyGILState_Release(PyGILState_STATE oldstate)
+ {
+ 	PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
+ 	if (tcur==NULL)
+ 		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,
+ 	   but while this is very new (April 2003), the extra check 
+ 	   by release-only users can't hurt.
+ 	*/
+ 	if (!PyThreadState_IsCurrent(tcur))
+ 		Py_FatalError("This thread state must be current when releasing");
+ 	assert (PyThreadState_IsCurrent(tcur));
+ 	tcur->gilstate_counter -= 1;
+ 	assert (tcur->gilstate_counter >= 0); /* illegal counter value */
+ 
+ 	/* If we are about to destroy this thread-state, we must 
+ 	   clear it while the lock is held, as destructors may run
+ 	*/
+ 	if (tcur->gilstate_counter==0) {
+ 		/* can't have been locked when we created it */
+ 		assert(oldstate==PyGILState_UNLOCKED);
+ 		PyThreadState_Clear(tcur);
+ 	}
+ 
+ 	/* Release the lock if necessary */
+ 	if (oldstate==PyGILState_UNLOCKED)
+ 		PyEval_ReleaseThread(tcur);
+ 
+ 	/* Now complete destruction of the thread if necessary */
+ 	if (tcur->gilstate_counter==0) {
+ 		/* Delete this thread from our TLS */
+ 		PyThread_delete_key_value(autoTLSkey);
+ 		/* Delete the thread-state */
+ 		PyThreadState_Delete(tcur);
+ 	}
+ }
+ #endif /* WITH_THREAD */

Index: pythonrun.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/pythonrun.c,v
retrieving revision 2.190
retrieving revision 2.191
diff -C2 -d -r2.190 -r2.191
*** pythonrun.c	17 Apr 2003 19:52:29 -0000	2.190
--- pythonrun.c	19 Apr 2003 15:41:52 -0000	2.191
***************
*** 51,54 ****
--- 51,59 ----
  extern void _PyUnicode_Fini(void);
  
+ #ifdef WITH_THREAD
+ extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
+ extern void _PyGILState_Fini(void);
+ #endif /* WITH_THREAD */
+ 
  int Py_DebugFlag; /* Needed by parser.c */
  int Py_VerboseFlag; /* Needed by import.c */
***************
*** 181,184 ****
--- 186,194 ----
  		initsite(); /* Module site */
  
+ 	/* auto-thread-state API, if available */
+ #ifdef WITH_THREAD
+ 	_PyGILState_Init(interp, tstate);
+ #endif /* WITH_THREAD */
+ 
  	PyModule_WarningsModule = PyImport_ImportModule("warnings");
  
***************
*** 244,247 ****
--- 254,262 ----
  	call_sys_exitfunc();
  	initialized = 0;
+ 
+ 	/* Cleanup auto-thread-state */
+ #ifdef WITH_THREAD
+ 	_PyGILState_Fini();
+ #endif /* WITH_THREAD */
  
  	/* Get current thread state and interpreter pointer */

Index: thread.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/thread.c,v
retrieving revision 2.45
retrieving revision 2.46
diff -C2 -d -r2.45 -r2.46
*** thread.c	19 Jul 2002 06:55:41 -0000	2.45
--- thread.c	19 Apr 2003 15:41:53 -0000	2.46
***************
*** 138,139 ****
--- 138,247 ----
  #endif
  */
+ 
+ #ifndef Py_HAVE_NATIVE_TLS
+ /* If the platform has not supplied a platform specific
+    TLS implementation, provide our own.
+ 
+    This code stolen from "thread_sgi.h", where it was the only
+    implementation of an existing Python TLS API.
+ */
+ /*
+  * Per-thread data ("key") support.
+  */
+ 
+ struct key {
+ 	struct key *next;
+ 	long id;
+ 	int key;
+ 	void *value;
+ };
+ 
+ static struct key *keyhead = NULL;
+ static int nkeys = 0;
+ static PyThread_type_lock keymutex = NULL;
+ 
+ static struct key *find_key(int key, void *value)
+ {
+ 	struct key *p;
+ 	long id = PyThread_get_thread_ident();
+ 	for (p = keyhead; p != NULL; p = p->next) {
+ 		if (p->id == id && p->key == key)
+ 			return p;
+ 	}
+ 	if (value == NULL)
+ 		return NULL;
+ 	p = (struct key *)malloc(sizeof(struct key));
+ 	if (p != NULL) {
+ 		p->id = id;
+ 		p->key = key;
+ 		p->value = value;
+ 		PyThread_acquire_lock(keymutex, 1);
+ 		p->next = keyhead;
+ 		keyhead = p;
+ 		PyThread_release_lock(keymutex);
+ 	}
+ 	return p;
+ }
+ 
+ int PyThread_create_key(void)
+ {
+ 	if (keymutex == NULL)
+ 		keymutex = PyThread_allocate_lock();
+ 	return ++nkeys;
+ }
+ 
+ void PyThread_delete_key(int key)
+ {
+ 	struct key *p, **q;
+ 	PyThread_acquire_lock(keymutex, 1);
+ 	q = &keyhead;
+ 	while ((p = *q) != NULL) {
+ 		if (p->key == key) {
+ 			*q = p->next;
+ 			free((void *)p);
+ 			/* NB This does *not* free p->value! */
+ 		}
+ 		else
+ 			q = &p->next;
+ 	}
+ 	PyThread_release_lock(keymutex);
+ }
+ 
+ int PyThread_set_key_value(int key, void *value)
+ {
+ 	struct key *p = find_key(key, value);
+ 	if (p == NULL)
+ 		return -1;
+ 	else
+ 		return 0;
+ }
+ 
+ void *PyThread_get_key_value(int key)
+ {
+ 	struct key *p = find_key(key, NULL);
+ 	if (p == NULL)
+ 		return NULL;
+ 	else
+ 		return p->value;
+ }
+ 
+ void PyThread_delete_key_value(int key)
+ {
+ 	long id = PyThread_get_thread_ident();
+ 	struct key *p, **q;
+ 	PyThread_acquire_lock(keymutex, 1);
+ 	q = &keyhead;
+ 	while ((p = *q) != NULL) {
+ 		if (p->key == key && p->id == id) {
+ 			*q = p->next;
+ 			free((void *)p);
+ 			/* NB This does *not* free p->value! */
+ 			break;
+ 		}
+ 		else
+ 			q = &p->next;
+ 	}
+ 	PyThread_release_lock(keymutex);
+ }
+ 
+ #endif /* Py_HAVE_NATIVE_TLS */

Index: thread_sgi.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/thread_sgi.h,v
retrieving revision 2.16
retrieving revision 2.17
diff -C2 -d -r2.16 -r2.17
*** thread_sgi.h	19 Jan 2002 22:02:55 -0000	2.16
--- thread_sgi.h	19 Apr 2003 15:41:53 -0000	2.17
***************
*** 378,459 ****
  		perror("usunsetlock");
  }
- 
- /*
-  * Per-thread data ("key") support.
-  */
- 
- struct key {
- 	struct key *next;
- 	long id;
- 	int key;
- 	void *value;
- };
- 
- static struct key *keyhead = NULL;
- static int nkeys = 0;
- static PyThread_type_lock keymutex = NULL;
- 
- static struct key *find_key(int key, void *value)
- {
- 	struct key *p;
- 	long id = PyThread_get_thread_ident();
- 	for (p = keyhead; p != NULL; p = p->next) {
- 		if (p->id == id && p->key == key)
- 			return p;
- 	}
- 	if (value == NULL)
- 		return NULL;
- 	p = (struct key *)malloc(sizeof(struct key));
- 	if (p != NULL) {
- 		p->id = id;
- 		p->key = key;
- 		p->value = value;
- 		PyThread_acquire_lock(keymutex, 1);
- 		p->next = keyhead;
- 		keyhead = p;
- 		PyThread_release_lock(keymutex);
- 	}
- 	return p;
- }
- 
- int PyThread_create_key(void)
- {
- 	if (keymutex == NULL)
- 		keymutex = PyThread_allocate_lock();
- 	return ++nkeys;
- }
- 
- void PyThread_delete_key(int key)
- {
- 	struct key *p, **q;
- 	PyThread_acquire_lock(keymutex, 1);
- 	q = &keyhead;
- 	while ((p = *q) != NULL) {
- 		if (p->key == key) {
- 			*q = p->next;
- 			free((void *)p);
- 			/* NB This does *not* free p->value! */
- 		}
- 		else
- 			q = &p->next;
- 	}
- 	PyThread_release_lock(keymutex);
- }
- 
- int PyThread_set_key_value(int key, void *value)
- {
- 	struct key *p = find_key(key, value);
- 	if (p == NULL)
- 		return -1;
- 	else
- 		return 0;
- }
- 
- void *PyThread_get_key_value(int key)
- {
- 	struct key *p = find_key(key, NULL);
- 	if (p == NULL)
- 		return NULL;
- 	else
- 		return p->value;
- }
--- 378,379 ----