Blame m4/threadlib.m4

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