|
1 | | -/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.38 2007/10/02 09:49:59 meskes Exp $ */ |
| 1 | +/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.39 2007/10/03 08:55:22 meskes Exp $ */ |
2 | 2 |
|
3 | 3 | #define POSTGRES_ECPG_INTERNAL |
4 | 4 | #include "postgres_fe.h" |
@@ -57,9 +57,7 @@ static struct sqlca_t sqlca_init = |
57 | 57 |
|
58 | 58 | #ifdef ENABLE_THREAD_SAFETY |
59 | 59 | static pthread_key_t sqlca_key; |
60 | | -#ifndef WIN32 |
61 | 60 | static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT; |
62 | | -#endif |
63 | 61 | #else |
64 | 62 | static struct sqlca_t sqlca = |
65 | 63 | { |
@@ -90,8 +88,8 @@ static struct sqlca_t sqlca = |
90 | 88 | #endif |
91 | 89 |
|
92 | 90 | #ifdef ENABLE_THREAD_SAFETY |
93 | | -NON_EXEC_STATIC pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
94 | | -NON_EXEC_STATIC pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 91 | +static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 92 | +static pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER; |
95 | 93 | #endif |
96 | 94 | static int simple_debug = 0; |
97 | 95 | static FILE *debugstream = NULL; |
@@ -125,7 +123,7 @@ ecpg_sqlca_key_destructor(void *arg) |
125 | 123 | free(arg); /* sqlca structure allocated in ECPGget_sqlca */ |
126 | 124 | } |
127 | 125 |
|
128 | | -NON_EXEC_STATIC void |
| 126 | +static void |
129 | 127 | ecpg_sqlca_key_init(void) |
130 | 128 | { |
131 | 129 | pthread_key_create(&sqlca_key, ecpg_sqlca_key_destructor); |
@@ -415,24 +413,37 @@ ECPGis_noind_null(enum ECPGttype type, void *ptr) |
415 | 413 | } |
416 | 414 |
|
417 | 415 | #ifdef WIN32 |
| 416 | +#ifdef ENABLE_THREAD_SAFETY |
| 417 | + |
| 418 | +void |
| 419 | +win32_pthread_mutex(volatile pthread_mutex_t *mutex) |
| 420 | +{ |
| 421 | + if (mutex->handle == NULL) |
| 422 | + { |
| 423 | + while (InterlockedExchange((LONG *)&mutex->initlock, 1) == 1) |
| 424 | + Sleep(0); |
| 425 | + if (mutex->handle == NULL) |
| 426 | + mutex->handle = CreateMutex(NULL, FALSE, NULL); |
| 427 | + InterlockedExchange((LONG *)&mutex->initlock, 0); |
| 428 | + } |
| 429 | +} |
418 | 430 |
|
419 | | -/* |
420 | | - * Initialize mutexes and call init-once functions on loading. |
421 | | - */ |
| 431 | +static pthread_mutex_t win32_pthread_once_lock = PTHREAD_MUTEX_INITIALIZER; |
422 | 432 |
|
423 | | -BOOL WINAPI |
424 | | -DllMain(HANDLE module, DWORD reason, LPVOID reserved) |
| 433 | +void |
| 434 | +win32_pthread_once(volatile pthread_once_t *once, void (*fn)(void)) |
425 | 435 | { |
426 | | - if (reason == DLL_PROCESS_ATTACH) |
| 436 | + if (!*once) |
427 | 437 | { |
428 | | - connections_mutex = CreateMutex(NULL, FALSE, NULL); |
429 | | - debug_mutex = CreateMutex(NULL, FALSE, NULL); |
430 | | - debug_init_mutex = CreateMutex(NULL, FALSE, NULL); |
431 | | - auto_mem_key_init(); |
432 | | - ecpg_actual_connection_init(); |
433 | | - ecpg_sqlca_key_init(); |
434 | | - descriptor_key_init(); |
| 438 | + pthread_mutex_lock(&win32_pthread_once_lock); |
| 439 | + if (!*once) |
| 440 | + { |
| 441 | + *once = true; |
| 442 | + fn(); |
| 443 | + } |
| 444 | + pthread_mutex_unlock(&win32_pthread_once_lock); |
435 | 445 | } |
436 | | - return TRUE; |
437 | 446 | } |
438 | | -#endif |
| 447 | + |
| 448 | +#endif /* ENABLE_THREAD_SAFETY */ |
| 449 | +#endif /* WIN32 */ |
0 commit comments