Blame gnulib/m4/threadlib.m4

Packit eba2e2
# threadlib.m4 serial 10 (gettext-0.18.2)
Packit eba2e2
dnl Copyright (C) 2005-2014 Free Software Foundation, Inc.
Packit eba2e2
dnl This file is free software; the Free Software Foundation
Packit eba2e2
dnl gives unlimited permission to copy and/or distribute it,
Packit eba2e2
dnl with or without modifications, as long as this notice is preserved.
Packit eba2e2
Packit eba2e2
dnl From Bruno Haible.
Packit eba2e2
Packit eba2e2
dnl gl_THREADLIB
Packit eba2e2
dnl ------------
Packit eba2e2
dnl Tests for a multithreading library to be used.
Packit eba2e2
dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO
Packit eba2e2
dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the
Packit eba2e2
dnl default is 'no', otherwise it is system dependent. In both cases, the user
Packit eba2e2
dnl can change the choice through the options --enable-threads=choice or
Packit eba2e2
dnl --disable-threads.
Packit eba2e2
dnl Defines at most one of the macros USE_POSIX_THREADS, USE_SOLARIS_THREADS,
Packit eba2e2
dnl USE_PTH_THREADS, USE_WINDOWS_THREADS
Packit eba2e2
dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use
Packit eba2e2
dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with
Packit eba2e2
dnl libtool).
Packit eba2e2
dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for
Packit eba2e2
dnl programs that really need multithread functionality. The difference
Packit eba2e2
dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak
Packit eba2e2
dnl symbols, typically LIBTHREAD="" whereas LIBMULTITHREAD="-lpthread".
Packit eba2e2
dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
Packit eba2e2
dnl multithread-safe programs.
Packit eba2e2
Packit eba2e2
AC_DEFUN([gl_THREADLIB_EARLY],
Packit eba2e2
[
Packit eba2e2
  AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
Packit eba2e2
])
Packit eba2e2
Packit eba2e2
dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once.
Packit eba2e2
Packit eba2e2
AC_DEFUN([gl_THREADLIB_EARLY_BODY],
Packit eba2e2
[
Packit eba2e2
  dnl Ordering constraints: This macro modifies CPPFLAGS in a way that
Packit eba2e2
  dnl influences the result of the autoconf tests that test for *_unlocked
Packit eba2e2
  dnl declarations, on AIX 5 at least. Therefore it must come early.
Packit eba2e2
  AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl
Packit eba2e2
  AC_BEFORE([$0], [gl_ARGP])dnl
Packit eba2e2
Packit eba2e2
  AC_REQUIRE([AC_CANONICAL_HOST])
Packit eba2e2
  dnl _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems.
Packit eba2e2
  dnl AC_USE_SYSTEM_EXTENSIONS was introduced in autoconf 2.60 and obsoletes
Packit eba2e2
  dnl AC_GNU_SOURCE.
Packit eba2e2
  m4_ifdef([AC_USE_SYSTEM_EXTENSIONS],
Packit eba2e2
    [AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])],
Packit eba2e2
    [AC_REQUIRE([AC_GNU_SOURCE])])
Packit eba2e2
  dnl Check for multithreading.
Packit eba2e2
  m4_ifdef([gl_THREADLIB_DEFAULT_NO],
Packit eba2e2
    [m4_divert_text([DEFAULTS], [gl_use_threads_default=no])],
Packit eba2e2
    [m4_divert_text([DEFAULTS], [gl_use_threads_default=])])
Packit eba2e2
  AC_ARG_ENABLE([threads],
Packit eba2e2
AC_HELP_STRING([--enable-threads={posix|solaris|pth|windows}], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [
Packit eba2e2
AC_HELP_STRING([--disable-threads], [build without multithread safety])]),
Packit eba2e2
    [gl_use_threads=$enableval],
Packit eba2e2
    [if test -n "$gl_use_threads_default"; then
Packit eba2e2
       gl_use_threads="$gl_use_threads_default"
Packit eba2e2
     else
Packit eba2e2
changequote(,)dnl
Packit eba2e2
       case "$host_os" in
Packit eba2e2
         dnl Disable multithreading by default on OSF/1, because it interferes
Packit eba2e2
         dnl with fork()/exec(): When msgexec is linked with -lpthread, its
Packit eba2e2
         dnl child process gets an endless segmentation fault inside execvp().
Packit eba2e2
         dnl Disable multithreading by default on Cygwin 1.5.x, because it has
Packit eba2e2
         dnl bugs that lead to endless loops or crashes. See
Packit eba2e2
         dnl <http://cygwin.com/ml/cygwin/2009-08/msg00283.html>.
Packit eba2e2
         osf*) gl_use_threads=no ;;
Packit eba2e2
         cygwin*)
