Blame m4/threadlib.m4

Packit Service 4684c1
# threadlib.m4 serial 27
Packit Service 4684c1
dnl Copyright (C) 2005-2020 Free Software Foundation, Inc.
Packit Service 4684c1
dnl This file is free software; the Free Software Foundation
Packit Service 4684c1
dnl gives unlimited permission to copy and/or distribute it,
Packit Service 4684c1
dnl with or without modifications, as long as this notice is preserved.
Packit Service 4684c1
Packit Service 4684c1
dnl From Bruno Haible.
Packit Service 4684c1
Packit Service 4684c1
AC_PREREQ([2.60])
Packit Service 4684c1
Packit Service 4684c1
dnl The general structure of the multithreading modules in gnulib is that we
Packit Service 4684c1
dnl have three set of modules:
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl   * POSIX API:
Packit Service 4684c1
dnl     pthread, which combines
Packit Service 4684c1
dnl       pthread-h
Packit Service 4684c1
dnl       pthread-thread
Packit Service 4684c1
dnl       pthread-once
Packit Service 4684c1
dnl       pthread-mutex
Packit Service 4684c1
dnl       pthread-rwlock
Packit Service 4684c1
dnl       pthread-cond
Packit Service 4684c1
dnl       pthread-tss
Packit Service 4684c1
dnl       pthread-spin
Packit Service 4684c1
dnl     sched_yield
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl   * ISO C API:
Packit Service 4684c1
dnl     threads, which combines
Packit Service 4684c1
dnl       threads-h
Packit Service 4684c1
dnl       thrd
Packit Service 4684c1
dnl       mtx
Packit Service 4684c1
dnl       cnd
Packit Service 4684c1
dnl       tss
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl   * Gnulib API, with an implementation that can be chosen at configure
Packit Service 4684c1
dnl     time through the option --enable-threads=...
Packit Service 4684c1
dnl       thread
Packit Service 4684c1
dnl       lock
Packit Service 4684c1
dnl       cond
Packit Service 4684c1
dnl       tls
Packit Service 4684c1
dnl       yield
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl They are independent, except for the fact that
Packit Service 4684c1
dnl   - the implementation of the ISO C API may use the POSIX (or some other
Packit Service 4684c1
dnl     platform dependent) API,
Packit Service 4684c1
dnl   - the implementation of the Gnulib API may use the POSIX or ISO C or
Packit Service 4684c1
dnl     some other platform dependent API, depending on the --enable-threads
Packit Service 4684c1
dnl     option.
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl This file contains macros for all of these APIs!
Packit Service 4684c1
Packit Service 4684c1
dnl ============================================================================
Packit Service 4684c1
dnl Macros for all thread APIs
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_ANYTHREADLIB_EARLY],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([AC_CANONICAL_HOST])
Packit Service 4684c1
  if test -z "$gl_anythreadlib_early_done"; then
Packit Service 4684c1
    case "$host_os" in
Packit Service 4684c1
      osf*)
Packit Service 4684c1
        # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
Packit Service 4684c1
        # groks <pthread.h>. cc also understands the flag -pthread, but
Packit Service 4684c1
        # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
Packit Service 4684c1
        # 2. putting a flag into CPPFLAGS that has an effect on the linker
Packit Service 4684c1
        # causes the AC_LINK_IFELSE test below to succeed unexpectedly,
Packit Service 4684c1
        # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
Packit Service 4684c1
        CPPFLAGS="$CPPFLAGS -D_REENTRANT"
Packit Service 4684c1
        ;;
Packit Service 4684c1
    esac
Packit Service 4684c1
    # Some systems optimize for single-threaded programs by default, and
Packit Service 4684c1
    # need special flags to disable these optimizations. For example, the
Packit Service 4684c1
    # definition of 'errno' in <errno.h>.
Packit Service 4684c1
    case "$host_os" in
Packit Service 4684c1
      aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
Packit Service 4684c1
      solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
Packit Service 4684c1
    esac
Packit Service 4684c1
    gl_anythreadlib_early_done=done
