summaryrefslogtreecommitdiff
path: root/src/port/pthread-win32.h
diff options
context:
space:
mode:
authorTom Lane2024-02-09 16:11:39 +0000
committerTom Lane2024-02-09 16:11:39 +0000
commit0028b55bc8e2fc80816094620d116e2cbbd8e5f3 (patch)
tree59fe1f31ab3ed847fb71464a517afeabd2f059f6 /src/port/pthread-win32.h
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/[email protected]
Diffstat (limited to 'src/port/pthread-win32.h')
-rw-r--r--src/port/pthread-win32.h11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/port/pthread-win32.h b/src/port/pthread-win32.h
index 97ccc17a126..5f33269057c 100644
--- a/src/port/pthread-win32.h
+++ b/src/port/pthread-win32.h
@@ -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);