]> granicus.if.org Git - postgresql/commitdiff
Add thread.c for libpq threading, and hook it into libpq/configure.
authorBruce Momjian <bruce@momjian.us>
Sat, 14 Jun 2003 14:35:42 +0000 (14:35 +0000)
committerBruce Momjian <bruce@momjian.us>
Sat, 14 Jun 2003 14:35:42 +0000 (14:35 +0000)
configure
configure.in
src/Makefile.global.in
src/include/pg_config.h.in
src/include/port.h
src/interfaces/libpq/Makefile
src/port/Makefile
src/port/threads.c [new file with mode: 0644]

index 3dd2f4dccc24e1951f9a3143dd273d0998a2ab6b..55530ade9705f9a2034a1bc7f4bb2b1febcf2756 100755 (executable)
--- a/configure
+++ b/configure
@@ -2824,7 +2824,11 @@ if test "${with_threads+set}" = set; then
 
   case $withval in
     yes)
-      :
+
+cat >>confdefs.h <<\_ACEOF
+#define USE_THREADS 1
+_ACEOF
+
       ;;
     no)
       :
@@ -2841,6 +2845,7 @@ else
 
 fi;
 
+
 echo "$as_me:$LINENO: result: $with_threads" >&5
 echo "${ECHO_T}$with_threads" >&6
 
@@ -3899,9 +3904,12 @@ case $host_os in
                        # these require no special flags or libraries
                        ;;
                freebsd2*|freebsd3*|freebsd4*) THREAD_CFLAGS="-pthread" ;;
-               freebsd*) THREAD_LIBS="-lc_r" ;;
-               linux*) THREAD_LIBS="-lpthread"
-                       THREAD_CFLAGS="-D_REENTRANT" ;;
+               freebsd*)
+                       THREAD_LIBS="-lc_r"
+                       ;;
+               linux*) THREAD_CFLAGS="-D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS"
+                       THREAD_LIBS="-lpthread"
+                       ;;
                *)
                        # other operating systems might fail because they have pthread.h but need
                        # special libs we don't know about yet.
@@ -3918,7 +3926,7 @@ so it can be added to the next release.  Report any compile or link flags,
 or libraries required for threading support.
 " >&2;}
    { (exit 1); exit 1; }; }
-       esac
+esac
 fi
 
 
@@ -12818,6 +12826,97 @@ _ACEOF
 fi
 
 
