Re: Compiling libpq with VisualC

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Andreas Pflug <pgadmin(at)pse-consulting(dot)de>
Cc: PostgreSQL Patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: Compiling libpq with VisualC
Date: 2004-06-12 04:49:05
Message-ID: 200406120449.i5C4n5q18636@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-hackers-win32 pgsql-patches


This patch has to be redone to make it thread-safe.

---------------------------------------------------------------------------

Andreas Pflug wrote:
> The appended patch implements ENABLE_THREAD_SAFETY for win32 compiled
> with Visual C. Two additional files will supply the needed pthread stuff.
>
> There's no SIGPIPE for native win32, so there are some #IFNDEF WIN32 to
> skip installing a signal handler for this. I'm not sure if this is going
> to show conflicts for CYGWIN or other unix-like builds on win32
> platforms, i.e. if WIN32 is defined in such cases or not; please check.
>
> Note: the previously posted win32.mak patch is superseded by this.
>
> Regards,
> Andreas

> Index: win32.mak
> ===================================================================
> RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/win32.mak,v
> retrieving revision 1.23
> diff -u -r1.23 win32.mak
> --- win32.mak 3 Jun 2004 00:11:13 -0000 1.23
> +++ win32.mak 4 Jun 2004 13:26:39 -0000
> @@ -74,19 +74,25 @@
> -(at)erase "$(OUTDIR)\$(OUTFILENAME)dll.lib"
> -(at)erase "$(INTDIR)\wchar.obj"
> -(at)erase "$(INTDIR)\encnames.obj"
> + -(at)erase "$(INTDIR)\pthread-win32.obj"
>
>
>
> -config: ..\..\include\pg_config.h
> +config: ..\..\include\pg_config.h pthread.h pg_config_paths.h
>
> ..\..\include\pg_config.h: ..\..\include\pg_config.h.win32
> copy ..\..\include\pg_config.h.win32 ..\..\include\pg_config.h
>
> +pthread.h: pthread.h.win32
> + copy pthread.h.win32 pthread.h
> +
> +pg_config_paths.h: win32.mak
> + echo #define SYSCONFDIR "" >pg_config_paths.h
>
> "$(OUTDIR)" :
> if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
>
> -CPP_PROJ=/nologo /W3 /GX $(OPT) /I "..\..\include" /D "FRONTEND" $(DEBUGDEF) /D\
> +CPP_PROJ=/nologo /W3 /GX $(OPT) /I "..\..\include" /I. /D "FRONTEND" $(DEBUGDEF) /D\
> "WIN32" /D "_WINDOWS" /Fp"$(INTDIR)\libpq.pch" /YX\
> /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\" /FD /c /D "HAVE_VSNPRINTF" /D "HAVE_STRDUP"
>
> @@ -95,6 +101,10 @@
> SSL_LIBS=ssleay32.lib libeay32.lib gdi32.lib
> !ENDIF
>
> +!IFDEF ENABLE_THREAD_SAFETY
> +CPP_PROJ=$(CPP_PROJ) /D ENABLE_THREAD_SAFETY
> +!ENDIF
> +
> CPP_SBRS=.
>
> LIB32=link.exe -lib
> @@ -121,7 +131,8 @@
> "$(INTDIR)\fe-secure.obj" \
> "$(INTDIR)\pqexpbuffer.obj" \
> "$(INTDIR)\wchar.obj" \
> - "$(INTDIR)\encnames.obj"
> + "$(INTDIR)\encnames.obj" \
> + "$(INTDIR)\pthread-win32.obj"
>
>
> RSC_PROJ=/l 0x409 /fo"$(INTDIR)\libpq.res"
> Index: fe-connect.c
> ===================================================================
> RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v
> retrieving revision 1.272
> diff -u -r1.272 fe-connect.c
> --- fe-connect.c 3 Jun 2004 00:07:38 -0000 1.272
> +++ fe-connect.c 4 Jun 2004 13:26:43 -0000
> @@ -882,11 +882,13 @@
> const char *node = NULL;
> int ret;
> #ifdef ENABLE_THREAD_SAFETY
> +#ifndef WIN32
> static pthread_once_t check_sigpipe_once = PTHREAD_ONCE_INIT;
>
> /* Check only on first connection request */
> pthread_once(&check_sigpipe_once, check_sigpipe_handler);
> #endif
> +#endif
>
> if (!conn)
> return 0;
> @@ -3187,7 +3189,13 @@
> default_threadlock(int acquire)
> {
> #ifdef ENABLE_THREAD_SAFETY
> - static pthread_mutex_t singlethread_lock = PTHREAD_MUTEX_INITIALIZER;
> + static pthread_mutex_t singlethread_lock;
> + static int mutex_initialized = 0;
> + if (!mutex_initialized)
> + {
> + mutex_initialized = 1;
> + pthread_mutex_init(&singlethread_lock, NULL);
> + }
> if (acquire)
> pthread_mutex_lock(&singlethread_lock);
> else
> Index: fe-secure.c
> ===================================================================
> RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-secure.c,v
> retrieving revision 1.41
> diff -u -r1.41 fe-secure.c
> --- fe-secure.c 3 Jun 2004 00:13:19 -0000 1.41
> +++ fe-secure.c 4 Jun 2004 13:26:45 -0000
> @@ -864,8 +864,13 @@
> init_ssl_system(PGconn *conn)
> {
> #ifdef ENABLE_THREAD_SAFETY
> -static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
> -
> + static pthread_mutex_t init_mutex;
> + static int mutex_initialized = 0;
> + if (!mutex_initialized)
> + {
> + mutex_initialized = 1;
> + pthread_mutex_init(&init_mutex, NULL);
> + }
> pthread_mutex_lock(&init_mutex);
>
> if (pq_initssllib && pq_lockarray == NULL) {
> @@ -1171,6 +1176,7 @@
>
>
> #ifdef ENABLE_THREAD_SAFETY
> +#ifndef WIN32
> /*
> * Check SIGPIPE handler and perhaps install our own.
> */
> @@ -1210,6 +1216,7 @@
> if (!PQinSend())
> exit(128 + SIGPIPE); /* typical return value for SIG_DFL */
> }
> +#endif
> #endif
>
> /*

> /*-------------------------------------------------------------------------
> *
> * pthread-win32.c
> * partial pthread implementation for win32
> *
> * Copyright (c) 2004, PostgreSQL Global Development Group
> * IDENTIFICATION
> * $PostgreSQL: $
> *
> *-------------------------------------------------------------------------
> */
>
>
> #include "windows.h"
> #include "pthread.h"
>
> HANDLE pthread_self()
> {
> return GetCurrentThread();
> }
>
> void pthread_setspecific(pthread_key_t key, void *val)
> {
> }
>
> void *pthread_getspecific(pthread_key_t key)
> {
> return NULL;
> }
>
> void pthread_mutex_init(pthread_mutex_t *mp, void *attr)
> {
> *mp = CreateMutex(0, 0, 0);
> }
>
> void pthread_mutex_lock(pthread_mutex_t *mp)
> {
> WaitForSingleObject(*mp, INFINITE);
> }
>
> void pthread_mutex_unlock(pthread_mutex_t *mp)
> {
> ReleaseMutex(*mp);
> }

> #ifndef __PTHREAD_H
> #define __PTHREAD_H
>
> typedef ULONG pthread_key_t;
> typedef HANDLE pthread_mutex_t;
> typedef int pthread_once_t;
>
> HANDLE pthread_self();
>
> void pthread_setspecific(pthread_key_t, void*);
> void* pthread_getspecific(pthread_key_t);
>
> void pthread_mutex_init(pthread_mutex_t *, void *attr);
> void pthread_mutex_lock(pthread_mutex_t*); // blocking
> void pthread_mutex_unlock(pthread_mutex_t*);
>
> #endif

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2004-06-12 04:50:50 Re: Compiling libpq with VisualC
Previous Message Bruce Momjian 2004-06-12 04:39:18 Release 7.4.3 branded

Browse pgsql-hackers-win32 by date

  From Date Subject
Next Message Bruce Momjian 2004-06-12 04:50:50 Re: Compiling libpq with VisualC
Previous Message pgsql 2004-06-12 02:24:44 Re: [pgsql-hackers-win32] Tablespaces

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2004-06-12 04:50:50 Re: Compiling libpq with VisualC
Previous Message Bruce Momjian 2004-06-12 00:25:20 Re: Compiling libpq with VisualC