|
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 | #definePOSTGRES_ECPG_INTERNAL
|
4 | 4 | #include"postgres_fe.h"
|
@@ -57,9 +57,7 @@ static struct sqlca_t sqlca_init =
|
57 | 57 |
|
58 | 58 | #ifdefENABLE_THREAD_SAFETY
|
59 | 59 | staticpthread_key_tsqlca_key;
|
60 |
| -#ifndefWIN32 |
61 | 60 | staticpthread_once_tsqlca_key_once=PTHREAD_ONCE_INIT;
|
62 |
| -#endif |
63 | 61 | #else
|
64 | 62 | staticstructsqlca_tsqlca=
|
65 | 63 | {
|
@@ -90,8 +88,8 @@ static struct sqlca_t sqlca =
|
90 | 88 | #endif
|
91 | 89 |
|
92 | 90 | #ifdefENABLE_THREAD_SAFETY
|
93 |
| -NON_EXEC_STATICpthread_mutex_tdebug_mutex=PTHREAD_MUTEX_INITIALIZER; |
94 |
| -NON_EXEC_STATICpthread_mutex_tdebug_init_mutex=PTHREAD_MUTEX_INITIALIZER; |
| 91 | +staticpthread_mutex_tdebug_mutex=PTHREAD_MUTEX_INITIALIZER; |
| 92 | +staticpthread_mutex_tdebug_init_mutex=PTHREAD_MUTEX_INITIALIZER; |
95 | 93 | #endif
|
96 | 94 | staticintsimple_debug=0;
|
97 | 95 | staticFILE*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_STATICvoid |
| 126 | +staticvoid |
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 | #ifdefWIN32
|
| 416 | +#ifdefENABLE_THREAD_SAFETY |
| 417 | + |
| 418 | +void |
| 419 | +win32_pthread_mutex(volatilepthread_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 | +staticpthread_mutex_twin32_pthread_once_lock=PTHREAD_MUTEX_INITIALIZER; |
422 | 432 |
|
423 |
| -BOOLWINAPI |
424 |
| -DllMain(HANDLEmodule,DWORDreason,LPVOIDreserved) |
| 433 | +void |
| 434 | +win32_pthread_once(volatilepthread_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 */ |