libpq Win32 Mutex performance patch - Mailing list pgsql-patches

From Andrew Chernow
Subject libpq Win32 Mutex performance patch
Date
Msg-id 47FFB0E4.8040105@esilo.com
Whole thread Raw
Responses Re: libpq Win32 Mutex performance patch  (Magnus Hagander <magnus@hagander.net>)
Re: libpq Win32 Mutex performance patch  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-patches
I noticed several months ago, and came across it again today, that
libpq's pthread-win32.c implementation is using CreateMutex rather than
CRITICAL_SECTION.  CreateMutex is like a semaphore in that it is
designed to be accessible via name system-wide.  Even when you don't
give it a name, thus bound to process that created it, it still carries
significant overhead compared to using win32 CRITICAL_SECTIONs.

The attached patch replaces the win32 mutex calls with critical section
calls.  The change will not affect the behavior of the windows
pthread_xxx functions.

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/
Index: src/port/pthread-win32.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/port/pthread-win32.h,v
retrieving revision 1.2
diff -c -r1.2 pthread-win32.h
*** src/port/pthread-win32.h    18 Apr 2007 08:32:40 -0000    1.2
--- src/port/pthread-win32.h    11 Apr 2008 18:35:33 -0000
***************
*** 2,8 ****
  #define __PTHREAD_H

  typedef ULONG pthread_key_t;
! typedef HANDLE pthread_mutex_t;
  typedef int pthread_once_t;

  DWORD        pthread_self(void);
--- 2,8 ----
  #define __PTHREAD_H

  typedef ULONG pthread_key_t;
! typedef CRITICAL_SECTION *pthread_mutex_t;
  typedef int pthread_once_t;

  DWORD        pthread_self(void);
Index: src/interfaces/libpq/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
*** src/interfaces/libpq/pthread-win32.c    1 Jan 2008 19:46:00 -0000    1.15
--- src/interfaces/libpq/pthread-win32.c    11 Apr 2008 18:35:33 -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,69 ----
  void
  pthread_mutex_init(pthread_mutex_t *mp, void *attr)
  {
!     if(mp)
!     {
!         *mp = (CRITICAL_SECTION *)malloc(sizeof(CRITICAL_SECTION));
!         if(*mp)
!             InitializeCriticalSection(*mp);
!     }
  }

  void
  pthread_mutex_lock(pthread_mutex_t *mp)
  {
!     if(mp && *mp)
!         EnterCriticalSection(*mp);
  }

  void
  pthread_mutex_unlock(pthread_mutex_t *mp)
  {
!     if(mp && *mp)
!         LeaveCriticalSection(*mp);
  }
+
+ /* If ever needed
+ pthread_mutex_destroy(pthread_mutex_t *mp)
+ {
+     if(mp && *mp)
+     {
+         DeleteCriticalSection(*mp);
+         *mp = NULL;
+     }
+ }
+ */
\ No newline at end of file

pgsql-patches by date:

Previous
From: "Merlin Moncure"
Date:
Subject: Re: libpq patch for pqtypes hook api and PGresult creation
Next
From: Magnus Hagander
Date:
Subject: Re: libpq Win32 Mutex performance patch