Andrew Chernow wrote:
> Tom Lane wrote:
> >Silently not locking is surely
> > not very safe.
> > 
> 
> Here is the dump code version of the patch.  If anyone wants the
> return value idea, let me know.

Here's a version of this patch that doesn't use malloc - instead, I had
to change the callers to it a bit.

This makes a difference only on Vista+ really, because prior to Vista
the function InitializeCriticalSection() *can* fail - it will throw an
exception and not return any error code. Which really isn't that
different from just crashing like this latest patch from Andrew would
have us do (on out of memory). 

I'm leaning towards going with the simpler one, which is the patch from
Andrew that crashes on out of memory.

Comments/preferences?

//Magnus
Index: fe-connect.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.357
diff -c -r1.357 fe-connect.c
*** fe-connect.c	31 Mar 2008 02:43:14 -0000	1.357
--- fe-connect.c	21 Apr 2008 11:35:33 -0000
***************
*** 3827,3841 ****
  #ifndef WIN32
  	static pthread_mutex_t singlethread_lock = PTHREAD_MUTEX_INITIALIZER;
  #else
! 	static pthread_mutex_t singlethread_lock = NULL;
  	static long mutex_initlock = 0;
  
! 	if (singlethread_lock == NULL)
  	{
  		while (InterlockedExchange(&mutex_initlock, 1) == 1)
! 			 /* loop, another thread own the lock */ ;
! 		if (singlethread_lock == NULL)
  			pthread_mutex_init(&singlethread_lock, NULL);
  		InterlockedExchange(&mutex_initlock, 0);
  	}
  #endif
--- 3827,3845 ----
  #ifndef WIN32
  	static pthread_mutex_t singlethread_lock = PTHREAD_MUTEX_INITIALIZER;
  #else
! 	static pthread_mutex_t singlethread_lock;
! 	static bool initialized = false;
  	static long mutex_initlock = 0;
  
! 	if (!initialized)
  	{
  		while (InterlockedExchange(&mutex_initlock, 1) == 1)
! 			/* loop, another thread own the lock */ ;
! 		if (!initialized)
! 		{
  			pthread_mutex_init(&singlethread_lock, NULL);
+ 			initialized = true;
+ 		}
  		InterlockedExchange(&mutex_initlock, 0);
  	}
  #endif
Index: fe-secure.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/interfaces/libpq/fe-secure.c,v
retrieving revision 1.104
diff -c -r1.104 fe-secure.c
*** fe-secure.c	31 Mar 2008 02:43:14 -0000	1.104
--- fe-secure.c	21 Apr 2008 11:44:22 -0000
***************
*** 809,823 ****
  #ifndef WIN32
  	static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
  #else
! 	static pthread_mutex_t init_mutex = NULL;
  	static long mutex_initlock = 0;
  
! 	if (init_mutex == NULL)
  	{
  		while (InterlockedExchange(&mutex_initlock, 1) == 1)
  			 /* loop, another thread own the lock */ ;
! 		if (init_mutex == NULL)
  			pthread_mutex_init(&init_mutex, NULL);
  		InterlockedExchange(&mutex_initlock, 0);
  	}
  #endif
--- 809,827 ----
  #ifndef WIN32
  	static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
  #else
! 	static pthread_mutex_t init_mutex;
! 	static bool initialized = false;
  	static long mutex_initlock = 0;
  
! 	if (!initialized)
  	{
  		while (InterlockedExchange(&mutex_initlock, 1) == 1)
  			 /* loop, another thread own the lock */ ;
! 		if (!initialized)
! 		{
  			pthread_mutex_init(&init_mutex, NULL);
+ 			initialized = true;
+ 		}
  		InterlockedExchange(&mutex_initlock, 0);
  	}
  #endif
Index: pthread-win32.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/interfaces/libpq/pthread-win32.c,v
retrieving revision 1.15
diff -c -r1.15 pthread-win32.c
*** pthread-win32.c	1 Jan 2008 19:46:00 -0000	1.15
--- pthread-win32.c	21 Apr 2008 11:27:38 -0000
***************
*** 35,51 ****
  void
  pthread_mutex_init(pthread_mutex_t *mp, void *attr)
  {
! 	*mp = CreateMutex(0, 0, 0);
  }
  
  void
  pthread_mutex_lock(pthread_mutex_t *mp)
  {
! 	WaitForSingleObject(*mp, INFINITE);
  }
  
  void
  pthread_mutex_unlock(pthread_mutex_t *mp)
  {
! 	ReleaseMutex(*mp);
  }
--- 35,51 ----
  void
  pthread_mutex_init(pthread_mutex_t *mp, void *attr)
  {
! 	InitializeCriticalSection(mp);
  }
  
  void
  pthread_mutex_lock(pthread_mutex_t *mp)
  {
! 	EnterCriticalSection(mp);
  }
  
  void
  pthread_mutex_unlock(pthread_mutex_t *mp)
  {
! 	LeaveCriticalSection(mp);
  }
-- 
Sent via pgsql-patches mailing list (pgsql-patches@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-patches

Reply via email to