Skip to content

Commit a41463e

Browse files
committed
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
1 parent c14a43f commit a41463e

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.275 2004/06/19 04:22:17 momjian Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.276 2004/07/12 14:11:17 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3193,10 +3193,16 @@ default_threadlock(int acquire)
31933193
#ifndef WIN32
31943194
static pthread_mutex_t singlethread_lock = PTHREAD_MUTEX_INITIALIZER;
31953195
#else
3196-
static pthread_mutex_t singlethread_lock;
3197-
static long mutex_initialized = 0;
3198-
if (!InterlockedExchange(&mutex_initialized, 1L))
3199-
pthread_mutex_init(&singlethread_lock, NULL);
3196+
static pthread_mutex_t singlethread_lock = NULL;
3197+
static long mutex_initlock = 0;
3198+
3199+
if (singlethread_lock == NULL) {
3200+
while(InterlockedExchange(&mutex_initlock, 1) == 1)
3201+
/* loop, another thread own the lock */ ;
3202+
if (singlethread_lock == NULL)
3203+
pthread_mutex_init(&singlethread_lock, NULL);
3204+
InterlockedExchange(&mutex_initlock,0);
3205+
}
32003206
#endif
32013207
if (acquire)
32023208
pthread_mutex_lock(&singlethread_lock);

src/interfaces/libpq/fe-secure.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.42 2004/06/19 04:22:17 momjian Exp $
14+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.43 2004/07/12 14:11:17 momjian Exp $
1515
*
1616
* NOTES
1717
* The client *requires* a valid server certificate. Since
@@ -867,10 +867,16 @@ init_ssl_system(PGconn *conn)
867867
#ifndef WIN32
868868
static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
869869
#else
870-
static pthread_mutex_t init_mutex;
871-
static long mutex_initialized = 0L;
872-
if (!InterlockedExchange(&mutex_initialized, 1L))
873-
pthread_mutex_init(&init_mutex, NULL);
870+
static pthread_mutex_t init_mutex = NULL;
871+
static long mutex_initlock = 0;
872+
873+
if (init_mutex == NULL) {
874+
while(InterlockedExchange(&mutex_initlock, 1) == 1)
875+
/* loop, another thread own the lock */ ;
876+
if (init_mutex == NULL)
877+
pthread_mutex_init(&init_mutex, NULL);
878+
InterlockedExchange(&mutex_initlock,0);
879+
}
874880
#endif
875881
pthread_mutex_lock(&init_mutex);
876882

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy