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