Packit Service 4684c1
  fi
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
dnl Checks whether the compiler and linker support weak declarations of symbols.
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_WEAK_SYMBOLS],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([AC_CANONICAL_HOST])
Packit Service 4684c1
  AC_CACHE_CHECK([whether imported symbols can be declared weak],
Packit Service 4684c1
    [gl_cv_have_weak],
Packit Service 4684c1
    [gl_cv_have_weak=no
Packit Service 4684c1
     dnl First, test whether the compiler accepts it syntactically.
Packit Service 4684c1
     AC_LINK_IFELSE(
Packit Service 4684c1
       [AC_LANG_PROGRAM(
Packit Service 4684c1
          [[extern void xyzzy ();
Packit Service 4684c1
#pragma weak xyzzy]],
Packit Service 4684c1
          [[xyzzy();]])],
Packit Service 4684c1
       [gl_cv_have_weak=maybe])
Packit Service 4684c1
     if test $gl_cv_have_weak = maybe; then
Packit Service 4684c1
       dnl Second, test whether it actually works. On Cygwin 1.7.2, with
Packit Service 4684c1
       dnl gcc 4.3, symbols declared weak always evaluate to the address 0.
Packit Service 4684c1
       AC_RUN_IFELSE(
Packit Service 4684c1
         [AC_LANG_SOURCE([[
Packit Service 4684c1
#include <stdio.h>
Packit Service 4684c1
#pragma weak fputs
Packit Service 4684c1
int main ()
Packit Service 4684c1
{
Packit Service 4684c1
  return (fputs == NULL);
Packit Service 4684c1
}]])],
Packit Service 4684c1
         [gl_cv_have_weak=yes],
Packit Service 4684c1
         [gl_cv_have_weak=no],
Packit Service 4684c1
         [dnl When cross-compiling, assume that only ELF platforms support
Packit Service 4684c1
          dnl weak symbols.
Packit Service 4684c1
          AC_EGREP_CPP([Extensible Linking Format],
Packit Service 4684c1
            [#ifdef __ELF__
Packit Service 4684c1
             Extensible Linking Format
Packit Service 4684c1
             #endif
Packit Service 4684c1
            ],
Packit Service 4684c1
            [gl_cv_have_weak="guessing yes"],
Packit Service 4684c1
            [gl_cv_have_weak="guessing no"])
Packit Service 4684c1
         ])
Packit Service 4684c1
     fi
Packit Service 4684c1
     dnl But when linking statically, weak symbols don't work.
Packit Service 4684c1
     case " $LDFLAGS " in
Packit Service 4684c1
       *" -static "*) gl_cv_have_weak=no ;;
Packit Service 4684c1
     esac
Packit Service 4684c1
     dnl Test for a bug in FreeBSD 11: A link error occurs when using a weak
Packit Service 4684c1
     dnl symbol and linking against a shared library that has a dependency on
Packit Service 4684c1
     dnl the shared library that defines the symbol.
Packit Service 4684c1
     case "$gl_cv_have_weak" in
Packit Service 4684c1
       *yes)
Packit Service 4684c1
         case "$host_os" in
Packit Service 4684c1
           freebsd* | dragonfly*)
Packit Service 4684c1
             : > conftest1.c
Packit Service 4684c1
             $CC $CPPFLAGS $CFLAGS $LDFLAGS -fPIC -shared -o libempty.so conftest1.c -lpthread >&AS_MESSAGE_LOG_FD 2>&1
Packit Service 4684c1
             cat <<EOF > conftest2.c
Packit Service 4684c1
#include <pthread.h>
Packit Service 4684c1
#pragma weak pthread_mutexattr_gettype
Packit Service 4684c1
int main ()
Packit Service 4684c1
{
Packit Service 4684c1
  return (pthread_mutexattr_gettype != NULL);
Packit Service 4684c1
}
Packit Service 4684c1
EOF
Packit Service 4684c1
             $CC $CPPFLAGS $CFLAGS $LDFLAGS -o conftest conftest2.c libempty.so >&AS_MESSAGE_LOG_FD 2>&1 \
Packit Service 4684c1
               || gl_cv_have_weak=no
Packit Service 4684c1
             rm -f conftest1.c libempty.so conftest2.c conftest
Packit Service 4684c1
             ;;
Packit Service 4684c1
         esac
Packit Service 4684c1
         ;;
Packit Service 4684c1
     esac
Packit Service 4684c1
    ])
Packit Service 4684c1
  case "$gl_cv_have_weak" in
Packit Service 4684c1
    *yes)
Packit Service 4684c1
      AC_DEFINE([HAVE_WEAK_SYMBOLS], [1],
Packit Service 4684c1
        [Define to 1 if the compiler and linker support weak declarations of symbols.])
Packit Service 4684c1
      ;;
