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