*** connection.h.save	Mon Sep 15 17:25:03 2003
--- connection.h	Mon Nov 10 14:22:07 2003
***************
*** 93,99 ****
  #define LEAVE_CONN_CS(x)	LeaveCriticalSection(&((x)->cs))
  #define DELETE_CONN_CS(x)	DeleteCriticalSection(&((x)->cs))
  #elif defined(POSIX_MULTITHREAD_SUPPORT)
! #define INIT_CONN_CS(x)		pthread_mutex_init(&((x)->cs),0)
  #define ENTER_CONN_CS(x)	pthread_mutex_lock(&((x)->cs))
  #define LEAVE_CONN_CS(x)	pthread_mutex_unlock(&((x)->cs))
  #define DELETE_CONN_CS(x)	pthread_mutex_destroy(&((x)->cs))
--- 93,99 ----
  #define LEAVE_CONN_CS(x)	LeaveCriticalSection(&((x)->cs))
  #define DELETE_CONN_CS(x)	DeleteCriticalSection(&((x)->cs))
  #elif defined(POSIX_MULTITHREAD_SUPPORT)
! #define INIT_CONN_CS(x)		pthread_mutex_init(&((x)->cs), getMutexAttr())
  #define ENTER_CONN_CS(x)	pthread_mutex_lock(&((x)->cs))
  #define LEAVE_CONN_CS(x)	pthread_mutex_unlock(&((x)->cs))
  #define DELETE_CONN_CS(x)	pthread_mutex_destroy(&((x)->cs))
*** psqlodbc.h.save	Wed Aug 27 19:17:53 2003
--- psqlodbc.h	Mon Nov 10 13:41:28 2003
***************
*** 5,11 ****
   *
   * Comments:		See "notice.txt" for copyright and license information.
   *
!  * $Id: psqlodbc.h,v 1.78 2003/08/27 10:17:53 hinoue Exp $
   *
   */
  
--- 5,11 ----
   *
   * Comments:		See "notice.txt" for copyright and license information.
   *
!  * $Id: psqlodbc.h,v 1.79 2003/10/25 04:19:22 hinoue Exp $
   *
   */
  
***************
*** 279,295 ****
  
  #include "misc.h"
  
  #ifdef	UNICODE_SUPPORT
  UInt4	ucs2strlen(const SQLWCHAR *ucs2str);
  char	*ucs2_to_utf8(const SQLWCHAR *ucs2str, Int4 ilen, UInt4 *olen, BOOL tolower);
  UInt4	utf8_to_ucs2_lf(const char * utf8str, Int4 ilen, BOOL lfconv, SQLWCHAR *ucs2str, UInt4 buflen);
  #define	utf8_to_ucs2(utf8str, ilen, ucs2str, buflen) utf8_to_ucs2_lf(utf8str, ilen, FALSE, ucs2str, buflen)
  #endif /* UNICODE_SUPPORT */
  /*#define	_MEMORY_DEBUG_ */
  #ifdef	_MEMORY_DEBUG_
! void	   *debug_alloc(size_t);
! void	   *debug_realloc(void *, size_t);
! char	   *debug_strdup(const char *);
  void		debug_free(void *);
  void		debug_memory_check(void);
  
--- 279,300 ----
  
  #include "misc.h"
  
+ int	initialize_global_cs(void);
+ #ifdef	POSIX_MULTITHTREAD_SUPPORT
+ const pthread_mutexattr_t *getMutexAttr(void);
+ #endif /* POSIX_MULTITHTREAD_SUPPORT */
  #ifdef	UNICODE_SUPPORT
  UInt4	ucs2strlen(const SQLWCHAR *ucs2str);
  char	*ucs2_to_utf8(const SQLWCHAR *ucs2str, Int4 ilen, UInt4 *olen, BOOL tolower);
  UInt4	utf8_to_ucs2_lf(const char * utf8str, Int4 ilen, BOOL lfconv, SQLWCHAR *ucs2str, UInt4 buflen);
  #define	utf8_to_ucs2(utf8str, ilen, ucs2str, buflen) utf8_to_ucs2_lf(utf8str, ilen, FALSE, ucs2str, buflen)
  #endif /* UNICODE_SUPPORT */
+ 
  /*#define	_MEMORY_DEBUG_ */
  #ifdef	_MEMORY_DEBUG_
! void		*debug_alloc(size_t);
! void		*debug_realloc(void *, size_t);
! char		*debug_strdup(const char *);
  void		debug_free(void *);
  void		debug_memory_check(void);
  
*** psqlodbc.c.save	Tue Dec 17 00:01:34 2002
--- psqlodbc.c	Mon Nov 10 15:21:25 2003
***************
*** 25,38 ****
  
  RETCODE SQL_API SQLDummyOrdinal(void);
  
- #ifdef WIN32
- HINSTANCE NEAR s_hModule;		/* Saved module handle. */
  #if defined(WIN_MULTITHREAD_SUPPORT)
  extern	CRITICAL_SECTION	qlog_cs, mylog_cs, conns_cs;
  #elif defined(POSIX_MULTITHREAD_SUPPORT)
  extern	pthread_mutex_t 	qlog_cs, mylog_cs, conns_cs;
  #endif /* WIN_MULTITHREAD_SUPPORT */
  
  /*	This is where the Driver Manager attaches to this Driver */
  BOOL		WINAPI
  DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
--- 25,78 ----
  
  RETCODE SQL_API SQLDummyOrdinal(void);
  
  #if defined(WIN_MULTITHREAD_SUPPORT)
  extern	CRITICAL_SECTION	qlog_cs, mylog_cs, conns_cs;
  #elif defined(POSIX_MULTITHREAD_SUPPORT)
  extern	pthread_mutex_t 	qlog_cs, mylog_cs, conns_cs;
+ static	pthread_mutexattr_t	recur_attr;
+ const pthread_mutexattr_t*	getMutexAttr(void)
+ {
+ 	static	int	init = 1;
+ 
+ 	if (init)
+ 	{
+ 		if (0 != pthread_mutexattr_init(&recur_attr))
+ 			return NULL;
+ 		if (0 != pthread_mutexattr_settype(&recur_attr, PTHREAD_MUTEX_RECURSIVE_NP))
+ 			return NULL;
+ 	}
+ 	init = 0;
+ 
+ 	return	&recur_attr;
+ }
  #endif /* WIN_MULTITHREAD_SUPPORT */
  
+ int	initialize_global_cs(void)
+ {
+ 	static	int	init = 1;
+ 
+ 	if (!init)
+ 		return 0;
+ 	init = 0;
+ #ifdef	POSIX_MULTITHREAD_SUPPORT
+ 	getMutexAttr();
+ #endif /* POSIX_MULTITHREAD_SUPPORT */
+ 	INIT_QLOG_CS;
+ 	INIT_MYLOG_CS;
+ 	INIT_CONNS_CS;
+ 
+ 	return 0;
+ }
+ 
+ static void finalize_global_cs(void)
+ {
+ 	DELETE_CONNS_CS;
+ 	DELETE_QLOG_CS;
+ 	DELETE_MYLOG_CS;
+ }
+ 
+ #ifdef WIN32
+ HINSTANCE NEAR s_hModule;		/* Saved module handle. */
  /*	This is where the Driver Manager attaches to this Driver */
  BOOL		WINAPI
  DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
***************
*** 59,67 ****
  				return FALSE;
  			}
  
! 			INIT_QLOG_CS;
! 			INIT_MYLOG_CS;
! 			INIT_CONNS_CS;
  			getCommonDefaults(DBMS_NAME, ODBCINST_INI, NULL);
  			break;
  
--- 99,105 ----
  				return FALSE;
  			}
  
! 			initialize_global_cs();
  			getCommonDefaults(DBMS_NAME, ODBCINST_INI, NULL);
  			break;
  
***************
*** 69,77 ****
  			break;
  
  		case DLL_PROCESS_DETACH:
! 			DELETE_CONNS_CS;
! 			DELETE_QLOG_CS;
! 			DELETE_MYLOG_CS;
  			WSACleanup();
  			return TRUE;
  
--- 107,113 ----
  			break;
  
  		case DLL_PROCESS_DETACH:
! 			finalize_global_cs();
  			WSACleanup();
  			return TRUE;
  
***************
*** 104,109 ****
--- 140,146 ----
  __attribute__((constructor))
  init(void)
  {
+ 	initialize_global_cs();
  	getCommonDefaults(DBMS_NAME, ODBCINST_INI, NULL);
  	return TRUE;
  }
***************
*** 117,122 ****
--- 154,160 ----
  BOOL
  _init(void)
  {
+ 	initialize_global_cs();
  	getCommonDefaults(DBMS_NAME, ODBCINST_INI, NULL);
  	return TRUE;
  }
***************
*** 124,129 ****
--- 162,168 ----
  BOOL
  _fini(void)
  {
+ 	finalize_global_cs();
  	return TRUE;
  }
  #endif   /* not __GNUC__ */
