summaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-02-09 11:11:39 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2024-02-09 11:11:39 -0500
commit0028b55bc8e2fc80816094620d116e2cbbd8e5f3 (patch)
tree59fe1f31ab3ed847fb71464a517afeabd2f059f6 /src/interfaces/ecpg
parent5c7038d70bb9c4d28a80b0a2051f73fafab5af3f (diff)
Clean up Windows-specific mutex code in libpq and ecpglib.
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
Diffstat (limited to 'src/interfaces/ecpg')
-rw-r--r--src/interfaces/ecpg/ecpglib/misc.c37
-rw-r--r--src/interfaces/ecpg/include/ecpg-pthread-win32.h22
2 files changed, 37 insertions, 22 deletions
diff --git a/src/interfaces/ecpg/ecpglib/misc.c b/src/interfaces/ecpg/ecpglib/misc.c
index 2b78caeaf52..58fff10697d 100644
--- a/src/interfaces/ecpg/ecpglib/misc.c
+++ b/src/interfaces/ecpg/ecpglib/misc.c
@@ -407,17 +407,38 @@ ECPGis_noind_null(enum ECPGttype type, const void *ptr)
#ifdef WIN32
-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;
diff --git a/src/interfaces/ecpg/include/ecpg-pthread-win32.h b/src/interfaces/ecpg/include/ecpg-pthread-win32.h
index 8252a178096..7b6ba46b349 100644
--- a/src/interfaces/ecpg/include/ecpg-pthread-win32.h
+++ b/src/interfaces/ecpg/include/ecpg-pthread-win32.h
@@ -12,28 +12,22 @@
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))