New macro gl_DISABLE_THREADS.
[gnulib.git] / m4 / lock.m4
index 0aff3fa..d590289 100644 (file)
@@ -1,11 +1,13 @@
-# lock.m4 serial 1 (gettext-0.15)
-dnl Copyright (C) 2005 Free Software Foundation, Inc.
+# lock.m4 serial 8 (gettext-0.18)
+dnl Copyright (C) 2005-2008 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.
 
 dnl From Bruno Haible.
 
+dnl gl_LOCK
+dnl -------
 dnl Tests for a multithreading library to be used.
 dnl Defines at most one of the macros USE_POSIX_THREADS, USE_SOLARIS_THREADS,
 dnl USE_PTH_THREADS, USE_WIN32_THREADS
@@ -19,7 +21,14 @@ 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
@@ -28,12 +37,58 @@ AC_DEFUN([gl_LOCK],
   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 _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems.
+  dnl AC_USE_SYSTEM_EXTENSIONS was introduced in autoconf 2.60 and obsoletes
+  dnl AC_GNU_SOURCE.
+  m4_ifdef([AC_USE_SYSTEM_EXTENSIONS],
+    [AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])],
+    [AC_REQUIRE([AC_GNU_SOURCE])])
   dnl Check for multithreading.
+  m4_divert_text([DEFAULTS], [gl_use_threads_default=])
   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],
+    [if test -n "$gl_use_threads_default"; then
+       gl_use_threads="$gl_use_threads_default"
+     else
+       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
+         dnl child process gets an endless segmentation fault inside execvp().
+         osf*) gl_use_threads=no ;;
+         *)    gl_use_threads=yes ;;
+       esac
+     fi
+    ])
+  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=
@@ -48,26 +103,12 @@ 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 -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
         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
@@ -132,16 +173,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. 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
       fi
     fi
@@ -220,6 +255,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
 ])
 
@@ -228,6 +269,18 @@ AC_DEFUN([gl_PREREQ_LOCK], [
   AC_REQUIRE([AC_C_INLINE])
 ])
 
+
+dnl gl_DISABLE_THREADS
+dnl ------------------
+dnl Sets the gl_LOCK default so that threads are not used by default.
+dnl The user can still override it at installation time, by using the
+dnl configure option '--enable-threads'.
+
+AC_DEFUN([gl_DISABLE_THREADS], [
+  m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
+])
+
+
 dnl Survey of platforms:
 dnl
 dnl Platform          Available   Compiler    Supports   test-lock