]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Clean up Windows-specific mutex code in libpq and ecpglib.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 9 Feb 2024 16:11:39 +0000 (11:11 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 9 Feb 2024 16:11:39 +0000 (11:11 -0500)
Fix pthread-win32.h and pthread-win32.c to provide a more complete
emulation of POSIX pthread mutexes: define PTHREAD_MUTEX_INITIALIZER
and make sure that pthread_mutex_lock() can operate on a mutex
object that's been initialized that way.  Then we don't need the
duplicative platform-specific logic in default_threadlock() and
pgtls_init(), which we'd otherwise need yet a third copy of for
an upcoming bug fix.

Also, since default_threadlock() supposes that pthread_mutex_lock()
cannot fail, try to ensure that that's actually true, by getting
rid of the malloc call that was formerly involved in initializing
an emulated mutex.  We can define an extra state for the spinlock
field instead.

Also, replace the similar code in ecpglib/misc.c with this version.
While ecpglib's version at least had a POSIX-compliant API, it
also had the potential of failing during mutex init (but here,
because of CreateMutex failure rather than malloc failure).  Since
all of misc.c's callers ignore failures, it seems like a wise idea
to avoid failures here too.

A further improvement in this area could be to unify libpq's and
ecpglib's implementations into a src/port/pthread-win32.c file.
But that doesn't seem like a bug fix, so I'll desist for now.

In preparation for the aforementioned bug fix, back-patch to all
supported branches.

Discussion: https://postgr.es/m/264860.1707163416@sss.pgh.pa.us

src/interfaces/ecpg/ecpglib/misc.c
src/interfaces/ecpg/include/ecpg-pthread-win32.h
src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/fe-secure-openssl.c
src/interfaces/libpq/pthread-win32.c
src/port/pthread-win32.h

index 1eef1ec044cde2dc8151b7d96d72ff835ab841bd..68fc627f10daecb58d6b9eabc29d3a50fcd752e0 100644 (file)
@@ -453,17 +453,38 @@ ECPGis_noind_null(enum ECPGttype type, const void *ptr)
 #ifdef WIN32
 #ifdef ENABLE_THREAD_SAFETY
 
-void
-win32_pthread_mutex(volatile pthread_mutex_t *mutex)
+int
+pthread_mutex_init(pthread_mutex_t *mp, void *attr)
+{
+       mp->initstate = 0;
+       return 0;
+}
+
+int
+pthread_mutex_lock(pthread_mutex_t *mp)
 {
-       if (mutex->handle == NULL)
+       /* Initialize the csection if not already done */
+       if (mp->initstate != 1)
        {
-               while (InterlockedExchange((LONG *) &mutex->initlock, 1) == 1)
-                       Sleep(0);
-               if (mutex->handle == NULL)
-                       mutex->handle = CreateMutex(NULL, FALSE, NULL);
-               InterlockedExchange((LONG *) &mutex->initlock, 0);
+               LONG            istate;
+
+               while ((istate = InterlockedExchange(&mp->initstate, 2)) == 2)
+                       Sleep(0);                       /* wait, another thread is doing this */
+               if (istate != 1)
+                       InitializeCriticalSection(&mp->csection);
+               InterlockedExchange(&mp->initstate, 1);
        }
+       EnterCriticalSection(&mp->csection);
+       return 0;
+}
+
+int
+pthread_mutex_unlock(pthread_mutex_t *mp)
+{
+       if (mp->initstate != 1)
+               return EINVAL;
+       LeaveCriticalSection(&mp->csection);
+       return 0;
 }
 
 static pthread_mutex_t win32_pthread_once_lock = PTHREAD_MUTEX_INITIALIZER;
index 33c897b633c57eda24c2e9dc32eec7f7ffc18a0c..2782e49d4cdf3e9707d8359f0ff19d5231c5e876 100644 (file)
 
 typedef struct pthread_mutex_t
 {
-       HANDLE          handle;
-       LONG            initlock;
+       /* initstate = 0: not initialized; 1: init done; 2: init in progress */
+       LONG            initstate;
+       CRITICAL_SECTION csection;
 } pthread_mutex_t;
 
 typedef DWORD pthread_key_t;
 typedef bool pthread_once_t;
 
-#define PTHREAD_MUTEX_INITIALIZER      { NULL, 0 }
+#define PTHREAD_MUTEX_INITIALIZER      { 0 }
 #define PTHREAD_ONCE_INIT                      false
 
-void           win32_pthread_mutex(volatile pthread_mutex_t *mutex);
-void           win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void));
+int                    pthread_mutex_init(pthread_mutex_t *, void *attr);
+int                    pthread_mutex_lock(pthread_mutex_t *);
+int                    pthread_mutex_unlock(pthread_mutex_t *);
 
-#define pthread_mutex_lock(mutex) \
-       do { \
-               if ((mutex)->handle == NULL) \
-                       win32_pthread_mutex((mutex)); \
-               WaitForSingleObject((mutex)->handle, INFINITE); \
-       } while(0)
-
-#define pthread_mutex_unlock(mutex) \
-       ReleaseMutex((mutex)->handle)
+void           win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void));
 
 #define pthread_getspecific(key) \
        TlsGetValue((key))
