From 028f9adaeaf453e96aa5590bbc1a0fe651279be6 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 26 Jul 2005 12:35:20 +0000 Subject: [PATCH] Portability fixes for IRIX, Solaris, HP-UX, AIX, OSF/1. --- lib/ChangeLog | 5 +++++ lib/lock.c | 37 +++++++++++++++++++++++++++++++++++++ lib/lock.h | 19 ++++++++++++++++--- m4/ChangeLog | 4 ++++ m4/lock.m4 | 47 +++++++++++++++++++++++++++++++++++------------ 5 files changed, 97 insertions(+), 15 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index ac752517d..b4d10a60d 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,8 @@ +2005-07-26 Bruno Haible + + * lock.h: Update from GNU gettext. + * lock.c: Update from GNU gettext. + 2005-07-18 Bruno Haible * lock.h (gl_once_t): New type. diff --git a/lib/lock.c b/lib/lock.c index 5c14fa3a3..a860459d1 100644 --- a/lib/lock.c +++ b/lib/lock.c @@ -32,6 +32,43 @@ /* Use the POSIX threads library. */ +# if PTHREAD_IN_USE_DETECTION_HARD + +/* The function to be executed by a dummy thread. */ +static void * +dummy_thread_func (void *arg) +{ + return arg; +} + +int +glthread_in_use (void) +{ + static int tested; + static int result; /* 1: linked with -lpthread, 0: only with libc */ + + if (!tested) + { + pthread_t thread; + + if (pthread_create (&thread, NULL, dummy_thread_func, NULL) != 0) + /* Thread creation failed. */ + result = 0; + else + { + /* Thread creation works. */ + void *retval; + if (pthread_join (thread, &retval) != 0) + abort (); + result = 1; + } + tested = 1; + } + return result; +} + +# endif + /* -------------------------- gl_lock_t datatype -------------------------- */ /* ------------------------- gl_rwlock_t datatype ------------------------- */ diff --git a/lib/lock.h b/lib/lock.h index cdddf6cd4..7d2eae0d0 100644 --- a/lib/lock.h +++ b/lib/lock.h @@ -68,6 +68,15 @@ # include # include +# if PTHREAD_IN_USE_DETECTION_HARD + +/* The pthread_in_use() detection needs to be done at runtime. */ +# define pthread_in_use() \ + glthread_in_use () +extern int glthread_in_use (void); + +# endif + # if USE_POSIX_THREADS_WEAK /* Use weak references to the POSIX threads library. */ @@ -109,12 +118,16 @@ # pragma weak pthread_self # endif -# pragma weak pthread_cancel -# define pthread_in_use() (pthread_cancel != NULL) +# if !PTHREAD_IN_USE_DETECTION_HARD +# pragma weak pthread_cancel +# define pthread_in_use() (pthread_cancel != NULL) +# endif # else -# define pthread_in_use() 1 +# if !PTHREAD_IN_USE_DETECTION_HARD +# define pthread_in_use() 1 +# endif # endif diff --git a/m4/ChangeLog b/m4/ChangeLog index a55c95630..b4f58da23 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2005-07-26 Bruno Haible + + * lock.m4: Update from GNU gettext. + 2005-07-25 Paul Eggert * regex.m4 (gl_INCLUDED_REGEX): Use AC_RUN_ELSE instead of the diff --git a/m4/lock.m4 b/m4/lock.m4 index be6a7176d..a9415ddbc 100644 --- a/m4/lock.m4 +++ b/m4/lock.m4 @@ -16,9 +16,17 @@ dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for dnl programs that really need multithread functionality. The difference dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak dnl symbols, typically LIBTHREAD="" whereas LIBMULTITHREAD="-lpthread". +dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for +dnl multithread-safe programs. AC_DEFUN([gl_LOCK], [ + dnl Ordering constraints: This macro modifies CPPFLAGS in a way that + dnl influences the result of the autoconf tests that test for *_unlocked + dnl declarations, on AIX 5 at least. Therefore it must come early. + AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl + AC_BEFORE([$0], [gl_ARGP])dnl + AC_REQUIRE([AC_CANONICAL_HOST]) AC_REQUIRE([AC_GNU_SOURCE]) dnl needed for pthread_rwlock_t on glibc systems dnl Check for multithreading. @@ -51,19 +59,22 @@ AC_HELP_STRING([--disable-threads], [build without multithread safety]), # -lgthreads case "$host_os" in osf*) - # On OSF/1, the compiler needs the flag -pthread so that it groks - # . For the linker, it is equivalent to -lpthread. - if test -n "$GCC"; then - # gcc-2.95 doesn't understand -pthread, only -D_REENTRANT. - CPPFLAGS="$CPPFLAGS -D_REENTRANT" - else - CPPFLAGS="$CPPFLAGS -pthread" - fi + # On OSF/1, the compiler needs the flag -D_REENTRANT so that it + # groks . cc also understands the flag -pthread, but + # we don't use it because 1. gcc-2.95 doesn't understand -pthread, + # 2. putting a flag into CPPFLAGS that has an effect on the linker + # causes the AC_TRY_LINK test below to succeed unexpectedly, + # leading to wrong values of LIBTHREAD and LTLIBTHREAD. + CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;; esac gl_have_pthread= + # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist + # in libc. IRIX 6.5 has the first one in both libc and libpthread, but + # the second one only in libpthread, and lock.c needs it. AC_TRY_LINK([#include ], - [pthread_mutex_lock((pthread_mutex_t*)0);], + [pthread_mutex_lock((pthread_mutex_t*)0); + pthread_mutexattr_init((pthread_mutexattr_t*)0);], [gl_have_pthread=yes]) # Test for libpthread by looking for pthread_kill. (Not pthread_self, # since it is defined as a macro on OSF/1.) @@ -71,7 +82,17 @@ AC_HELP_STRING([--disable-threads], [build without multithread safety]), # The program links fine without libpthread. But it may actually # need to link with libpthread in order to create multiple threads. AC_CHECK_LIB(pthread, pthread_kill, - [LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread]) + [LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread + # On Solaris and HP-UX, most pthread functions exist also in libc. + # Therefore pthread_in_use() needs to actually try to create a + # thread: pthread_create from libc will fail, whereas + # pthread_create will actually create a thread. + case "$host_os" in + solaris* | hpux*) + AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], 1, + [Define if the pthread_in_use() detection is hard.]) + esac + ]) else # Some library is needed. Try libpthread and libc_r. AC_CHECK_LIB(pthread, pthread_kill, @@ -115,7 +136,8 @@ int x = (int)PTHREAD_MUTEX_RECURSIVE; [AC_DEFINE([HAVE_PTHREAD_MUTEX_RECURSIVE], 1, [Define if the defines PTHREAD_MUTEX_RECURSIVE.])]) # Some systems optimize for single-threaded programs by default, and - # need special flags to disable these optimizations. + # need special flags to disable these optimizations. For example, the + # definition of 'errno' in . case "$host_os" in aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;; solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;; @@ -235,7 +257,8 @@ dnl dnl Solaris 7,8,9 posix -lpthread Y Sol 7,8: 0.0; Sol 9: OK dnl solaris -lthread Y Sol 7,8: 0.0; Sol 9: OK dnl -dnl HP-UX 11 posix -lpthread Y OK +dnl HP-UX 11 posix -lpthread N (cc) OK +dnl Y (gcc) dnl dnl IRIX 6.5 posix -lpthread Y 0.5 dnl -- 2.11.0