+#
+# Check for re-entrant versions of certain functions
+#
+# Include special flags if required
+#
+_CFLAGS="$CFLAGS"
+_LIB="$LIBS"
+CFLAGS="$CFLAGS $TREAD_CFLAGS"
+LIBS="$LIBS $THREAD_LIBS"
+
+
+
+for ac_func in strerror_r getpwuid_r gethostbyname_r
+do
+as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
+echo "$as_me:$LINENO: checking for $ac_func" >&5
+echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6
+if eval "test \"\${$as_ac_var+set}\" = set"; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+#line $LINENO "configure"
+#include "confdefs.h"
+/* System header to define __stub macros and hopefully few prototypes,
+    which can conflict with char $ac_func (); below.  */
+#include <assert.h>
+/* Override any gcc2 internal prototype to avoid an error.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+/* We use char because int might match the return type of a gcc2
+   builtin and then its argument prototype would still apply.  */
+char $ac_func ();
+char (*f) ();
+
+#ifdef F77_DUMMY_MAIN
+#  ifdef __cplusplus
+     extern "C"
+#  endif
+   int F77_DUMMY_MAIN() { return 1; }
+#endif
+int
+main ()
+{
+/* The GNU C library defines this for functions which it implements
+    to always fail with ENOSYS.  Some functions are actually named
+    something starting with __ and the normal name is an alias.  */
+#if defined (__stub_$ac_func) || defined (__stub___$ac_func)
+choke me
+#else
+f = $ac_func;
+#endif
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+         { ac_try='test -s conftest$ac_exeext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  eval "$as_ac_var=yes"
+else
+  echo "$as_me: failed program was:" >&5
+cat conftest.$ac_ext >&5
+eval "$as_ac_var=no"
+fi
+rm -f conftest.$ac_objext conftest$ac_exeext conftest.$ac_ext
+fi
+echo "$as_me:$LINENO: result: `eval echo '${'$as_ac_var'}'`" >&5
+echo "${ECHO_T}`eval echo '${'$as_ac_var'}'`" >&6
+if test `eval echo '${'$as_ac_var'}'` = yes; then
+  cat >>confdefs.h <<_ACEOF
+#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1
+_ACEOF
+
+fi
+done
+
+CFLAGS="$_CFLAGS"
+LIB="$_LIBS"
+
+
 
 # This test makes sure that run tests work at all.  Sometimes a shared
 # library is found by the linker, but the runtime linker can't find it.
@@ -17603,8 +17702,8 @@ s,@with_pam@,$with_pam,;t t
 s,@with_rendezvous@,$with_rendezvous,;t t
 s,@with_openssl@,$with_openssl,;t t
 s,@ELF_SYS@,$ELF_SYS,;t t
-s,@THREAD_LIBS@,$THREAD_LIBS,;t t
 s,@THREAD_CFLAGS@,$THREAD_CFLAGS,;t t
+s,@THREAD_LIBS@,$THREAD_LIBS,;t t
 s,@AWK@,$AWK,;t t
 s,@FLEX@,$FLEX,;t t
 s,@FLEXFLAGS@,$FLEXFLAGS,;t t
index f323dc767fcd63c26d45925c0b571bc8ed201c14..b8c533d05dfd0a272ee7508cabb50a45f934cec5 100644 (file)
@@ -1,5 +1,5 @@
 dnl Process this file with autoconf to produce a configure script.
-dnl $Header: /cvsroot/pgsql/configure.in,v 1.261 2003/06/13 23:10:07 momjian Exp $
+dnl $Header: /cvsroot/pgsql/configure.in,v 1.262 2003/06/14 14:35:42 momjian Exp $
 dnl
 dnl Developers, please strive to achieve this order:
 dnl
@@ -323,7 +323,9 @@ IFS=$ac_save_IFS
 # Enable libpq to be thread-safe
 #
 AC_MSG_CHECKING([allow threaded libpq])
-PGAC_ARG_BOOL(with, threads, no, [  --with-threads          allow libpq to be thread-safe])
+PGAC_ARG_BOOL(with, threads, no, [  --with-threads          allow libpq to be thread-safe],
+              [AC_DEFINE([USE_THREADS], 1, [Define to 1 to build libpq with threads. (--with-threads)])])
+
 AC_MSG_RESULT([$with_threads])
 AC_SUBST(with_threads)
 
@@ -559,9 +561,12 @@ case $host_os in
                        # these require no special flags or libraries
                        ;;
                freebsd2*|freebsd3*|freebsd4*) THREAD_CFLAGS="-pthread" ;;
-               freebsd*) THREAD_LIBS="-lc_r" ;;
-               linux*) THREAD_LIBS="-lpthread"
-                       THREAD_CFLAGS="-D_REENTRANT" ;;
+               freebsd*)
+                       THREAD_LIBS="-lc_r"
+                       ;;
+               linux*) THREAD_CFLAGS="-D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS"
+                       THREAD_LIBS="-lpthread"
+                       ;;
                *)
                        # other operating systems might fail because they have pthread.h but need
                        # special libs we don't know about yet.
@@ -571,10 +576,10 @@ Please report your platform threading info to the PostgreSQL mailing lists
 so it can be added to the next release.  Report any compile or link flags,
 or libraries required for threading support.
 ])
-       esac
+esac
 fi
-AC_SUBST(THREAD_LIBS)
 AC_SUBST(THREAD_CFLAGS)
+AC_SUBST(THREAD_LIBS)
 
 #
 # Assignments
@@ -983,6 +988,20 @@ AC_CHECK_FUNCS(atexit, [],
 
 AC_FUNC_FSEEKO
 
+#
+# Check for re-entrant versions of certain functions
+#
+# Include special flags if required
+#
+_CFLAGS="$CFLAGS"
+_LIB="$LIBS"
+CFLAGS="$CFLAGS $TREAD_CFLAGS"
+LIBS="$LIBS $THREAD_LIBS"
+AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r])
+CFLAGS="$_CFLAGS"
+LIB="$_LIBS"
+
+
 
 # This test makes sure that run tests work at all.  Sometimes a shared
 # library is found by the linker, but the runtime linker can't find it.
index b08830bb9f81152bcb16d69edfb8d6429daef08c..c1087b743c517c3d91a15c8c9542f87cd3ef4682 100644 (file)
@@ -1,5 +1,5 @@
 # -*-makefile-*-