index a80901349b264634cf21a4c1c10e11bc0911325e..47dcb37fd49ccf457439e19d4d1a95e0a1b70de1 100644 (file)
@@ -7443,24 +7443,8 @@ static void
 default_threadlock(int acquire)
 {
 #ifdef ENABLE_THREAD_SAFETY
-#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)
-               {
-                       if (pthread_mutex_init(&singlethread_lock, NULL))
-                               Assert(false);
-               }
-               InterlockedExchange(&mutex_initlock, 0);
-       }
-#endif
        if (acquire)
        {
                if (pthread_mutex_lock(&singlethread_lock))
index 61f37678a6b9a8a87f6cf7fb047c7c2a4c69f046..1c9dd76bea537abc78384d83bffc8abd37f4f473 100644 (file)
@@ -96,12 +96,7 @@ static bool ssl_lib_initialized = false;
 #ifdef ENABLE_THREAD_SAFETY
 static long crypto_open_connections = 0;
 
-#ifndef WIN32
 static pthread_mutex_t ssl_config_mutex = PTHREAD_MUTEX_INITIALIZER;
-#else
-static pthread_mutex_t ssl_config_mutex = NULL;
-static long win32_ssl_create_mutex = 0;
-#endif
 #endif                                                 /* ENABLE_THREAD_SAFETY */
 
 static PQsslKeyPassHook_OpenSSL_type PQsslKeyPassHook = NULL;
@@ -777,20 +772,6 @@ int
 pgtls_init(PGconn *conn, bool do_ssl, bool do_crypto)
 {
 #ifdef ENABLE_THREAD_SAFETY
-#ifdef WIN32
-       /* Also see similar code in fe-connect.c, default_threadlock() */
-       if (ssl_config_mutex == NULL)
-       {
-               while (InterlockedExchange(&win32_ssl_create_mutex, 1) == 1)
-                        /* loop, another thread own the lock */ ;
-               if (ssl_config_mutex == NULL)
-               {
-                       if (pthread_mutex_init(&ssl_config_mutex, NULL))
-                               return -1;
-               }
-               InterlockedExchange(&win32_ssl_create_mutex, 0);
-       }
-#endif
        if (pthread_mutex_lock(&ssl_config_mutex))
                return -1;
 
@@ -881,7 +862,6 @@ static void
 destroy_ssl_system(void)
 {
 #if defined(ENABLE_THREAD_SAFETY) && defined(HAVE_CRYPTO_LOCK)
-       /* Mutex is created in pgtls_init() */
        if (pthread_mutex_lock(&ssl_config_mutex))
                return;
 
index c0e056ea213e1790288d6682b27519e5f4e8a901..c9609c05816c5f032ed1db0587f259646dc9bbd7 100644 (file)
@@ -34,27 +34,33 @@ pthread_getspecific(pthread_key_t key)
 int
 pthread_mutex_init(pthread_mutex_t *mp, void *attr)
 {
-       *mp = (CRITICAL_SECTION *) malloc(sizeof(CRITICAL_SECTION));
-       if (!*mp)
-               return 1;
-       InitializeCriticalSection(*mp);
+       mp->initstate = 0;
        return 0;
 }
 
 int
 pthread_mutex_lock(pthread_mutex_t *mp)
 {
-       if (!*mp)
-               return 1;
-       EnterCriticalSection(*mp);
+       /* Initialize the csection if not already done */
+       if (mp->initstate != 1)
+       {
+               LONG            istate;
+
+               while ((istate = InterlockedExchange(&mp->initstate, 2)) == 2)
+                       Sleep(0);                       /* wait, another thread is doing this */
+               if (istate != 1)
+                       InitializeCriticalSection(&mp->csection);
+               InterlockedExchange(&mp->initstate, 1);
+       }
+       EnterCriticalSection(&mp->csection);
        return 0;
 }
 
 int
 pthread_mutex_unlock(pthread_mutex_t *mp)
 {
-       if (!*mp)
-               return 1;
-       LeaveCriticalSection(*mp);
+       if (mp->initstate != 1)
+               return EINVAL;
+       LeaveCriticalSection(&mp->csection);
        return 0;
 }
index 97ccc17a1263c1a1b5381552dbef3c91a3afa901..5f33269057c5af3c688f5b96672e697b599ab45b 100644 (file)
@@ -5,7 +5,16 @@
 #define __PTHREAD_H
 
 typedef ULONG pthread_key_t;
-typedef CRITICAL_SECTION *pthread_mutex_t;
+
+typedef struct pthread_mutex_t
+{
+       /* initstate = 0: not initialized; 1: init done; 2: init in progress */
+       LONG            initstate;
+       CRITICAL_SECTION csection;
+} pthread_mutex_t;
+
+#define PTHREAD_MUTEX_INITIALIZER      { 0 }
+
 typedef int pthread_once_t;
 
 DWORD          pthread_self(void);