1dnl Available from the GNU Autoconf Macro Archive at: 2dnl http://www.gnu.org/software/ac-archive/htmldoc/acx_pthread.html 3dnl 4AC_DEFUN([ACX_PTHREAD], [ 5AC_REQUIRE([AC_CANONICAL_HOST]) 6AC_LANG_SAVE 7AC_LANG_C 8acx_pthread_ok=no 9 10# We used to check for pthread.h first, but this fails if pthread.h 11# requires special compiler flags (e.g. on True64 or Sequent). 12# It gets checked for in the link test anyway. 13 14# First of all, check if the user has set any of the PTHREAD_LIBS, 15# etcetera environment variables, and if threads linking works using 16# them: 17if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then 18 save_CFLAGS="$CFLAGS" 19 CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 20 save_LIBS="$LIBS" 21 LIBS="$PTHREAD_LIBS $LIBS" 22 AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS]) 23 AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes) 24 AC_MSG_RESULT($acx_pthread_ok) 25 if test x"$acx_pthread_ok" = xno; then 26 PTHREAD_LIBS="" 27 PTHREAD_CFLAGS="" 28 fi 29 LIBS="$save_LIBS" 30 CFLAGS="$save_CFLAGS" 31fi 32 33# We must check for the threads library under a number of different 34# names; the ordering is very important because some systems 35# (e.g. DEC) have both -lpthread and -lpthreads, where one of the 36# libraries is broken (non-POSIX). 37 38# Create a list of thread flags to try. Items starting with a "-" are 39# C compiler flags, and other items are library names, except for "none" 40# which indicates that we try without any flags at all. 41 42acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt" 43 44# The ordering *is* (sometimes) important. Some notes on the 45# individual items follow: 46 47# pthreads: AIX (must check this before -lpthread) 48# none: in case threads are in libc; should be tried before -Kthread and 49# other compiler flags to prevent continual compiler warnings 50# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) 51# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) 52# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) 53# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads) 54# -pthreads: Solaris/gcc 55# -mthreads: Mingw32/gcc, Lynx/gcc 56# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it 57# doesn't hurt to check since this sometimes defines pthreads too; 58# also defines -D_REENTRANT) 59# pthread: Linux, etcetera 60# --thread-safe: KAI C++ 61 62case "${host_cpu}-${host_os}" in 63 *solaris*) 64 65 # On Solaris (at least, for some versions), libc contains stubbed 66 # (non-functional) versions of the pthreads routines, so link-based 67 # tests will erroneously succeed. (We need to link with -pthread or 68 # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather 69 # a function called by this macro, so we could check for that, but 70 # who knows whether they'll stub that too in a future libc.) So, 71 # we'll just look for -pthreads and -lpthread first: 72 73 acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags" 74 ;; 75esac 76 77if test x"$acx_pthread_ok" = xno; then 78for flag in $acx_pthread_flags; do 79 80 case $flag in 81 none) 82 AC_MSG_CHECKING([whether pthreads work without any flags]) 83 ;; 84 85 -*) 86 AC_MSG_CHECKING([whether pthreads work with $flag]) 87 PTHREAD_CFLAGS="$flag" 88 ;; 89 90 *) 91 AC_MSG_CHECKING([for the pthreads library -l$flag]) 92 PTHREAD_LIBS="-l$flag" 93 ;; 94 esac 95 96 save_LIBS="$LIBS" 97 save_CFLAGS="$CFLAGS" 98 LIBS="$PTHREAD_LIBS $LIBS" 99 CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 100 101 # Check for various functions. We must include pthread.h, 102 # since some functions may be macros. (On the Sequent, we 103 # need a special flag -Kthread to make this header compile.) 104 # We check for pthread_join because it is in -lpthread on IRIX 105 # while pthread_create is in libc. We check for pthread_attr_init 106 # due to DEC craziness with -lpthreads. We check for 107 # pthread_cleanup_push because it is one of the few pthread 108 # functions on Solaris that doesn't have a non-functional libc stub. 109 # We try pthread_create on general principles. 110 AC_TRY_LINK([#include <pthread.h>], 111 [pthread_t th; pthread_join(th, 0); 112 pthread_attr_init(0); pthread_cleanup_push(0, 0); 113 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], 114 [acx_pthread_ok=yes]) 115 116 LIBS="$save_LIBS" 117 CFLAGS="$save_CFLAGS" 118 119 AC_MSG_RESULT($acx_pthread_ok) 120 if test "x$acx_pthread_ok" = xyes; then 121 break; 122 fi 123 124 PTHREAD_LIBS="" 125 PTHREAD_CFLAGS="" 126done 127fi 128 129# Various other checks: 130if test "x$acx_pthread_ok" = xyes; then 131 save_LIBS="$LIBS" 132 LIBS="$PTHREAD_LIBS $LIBS" 133 save_CFLAGS="$CFLAGS" 134 CFLAGS="$CFLAGS $PTHREAD_CFLAGS" 135 136 # Detect AIX lossage: threads are created detached by default 137 # and the JOINABLE attribute has a nonstandard name (UNDETACHED). 138 AC_MSG_CHECKING([for joinable pthread attribute]) 139 AC_TRY_LINK([#include <pthread.h>], 140 [int attr=PTHREAD_CREATE_JOINABLE;], 141 ok=PTHREAD_CREATE_JOINABLE, ok=unknown) 142 if test x"$ok" = xunknown; then 143 AC_TRY_LINK([#include <pthread.h>], 144 [int attr=PTHREAD_CREATE_UNDETACHED;], 145 ok=PTHREAD_CREATE_UNDETACHED, ok=unknown) 146 fi 147 if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then 148 AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok, 149 [Define to the necessary symbol if this constant 150 uses a non-standard name on your system.]) 151 fi 152 AC_MSG_RESULT(${ok}) 153 if test x"$ok" = xunknown; then 154 AC_MSG_WARN([we do not know how to create joinable pthreads]) 155 fi 156 157 AC_MSG_CHECKING([if more special flags are required for pthreads]) 158 flag=no 159 case "${host_cpu}-${host_os}" in 160 *-aix* | *-freebsd*) flag="-D_THREAD_SAFE";; 161 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";; 162 esac 163 AC_MSG_RESULT(${flag}) 164 if test "x$flag" != xno; then 165 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS" 166 fi 167 168 LIBS="$save_LIBS" 169 CFLAGS="$save_CFLAGS" 170 171 # More AIX lossage: must compile with cc_r 172 AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC}) 173else 174 PTHREAD_CC="$CC" 175fi 176 177AC_SUBST(PTHREAD_LIBS) 178AC_SUBST(PTHREAD_CFLAGS) 179AC_SUBST(PTHREAD_CC) 180 181# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: 182if test x"$acx_pthread_ok" = xyes; then 183 ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1]) 184 : 185else 186 acx_pthread_ok=no 187 $2 188fi 189AC_LANG_RESTORE 190])dnl ACX_PTHREAD 191