[Python-checkins] CVS: python/dist/src/Python thread_nt.h,2.6,2.7

Guido van Rossum python-dev@python.org
Thu, 4 May 2000 14:47:19 -0400 (EDT)


Update of /projects/cvsroot/python/dist/src/Python
In directory eric:/projects/python/develop/guido/src/Python

Modified Files:
	thread_nt.h 
Log Message:
Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru,
who wrote:

Here's the new version of thread_nt.h.  More particular, there is a
new version of thread lock that uses kernel object (e.g. semaphore)
only in case of contention; in other case it simply uses interlocked
functions, which are faster by the order of magnitude.  It doesn't
make much difference without threads present, but as soon as thread
machinery initialised and (mostly) the interpreter global lock is on,
difference becomes tremendous.  I've included a small script, which
initialises threads and launches pystone.  With original thread_nt.h,
Pystone results with initialised threads are twofold worse then w/o
threads.  With the new version, only 10% worse.  I have used this
patch for about 6 months (with threaded and non-threaded
applications).  It works remarkably well (though I'd desperately
prefer Python was free-threaded; I hope, it will soon).


Index: thread_nt.h
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Python/thread_nt.h,v
retrieving revision 2.6
retrieving revision 2.7
diff -C2 -r2.6 -r2.7
*** thread_nt.h	2000/01/20 22:32:56	2.6
--- thread_nt.h	2000/05/04 18:47:15	2.7
***************
*** 31,34 ****
--- 31,35 ----
  
  /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
+ /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
  
  #include <windows.h>
***************
*** 36,39 ****
--- 37,141 ----
  #include <process.h>
  
+ typedef struct NRMUTEX {
+ 	LONG   owned ;
+ 	DWORD  thread_id ;
+ 	HANDLE hevent ;
+ } NRMUTEX, *PNRMUTEX ;
+ 
+ 
+ typedef PVOID WINAPI interlocked_cmp_xchg_t(PVOID *dest, PVOID exc, PVOID comperand) ;
+ 
+ /* Sorry mate, but we haven't got InterlockedCompareExchange in Win95! */
+ static PVOID WINAPI interlocked_cmp_xchg(PVOID *dest, PVOID exc, PVOID comperand)
+ {
+ 	static LONG spinlock = 0 ;
+ 	PVOID result ;
+ 
+ 	/* Acqire spinlock (yielding control to other threads if cant aquire for the moment) */
+ 	while(InterlockedExchange(&spinlock, 1)) Sleep(0) ;
+ 	result = *dest ;
+ 	if (result == comperand)
+ 		*dest = exc ;
+ 	/* Release spinlock */
+ 	spinlock = 0 ;
+ 	return result ;
+ } ;
+ 
+ static interlocked_cmp_xchg_t *ixchg ;
+ BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex)
+ {
+ 	if (!ixchg)
+ 	{
+ 		/* Sorely, Win95 has no InterlockedCompareExchange API (Win98 has), so we have to use emulation */
+ 		HANDLE kernel = GetModuleHandle("kernel32.dll") ;
+ 		if (!kernel || (ixchg = (interlocked_cmp_xchg_t *)GetProcAddress(kernel, "InterlockedCompareExchange")) == NULL)
+ 			ixchg = interlocked_cmp_xchg ;
+ 	}
+ 
+ 	mutex->owned = -1 ;  /* No threads have entered NonRecursiveMutex */
+ 	mutex->thread_id = 0 ;
+ 	mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ;
+ 	return mutex->hevent != NULL ;	/* TRUE if the mutex is created */
+ }
+ 
+ #define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand)))
+ 
+ VOID DeleteNonRecursiveMutex(PNRMUTEX mutex)
+ {
+ 	/* No in-use check */
+ 	CloseHandle(mutex->hevent) ;
+ 	mutex->hevent = NULL ; /* Just in case */
+ }
+ 
+ DWORD EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)
+ {
+ 	/* Assume that the thread waits successfully */
+ 	DWORD ret ;
+ 
+ 	/* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */
+ 	if (!wait)
+ 	{
+ 		if (InterlockedCompareExchange((PVOID *)&mutex->owned, (PVOID)0, (PVOID)-1) != (PVOID)-1)
+ 			return WAIT_TIMEOUT ;
+ 		ret = WAIT_OBJECT_0 ;
+ 	}
+ 	else
+ 		ret = InterlockedIncrement(&mutex->owned) ?
+ 			/* Some thread owns the mutex, let's wait... */
+ 			WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ;
+ 
+ 	mutex->thread_id = GetCurrentThreadId() ; /* We own it */
+ 	return ret ;
+ }
+ 
+ BOOL LeaveNonRecursiveMutex(PNRMUTEX mutex)
+ {
+ 	/* We don't own the mutex */
+ 	mutex->thread_id = 0 ;
+ 	return
+ 		InterlockedDecrement(&mutex->owned) < 0 ||
+ 		SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */
+ }
+ 
+ PNRMUTEX AllocNonRecursiveMutex()
+ {
+ 	PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ;
+ 	if (mutex && !InitializeNonRecursiveMutex(mutex))
+ 	{
+ 		free(mutex) ;
+ 		mutex = NULL ;
+ 	}
+ 	return mutex ;
+ }
+ 
+ void FreeNonRecursiveMutex(PNRMUTEX mutex)
+ {
+ 	if (mutex)
+ 	{
+ 		DeleteNonRecursiveMutex(mutex) ;
+ 		free(mutex) ;
+ 	}
+ }
+ 
  long PyThread_get_thread_ident(void);
  