Packit eba2e2
               case `uname -r` in
Packit eba2e2
                 1.[0-5].*) gl_use_threads=no ;;
Packit eba2e2
                 *)         gl_use_threads=yes ;;
Packit eba2e2
               esac
Packit eba2e2
               ;;
Packit eba2e2
         *)    gl_use_threads=yes ;;
Packit eba2e2
       esac
Packit eba2e2
changequote([,])dnl
Packit eba2e2
     fi
Packit eba2e2
    ])
Packit eba2e2
  if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
Packit eba2e2
    # For using <pthread.h>:
Packit eba2e2
    case "$host_os" in
Packit eba2e2
      osf*)
Packit eba2e2
        # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
Packit eba2e2
        # groks <pthread.h>. cc also understands the flag -pthread, but
Packit eba2e2
        # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
Packit eba2e2
        # 2. putting a flag into CPPFLAGS that has an effect on the linker
Packit eba2e2
        # causes the AC_LINK_IFELSE test below to succeed unexpectedly,
Packit eba2e2
        # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
Packit eba2e2
        CPPFLAGS="$CPPFLAGS -D_REENTRANT"
Packit eba2e2
        ;;
Packit eba2e2
    esac
Packit eba2e2
    # Some systems optimize for single-threaded programs by default, and
Packit eba2e2
    # need special flags to disable these optimizations. For example, the
Packit eba2e2
    # definition of 'errno' in <errno.h>.
Packit eba2e2
    case "$host_os" in
Packit eba2e2
      aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
Packit eba2e2
      solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
Packit eba2e2
    esac
Packit eba2e2
  fi
