Blame m4/threads.m4

Packit 8dc392
Packit 8dc392
dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
Packit 8dc392
dnl
Packit 8dc392
dnl Modified by Wojciech Jarosz (2005) to include check for POSIX
Packit 8dc392
dnl semaphore usability. Defines HAVE_POSIX_SEMAPHORES if found.
Packit 8dc392
dnl
Packit 8dc392
dnl This macro figures out how to build C programs using POSIX threads.
Packit 8dc392
dnl It sets the PTHREAD_LIBS output variable to the threads library and
Packit 8dc392
dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
Packit 8dc392
dnl C compiler flags that are needed. (The user can also force certain
Packit 8dc392
dnl compiler flags/libs to be tested by setting these environment
Packit 8dc392
dnl variables.)
Packit 8dc392
dnl
Packit 8dc392
dnl Also sets PTHREAD_CC to any special C compiler that is needed for
Packit 8dc392
dnl multi-threaded programs (defaults to the value of CC otherwise).
Packit 8dc392
dnl (This is necessary on AIX to use the special cc_r compiler alias.)
Packit 8dc392
dnl
Packit 8dc392
dnl NOTE: You are assumed to not only compile your program with these
Packit 8dc392
dnl flags, but also link it with them as well. e.g. you should link
Packit 8dc392
dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
Packit 8dc392
dnl $LIBS
Packit 8dc392
dnl
Packit 8dc392
dnl If you are only building threads programs, you may wish to use
Packit 8dc392
dnl these variables in your default LIBS, CFLAGS, and CC:
Packit 8dc392
dnl
Packit 8dc392
dnl        LIBS="$PTHREAD_LIBS $LIBS"
Packit 8dc392
dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
Packit 8dc392
dnl        CC="$PTHREAD_CC"
Packit 8dc392
dnl
Packit 8dc392
dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
Packit 8dc392
dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
Packit 8dc392
dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
Packit 8dc392
dnl
Packit 8dc392
dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
Packit 8dc392
dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
Packit 8dc392
dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
Packit 8dc392
dnl default action will define HAVE_PTHREAD.
Packit 8dc392
dnl
Packit 8dc392
dnl Please let the authors know if this macro fails on any platform, or
Packit 8dc392
dnl if you have any other suggestions or comments. This macro was based
Packit 8dc392
dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
Packit 8dc392
dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
Packit 8dc392
dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
Packit 8dc392
dnl We are also grateful for the helpful feedback of numerous users.
Packit 8dc392
dnl
Packit 8dc392
dnl @category InstalledPackages
Packit 8dc392
dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
Packit 8dc392
dnl @version 2005-01-14
Packit 8dc392
dnl @license GPLWithACException
Packit 8dc392
Packit 8dc392
AC_DEFUN([ACX_PTHREAD], [
Packit 8dc392
AC_REQUIRE([AC_CANONICAL_HOST])
Packit 8dc392
AC_LANG_SAVE
Packit 8dc392
AC_LANG_C
Packit 8dc392
acx_pthread_ok=no
Packit 8dc392
Packit 8dc392
# We used to check for pthread.h first, but this fails if pthread.h
Packit 8dc392
# requires special compiler flags (e.g. on True64 or Sequent).
Packit 8dc392
# It gets checked for in the link test anyway.
Packit 8dc392
Packit 8dc392
# First of all, check if the user has set any of the PTHREAD_LIBS,
Packit 8dc392
# etcetera environment variables, and if threads linking works using
Packit 8dc392
# them:
Packit 8dc392
if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
Packit 8dc392
        save_CFLAGS="$CFLAGS"
Packit 8dc392
        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
Packit 8dc392
        save_LIBS="$LIBS"
Packit 8dc392
        LIBS="$PTHREAD_LIBS $LIBS"
Packit 8dc392
        AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
Packit 8dc392
        AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
Packit 8dc392
        AC_MSG_RESULT($acx_pthread_ok)
Packit 8dc392
        if test x"$acx_pthread_ok" = xno; then
Packit 8dc392
                PTHREAD_LIBS=""
Packit 8dc392
                PTHREAD_CFLAGS=""
Packit 8dc392
        fi
Packit 8dc392
        LIBS="$save_LIBS"
Packit 8dc392
        CFLAGS="$save_CFLAGS"
Packit 8dc392
fi
Packit 8dc392
Packit 8dc392
# We must check for the threads library under a number of different
Packit 8dc392
# names; the ordering is very important because some systems
Packit 8dc392
# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
Packit 8dc392
# libraries is broken (non-POSIX).
Packit 8dc392
Packit 8dc392
# Create a list of thread flags to try.  Items starting with a "-" are
Packit 8dc392
# C compiler flags, and other items are library names, except for "none"
Packit 8dc392
# which indicates that we try without any flags at all, and "pthread-config"
Packit 8dc392
# which is a program returning the flags for the Pth emulation library.
Packit 8dc392
Packit 8dc392
acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
Packit 8dc392
Packit 8dc392
# The ordering *is* (sometimes) important.  Some notes on the
Packit 8dc392
# individual items follow:
Packit 8dc392
Packit 8dc392
# pthreads: AIX (must check this before -lpthread)
Packit 8dc392
# none: in case threads are in libc; should be tried before -Kthread and
Packit 8dc392
#       other compiler flags to prevent continual compiler warnings
Packit 8dc392
# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
Packit 8dc392
# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
Packit 8dc392
# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
Packit 8dc392
# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
Packit 8dc392
# -pthreads: Solaris/gcc
Packit 8dc392
# -mthreads: Mingw32/gcc, Lynx/gcc
Packit 8dc392
# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
Packit 8dc392
#      doesn't hurt to check since this sometimes defines pthreads too;
Packit 8dc392
#      also defines -D_REENTRANT)
Packit 8dc392
# pthread: Linux, etcetera
Packit 8dc392
# --thread-safe: KAI C++
Packit 8dc392
# pthread-config: use pthread-config program (for GNU Pth library)
Packit 8dc392
Packit 8dc392
case "${host_cpu}-${host_os}" in
Packit 8dc392
        *solaris*)
Packit 8dc392
Packit 8dc392
        # On Solaris (at least, for some versions), libc contains stubbed
Packit 8dc392
        # (non-functional) versions of the pthreads routines, so link-based
Packit 8dc392
        # tests will erroneously succeed.  (We need to link with -pthread or
Packit 8dc392
        # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
Packit 8dc392
        # a function called by this macro, so we could check for that, but
Packit 8dc392
        # who knows whether they'll stub that too in a future libc.)  So,
Packit 8dc392
        # we'll just look for -pthreads and -lpthread first:
Packit 8dc392
Packit 8dc392
        acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
Packit 8dc392
        ;;
Packit 8dc392
esac
Packit 8dc392
Packit 8dc392
if test x"$acx_pthread_ok" = xno; then
Packit 8dc392
for flag in $acx_pthread_flags; do
Packit 8dc392
Packit 8dc392
        case $flag in
Packit 8dc392
                none)
