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