Packit Service 4684c1
  esac
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
dnl ============================================================================
Packit Service 4684c1
dnl Macros for the POSIX API
Packit Service 4684c1
Packit Service 4684c1
dnl gl_PTHREADLIB
Packit Service 4684c1
dnl -------------
Packit Service 4684c1
dnl Tests for the libraries needs for using the POSIX threads API.
Packit Service 4684c1
dnl Sets the variable LIBPTHREAD to the linker options for use in a Makefile.
Packit Service 4684c1
dnl Sets the variable LIBPMULTITHREAD, for programs that really need
Packit Service 4684c1
dnl multithread functionality. The difference between LIBPTHREAD and
Packit Service 4684c1
dnl LIBPMULTITHREAD is that on platforms supporting weak symbols, typically
Packit Service 4684c1
dnl LIBPTHREAD is empty whereas LIBPMULTITHREAD is not.
Packit Service 4684c1
dnl Sets the variable LIB_SCHED_YIELD to the linker options needed to use the
Packit Service 4684c1
dnl sched_yield() function.
Packit Service 4684c1
dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
Packit Service 4684c1
dnl multithread-safe programs.
Packit Service 4684c1
dnl Defines the C macro HAVE_PTHREAD_API if (at least parts of) the POSIX
Packit Service 4684c1
dnl threads API is available.
Packit Service 4684c1
Packit Service 4684c1
dnl The guts of gl_PTHREADLIB. Needs to be expanded only once.
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_PTHREADLIB_BODY],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
Packit Service 4684c1
  if test -z "$gl_pthreadlib_body_done"; then
Packit Service 4684c1
    gl_pthread_api=no
Packit Service 4684c1
    LIBPTHREAD=
Packit Service 4684c1
    LIBPMULTITHREAD=
Packit Service 4684c1
    # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
Packit Service 4684c1
    # it groks <pthread.h>. It's added above, in gl_ANYTHREADLIB_EARLY.
Packit Service 4684c1
    AC_CHECK_HEADER([pthread.h],
Packit Service 4684c1
      [gl_have_pthread_h=yes], [gl_have_pthread_h=no])
Packit Service 4684c1
    if test "$gl_have_pthread_h" = yes; then
Packit Service 4684c1
      # Other possible tests:
Packit Service 4684c1
      #   -lpthreads (FSU threads, PCthreads)
Packit Service 4684c1
      #   -lgthreads
Packit Service 4684c1
      # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
Packit Service 4684c1
      # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
Packit Service 4684c1
      # the second one only in libpthread, and lock.c needs it.
Packit Service 4684c1
      #
Packit Service 4684c1
      # If -pthread works, prefer it to -lpthread, since Ubuntu 14.04
Packit Service 4684c1
      # needs -pthread for some reason.  See:
Packit Service 4684c1
      # https://lists.gnu.org/r/bug-gnulib/2014-09/msg00023.html
Packit Service 4684c1
      save_LIBS=$LIBS
Packit Service 4684c1
      for gl_pthread in '' '-pthread'; do
Packit Service 4684c1
        LIBS="$LIBS $gl_pthread"
Packit Service 4684c1
        AC_LINK_IFELSE(
Packit Service 4684c1
          [AC_LANG_PROGRAM(
Packit Service 4684c1
             [[#include <pthread.h>
Packit Service 4684c1
               pthread_mutex_t m;
Packit Service 4684c1
               pthread_mutexattr_t ma;
Packit Service 4684c1
             ]],
Packit Service 4684c1
             [[pthread_mutex_lock (&m);
Packit Service 4684c1
               pthread_mutexattr_init (&ma);]])],
Packit Service 4684c1
          [gl_pthread_api=yes
Packit Service 4684c1
           LIBPTHREAD=$gl_pthread
Packit Service 4684c1
           LIBPMULTITHREAD=$gl_pthread])
Packit Service 4684c1
        LIBS=$save_LIBS
Packit Service 4684c1
        test $gl_pthread_api = yes && break
Packit Service 4684c1
      done
Packit Service 4684c1
Packit Service 4684c1
      # Test for libpthread by looking for pthread_kill. (Not pthread_self,
Packit Service 4684c1
      # since it is defined as a macro on OSF/1.)
Packit Service 4684c1
      if test $gl_pthread_api = yes && test -z "$LIBPTHREAD"; then
Packit Service 4684c1
        # The program links fine without libpthread. But it may actually
Packit Service 4684c1
        # need to link with libpthread in order to create multiple threads.