Packit 8dc392
                AC_MSG_CHECKING([whether pthreads work without any flags])
Packit 8dc392
                ;;
Packit 8dc392
Packit 8dc392
                -*)
Packit 8dc392
                AC_MSG_CHECKING([whether pthreads work with $flag])
Packit 8dc392
                PTHREAD_CFLAGS="$flag"
Packit 8dc392
                PTHREAD_LIBS="$flag"
Packit 8dc392
                ;;
Packit 8dc392
Packit 8dc392
		pthread-config)
Packit 8dc392
		AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
Packit 8dc392
		if test x"$acx_pthread_config" = xno; then continue; fi
Packit 8dc392
		PTHREAD_CFLAGS="`pthread-config --cflags`"
Packit 8dc392
		PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
Packit 8dc392
		;;
Packit 8dc392
Packit 8dc392
                *)
Packit 8dc392
                AC_MSG_CHECKING([for the pthreads library -l$flag])
Packit 8dc392
                PTHREAD_LIBS="-l$flag"
Packit 8dc392
                ;;
Packit 8dc392
        esac
Packit 8dc392
Packit 8dc392
        save_LIBS="$LIBS"
Packit 8dc392
        save_CFLAGS="$CFLAGS"
Packit 8dc392
        LIBS="$PTHREAD_LIBS $LIBS"