***************
*** 66,70 ****
  	if (rv != -1) {
  		success = 1;
! 		dprintf(("%ld: PyThread_start_new_thread succeeded: %ld\n", PyThread_get_thread_ident(), rv));
  	}
  
--- 168,172 ----
  	if (rv != -1) {
  		success = 1;
! 		dprintf(("%ld: PyThread_start_new_thread succeeded: %ld\n", PyThread_get_thread_ident(), aThreadId));
  	}
  
***************
*** 80,84 ****
  	if (!initialized)
  		PyThread_init_thread();
!         
  	return GetCurrentThreadId();
  }
--- 182,186 ----
  	if (!initialized)
  		PyThread_init_thread();
! 
  	return GetCurrentThreadId();
  }
***************
*** 134,138 ****
  PyThread_type_lock PyThread_allocate_lock(void)
  {
! 	HANDLE aLock;
  
  	dprintf(("PyThread_allocate_lock called\n"));
--- 236,240 ----
  PyThread_type_lock PyThread_allocate_lock(void)
  {
! 	PNRMUTEX aLock;
  
  	dprintf(("PyThread_allocate_lock called\n"));
***************
*** 140,148 ****
  		PyThread_init_thread();
  
! 		aLock = CreateSemaphore(NULL,           /* Security attributes          */
!                          1,                     /* Initial value                */
!                          1,                     /* Maximum value                */
!                          NULL);       
!   /* Name of semaphore            */
  
  	dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock));
--- 242,246 ----
  		PyThread_init_thread();
  
! 	aLock = AllocNonRecursiveMutex() ;
  
  	dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock));
***************
*** 155,159 ****
  	dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  
! 	CloseHandle((HANDLE) aLock);
  }
  
--- 253,257 ----
  	dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  
! 	FreeNonRecursiveMutex(aLock) ;
  }
  
***************
*** 166,179 ****
  int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
  {
! 	int success = 1;
! 	DWORD waitResult;
  
  	dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),(long)aLock, waitflag));
- 
- 	waitResult = WaitForSingleObject((HANDLE) aLock, (waitflag == 1 ? INFINITE : 0));
  
! 	if (waitResult != WAIT_OBJECT_0) {
! 		success = 0;    /* We failed */
! 	}
  
  	dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n", PyThread_get_thread_ident(),(long)aLock, waitflag, success));
--- 264,272 ----
  int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
  {
! 	int success ;
  
  	dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),(long)aLock, waitflag));
  
! 	success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag == 1 ? INFINITE : 0)) == WAIT_OBJECT_0 ;
  
  	dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n", PyThread_get_thread_ident(),(long)aLock, waitflag, success));
***************
*** 186,196 ****
  	dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  
! 	if (!ReleaseSemaphore(
!                         (HANDLE) aLock,                         /* Handle of semaphore                          */
!                         1,                                      /* increment count by one                       */
!                         NULL))                                  /* not interested in previous count             */
! 		{
  		dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n", PyThread_get_thread_ident(), (long)aLock, GetLastError()));
- 		}
  }
  
--- 279,284 ----
  	dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
  
! 	if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
  		dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n", PyThread_get_thread_ident(), (long)aLock, GetLastError()));
  }
  
***************
*** 207,213 ****
  
  	aSemaphore = CreateSemaphore( NULL,           /* Security attributes          */
!                                   value,          /* Initial value                */
!                                   INT_MAX,        /* Maximum value                */
!                                   NULL);          /* Name of semaphore            */
  
  	dprintf(("%ld: PyThread_allocate_sema() -> %lx\n", PyThread_get_thread_ident(), (long)aSemaphore));
--- 295,301 ----
  
  	aSemaphore = CreateSemaphore( NULL,           /* Security attributes          */
! 	                              value,          /* Initial value                */
! 	                              INT_MAX,        /* Maximum value                */
! 	                              NULL);          /* Name of semaphore            */
  
  	dprintf(("%ld: PyThread_allocate_sema() -> %lx\n", PyThread_get_thread_ident(), (long)aSemaphore));