Blame m4/threads.m4

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