-# $Header: /cvsroot/pgsql/src/Makefile.global.in,v 1.163 2003/05/27 16:36:50 momjian Exp $
+# $Header: /cvsroot/pgsql/src/Makefile.global.in,v 1.164 2003/06/14 14:35:42 momjian Exp $
 
 #------------------------------------------------------------------------------
 # All PostgreSQL makefiles include this file and use the variables it sets,
@@ -121,6 +121,7 @@ localedir := @localedir@
 #
 # Records the choice of the various --enable-xxx and --with-xxx options.
 
+with_threads   = @with_threads@
 with_java      = @with_java@
 with_perl      = @with_perl@
 with_python    = @with_python@
@@ -340,7 +341,7 @@ endif
 #
 # substitute implementations of the C library
 
-LIBOBJS = @LIBOBJS@ path.o
+LIBOBJS = @LIBOBJS@ path.o threads.o
 
 ifneq (,$(LIBOBJS))
 LIBS += -lpgport
index 60f9536dff09d15b2ddb8315c7d76afe41d7178c..6e8cac0e99a46fc7e160babf418ba4263573fe51 100644 (file)
 /* Define to 1 if you have the `getaddrinfo' function. */
 #undef HAVE_GETADDRINFO
 
+/* Define to 1 if you have the `gethostbyname_r' function. */
+#undef HAVE_GETHOSTBYNAME_R
+
 /* Define to 1 if you have the `gethostname' function. */
 #undef HAVE_GETHOSTNAME
 
 /* Define to 1 if you have the `getpeereid' function. */
 #undef HAVE_GETPEEREID
 
+/* Define to 1 if you have the `getpwuid_r' function. */
+#undef HAVE_GETPWUID_R
+
 /* Define to 1 if you have the `getrusage' function. */
 #undef HAVE_GETRUSAGE
 
 /* Define to 1 if you have the `strerror' function. */
 #undef HAVE_STRERROR
 
+/* Define to 1 if you have the `strerror_r' function. */
+#undef HAVE_STRERROR_R
+
 /* Define to 1 if cpp supports the ANSI # stringizing operator. */
 #undef HAVE_STRINGIZE
 
 /* Define to select SysV-style shared memory. */
 #undef USE_SYSV_SHARED_MEMORY
 
+/* Define to 1 to build libpq with threads. (--with-threads) */
+#undef USE_THREADS
+
 /* Define to select unnamed POSIX semaphores. */
 #undef USE_UNNAMED_POSIX_SEMAPHORES
 
index 783d41cc0ed0d58a3a9a24ad10103b5f71960640..16f733d67ca75db05f0d1f6e40aeec2934b48c4d 100644 (file)
@@ -6,11 +6,15 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: port.h,v 1.6 2003/06/12 08:15:29 momjian Exp $
+ * $Id: port.h,v 1.7 2003/06/14 14:35:42 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
 
+/* for thread.c */
+#include <pwd.h>
+#include <netdb.h>
+
 /* Portable path handling for Unix/Win32 */
 bool is_absolute_path(const char *filename);
 char *first_path_separator(const char *filename);
@@ -98,3 +102,15 @@ extern long random(void);
 #ifndef HAVE_SRANDOM
 extern void srandom(unsigned int seed);
 #endif
+
+/* thread.h */
+extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
+
+extern int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer,
+                     size_t buflen, struct passwd **result);
+
+extern int pqGethostbyname(const char *name,
+                          struct hostent *resbuf,
+                          char *buf, size_t buflen,
+                          struct hostent **result,
+                          int *herrno);
index b6a5ec8f753c3472a333375d745ac411d835bbbc..2a178b8e9d2ce20ab49c98645da65d1570a8b760 100644 (file)
@@ -4,7 +4,7 @@
 #
 # Copyright (c) 1994, Regents of the University of California
 #
-# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.81 2003/06/12 17:31:50 momjian Exp $
+# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.82 2003/06/14 14:35:42 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -23,7 +23,7 @@ override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) $(THREAD_CFLAGS) -DFRONTEND -DSYSCO
 OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
       fe-protocol2.o fe-protocol3.o pqexpbuffer.o pqsignal.o fe-secure.o \
       dllist.o md5.o ip.o wchar.o encnames.o \
-      $(filter crypt.o getaddrinfo.o inet_aton.o snprintf.o strerror.o path.o, $(LIBOBJS))
+      $(filter crypt.o getaddrinfo.o inet_aton.o snprintf.o strerror.o path.o threads.o, $(LIBOBJS))
 
 
 # Add libraries that libpq depends (or might depend) on into the
