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