Blame m4/threadlib.m4

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