Re: win32 libpq ENABLE_THREAD_SAFETY - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: win32 libpq ENABLE_THREAD_SAFETY
Date
Msg-id 200406190442.i5J4gqc07301@candle.pha.pa.us
Whole thread Raw
In response to win32 libpq ENABLE_THREAD_SAFETY  (Andreas Pflug <pgadmin@pse-consulting.de>)
List pgsql-patches
Patch applied.  Thanks.  I updated the comments at the top of win32.mak
too to document this new option.

I also made some changes so our Win32 native port can use threads too.
I need to modify the configure tests but for now the attached applied
patch will set up the groundwork for it.  Mingw does support threads,
right?

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

Andreas Pflug wrote:
> So here's the updated ENABLE_THREAD_SAFETY patch for win32, to be
> compiled under VC5/6/7 (tested with VC6).
>
> nmake -f win32.mak [DEBUG=1] [USE_SSL=1] [ENABLE_THREAD_SAFETY=1]
>
> are supported.
>
> Regards,
> Andreas
>

> /*-------------------------------------------------------------------------
> *
> * 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

> Index: interfaces/libpq/fe-connect.c
> ===================================================================
> RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v
> retrieving revision 1.274
> diff -u -r1.274 fe-connect.c
> --- interfaces/libpq/fe-connect.c    10 Jun 2004 22:26:24 -0000    1.274
> +++ interfaces/libpq/fe-connect.c    13 Jun 2004 19:13:58 -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;
> @@ -3183,11 +3185,19 @@
>  }
>
>  static pgthreadlock_t default_threadlock;
> +
>  static void
>  default_threadlock(int acquire)
>  {
>  #ifdef ENABLE_THREAD_SAFETY
> +#ifndef WIN32
>      static pthread_mutex_t singlethread_lock = PTHREAD_MUTEX_INITIALIZER;
> +#else
> +    static pthread_mutex_t singlethread_lock;
> +        static long mutex_initialized = 0;
> +        if (!InterlockedExchange(&mutex_initialized, 1L))
> +                pthread_mutex_init(&singlethread_lock, NULL);
> +#endif
>      if (acquire)
>          pthread_mutex_lock(&singlethread_lock);
>      else
> Index: interfaces/libpq/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
> --- interfaces/libpq/fe-secure.c    3 Jun 2004 00:13:19 -0000    1.41
> +++ interfaces/libpq/fe-secure.c    13 Jun 2004 19:14:00 -0000
> @@ -864,8 +864,14 @@
>  init_ssl_system(PGconn *conn)
>  {
>  #ifdef ENABLE_THREAD_SAFETY
> -static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
> -
> +#ifndef WIN32
> +        static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
> +#else
> +        static pthread_mutex_t init_mutex;
> +        static long mutex_initialized = 0L;
> +        if (!InterlockedExchange(&mutex_initialized, 1L))
> +                pthread_mutex_init(&init_mutex, NULL);
> +#endif
>      pthread_mutex_lock(&init_mutex);
>
>      if (pq_initssllib && pq_lockarray == NULL) {
> @@ -1171,6 +1177,7 @@
>
>
>  #ifdef ENABLE_THREAD_SAFETY
> +#ifndef WIN32
>  /*
>   *    Check SIGPIPE handler and perhaps install our own.
>   */
> @@ -1210,6 +1217,7 @@
>      if (!PQinSend())
>          exit(128 + SIGPIPE);    /* typical return value for SIG_DFL */
>  }
> +#endif
>  #endif
>
>  /*
> Index: interfaces/libpq/win32.mak
> ===================================================================
> RCS file: /projects/cvsroot/pgsql-server/src/interfaces/libpq/win32.mak,v
> retrieving revision 1.24
> diff -u -r1.24 win32.mak
> --- interfaces/libpq/win32.mak    4 Jun 2004 13:30:04 -0000    1.24
> +++ interfaces/libpq/win32.mak    13 Jun 2004 19:14:01 -0000
> @@ -74,21 +74,25 @@
>      -@erase "$(OUTDIR)\$(OUTFILENAME)dll.lib"
>      -@erase "$(INTDIR)\wchar.obj"
>      -@erase "$(INTDIR)\encnames.obj"
> +    -@erase "$(INTDIR)\pthread-win32.obj"
>
>
>
> -config: ..\..\include\pg_config.h pg_config_paths.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"
>
> @@ -127,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"

>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
>       subscribe-nomail command to majordomo@postgresql.org so that your
>       message can get through to the mailing list cleanly

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/interfaces/libpq/Makefile
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/Makefile,v
retrieving revision 1.109
diff -c -c -r1.109 Makefile
*** src/interfaces/libpq/Makefile    24 May 2004 01:01:38 -0000    1.109
--- src/interfaces/libpq/Makefile    19 Jun 2004 04:40:39 -0000
***************
*** 25,32 ****
--- 25,38 ----
      fe-protocol2.o fe-protocol3.o pqexpbuffer.o pqsignal.o fe-secure.o \
      dllist.o md5.o ip.o wchar.o encnames.o noblock.o pgstrcasecmp.o thread.o \
      $(filter crypt.o getaddrinfo.o inet_aton.o open.o snprintf.o strerror.o, $(LIBOBJS))
+
  ifeq ($(PORTNAME), win32)
  OBJS+=win32.o
+ ifeq ($(enable_thread_safety), yes)
+ # This doesn't work yet because configure test fails.  2004-06-19
+ OBJS+=pthread-win32.o
+ PTHREAD_H_WIN32=yes
+ endif
  endif


***************
*** 39,45 ****
  endif


! all: all-lib

  # Shared library stuff
  include $(top_srcdir)/src/Makefile.shlib
--- 45,51 ----
  endif


! all: $(PTHREAD_H_WIN32) all-lib

  # Shared library stuff
  include $(top_srcdir)/src/Makefile.shlib
***************
*** 66,71 ****
--- 72,82 ----
      rm -f $@ && $(LN_S) $< .


+ ifeq ($(PTHREAD_H_WIN32))
+ pthread.h : % : $(top_srcdir)/src/interfaces/libpq/pthread.h.win
+     rm -f $@ && $(LN_S) $< .
+ endif
+
  install: all installdirs install-lib
      $(INSTALL_DATA) $(srcdir)/libpq-fe.h $(DESTDIR)$(includedir)
      $(INSTALL_DATA) $(srcdir)/libpq-int.h $(DESTDIR)$(includedir_internal)
***************
*** 79,82 ****
      rm -f $(DESTDIR)$(includedir)/libpq-fe.h $(DESTDIR)$(includedir_internal)/libpq-int.h
$(DESTDIR)$(includedir_internal)/pqexpbuffer.h

  clean distclean maintainer-clean: clean-lib
!     rm -f $(OBJS) crypt.c getaddrinfo.c inet_aton.c noblock.c pgstrcasecmp.c snprintf.c strerror.c open.c thread.c
dllist.cmd5.c ip.c encnames.c wchar.c 
--- 90,93 ----
      rm -f $(DESTDIR)$(includedir)/libpq-fe.h $(DESTDIR)$(includedir_internal)/libpq-int.h
$(DESTDIR)$(includedir_internal)/pqexpbuffer.h

  clean distclean maintainer-clean: clean-lib
!     rm -f $(OBJS) crypt.c getaddrinfo.c inet_aton.c noblock.c pgstrcasecmp.c snprintf.c strerror.c open.c thread.c
dllist.cmd5.c ip.c encnames.c wchar.c pthread.h 

pgsql-patches by date:

Previous
From: Christopher Kings-Lynne
Date:
Subject: Re: Tablespace patch review
Next
From: Tom Lane
Date:
Subject: Re: Tablespace patch review