Packit 8dc392
        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
Packit 8dc392
Packit 8dc392
        # Check for various functions.  We must include pthread.h,
Packit 8dc392
        # since some functions may be macros.  (On the Sequent, we
Packit 8dc392
        # need a special flag -Kthread to make this header compile.)
Packit 8dc392
        # We check for pthread_join because it is in -lpthread on IRIX
Packit 8dc392
        # while pthread_create is in libc.  We check for pthread_attr_init
Packit 8dc392
        # due to DEC craziness with -lpthreads.  We check for
Packit 8dc392
        # pthread_cleanup_push because it is one of the few pthread
Packit 8dc392
        # functions on Solaris that doesn't have a non-functional libc stub.
Packit 8dc392
        # We try pthread_create on general principles.
Packit 8dc392
        AC_TRY_LINK([#include <pthread.h>],
Packit 8dc392
                    [pthread_t th; pthread_join(th, 0);
Packit 8dc392
                     pthread_attr_init(0); pthread_cleanup_push(0, 0);
Packit 8dc392
                     pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
Packit 8dc392
                    [acx_pthread_ok=yes])
Packit 8dc392
Packit 8dc392
        LIBS="$save_LIBS"
Packit 8dc392
        CFLAGS="$save_CFLAGS"
Packit 8dc392
Packit 8dc392
        AC_MSG_RESULT($acx_pthread_ok)
Packit 8dc392
        if test "x$acx_pthread_ok" = xyes; then
Packit 8dc392
                break;
Packit 8dc392
        fi
Packit 8dc392
Packit 8dc392
        PTHREAD_LIBS=""
Packit 8dc392
        PTHREAD_CFLAGS=""
Packit 8dc392
done
Packit 8dc392
fi
Packit 8dc392
Packit 8dc392
# Various other checks:
Packit 8dc392
if test "x$acx_pthread_ok" = xyes; then
Packit 8dc392
        save_LIBS="$LIBS"
Packit 8dc392
        LIBS="$PTHREAD_LIBS $LIBS"
Packit 8dc392
        save_CFLAGS="$CFLAGS"
Packit 8dc392
        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
Packit 8dc392
Packit 8dc392
        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
Packit 8dc392
	AC_MSG_CHECKING([for joinable pthread attribute])
Packit 8dc392
	attr_name=unknown
Packit 8dc392
	for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
Packit 8dc392
	    AC_TRY_LINK([#include <pthread.h>], [int attr=$attr;],
Packit 8dc392
                        [attr_name=$attr; break])
Packit 8dc392
	done
Packit 8dc392
        AC_MSG_RESULT($attr_name)
Packit 8dc392
        if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
Packit 8dc392
            AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
Packit 8dc392
                               [Define to necessary symbol if this constant
Packit 8dc392
                                uses a non-standard name on your system.])
Packit 8dc392
        fi
Packit 8dc392
Packit 8dc392
        AC_MSG_CHECKING([if more special flags are required for pthreads])
Packit 8dc392
        flag=no
Packit 8dc392
        case "${host_cpu}-${host_os}" in
Packit 8dc392
            *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
Packit 8dc392
            *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
Packit 8dc392
        esac
Packit 8dc392
        AC_MSG_RESULT(${flag})
Packit 8dc392
        if test "x$flag" != xno; then
Packit 8dc392
            PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
Packit 8dc392
        fi
Packit 8dc392
Packit 8dc392
        LIBS="$save_LIBS"
Packit 8dc392
        CFLAGS="$save_CFLAGS"
Packit 8dc392
Packit 8dc392
        # More AIX lossage: must compile with cc_r
Packit 8dc392
        AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
Packit 8dc392
else
Packit 8dc392
        PTHREAD_CC="$CC"
Packit 8dc392
fi
Packit 8dc392
Packit 8dc392
AC_SUBST(PTHREAD_LIBS)
Packit 8dc392
AC_SUBST(PTHREAD_CFLAGS)
Packit 8dc392
AC_SUBST(PTHREAD_CC)
Packit 8dc392
Packit 8dc392
# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
Packit 8dc392
if test x"$acx_pthread_ok" = xyes; then
Packit 8dc392
        ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
Packit 8dc392
        :
Packit 8dc392
else
Packit 8dc392
        acx_pthread_ok=no
Packit 8dc392
        $2
Packit 8dc392
fi
Packit 8dc392
Packit 8dc392
AC_LANG_RESTORE
Packit 8dc392
])dnl ACX_PTHREAD
Packit 8dc392
Packit 8dc392
Packit 8dc392
dnl
Packit 8dc392
dnl Posix Semaphore support
Packit 8dc392
dnl
Packit 8dc392
Packit 8dc392
AC_DEFUN([AM_POSIX_SEM],
Packit 8dc392
[
Packit 8dc392
AC_ARG_ENABLE([posix-sem], AC_HELP_STRING([--disable-posix-sem],
Packit 8dc392
    [do not attempt to use POSIX unnamed semaphores]))
Packit 8dc392
Packit 8dc392
am_posix_sem_ok=no
Packit 8dc392
if test "${enable_posix_sem:-yes}" != "no"; then
Packit 8dc392
    AC_CHECK_HEADERS([semaphore.h], [
Packit 8dc392
	AC_SEARCH_LIBS(sem_init, [posix4 pthread], [
Packit 8dc392
	    AC_MSG_CHECKING([whether to use POSIX unnamed semaphores])
Packit 8dc392
	    AC_RUN_IFELSE([
Packit 8dc392
		AC_LANG_PROGRAM([#include <semaphore.h>], [
Packit 8dc392
		    sem_t mysem;
Packit 8dc392
		    if (sem_init (&mysem, 1, 1) == 0)
Packit 8dc392
		    {
Packit 8dc392
			if (sem_wait (&mysem) == 0)
Packit 8dc392
			{
Packit 8dc392
			    sem_post (&mysem);
Packit 8dc392
			    sem_destroy (&mysem);
Packit 8dc392
			    return 0;
Packit 8dc392
			}
Packit 8dc392
		    }
Packit 8dc392
		    return 1;
Packit 8dc392
		])
Packit 8dc392
		], [
Packit 8dc392
		AC_MSG_RESULT([yes])
Packit 8dc392
		am_posix_sem_ok=yes], [
Packit 8dc392
		AC_MSG_RESULT([no (pshared not usable)])], [
Packit 8dc392
		AC_MSG_RESULT([no (cannot check usability when cross compiling)])])
Packit 8dc392
	])
Packit 8dc392
    ])
Packit 8dc392
fi
Packit 8dc392
Packit 8dc392
# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
Packit 8dc392
if test x"$am_posix_sem_ok" = xyes; then
Packit 8dc392
        ifelse([$1],,AC_DEFINE(HAVE_POSIX_SEMAPHORES),[$1])
Packit 8dc392
        :
Packit 8dc392
else
Packit 8dc392
        am_posix_sem_ok=no
Packit 8dc392
        $2
Packit 8dc392
fi
Packit 8dc392
])
Packit 8dc392
Packit 8dc392