Packit Service 4684c1
        AC_CHECK_LIB([pthread], [pthread_kill],
Packit Service 4684c1
          [LIBPMULTITHREAD=-lpthread
Packit Service 4684c1
           # On Solaris and HP-UX, most pthread functions exist also in libc.
Packit Service 4684c1
           # Therefore pthread_in_use() needs to actually try to create a
Packit Service 4684c1
           # thread: pthread_create from libc will fail, whereas
Packit Service 4684c1
           # pthread_create will actually create a thread.
Packit Service 4684c1
           # On Solaris 10 or newer, this test is no longer needed, because
Packit Service 4684c1
           # libc contains the fully functional pthread functions.
Packit Service 4684c1
           case "$host_os" in
Packit Service 4684c1
             solaris | solaris2.[1-9] | solaris2.[1-9].* | hpux*)
Packit Service 4684c1
               AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
Packit Service 4684c1
                 [Define if the pthread_in_use() detection is hard.])
Packit Service 4684c1
           esac
Packit Service 4684c1
          ])
Packit Service 4684c1
      elif test $gl_pthread_api != yes; then
Packit Service 4684c1
        # Some library is needed. Try libpthread and libc_r.
Packit Service 4684c1
        AC_CHECK_LIB([pthread], [pthread_kill],
Packit Service 4684c1
          [gl_pthread_api=yes
Packit Service 4684c1
           LIBPTHREAD=-lpthread
Packit Service 4684c1
           LIBPMULTITHREAD=-lpthread])
Packit Service 4684c1
        if test $gl_pthread_api != yes; then
Packit Service 4684c1
          # For FreeBSD 4.
Packit Service 4684c1
          AC_CHECK_LIB([c_r], [pthread_kill],
Packit Service 4684c1
            [gl_pthread_api=yes
Packit Service 4684c1
             LIBPTHREAD=-lc_r
Packit Service 4684c1
             LIBPMULTITHREAD=-lc_r])
Packit Service 4684c1
        fi
Packit Service 4684c1
      fi
Packit Service 4684c1
    fi
Packit Service 4684c1
    AC_MSG_CHECKING([whether POSIX threads API is available])
Packit Service 4684c1
    AC_MSG_RESULT([$gl_pthread_api])
Packit Service 4684c1
    AC_SUBST([LIBPTHREAD])
Packit Service 4684c1
    AC_SUBST([LIBPMULTITHREAD])
Packit Service 4684c1
    if test $gl_pthread_api = yes; then
Packit Service 4684c1
      AC_DEFINE([HAVE_PTHREAD_API], [1],
Packit Service 4684c1
        [Define if you have the <pthread.h> header and the POSIX threads API.])
Packit Service 4684c1
    fi
Packit Service 4684c1
Packit Service 4684c1
    dnl On some systems, sched_yield is in librt, rather than in libpthread.
Packit Service 4684c1
    AC_LINK_IFELSE(
Packit Service 4684c1
      [AC_LANG_PROGRAM(
Packit Service 4684c1
         [[#include <sched.h>]],
Packit Service 4684c1
         [[sched_yield ();]])],
Packit Service 4684c1
      [LIB_SCHED_YIELD=
Packit Service 4684c1
      ],
Packit Service 4684c1
      [dnl Solaris 7...10 has sched_yield in librt, not in libpthread or libc.
Packit Service 4684c1
       AC_CHECK_LIB([rt], [sched_yield], [LIB_SCHED_YIELD=-lrt],
Packit Service 4684c1
         [dnl Solaris 2.5.1, 2.6 has sched_yield in libposix4, not librt.
Packit Service 4684c1
          AC_CHECK_LIB([posix4], [sched_yield], [LIB_SCHED_YIELD=-lposix4])])
Packit Service 4684c1
      ])
Packit Service 4684c1
    AC_SUBST([LIB_SCHED_YIELD])
Packit Service 4684c1
Packit Service 4684c1
    gl_pthreadlib_body_done=done
Packit Service 4684c1
  fi
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_PTHREADLIB],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
Packit Service 4684c1
  gl_PTHREADLIB_BODY
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
dnl ============================================================================
Packit Service 4684c1
dnl Macros for the ISO C API
Packit Service 4684c1
Packit Service 4684c1
dnl gl_STDTHREADLIB
Packit Service 4684c1
dnl ---------------
Packit Service 4684c1
dnl Tests for the libraries needs for using the ISO C threads API.
Packit Service 4684c1
dnl Sets the variable LIBSTDTHREAD to the linker options for use in a Makefile.
Packit Service 4684c1
dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
Packit Service 4684c1
dnl multithread-safe programs.
Packit Service 4684c1
dnl Defines the C macro HAVE_THREADS_H if (at least parts of) the ISO C threads
Packit Service 4684c1
dnl API is available.
Packit Service 4684c1
Packit Service 4684c1
dnl The guts of gl_STDTHREADLIB. Needs to be expanded only once.
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_STDTHREADLIB_BODY],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
Packit Service 4684c1
  AC_REQUIRE([AC_CANONICAL_HOST])
