aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpq/fe-connect.c
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>2004-07-12 14:11:17 +0000
committerBruce Momjian <bruce@momjian.us>2004-07-12 14:11:17 +0000
commita41463e31c4e3c231a84c47088e76d39191c4f36 (patch)
tree7844c4b346185ff74dcbe94d7b709e3d1da6018e /src/interfaces/libpq/fe-connect.c
parentc14a43f657c33189582ca1a7a60ab419dc6164a5 (diff)
downloadpostgresql-a41463e31c4e3c231a84c47088e76d39191c4f36.tar.gz
postgresql-a41463e31c4e3c231a84c47088e76d39191c4f36.zip
win32 doesn't support a static initializer for mutexes, thus the first
user must initialize the lock. The problem are concurrent "first" users - the pthread_mutex_t initialization must be synchronized. The current implementation is broken, the attached patches fixes that: mutex_initlock is a spinlock. If the pthread_mutex_t mutex is not initialized, then the spinlock is acquired, if the pthread_mutex_t is initialized if it's not yet initialized and then the spinlock is dropped. Manfred Spraul
Diffstat (limited to 'src/interfaces/libpq/fe-connect.c')
-rw-r--r--src/interfaces/libpq/fe-connect.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index c323c45fc6f..9b60efa3e67 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.275 2004/06/19 04:22:17 momjian Exp $
+ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.276 2004/07/12 14:11:17 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -3193,10 +3193,16 @@ default_threadlock(int acquire)
#ifndef WIN32
static pthread_mutex_t singlethread_lock = PTHREAD_MUTEX_INITIALIZER;
#else
- static pthread_mutex_t singlethread_lock;
- static long mutex_initialized = 0;
- if (!InterlockedExchange(&mutex_initialized, 1L))
- pthread_mutex_init(&singlethread_lock, NULL);
+ 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
if (acquire)
pthread_mutex_lock(&singlethread_lock);