diff -cpr HEAD/src/interfaces/ecpg/ecpglib/connect.c remove-dllmain/src/interfaces/ecpg/ecpglib/connect.c
*** HEAD/src/interfaces/ecpg/ecpglib/connect.c	Wed Oct  3 11:07:27 2007
--- remove-dllmain/src/interfaces/ecpg/ecpglib/connect.c	Wed Oct  3 11:15:56 2007
***************
*** 11,27 ****
  #include "sqlca.h"
  
  #ifdef ENABLE_THREAD_SAFETY
! NON_EXEC_STATIC pthread_mutex_t	connections_mutex = PTHREAD_MUTEX_INITIALIZER;
  static pthread_key_t	actual_connection_key;
- #ifndef WIN32
  static pthread_once_t	actual_connection_key_once = PTHREAD_ONCE_INIT;
  #endif
- #endif
  static struct connection *actual_connection = NULL;
  static struct connection *all_connections = NULL;
  
  #ifdef ENABLE_THREAD_SAFETY
! NON_EXEC_STATIC void
  ecpg_actual_connection_init(void)
  {
  	pthread_key_create(&actual_connection_key, NULL);
--- 11,25 ----
  #include "sqlca.h"
  
  #ifdef ENABLE_THREAD_SAFETY
! static pthread_mutex_t	connections_mutex = PTHREAD_MUTEX_INITIALIZER;
  static pthread_key_t	actual_connection_key;
  static pthread_once_t	actual_connection_key_once = PTHREAD_ONCE_INIT;
  #endif
  static struct connection *actual_connection = NULL;
  static struct connection *all_connections = NULL;
  
  #ifdef ENABLE_THREAD_SAFETY
! static void
  ecpg_actual_connection_init(void)
  {
  	pthread_key_create(&actual_connection_key, NULL);
*************** ECPGconnect(int lineno, int c, const cha
*** 447,453 ****
  
  	this->cache_head = NULL;
  	this->prep_stmts = NULL;
- 	this->descriptors = NULL;
  
  	if (all_connections == NULL)
  		this->next = NULL;
--- 445,450 ----
diff -cpr HEAD/src/interfaces/ecpg/ecpglib/descriptor.c remove-dllmain/src/interfaces/ecpg/ecpglib/descriptor.c
*** HEAD/src/interfaces/ecpg/ecpglib/descriptor.c	Wed Oct  3 11:07:27 2007
--- remove-dllmain/src/interfaces/ecpg/ecpglib/descriptor.c	Wed Oct  3 11:15:56 2007
*************** static void descriptor_deallocate_all(st
*** 21,29 ****
  /* We manage descriptors separately for each thread. */
  #ifdef ENABLE_THREAD_SAFETY
  static pthread_key_t	descriptor_key;
- #ifndef WIN32
  static pthread_once_t	descriptor_once = PTHREAD_ONCE_INIT;
- #endif
  
  static void
  descriptor_destructor(void *arg)
--- 21,27 ----
*************** descriptor_destructor(void *arg)
*** 31,37 ****
  	descriptor_deallocate_all(arg);
  }
  
! NON_EXEC_STATIC void
  descriptor_key_init(void)
  {
  	pthread_key_create(&descriptor_key, descriptor_destructor);
--- 29,35 ----
  	descriptor_deallocate_all(arg);
  }
  
! static void
  descriptor_key_init(void)
  {
  	pthread_key_create(&descriptor_key, descriptor_destructor);
diff -cpr HEAD/src/interfaces/ecpg/ecpglib/extern.h remove-dllmain/src/interfaces/ecpg/ecpglib/extern.h
*** HEAD/src/interfaces/ecpg/ecpglib/extern.h	Wed Oct  3 11:12:17 2007
--- remove-dllmain/src/interfaces/ecpg/ecpglib/extern.h	Wed Oct  3 11:15:56 2007
*************** bool ECPGget_data(const PGresult *, int,
*** 36,43 ****
  
  #ifdef ENABLE_THREAD_SAFETY
  void		ecpg_pthreads_init(void);
- #else
- #define		ecpg_pthreads_init()		((void)0)
  #endif
  struct connection *ECPGget_connection(const char *);
  char	   *ECPGalloc(long, int);
--- 36,41 ----
*************** struct connection
*** 94,100 ****
  	int			autocommit;
  	struct ECPGtype_information_cache *cache_head;
  	struct prepared_statement *prep_stmts;
- 	struct descriptor *descriptors;
  	struct connection *next;
  };
  
--- 92,97 ----
diff -cpr HEAD/src/interfaces/ecpg/ecpglib/memory.c remove-dllmain/src/interfaces/ecpg/ecpglib/memory.c
*** HEAD/src/interfaces/ecpg/ecpglib/memory.c	Tue Oct  2 11:00:11 2007
--- remove-dllmain/src/interfaces/ecpg/ecpglib/memory.c	Wed Oct  3 11:15:56 2007
*************** struct auto_mem
*** 70,78 ****
  
  #ifdef ENABLE_THREAD_SAFETY
  static pthread_key_t	auto_mem_key;
- #ifndef WIN32
  static pthread_once_t	auto_mem_once = PTHREAD_ONCE_INIT;
- #endif
  
  static void
  auto_mem_destructor(void *arg)
--- 70,76 ----
*************** auto_mem_destructor(void *arg)
*** 80,86 ****
  	ECPGfree_auto_mem();
  }
  
! NON_EXEC_STATIC void
  auto_mem_key_init(void)
  {
  	pthread_key_create(&auto_mem_key, auto_mem_destructor);
--- 78,84 ----
  	ECPGfree_auto_mem();
  }
  
! static void
  auto_mem_key_init(void)
  {
  	pthread_key_create(&auto_mem_key, auto_mem_destructor);
diff -cpr HEAD/src/interfaces/ecpg/ecpglib/misc.c remove-dllmain/src/interfaces/ecpg/ecpglib/misc.c
*** HEAD/src/interfaces/ecpg/ecpglib/misc.c	Wed Oct  3 11:07:27 2007
--- remove-dllmain/src/interfaces/ecpg/ecpglib/misc.c	Wed Oct  3 11:15:56 2007
*************** static struct sqlca_t sqlca_init =
*** 57,65 ****
  
  #ifdef ENABLE_THREAD_SAFETY
  static pthread_key_t sqlca_key;
- #ifndef WIN32
  static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT;
- #endif
  #else
  static struct sqlca_t sqlca =
  {
--- 57,63 ----
*************** static struct sqlca_t sqlca =
*** 90,97 ****
  #endif
  
  #ifdef ENABLE_THREAD_SAFETY
! NON_EXEC_STATIC pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
! NON_EXEC_STATIC pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER;
  #endif
  static int	simple_debug = 0;
  static FILE *debugstream = NULL;
--- 88,95 ----
  #endif
  
  #ifdef ENABLE_THREAD_SAFETY
! static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
! static pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER;
  #endif
  static int	simple_debug = 0;
  static FILE *debugstream = NULL;
*************** ecpg_sqlca_key_destructor(void *arg)
*** 125,131 ****
  	free(arg);				/* sqlca structure allocated in ECPGget_sqlca */
  }
  
! NON_EXEC_STATIC void
  ecpg_sqlca_key_init(void)
  {
  	pthread_key_create(&sqlca_key, ecpg_sqlca_key_destructor);
--- 123,129 ----
  	free(arg);				/* sqlca structure allocated in ECPGget_sqlca */
  }
  
! static void
  ecpg_sqlca_key_init(void)
  {
  	pthread_key_create(&sqlca_key, ecpg_sqlca_key_destructor);
*************** ECPGis_noind_null(enum ECPGttype type, v
*** 415,438 ****
  }
  
  #ifdef WIN32
  
! /*
!  * Initialize mutexes and call init-once functions on loading.
!  */
! 
! BOOL WINAPI
! DllMain(HANDLE module, DWORD reason, LPVOID reserved)
! {
! 	if (reason == DLL_PROCESS_ATTACH)
! 	{
! 		connections_mutex = CreateMutex(NULL, FALSE, NULL);
! 		debug_mutex = CreateMutex(NULL, FALSE, NULL);
! 		debug_init_mutex = CreateMutex(NULL, FALSE, NULL);
! 		auto_mem_key_init();
! 		ecpg_actual_connection_init();
! 		ecpg_sqlca_key_init();
! 		descriptor_key_init();
  	}
- 	return TRUE;
  }
! #endif
--- 413,449 ----
  }
  
  #ifdef WIN32
+ #ifdef ENABLE_THREAD_SAFETY
  
! void
! win32_pthread_mutex(volatile pthread_mutex_t *mutex)
! {
! 	if (mutex->handle == NULL)
! 	{
! 		while (InterlockedExchange((LONG *)&mutex->initlock, 1) == 1)
! 			Sleep(0);
! 		if (mutex->handle == NULL)
! 			mutex->handle = CreateMutex(NULL, FALSE, NULL);
! 		InterlockedExchange((LONG *)&mutex->initlock, 0);
  	}
  }
! 
! static pthread_mutex_t	win32_pthread_once_lock = PTHREAD_MUTEX_INITIALIZER;
! 
! void
! win32_pthread_once(volatile pthread_once_t *once, void (*fn)(void))
! {
! 	if (!*once)
! 	{
! 		pthread_mutex_lock(&win32_pthread_once_lock);
! 		if (!*once)
! 		{
! 			*once = true;
! 			fn();
! 		}
! 		pthread_mutex_unlock(&win32_pthread_once_lock);
! 	}
! }
! 
! #endif	/* ENABLE_THREAD_SAFETY */
! #endif	/* WIN32 */
diff -cpr HEAD/src/interfaces/ecpg/include/ecpg-pthread-win32.h remove-dllmain/src/interfaces/ecpg/include/ecpg-pthread-win32.h
*** HEAD/src/interfaces/ecpg/include/ecpg-pthread-win32.h	Wed Oct  3 11:07:27 2007
--- remove-dllmain/src/interfaces/ecpg/include/ecpg-pthread-win32.h	Wed Oct  3 11:15:56 2007
***************
*** 10,31 ****
  #ifndef WIN32
  
  #include <pthread.h>
- #define NON_EXEC_STATIC		static
  
  #else
  
! #define NON_EXEC_STATIC
  
- typedef HANDLE		pthread_mutex_t;
  typedef DWORD		pthread_key_t;
  
! #define PTHREAD_MUTEX_INITIALIZER	INVALID_HANDLE_VALUE
  
  #define pthread_mutex_lock(mutex) \
! 	WaitForSingleObject(*(mutex), INFINITE);
  
  #define pthread_mutex_unlock(mutex) \
! 	ReleaseMutex(*(mutex))
  
  #define pthread_getspecific(key) \
  	TlsGetValue((key))
--- 10,42 ----
  #ifndef WIN32
  
  #include <pthread.h>
  
  #else
  
! typedef struct pthread_mutex_t
! {
! 	HANDLE		handle;
! 	LONG		initlock;
! } pthread_mutex_t;
  
  typedef DWORD		pthread_key_t;
+ typedef bool		pthread_once_t;
  
! #define PTHREAD_MUTEX_INITIALIZER	{ NULL, 0 }
! #define PTHREAD_ONCE_INIT			false
! 
! void win32_pthread_mutex(volatile pthread_mutex_t *mutex);
! void win32_pthread_once(volatile pthread_once_t *once, void (*fn)(void));
  
  #define pthread_mutex_lock(mutex) \
! 	do { \
! 		if ((mutex)->handle == NULL) \
! 			win32_pthread_mutex((mutex)); \
! 		WaitForSingleObject((mutex)->handle, INFINITE); \
! 	} while(0)
  
  #define pthread_mutex_unlock(mutex) \
! 	ReleaseMutex((mutex)->handle)
  
  #define pthread_getspecific(key) \
  	TlsGetValue((key))
*************** typedef DWORD		pthread_key_t;
*** 37,54 ****
  #define pthread_key_create(key, destructor) \
  	do { *(key) = TlsAlloc(); ((void)(destructor)); } while(0)
  
! /* init-once functions are always called when libecpg is loaded */
! #define pthread_once(key, fn) \
! 	((void)0)
! 
! extern pthread_mutex_t	connections_mutex;
! extern pthread_mutex_t	debug_mutex;
! extern pthread_mutex_t	debug_init_mutex;
! extern void auto_mem_key_init(void);
! extern void ecpg_actual_connection_init(void);
! extern void ecpg_sqlca_key_init(void);
! extern void descriptor_key_init(void);
! extern BOOL WINAPI DllMain(HANDLE module, DWORD reason, LPVOID reserved);
  
  #endif	/* WIN32 */
  
--- 48,58 ----
  #define pthread_key_create(key, destructor) \
  	do { *(key) = TlsAlloc(); ((void)(destructor)); } while(0)
  
! #define pthread_once(once, fn) \
! 	do { \
! 		if (!*(once)) \
! 			win32_pthread_once((once), (fn)); \
! 	} while(0)
  
  #endif	/* WIN32 */
  