Packit Service 4684c1
  if test -z "$gl_stdthreadlib_body_done"; then
Packit Service 4684c1
    AC_CHECK_HEADERS_ONCE([threads.h])
Packit Service 4684c1
Packit Service 4684c1
    case "$host_os" in
Packit Service 4684c1
      mingw*)
Packit Service 4684c1
        LIBSTDTHREAD=
Packit Service 4684c1
        ;;
Packit Service 4684c1
      *)
Packit Service 4684c1
        gl_PTHREADLIB_BODY
Packit Service 4684c1
        if test $ac_cv_header_threads_h = yes; then
Packit Service 4684c1
          dnl glibc >= 2.29 has thrd_create in libpthread.
Packit Service 4684c1
          dnl FreeBSD >= 10 has thrd_create in libstdthreads; this library depends
Packit Service 4684c1
          dnl on libpthread (for the symbol 'pthread_mutexattr_gettype').
Packit Service 4684c1
          dnl AIX >= 7.1 and Solaris >= 11.4 have thrd_create in libc.
Packit Service 4684c1
          AC_CHECK_FUNCS([thrd_create])
Packit Service 4684c1
          if test $ac_cv_func_thrd_create = yes; then
Packit Service 4684c1
            LIBSTDTHREAD=
Packit Service 4684c1
          else
Packit Service 4684c1
            AC_CHECK_LIB([stdthreads], [thrd_create], [
Packit Service 4684c1
              LIBSTDTHREAD='-lstdthreads -lpthread'
Packit Service 4684c1
            ], [
Packit Service 4684c1
              dnl Guess that thrd_create is in libpthread.
Packit Service 4684c1
              LIBSTDTHREAD="$LIBPMULTITHREAD"
Packit Service 4684c1
            ])
Packit Service 4684c1
          fi
Packit Service 4684c1
        else
Packit Service 4684c1
          dnl Libraries needed by thrd.c, mtx.c, cnd.c, tss.c.
Packit Service 4684c1
          LIBSTDTHREAD="$LIBPMULTITHREAD $LIB_SCHED_YIELD"
Packit Service 4684c1
        fi
Packit Service 4684c1
        ;;
Packit Service 4684c1
    esac
Packit Service 4684c1
    AC_SUBST([LIBSTDTHREAD])
Packit Service 4684c1
Packit Service 4684c1
    AC_MSG_CHECKING([whether ISO C threads API is available])
Packit Service 4684c1
    AC_MSG_RESULT([$ac_cv_header_threads_h])
Packit Service 4684c1
    gl_stdthreadlib_body_done=done
