Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Patches > Re: libpq Win32...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 3616 of 3869
Post > Topic >>

Re: libpq Win32 Mutex performance patch

by magnus@[EMAIL PROTECTED] (Magnus Hagander) Apr 21, 2008 at 01:51 PM

--MP_/f_xIYK4yX98e4hXtgknqwNP
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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 cra****ng 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

--MP_/f_xIYK4yX98e4hXtgknqwNP
Content-Type: text/x-patch; name=libpq.diff
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=libpq.diff

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);
  }

--MP_/f_xIYK4yX98e4hXtgknqwNP
Content-Type: text/plain
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0


-- 
Sent via pgsql-patches mailing list (pgsql-patches@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-patches

--MP_/f_xIYK4yX98e4hXtgknqwNP--
 




 1 Posts in Topic:
Re: libpq Win32 Mutex performance patch
magnus@[EMAIL PROTECTED]   2008-04-21 13:51:10 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan13V112 Sun Jul 6 18:38:47 CDT 2008.