* m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Check for a nanosleep that
[gnulib.git] / m4 / lock.m4
index be6a717..0224f2f 100644 (file)
@@ -1,5 +1,5 @@
-# lock.m4 serial 1 (gettext-0.15)
-dnl Copyright (C) 2005 Free Software Foundation, Inc.
+# lock.m4 serial 6 (gettext-0.16)
+dnl Copyright (C) 2005-2006 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
@@ -16,16 +16,67 @@ 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],
+AC_DEFUN([gl_LOCK_EARLY],
+[
+  AC_REQUIRE([gl_LOCK_EARLY_BODY])
+])
+
+dnl The guts of gl_LOCK_EARLY. Needs to be expanded only once.
+
+AC_DEFUN([gl_LOCK_EARLY_BODY],
 [
+  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.
   AC_ARG_ENABLE(threads,
 AC_HELP_STRING([--enable-threads={posix|solaris|pth|win32}], [specify multithreading API])
 AC_HELP_STRING([--disable-threads], [build without multithread safety]),
-    gl_use_threads=$enableval, gl_use_threads=yes)
+    [gl_use_threads=$enableval],
+    [case "$host_os" in
+       dnl Disable multithreading by default on OSF/1, because it interferes
+       dnl with fork()/exec(): When msgexec is linked with -lpthread, its child
+       dnl process gets an endless segmentation fault inside execvp().
+       osf*) gl_use_threads=no ;;
+       *)    gl_use_threads=yes ;;
+     esac
+    ])
+  if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
+    # For using <pthread.h>:
+    case "$host_os" in
+      osf*)
+        # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
+        # groks <pthread.h>. 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
+    # Some systems optimize for single-threaded programs by default, and
+    # need special flags to disable these optimizations. For example, the
+    # definition of 'errno' in <errno.h>.
+    case "$host_os" in
+      aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
+      solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
+    esac
+  fi
+])
+
+dnl The guts of gl_LOCK. Needs to be expanded only once.
+
+AC_DEFUN([gl_LOCK_BODY],
+[
+  AC_REQUIRE([gl_LOCK_EARLY_BODY])
   gl_threads_api=none
   LIBTHREAD=
   LTLIBTHREAD=
@@ -40,30 +91,19 @@ AC_HELP_STRING([--disable-threads], [build without multithread safety]),
     AC_MSG_RESULT([$gl_have_weak])
     if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
       # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
-      # it groks <pthread.h>.
-      gl_save_CPPFLAGS="$CPPFLAGS"
-      CPPFLAGS="$CPPFLAGS -D_REENTRANT"
+      # it groks <pthread.h>. It's added above, in gl_LOCK_EARLY_BODY.
       AC_CHECK_HEADER(pthread.h, gl_have_pthread_h=yes, gl_have_pthread_h=no)
-      CPPFLAGS="$gl_save_CPPFLAGS"
       if test "$gl_have_pthread_h" = yes; then
         # Other possible tests:
         #   -lpthreads (FSU threads, PCthreads)
         #   -lgthreads
-        case "$host_os" in
-          osf*)
-            # On OSF/1, the compiler needs the flag -pthread so that it groks
-            # <pthread.h>. 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
-            ;;
-        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.h>],
-          [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 +111,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,
@@ -111,15 +161,10 @@ AC_HELP_STRING([--disable-threads], [build without multithread safety]),
 error "No, in FreeBSD 4.0 recursive mutexes actually don't work."
 #else
 int x = (int)PTHREAD_MUTEX_RECURSIVE;
+return !x;
 #endif],
             [AC_DEFINE([HAVE_PTHREAD_MUTEX_RECURSIVE], 1,
                [Define if the <pthread.h> defines PTHREAD_MUTEX_RECURSIVE.])])
-          # Some systems optimize for single-threaded programs by default, and
-          # need special flags to disable these optimizations.
-          case "$host_os" in
-            aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
-            solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
-          esac
         fi
       fi
     fi
@@ -198,6 +243,12 @@ int x = (int)PTHREAD_MUTEX_RECURSIVE;
   AC_SUBST(LTLIBTHREAD)
   AC_SUBST(LIBMULTITHREAD)
   AC_SUBST(LTLIBMULTITHREAD)
+])
+
+AC_DEFUN([gl_LOCK],
+[
+  AC_REQUIRE([gl_LOCK_EARLY])
+  AC_REQUIRE([gl_LOCK_BODY])
   gl_PREREQ_LOCK
 ])
 
@@ -235,13 +286,14 @@ 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
 dnl AIX 4.3,5.1       posix       -lpthread       N      AIX 4: 0.5; AIX 5: OK
 dnl
-dnl OSF/1 4.0,5.1     posix       -pthread (cc)   Y      OK
+dnl OSF/1 4.0,5.1     posix       -pthread (cc)   N      OK
 dnl                               -lpthread (gcc) Y
 dnl
 dnl Cygwin            posix       -lpthread       Y      OK