Packit Service 4684c1
  fi
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_STDTHREADLIB],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
Packit Service 4684c1
  gl_STDTHREADLIB_BODY
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
dnl ============================================================================
Packit Service 4684c1
dnl Macros for the Gnulib API
Packit Service 4684c1
Packit Service 4684c1
dnl gl_THREADLIB
Packit Service 4684c1
dnl ------------
Packit Service 4684c1
dnl Tests for a multithreading library to be used.
Packit Service 4684c1
dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO
Packit Service 4684c1
dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the
Packit Service 4684c1
dnl default is 'no', otherwise it is system dependent. In both cases, the user
Packit Service 4684c1
dnl can change the choice through the options --enable-threads=choice or
Packit Service 4684c1
dnl --disable-threads.
Packit Service 4684c1
dnl Defines at most one of the macros USE_ISOC_THREADS, USE_POSIX_THREADS,
Packit Service 4684c1
dnl USE_ISOC_AND_POSIX_THREADS, USE_WINDOWS_THREADS.
Packit Service 4684c1
dnl The choice --enable-threads=isoc+posix is available only on platforms that
Packit Service 4684c1
dnl have both the ISO C and the POSIX threads APIs. It has the effect of using
Packit Service 4684c1
dnl the ISO C API for most things and the POSIX API only for creating and
Packit Service 4684c1
dnl controlling threads (because there is no equivalent to pthread_atfork in
Packit Service 4684c1
dnl the ISO C API).
Packit Service 4684c1
dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use
Packit Service 4684c1
dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with
Packit Service 4684c1
dnl libtool).
Packit Service 4684c1
dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for
Packit Service 4684c1
dnl programs that really need multithread functionality. The difference
Packit Service 4684c1
dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak
Packit Service 4684c1
dnl symbols, typically LIBTHREAD is empty whereas LIBMULTITHREAD is not.
Packit Service 4684c1
dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
Packit Service 4684c1
dnl multithread-safe programs.
Packit Service 4684c1
dnl Since support for GNU pth was removed, $LTLIBTHREAD and $LIBTHREAD have the
Packit Service 4684c1
dnl same value, and similarly $LTLIBMULTITHREAD and $LIBMULTITHREAD have the
Packit Service 4684c1
dnl same value. Only system libraries are needed.
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_THREADLIB_EARLY],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once.
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_THREADLIB_EARLY_BODY],
Packit Service 4684c1
[
Packit Service 4684c1
  dnl Ordering constraints: This macro modifies CPPFLAGS in a way that
Packit Service 4684c1
  dnl influences the result of the autoconf tests that test for *_unlocked
Packit Service 4684c1
  dnl declarations, on AIX 5 at least. Therefore it must come early.
Packit Service 4684c1
  AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl
Packit Service 4684c1
  AC_BEFORE([$0], [gl_ARGP])dnl
Packit Service 4684c1
Packit Service 4684c1
  AC_REQUIRE([AC_CANONICAL_HOST])
Packit Service 4684c1
  dnl _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems.
Packit Service 4684c1
  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
Packit Service 4684c1
  dnl Check for multithreading.
Packit Service 4684c1
  m4_ifdef([gl_THREADLIB_DEFAULT_NO],
Packit Service 4684c1
    [m4_divert_text([DEFAULTS], [gl_use_threads_default=no])],
Packit Service 4684c1
    [m4_divert_text([DEFAULTS], [gl_use_threads_default=])])
Packit Service 4684c1
  m4_divert_text([DEFAULTS], [gl_use_winpthreads_default=])
Packit Service 4684c1
  AC_ARG_ENABLE([threads],
Packit Service 4684c1
AC_HELP_STRING([--enable-threads={isoc|posix|isoc+posix|windows}], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [
Packit Service 4684c1
AC_HELP_STRING([--disable-threads], [build without multithread safety])]),
Packit Service 4684c1
    [gl_use_threads=$enableval],
Packit Service 4684c1
    [if test -n "$gl_use_threads_default"; then
Packit Service 4684c1
       gl_use_threads="$gl_use_threads_default"
Packit Service 4684c1
     else
Packit Service 4684c1
changequote(,)dnl
Packit Service 4684c1
       case "$host_os" in
Packit Service 4684c1
         dnl Disable multithreading by default on OSF/1, because it interferes
Packit Service 4684c1
         dnl with fork()/exec(): When msgexec is linked with -lpthread, its
Packit Service 4684c1
         dnl child process gets an endless segmentation fault inside execvp().
Packit Service 4684c1
         osf*) gl_use_threads=no ;;
Packit Service 4684c1
         dnl Disable multithreading by default on Cygwin 1.5.x, because it has
Packit Service 4684c1
         dnl bugs that lead to endless loops or crashes. See
Packit Service 4684c1
         dnl <https://cygwin.com/ml/cygwin/2009-08/msg00283.html>.
Packit Service 4684c1
         cygwin*)
Packit Service 4684c1
               case `uname -r` in
Packit Service 4684c1
                 1.[0-5].*) gl_use_threads=no ;;
Packit Service 4684c1
                 *)         gl_use_threads=yes ;;
Packit Service 4684c1
               esac
Packit Service 4684c1
               ;;
Packit Service 4684c1
         dnl Obey gl_AVOID_WINPTHREAD on mingw.
Packit Service 4684c1
         mingw*)
Packit Service 4684c1
               case "$gl_use_winpthreads_default" in
Packit Service 4684c1
                 yes) gl_use_threads=posix ;;
Packit Service 4684c1
                 no)  gl_use_threads=windows ;;
Packit Service 4684c1
                 *)   gl_use_threads=yes ;;
Packit Service 4684c1
               esac
Packit Service 4684c1
               ;;
Packit Service 4684c1
         *)    gl_use_threads=yes ;;
Packit Service 4684c1
       esac
Packit Service 4684c1
changequote([,])dnl
Packit Service 4684c1
     fi
Packit Service 4684c1
    ])
Packit Service 4684c1
  if test "$gl_use_threads" = yes \
Packit Service 4684c1
     || test "$gl_use_threads" = isoc \
Packit Service 4684c1
     || test "$gl_use_threads" = posix \
Packit Service 4684c1
     || test "$gl_use_threads" = isoc+posix; then
Packit Service 4684c1
    # For using <threads.h> or <pthread.h>:
Packit Service 4684c1
    gl_ANYTHREADLIB_EARLY