Packit eba2e2
])
Packit eba2e2
Packit eba2e2
dnl The guts of gl_THREADLIB. Needs to be expanded only once.
Packit eba2e2
Packit eba2e2
AC_DEFUN([gl_THREADLIB_BODY],
Packit eba2e2
[
Packit eba2e2
  AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
Packit eba2e2
  gl_threads_api=none
Packit eba2e2
  LIBTHREAD=
Packit eba2e2
  LTLIBTHREAD=
Packit eba2e2
  LIBMULTITHREAD=
Packit eba2e2
  LTLIBMULTITHREAD=
Packit eba2e2
  if test "$gl_use_threads" != no; then
Packit eba2e2
    dnl Check whether the compiler and linker support weak declarations.
Packit eba2e2
    AC_CACHE_CHECK([whether imported symbols can be declared weak],
Packit eba2e2
      [gl_cv_have_weak],
Packit eba2e2
      [gl_cv_have_weak=no
Packit eba2e2
       dnl First, test whether the compiler accepts it syntactically.
Packit eba2e2
       AC_LINK_IFELSE(
Packit eba2e2
         [AC_LANG_PROGRAM(
Packit eba2e2
            [[extern void xyzzy ();
Packit eba2e2
#pragma weak xyzzy]],
Packit eba2e2
            [[xyzzy();]])],
Packit eba2e2
         [gl_cv_have_weak=maybe])
Packit eba2e2
       if test $gl_cv_have_weak = maybe; then
Packit eba2e2
         dnl Second, test whether it actually works. On Cygwin 1.7.2, with
Packit eba2e2
         dnl gcc 4.3, symbols declared weak always evaluate to the address 0.
Packit eba2e2
         AC_RUN_IFELSE(
Packit eba2e2
           [AC_LANG_SOURCE([[
Packit eba2e2
#include <stdio.h>
Packit eba2e2
#pragma weak fputs
Packit eba2e2
int main ()
Packit eba2e2
{
Packit eba2e2
  return (fputs == NULL);
Packit eba2e2
}]])],
Packit eba2e2
           [gl_cv_have_weak=yes],
Packit eba2e2
           [gl_cv_have_weak=no],
Packit eba2e2
           [dnl When cross-compiling, assume that only ELF platforms support
Packit eba2e2
            dnl weak symbols.
Packit eba2e2
            AC_EGREP_CPP([Extensible Linking Format],
Packit eba2e2
              [#ifdef __ELF__
Packit eba2e2
               Extensible Linking Format
Packit eba2e2
               #endif
Packit eba2e2
              ],
Packit eba2e2
              [gl_cv_have_weak="guessing yes"],
Packit eba2e2
              [gl_cv_have_weak="guessing no"])
Packit eba2e2
           ])
Packit eba2e2
       fi
Packit eba2e2
      ])
Packit eba2e2
    if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
Packit eba2e2
      # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
Packit eba2e2
      # it groks <pthread.h>. It's added above, in gl_THREADLIB_EARLY_BODY.
Packit eba2e2
      AC_CHECK_HEADER([pthread.h],
Packit eba2e2
        [gl_have_pthread_h=yes], [gl_have_pthread_h=no])
Packit eba2e2
      if test "$gl_have_pthread_h" = yes; then
Packit eba2e2
        # Other possible tests:
Packit eba2e2
        #   -lpthreads (FSU threads, PCthreads)
Packit eba2e2
        #   -lgthreads
Packit eba2e2
        gl_have_pthread=
Packit eba2e2
        # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
Packit eba2e2
        # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
Packit eba2e2
        # the second one only in libpthread, and lock.c needs it.
Packit eba2e2
        AC_LINK_IFELSE(
Packit eba2e2
          [AC_LANG_PROGRAM(
Packit eba2e2
             [[#include <pthread.h>]],
Packit eba2e2
             [[pthread_mutex_lock((pthread_mutex_t*)0);
Packit eba2e2
               pthread_mutexattr_init((pthread_mutexattr_t*)0);]])],
Packit eba2e2
          [gl_have_pthread=yes])
Packit eba2e2
        # Test for libpthread by looking for pthread_kill. (Not pthread_self,
Packit eba2e2
        # since it is defined as a macro on OSF/1.)
Packit eba2e2
        if test -n "$gl_have_pthread"; then
Packit eba2e2
          # The program links fine without libpthread. But it may actually
Packit eba2e2
          # need to link with libpthread in order to create multiple threads.
Packit eba2e2
          AC_CHECK_LIB([pthread], [pthread_kill],
Packit eba2e2
            [LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread
Packit eba2e2
             # On Solaris and HP-UX, most pthread functions exist also in libc.
Packit eba2e2
             # Therefore pthread_in_use() needs to actually try to create a
Packit eba2e2
             # thread: pthread_create from libc will fail, whereas
Packit eba2e2
             # pthread_create will actually create a thread.
Packit eba2e2
             case "$host_os" in
Packit eba2e2
               solaris* | hpux*)
Packit eba2e2
                 AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
Packit eba2e2
                   [Define if the pthread_in_use() detection is hard.])
Packit eba2e2
             esac
Packit eba2e2
            ])
Packit eba2e2
        else
Packit eba2e2
          # Some library is needed. Try libpthread and libc_r.
Packit eba2e2
          AC_CHECK_LIB([pthread], [pthread_kill],
Packit eba2e2
            [gl_have_pthread=yes
Packit eba2e2
             LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
Packit eba2e2
             LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
Packit eba2e2
          if test -z "$gl_have_pthread"; then
Packit eba2e2
            # For FreeBSD 4.
Packit eba2e2
            AC_CHECK_LIB([c_r], [pthread_kill],
Packit eba2e2
              [gl_have_pthread=yes
Packit eba2e2
               LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
Packit eba2e2
               LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
Packit eba2e2
          fi
Packit eba2e2
        fi
Packit eba2e2
        if test -n "$gl_have_pthread"; then
Packit eba2e2
          gl_threads_api=posix
Packit eba2e2
          AC_DEFINE([USE_POSIX_THREADS], [1],
Packit eba2e2
            [Define if the POSIX multithreading library can be used.])
Packit eba2e2
          if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
Packit eba2e2
            if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
Packit eba2e2
              AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
Packit eba2e2
                [Define if references to the POSIX multithreading library should be made weak.])
Packit eba2e2
              LIBTHREAD=
Packit eba2e2
              LTLIBTHREAD=
Packit eba2e2
            fi
Packit eba2e2
          fi
Packit eba2e2
        fi
Packit eba2e2
      fi
Packit eba2e2
    fi
Packit eba2e2
    if test -z "$gl_have_pthread"; then
Packit eba2e2
      if test "$gl_use_threads" = yes || test "$gl_use_threads" = solaris; then
Packit eba2e2
        gl_have_solaristhread=
Packit eba2e2
        gl_save_LIBS="$LIBS"
Packit eba2e2
        LIBS="$LIBS -lthread"
Packit eba2e2
        AC_LINK_IFELSE(
Packit eba2e2
          [AC_LANG_PROGRAM(
Packit eba2e2
             [[
Packit eba2e2
#include <thread.h>
Packit eba2e2
#include <synch.h>
Packit eba2e2
             ]],
Packit eba2e2
             [[thr_self();]])],
Packit eba2e2
          [gl_have_solaristhread=yes])
Packit eba2e2
        LIBS="$gl_save_LIBS"
Packit eba2e2
        if test -n "$gl_have_solaristhread"; then
Packit eba2e2
          gl_threads_api=solaris
Packit eba2e2
          LIBTHREAD=-lthread
Packit eba2e2
          LTLIBTHREAD=-lthread
Packit eba2e2
          LIBMULTITHREAD="$LIBTHREAD"
Packit eba2e2
          LTLIBMULTITHREAD="$LTLIBTHREAD"
Packit eba2e2
          AC_DEFINE([USE_SOLARIS_THREADS], [1],
Packit eba2e2
            [Define if the old Solaris multithreading library can be used.])
Packit eba2e2
          if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
Packit eba2e2
            AC_DEFINE([USE_SOLARIS_THREADS_WEAK], [1],
Packit eba2e2
              [Define if references to the old Solaris multithreading library should be made weak.])
Packit eba2e2
            LIBTHREAD=
Packit eba2e2
            LTLIBTHREAD=
Packit eba2e2
          fi
Packit eba2e2
        fi
Packit eba2e2
      fi
Packit eba2e2
    fi
Packit eba2e2
    if test "$gl_use_threads" = pth; then
Packit eba2e2
      gl_save_CPPFLAGS="$CPPFLAGS"
Packit eba2e2
      AC_LIB_LINKFLAGS([pth])
Packit eba2e2
      gl_have_pth=
Packit eba2e2
      gl_save_LIBS="$LIBS"
Packit eba2e2
      LIBS="$LIBS $LIBPTH"
Packit eba2e2
      AC_LINK_IFELSE(
Packit eba2e2
        [AC_LANG_PROGRAM([[#include <pth.h>]], [[pth_self();]])],
Packit eba2e2
        [gl_have_pth=yes])
Packit eba2e2
      LIBS="$gl_save_LIBS"
Packit eba2e2
      if test -n "$gl_have_pth"; then
Packit eba2e2
        gl_threads_api=pth
Packit eba2e2
        LIBTHREAD="$LIBPTH"
Packit eba2e2
        LTLIBTHREAD="$LTLIBPTH"
Packit eba2e2
        LIBMULTITHREAD="$LIBTHREAD"
Packit eba2e2
        LTLIBMULTITHREAD="$LTLIBTHREAD"
Packit eba2e2
        AC_DEFINE([USE_PTH_THREADS], [1],
Packit eba2e2
          [Define if the GNU Pth multithreading library can be used.])
Packit eba2e2
        if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
Packit eba2e2
          if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
Packit eba2e2
            AC_DEFINE([USE_PTH_THREADS_WEAK], [1],
Packit eba2e2
              [Define if references to the GNU Pth multithreading library should be made weak.])
Packit eba2e2
            LIBTHREAD=
Packit eba2e2
            LTLIBTHREAD=
Packit eba2e2
          fi
Packit eba2e2
        fi
Packit eba2e2
      else
Packit eba2e2
        CPPFLAGS="$gl_save_CPPFLAGS"
Packit eba2e2
      fi
Packit eba2e2
    fi
Packit eba2e2
    if test -z "$gl_have_pthread"; then
Packit eba2e2
      case "$gl_use_threads" in
Packit eba2e2
        yes | windows | win32) # The 'win32' is for backward compatibility.
Packit eba2e2
          if { case "$host_os" in
Packit eba2e2
                 mingw*) true;;
Packit eba2e2
                 *) false;;
Packit eba2e2
               esac
Packit eba2e2
             }; then
Packit eba2e2
            gl_threads_api=windows
Packit eba2e2
            AC_DEFINE([USE_WINDOWS_THREADS], [1],
Packit eba2e2
              [Define if the native Windows multithreading API can be used.])
Packit eba2e2
          fi
Packit eba2e2
          ;;
Packit eba2e2
      esac
Packit eba2e2
    fi
Packit eba2e2
  fi
Packit eba2e2
  AC_MSG_CHECKING([for multithread API to use])
Packit eba2e2
  AC_MSG_RESULT([$gl_threads_api])
Packit eba2e2
  AC_SUBST([LIBTHREAD])
Packit eba2e2
  AC_SUBST([LTLIBTHREAD])
Packit eba2e2
  AC_SUBST([LIBMULTITHREAD])
Packit eba2e2
  AC_SUBST([LTLIBMULTITHREAD])
Packit eba2e2
])
Packit eba2e2
Packit eba2e2
AC_DEFUN([gl_THREADLIB],
Packit eba2e2
[
Packit eba2e2
  AC_REQUIRE([gl_THREADLIB_EARLY])
Packit eba2e2
  AC_REQUIRE([gl_THREADLIB_BODY])
Packit eba2e2
])
Packit eba2e2
Packit eba2e2
Packit eba2e2
dnl gl_DISABLE_THREADS
Packit eba2e2
dnl ------------------
Packit eba2e2
dnl Sets the gl_THREADLIB default so that threads are not used by default.
Packit eba2e2
dnl The user can still override it at installation time, by using the
Packit eba2e2
dnl configure option '--enable-threads'.
Packit eba2e2
Packit eba2e2
AC_DEFUN([gl_DISABLE_THREADS], [
Packit eba2e2
  m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
Packit eba2e2
])
Packit eba2e2
Packit eba2e2
Packit eba2e2
dnl Survey of platforms:
Packit eba2e2
dnl
Packit eba2e2
dnl Platform           Available  Compiler    Supports   test-lock
Packit eba2e2
dnl                    flavours   option      weak       result
Packit eba2e2
dnl ---------------    ---------  ---------   --------   ---------
Packit eba2e2
dnl Linux 2.4/glibc    posix      -lpthread       Y      OK
Packit eba2e2
dnl
Packit eba2e2
dnl GNU Hurd/glibc     posix
Packit eba2e2
dnl
Packit eba2e2
dnl FreeBSD 5.3        posix      -lc_r           Y
Packit eba2e2
dnl                    posix      -lkse ?         Y
Packit eba2e2
dnl                    posix      -lpthread ?     Y
Packit eba2e2
dnl                    posix      -lthr           Y
Packit eba2e2
dnl
Packit eba2e2
dnl FreeBSD 5.2        posix      -lc_r           Y
Packit eba2e2
dnl                    posix      -lkse           Y
Packit eba2e2
dnl                    posix      -lthr           Y
Packit eba2e2
dnl
Packit eba2e2
dnl FreeBSD 4.0,4.10   posix      -lc_r           Y      OK
Packit eba2e2
dnl
Packit eba2e2
dnl NetBSD 1.6         --
Packit eba2e2
dnl
Packit eba2e2
dnl OpenBSD 3.4        posix      -lpthread       Y      OK
Packit eba2e2
dnl
Packit eba2e2
dnl Mac OS X 10.[123]  posix      -lpthread       Y      OK
Packit eba2e2
dnl
Packit eba2e2
dnl Solaris 7,8,9      posix      -lpthread       Y      Sol 7,8: 0.0; Sol 9: OK
Packit eba2e2
dnl                    solaris    -lthread        Y      Sol 7,8: 0.0; Sol 9: OK
Packit eba2e2
dnl
Packit eba2e2
dnl HP-UX 11           posix      -lpthread       N (cc) OK
Packit eba2e2
dnl                                               Y (gcc)
Packit eba2e2
dnl
Packit eba2e2
dnl IRIX 6.5           posix      -lpthread       Y      0.5
Packit eba2e2
dnl
Packit eba2e2
dnl AIX 4.3,5.1        posix      -lpthread       N      AIX 4: 0.5; AIX 5: OK
Packit eba2e2
dnl
Packit eba2e2
dnl OSF/1 4.0,5.1      posix      -pthread (cc)   N      OK
Packit eba2e2
dnl                               -lpthread (gcc) Y
Packit eba2e2
dnl
Packit eba2e2
dnl Cygwin             posix      -lpthread       Y      OK
Packit eba2e2
dnl
Packit eba2e2
dnl Any of the above   pth        -lpth                  0.0
Packit eba2e2
dnl
Packit eba2e2
dnl Mingw              windows                    N      OK
Packit eba2e2
dnl
Packit eba2e2
dnl BeOS 5             --
Packit eba2e2
dnl
Packit eba2e2
dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
Packit eba2e2
dnl turned off:
Packit eba2e2
dnl   OK if all three tests terminate OK,
Packit eba2e2
dnl   0.5 if the first test terminates OK but the second one loops endlessly,
Packit eba2e2
dnl   0.0 if the first test already loops endlessly.