@@ -46,9 +46,13 @@ backend_src = $(top_srcdir)/src/backend
 # For port modules, this only happens if configure decides the module
 # is needed (see filter hack in OBJS, above).
 
-crypt.c getaddrinfo.c inet_aton.c snprintf.c strerror.c path.c: %.c : $(top_srcdir)/src/port/%.c
+crypt.c getaddrinfo.c inet_aton.c snprintf.c strerror.c path.c threads.c: %.c : $(top_srcdir)/src/port/%.c
        rm -f $@ && $(LN_S) $< .
 
+# compile this with thread flags
+thread.o: thread.c
+       $(CC) $(CFLAGS) $(THREAD_CFLAGS) -c thread.c
+
 dllist.c: $(backend_src)/lib/dllist.c
        rm -f $@ && $(LN_S) $< .
 
index 04d9e0499404a3cf125b2ab52c6885af804b1b7b..d6de2d88907e18c1cf5becb583a26acb4faccbc6 100644 (file)
@@ -7,7 +7,7 @@
 # with broken/missing library files.
 
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/port/Makefile,v 1.3 2002/07/27 20:10:05 petere Exp $
+#    $Header: /cvsroot/pgsql/src/port/Makefile,v 1.4 2003/06/14 14:35:42 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -22,5 +22,8 @@ endif
 libpgport.a: $(LIBOBJS)
        $(AR) crs $@ $^
 
+thread.o: thread.c
+       $(CC) $(CFLAGS) $(THREAD_CFLAGS) -c thread.c
+
 clean distclean maintainer-clean:
        rm -f libpgport.a $(LIBOBJS)
diff --git a/src/port/threads.c b/src/port/threads.c
new file mode 100644 (file)
index 0000000..5012cac
--- /dev/null
@@ -0,0 +1,85 @@
+/*-------------------------------------------------------------------------
+ *
+ * threads.c
+ *
+ *               Prototypes and macros around system calls, used to help make
+ *               threaded libraries reentrant and safe to use from threaded applications.
+ *
+ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
+ *
+ * $Id: threads.c,v 1.1 2003/06/14 14:35:42 momjian Exp $
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+/*
+ * Wrapper around strerror and strerror_r to use the former if it is
+ * available and also return a more useful value (the error string).
+ */
+char *
+pqStrerror(int errnum, char *strerrbuf, size_t buflen)
+{
+#if defined(USE_THREADS) && defined(HAVE_STRERROR_R)
+       /* reentrant strerror_r is available */
+       /* some early standards had strerror_r returning char * */
+       strerror_r(errnum, strerrbuf, buflen);
+       return (strerrbuf);
+#else
+       /* no strerror_r() available, just use strerror */
+       return strerror(errnum);
+#endif
+}
+
+/*
+ * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
+ * behaviour, if it is not available.
+ */
+int
+pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
+                  size_t buflen, struct passwd ** result)
+{
+#if defined(USE_THREADS) && defined(HAVE_GETPWUID_R)
+       /*
+        * broken (well early POSIX draft) getpwuid_r() which returns 'struct
+        * passwd *'
+        */
+       *result = getpwuid_r(uid, resultbuf, buffer, buflen);
+#else
+       /* no getpwuid_r() available, just use getpwuid() */
+       *result = getpwuid(uid);
+#endif
+       return (*result == NULL) ? -1 : 0;
+}
+
+/*
+ * Wrapper around gethostbyname() or gethostbyname_r() to mimic
+ * POSIX gethostbyname_r() behaviour, if it is not available.
+ */
+int
+pqGethostbyname(const char *name,
+                               struct hostent * resbuf,
+                               char *buf, size_t buflen,
+                               struct hostent ** result,
+                               int *herrno)
+{
+#if defined(USE_THREADS) && defined(HAVE_GETHOSTBYNAME_R)
+       /*
+        * broken (well early POSIX draft) gethostbyname_r() which returns
+        * 'struct hostent *'
+        */
+       *result = gethostbyname_r(name, resbuf, buf, buflen, herrno);
+       return (*result == NULL) ? -1 : 0;
+#else
+       /* no gethostbyname_r(), just use gethostbyname() */
+       *result = gethostbyname(name);
+       if (*result != NULL)
+               return 0;
+       else
+       {
+               *herrno = h_errno;
+               return -1;
+       }
+#endif
+}