Packit Service 4684c1
  fi
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
dnl The guts of gl_THREADLIB. Needs to be expanded only once.
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_THREADLIB_BODY],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
Packit Service 4684c1
  gl_threads_api=none
Packit Service 4684c1
  LIBTHREAD=
Packit Service 4684c1
  LTLIBTHREAD=
Packit Service 4684c1
  LIBMULTITHREAD=
Packit Service 4684c1
  LTLIBMULTITHREAD=
Packit Service 4684c1
  if test "$gl_use_threads" != no; then
Packit Service 4684c1
    dnl Check whether the compiler and linker support weak declarations.
Packit Service 4684c1
    gl_WEAK_SYMBOLS
Packit Service 4684c1
    if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
Packit Service 4684c1
      dnl If we use weak symbols to implement pthread_in_use / pth_in_use /
Packit Service 4684c1
      dnl thread_in_use, we also need to test whether the ISO C 11 thrd_create
Packit Service 4684c1
      dnl facility is in use.
Packit Service 4684c1
      AC_CHECK_HEADERS_ONCE([threads.h])
Packit Service 4684c1
      :
Packit Service 4684c1
    fi
Packit Service 4684c1
    if test "$gl_use_threads" = isoc || test "$gl_use_threads" = isoc+posix; then
Packit Service 4684c1
      AC_CHECK_HEADERS_ONCE([threads.h])
Packit Service 4684c1
      gl_have_isoc_threads="$ac_cv_header_threads_h"
Packit Service 4684c1
    fi
Packit Service 4684c1
    if test "$gl_use_threads" = yes \
Packit Service 4684c1
       || test "$gl_use_threads" = posix \
Packit Service 4684c1
       || test "$gl_use_threads" = isoc+posix; then
Packit Service 4684c1
      gl_PTHREADLIB_BODY
Packit Service 4684c1
      LIBTHREAD=$LIBPTHREAD LTLIBTHREAD=$LIBPTHREAD
Packit Service 4684c1
      LIBMULTITHREAD=$LIBPMULTITHREAD LTLIBMULTITHREAD=$LIBPMULTITHREAD
Packit Service 4684c1
      if test $gl_pthread_api = yes; then
Packit Service 4684c1
        if test "$gl_use_threads" = isoc+posix && test "$gl_have_isoc_threads" = yes; then
Packit Service 4684c1
          gl_threads_api='isoc+posix'
Packit Service 4684c1
          AC_DEFINE([USE_ISOC_AND_POSIX_THREADS], [1],
Packit Service 4684c1
            [Define if the combination of the ISO C and POSIX multithreading APIs can be used.])
Packit Service 4684c1
          LIBTHREAD= LTLIBTHREAD=
Packit Service 4684c1
        else
Packit Service 4684c1
          gl_threads_api=posix
Packit Service 4684c1
          AC_DEFINE([USE_POSIX_THREADS], [1],
Packit Service 4684c1
            [Define if the POSIX multithreading library can be used.])
Packit Service 4684c1
          if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
Packit Service 4684c1
            if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
Packit Service 4684c1
              AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
Packit Service 4684c1
                [Define if references to the POSIX multithreading library should be made weak.])
Packit Service 4684c1
              LIBTHREAD= LTLIBTHREAD=
Packit Service 4684c1
            fi
Packit Service 4684c1
          fi
Packit Service 4684c1
        fi
Packit Service 4684c1
      fi
Packit Service 4684c1
    fi
Packit Service 4684c1
    if test $gl_threads_api = none; then
Packit Service 4684c1
      if test "$gl_use_threads" = isoc && test "$gl_have_isoc_threads" = yes; then
Packit Service 4684c1
        gl_STDTHREADLIB_BODY
Packit Service 4684c1
        LIBTHREAD=$LIBSTDTHREAD LTLIBTHREAD=$LIBSTDTHREAD
Packit Service 4684c1
        LIBMULTITHREAD=$LIBSTDTHREAD LTLIBMULTITHREAD=$LIBSTDTHREAD
Packit Service 4684c1
        gl_threads_api=isoc
Packit Service 4684c1
        AC_DEFINE([USE_ISOC_THREADS], [1],
Packit Service 4684c1
          [Define if the ISO C multithreading library can be used.])
Packit Service 4684c1
      fi
Packit Service 4684c1
    fi
Packit Service 4684c1
    if test $gl_threads_api = none; then
Packit Service 4684c1
      case "$gl_use_threads" in
Packit Service 4684c1
        yes | windows | win32) # The 'win32' is for backward compatibility.
Packit Service 4684c1
          if { case "$host_os" in
Packit Service 4684c1
                 mingw*) true;;
