| 1 |
51 |
bertin |
dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
|
| 2 |
|
|
dnl
|
| 3 |
|
|
dnl This macro figures out how to build C programs using POSIX
|
| 4 |
|
|
dnl threads. It sets the PTHREAD_LIBS output variable to the threads
|
| 5 |
|
|
dnl library and linker flags, and the PTHREAD_CFLAGS output variable
|
| 6 |
|
|
dnl to any special C compiler flags that are needed. (The user can also
|
| 7 |
|
|
dnl force certain compiler flags/libs to be tested by setting these
|
| 8 |
|
|
dnl environment variables.)
|
| 9 |
|
|
dnl
|
| 10 |
|
|
dnl Also sets PTHREAD_CC to any special C compiler that is needed for
|
| 11 |
|
|
dnl multi-threaded programs (defaults to the value of CC otherwise).
|
| 12 |
|
|
dnl (This is necessary on AIX to use the special cc_r compiler alias.)
|
| 13 |
|
|
dnl
|
| 14 |
|
|
dnl If you are only building threads programs, you may wish to
|
| 15 |
|
|
dnl use these variables in your default LIBS, CFLAGS, and CC:
|
| 16 |
|
|
dnl
|
| 17 |
|
|
dnl LIBS="$PTHREAD_LIBS $LIBS"
|
| 18 |
|
|
dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
|
| 19 |
|
|
dnl CC="$PTHREAD_CC"
|
| 20 |
|
|
dnl
|
| 21 |
|
|
dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
|
| 22 |
|
|
dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE
|
| 23 |
|
|
dnl to that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
|
| 24 |
|
|
dnl
|
| 25 |
|
|
dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
|
| 26 |
|
|
dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands
|
| 27 |
|
|
dnl to run it if it is not found. If ACTION-IF-FOUND is not specified,
|
| 28 |
|
|
dnl the default action will define HAVE_PTHREAD.
|
| 29 |
|
|
dnl
|
| 30 |
|
|
dnl Please let the authors know if this macro fails on any platform,
|
| 31 |
|
|
dnl or if you have any other suggestions or comments. This macro was
|
| 32 |
|
|
dnl based on work by SGJ on autoconf scripts for FFTW (www.fftw.org)
|
| 33 |
|
|
dnl (with help from M. Frigo), as well as ac_pthread and hb_pthread
|
| 34 |
|
|
dnl macros posted by AFC to the autoconf macro repository. We are also
|
| 35 |
|
|
dnl grateful for the helpful feedback of numerous users.
|
| 36 |
|
|
dnl
|
| 37 |
|
|
dnl @version $Id: acx_pthread.m4,v 1.4 2001/03/16 08:40:17 simons Exp $
|
| 38 |
|
|
dnl @author Steven G. Johnson <stevenj@alum.mit.edu> and Alejandro Forero Cuervo <bachue@bachue.com>
|
| 39 |
|
|
dnl modified by E.Bertin <bertin@iap.fr> (ordering of flags and deactivated
|
| 40 |
|
|
dnl pthread.h check) 2002/04/25
|
| 41 |
|
|
|
| 42 |
|
|
AC_DEFUN([ACX_PTHREAD], [
|
| 43 |
|
|
AC_REQUIRE([AC_CANONICAL_HOST])
|
| 44 |
|
|
acx_pthread_ok=no
|
| 45 |
|
|
|
| 46 |
|
|
# First, check if the POSIX threads header, pthread.h, is available.
|
| 47 |
|
|
# If it isn't, don't bother looking for the threads libraries.
|
| 48 |
|
|
# AC_CHECK_HEADER(pthread.h, , acx_pthread_ok=noheader)
|
| 49 |
|
|
AC_CHECK_HEADER(pthread.h, , acx_pthread_ok=no)
|
| 50 |
|
|
|
| 51 |
|
|
# We must check for the threads library under a number of different
|
| 52 |
|
|
# names; the ordering is very important because some systems
|
| 53 |
|
|
# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
|
| 54 |
|
|
# libraries is broken (non-POSIX).
|
| 55 |
|
|
|
| 56 |
|
|
# First of all, check if the user has set any of the PTHREAD_LIBS,
|
| 57 |
|
|
# etcetera environment variables, and if threads linking works using
|
| 58 |
|
|
# them:
|
| 59 |
|
|
if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
|
| 60 |
|
|
save_CFLAGS="$CFLAGS"
|
| 61 |
|
|
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
|
| 62 |
|
|
save_LIBS="$LIBS"
|
| 63 |
|
|
LIBS="$PTHREAD_LIBS $LIBS"
|
| 64 |
|
|
AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
|
| 65 |
|
|
AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
|
| 66 |
|
|
AC_MSG_RESULT($acx_pthread_ok)
|
| 67 |
|
|
if test x"$acx_pthread_ok" = xno; then
|
| 68 |
|
|
PTHREAD_LIBS=""
|
| 69 |
|
|
PTHREAD_CFLAGS=""
|
| 70 |
|
|
fi
|
| 71 |
|
|
LIBS="$save_LIBS"
|
| 72 |
|
|
CFLAGS="$save_CFLAGS"
|
| 73 |
|
|
fi
|
| 74 |
|
|
|
| 75 |
|
|
# Create a list of thread flags to try. Items starting with a "-" are
|
| 76 |
|
|
# C compiler flags, and other items are library names, except for "none"
|
| 77 |
|
|
# which indicates that we try without any flags at all.
|
| 78 |
|
|
|
| 79 |
|
|
acx_pthread_flags="pthreads none -Kthread -kthread lthread pthread -pthread -pthreads -mthreads --thread-safe -mt"
|
| 80 |
|
|
|
| 81 |
|
|
# The ordering *is* (sometimes) important. Some notes on the
|
| 82 |
|
|
# individual items follow:
|
| 83 |
|
|
|
| 84 |
|
|
# pthreads: AIX (must check this before -lpthread)
|
| 85 |
|
|
# none: in case threads are in libc; should be tried before -Kthread and
|
| 86 |
|
|
# other compiler flags to prevent continual compiler warnings
|
| 87 |
|
|
# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
|
| 88 |
|
|
# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
|
| 89 |
|
|
# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
|
| 90 |
|
|
# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
|
| 91 |
|
|
# -pthreads: Solaris/gcc
|
| 92 |
|
|
# -mthreads: Mingw32/gcc, Lynx/gcc
|
| 93 |
|
|
# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
|
| 94 |
|
|
# doesn't hurt to check since this sometimes defines pthreads too;
|
| 95 |
|
|
# also defines -D_REENTRANT)
|
| 96 |
|
|
# pthread: Linux, etcetera
|
| 97 |
|
|
# --thread-safe: KAI C++
|
| 98 |
|
|
|
| 99 |
|
|
case "${host_cpu}-${host_os}" in
|
| 100 |
|
|
*solaris*)
|
| 101 |
|
|
|
| 102 |
|
|
# On Solaris (at least, for some versions), libc contains stubbed
|
| 103 |
|
|
# (non-functional) versions of the pthreads routines, so link-based
|
| 104 |
|
|
# tests will erroneously succeed. (We need to link with -pthread or
|
| 105 |
|
|
# -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
|
| 106 |
|
|
# a function called by this macro, so we could check for that, but
|
| 107 |
|
|
# who knows whether they'll stub that too in a future libc.) So,
|
| 108 |
|
|
# we'll just look for -pthreads and -lpthread first:
|
| 109 |
|
|
|
| 110 |
|
|
acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
|
| 111 |
|
|
;;
|
| 112 |
|
|
esac
|
| 113 |
|
|
|
| 114 |
|
|
if test x"$acx_pthread_ok" = xno; then
|
| 115 |
|
|
for flag in $acx_pthread_flags; do
|
| 116 |
|
|
|
| 117 |
|
|
case $flag in
|
| 118 |
|
|
none)
|
| 119 |
|
|
AC_MSG_CHECKING([whether pthreads work without any flags])
|
| 120 |
|
|
;;
|
| 121 |
|
|
|
| 122 |
|
|
-*)
|
| 123 |
|
|
AC_MSG_CHECKING([whether pthreads work with $flag])
|
| 124 |
|
|
PTHREAD_CFLAGS="$flag"
|
| 125 |
|
|
;;
|
| 126 |
|
|
|
| 127 |
|
|
*)
|
| 128 |
|
|
AC_MSG_CHECKING([for the pthreads library -l$flag])
|
| 129 |
|
|
PTHREAD_LIBS="-l$flag"
|
| 130 |
|
|
;;
|
| 131 |
|
|
esac
|
| 132 |
|
|
|
| 133 |
|
|
save_LIBS="$LIBS"
|
| 134 |
|
|
save_CFLAGS="$CFLAGS"
|
| 135 |
|
|
LIBS="$PTHREAD_LIBS $LIBS"
|
| 136 |
|
|
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
|
| 137 |
|
|
|
| 138 |
|
|
# Check for various functions. We must include pthread.h,
|
| 139 |
|
|
# since some functions may be macros. (On the Sequent, we
|
| 140 |
|
|
# need a special flag -Kthread to make this header compile.)
|
| 141 |
|
|
# We check for pthread_join because it is in -lpthread on IRIX
|
| 142 |
|
|
# while pthread_create is in libc. We check for pthread_attr_init
|
| 143 |
|
|
# due to DEC craziness with -lpthreads. We check for
|
| 144 |
|
|
# pthread_cleanup_push because it is one of the few pthread
|
| 145 |
|
|
# functions on Solaris that doesn't have a non-functional libc stub.
|
| 146 |
|
|
# We try pthread_create on general principles.
|
| 147 |
|
|
AC_TRY_LINK([#include <pthread.h>],
|
| 148 |
|
|
[pthread_t th; pthread_join(th, 0);
|
| 149 |
|
|
pthread_attr_init(0); pthread_cleanup_push(0, 0);
|
| 150 |
|
|
pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
|
| 151 |
|
|
[acx_pthread_ok=yes])
|
| 152 |
|
|
|
| 153 |
|
|
LIBS="$save_LIBS"
|
| 154 |
|
|
CFLAGS="$save_CFLAGS"
|
| 155 |
|
|
|
| 156 |
|
|
AC_MSG_RESULT($acx_pthread_ok)
|
| 157 |
|
|
if test "x$acx_pthread_ok" = xyes; then
|
| 158 |
|
|
break;
|
| 159 |
|
|
fi
|
| 160 |
|
|
|
| 161 |
|
|
PTHREAD_LIBS=""
|
| 162 |
|
|
PTHREAD_CFLAGS=""
|
| 163 |
|
|
done
|
| 164 |
|
|
fi
|
| 165 |
|
|
|
| 166 |
|
|
# Various other checks:
|
| 167 |
|
|
if test "x$acx_pthread_ok" = xyes; then
|
| 168 |
|
|
save_LIBS="$LIBS"
|
| 169 |
|
|
LIBS="$PTHREAD_LIBS $LIBS"
|
| 170 |
|
|
save_CFLAGS="$CFLAGS"
|
| 171 |
|
|
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
|
| 172 |
|
|
|
| 173 |
|
|
# Detect AIX lossage: threads are created detached by default
|
| 174 |
|
|
# and the JOINABLE attribute has a nonstandard name (UNDETACHED).
|
| 175 |
|
|
AC_MSG_CHECKING([for joinable pthread attribute])
|
| 176 |
|
|
AC_TRY_LINK([#include <pthread.h>],
|
| 177 |
|
|
[int attr=PTHREAD_CREATE_JOINABLE;],
|
| 178 |
|
|
ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
|
| 179 |
|
|
if test x"$ok" = xunknown; then
|
| 180 |
|
|
AC_TRY_LINK([#include <pthread.h>],
|
| 181 |
|
|
[int attr=PTHREAD_CREATE_UNDETACHED;],
|
| 182 |
|
|
ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
|
| 183 |
|
|
fi
|
| 184 |
|
|
if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
|
| 185 |
|
|
AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
|
| 186 |
|
|
[Define to the necessary symbol if this constant
|
| 187 |
|
|
uses a non-standard name on your system.])
|
| 188 |
|
|
fi
|
| 189 |
|
|
AC_MSG_RESULT(${ok})
|
| 190 |
|
|
if test x"$ok" = xunknown; then
|
| 191 |
|
|
AC_MSG_WARN([we do not know how to create joinable pthreads])
|
| 192 |
|
|
fi
|
| 193 |
|
|
|
| 194 |
|
|
AC_MSG_CHECKING([if more special flags are required for pthreads])
|
| 195 |
|
|
flag=no
|
| 196 |
|
|
case "${host_cpu}-${host_os}" in
|
| 197 |
|
|
*-aix* | *-freebsd*) flag="-D_THREAD_SAFE";;
|
| 198 |
|
|
*solaris* | alpha*-osf*) flag="-D_REENTRANT";;
|
| 199 |
|
|
esac
|
| 200 |
|
|
AC_MSG_RESULT(${flag})
|
| 201 |
|
|
if test "x$flag" != xno; then
|
| 202 |
|
|
PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
|
| 203 |
|
|
fi
|
| 204 |
|
|
|
| 205 |
|
|
LIBS="$save_LIBS"
|
| 206 |
|
|
CFLAGS="$save_CFLAGS"
|
| 207 |
|
|
|
| 208 |
|
|
# More AIX lossage: must compile with cc_r
|
| 209 |
|
|
AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
|
| 210 |
|
|
else
|
| 211 |
|
|
PTHREAD_CC="$CC"
|
| 212 |
|
|
fi
|
| 213 |
|
|
|
| 214 |
|
|
AC_SUBST(PTHREAD_LIBS)
|
| 215 |
|
|
AC_SUBST(PTHREAD_CFLAGS)
|
| 216 |
|
|
AC_SUBST(PTHREAD_CC)
|
| 217 |
|
|
|
| 218 |
|
|
# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
|
| 219 |
|
|
if test x"$acx_pthread_ok" = xyes; then
|
| 220 |
|
|
ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
|
| 221 |
|
|
:
|
| 222 |
|
|
else
|
| 223 |
|
|
acx_pthread_ok=no
|
| 224 |
|
|
$2
|
| 225 |
|
|
fi
|
| 226 |
|
|
|
| 227 |
|
|
])dnl ACX_PTHREAD
|