Packit Service 4684c1
                 *) false;;
Packit Service 4684c1
               esac
Packit Service 4684c1
             }; then
Packit Service 4684c1
            gl_threads_api=windows
Packit Service 4684c1
            AC_DEFINE([USE_WINDOWS_THREADS], [1],
Packit Service 4684c1
              [Define if the native Windows multithreading API can be used.])
Packit Service 4684c1
          fi
Packit Service 4684c1
          ;;
Packit Service 4684c1
      esac
Packit Service 4684c1
    fi
Packit Service 4684c1
  fi
Packit Service 4684c1
  AC_MSG_CHECKING([for multithread API to use])
Packit Service 4684c1
  AC_MSG_RESULT([$gl_threads_api])
Packit Service 4684c1
  AC_SUBST([LIBTHREAD])
Packit Service 4684c1
  AC_SUBST([LTLIBTHREAD])
Packit Service 4684c1
  AC_SUBST([LIBMULTITHREAD])
Packit Service 4684c1
  AC_SUBST([LTLIBMULTITHREAD])
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_THREADLIB],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([gl_THREADLIB_EARLY])
Packit Service 4684c1
  AC_REQUIRE([gl_THREADLIB_BODY])
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
dnl gl_DISABLE_THREADS
Packit Service 4684c1
dnl ------------------
Packit Service 4684c1
dnl Sets the gl_THREADLIB default so that threads are not used by default.
Packit Service 4684c1
dnl The user can still override it at installation time, by using the
Packit Service 4684c1
dnl configure option '--enable-threads'.
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_DISABLE_THREADS], [
Packit Service 4684c1
  m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
dnl gl_AVOID_WINPTHREAD
Packit Service 4684c1
dnl -------------------
Packit Service 4684c1
dnl Sets the gl_THREADLIB default so that on mingw, a dependency to the
Packit Service 4684c1
dnl libwinpthread DLL (mingw-w64 winpthreads library) is avoided.
Packit Service 4684c1
dnl The user can still override it at installation time, by using the
Packit Service 4684c1
dnl configure option '--enable-threads'.
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_AVOID_WINPTHREAD], [
Packit Service 4684c1
  m4_divert_text([INIT_PREPARE], [gl_use_winpthreads_default=no])
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
dnl ============================================================================
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
dnl Survey of platforms:
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl Platform           Available  Compiler    Supports   test-lock
Packit Service 4684c1
dnl                    flavours   option      weak       result
Packit Service 4684c1
dnl ---------------    ---------  ---------   --------   ---------
Packit Service 4684c1
dnl Linux 2.4/glibc    posix      -lpthread       Y      OK
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl GNU Hurd/glibc     posix
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl Ubuntu 14.04       posix      -pthread        Y      OK
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl FreeBSD 5.3        posix      -lc_r           Y
Packit Service 4684c1
dnl                    posix      -lkse ?         Y
Packit Service 4684c1
dnl                    posix      -lpthread ?     Y
Packit Service 4684c1
dnl                    posix      -lthr           Y
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl FreeBSD 5.2        posix      -lc_r           Y
Packit Service 4684c1
dnl                    posix      -lkse           Y
Packit Service 4684c1
dnl                    posix      -lthr           Y
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl FreeBSD 4.0,4.10   posix      -lc_r           Y      OK
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl NetBSD 1.6         --
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl OpenBSD 3.4        posix      -lpthread       Y      OK
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl Mac OS X 10.[123]  posix      -lpthread       Y      OK
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl Solaris 7,8,9      posix      -lpthread       Y      Sol 7,8: 0.0; Sol 9: OK
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl HP-UX 11           posix      -lpthread       N (cc) OK
Packit Service 4684c1
dnl                                               Y (gcc)
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl IRIX 6.5           posix      -lpthread       Y      0.5
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl AIX 4.3,5.1        posix      -lpthread       N      AIX 4: 0.5; AIX 5: OK
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl OSF/1 4.0,5.1      posix      -pthread (cc)   N      OK
Packit Service 4684c1
dnl                               -lpthread (gcc) Y
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl Cygwin             posix      -lpthread       Y      OK
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl Mingw              windows                    N      OK
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl BeOS 5             --
Packit Service 4684c1
dnl
Packit Service 4684c1
dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
Packit Service 4684c1
dnl turned off:
Packit Service 4684c1
dnl   OK if all three tests terminate OK,
Packit Service 4684c1
dnl   0.5 if the first test terminates OK but the second one loops endlessly,
Packit Service 4684c1
dnl   0.0 if the first test already loops endlessly.