adam@1528: # =========================================================================== adam@1528: # http://www.gnu.org/software/autoconf-archive/ax_pthread.html adam@1528: # =========================================================================== adam@1528: # adam@1528: # SYNOPSIS adam@1528: # adam@1528: # AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) adam@1528: # adam@1528: # DESCRIPTION adam@1528: # adam@1528: # This macro figures out how to build C programs using POSIX threads. It adam@1528: # sets the PTHREAD_LIBS output variable to the threads library and linker adam@1528: # flags, and the PTHREAD_CFLAGS output variable to any special C compiler adam@1528: # flags that are needed. (The user can also force certain compiler adam@1528: # flags/libs to be tested by setting these environment variables.) adam@1528: # adam@1528: # Also sets PTHREAD_CC to any special C compiler that is needed for adam@1528: # multi-threaded programs (defaults to the value of CC otherwise). (This adam@1528: # is necessary on AIX to use the special cc_r compiler alias.) adam@1528: # adam@1528: # NOTE: You are assumed to not only compile your program with these flags, adam@1528: # but also link it with them as well. e.g. you should link with adam@1528: # $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS adam@1528: # adam@1528: # If you are only building threads programs, you may wish to use these adam@1528: # variables in your default LIBS, CFLAGS, and CC: adam@1528: # adam@1528: # LIBS="$PTHREAD_LIBS $LIBS" adam@1528: # CFLAGS="$CFLAGS $PTHREAD_CFLAGS" adam@1528: # CC="$PTHREAD_CC" adam@1528: # adam@1528: # In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant adam@1528: # has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to that name adam@1528: # (e.g. PTHREAD_CREATE_UNDETACHED on AIX). adam@1528: # adam@1528: # Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the adam@1528: # PTHREAD_PRIO_INHERIT symbol is defined when compiling with adam@1528: # PTHREAD_CFLAGS. adam@1528: # adam@1528: # ACTION-IF-FOUND is a list of shell commands to run if a threads library adam@1528: # is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it adam@1528: # is not found. If ACTION-IF-FOUND is not specified, the default action adam@1528: # will define HAVE_PTHREAD. adam@1528: # adam@1528: # Please let the authors know if this macro fails on any platform, or if adam@1528: # you have any other suggestions or comments. This macro was based on work adam@1528: # by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help adam@1528: # from M. Frigo), as well as ac_pthread and hb_pthread macros posted by adam@1528: # Alejandro Forero Cuervo to the autoconf macro repository. We are also adam@1528: # grateful for the helpful feedback of numerous users. adam@1528: # adam@1528: # Updated for Autoconf 2.68 by Daniel Richard G. adam@1528: # adam@1528: # LICENSE adam@1528: # adam@1528: # Copyright (c) 2008 Steven G. Johnson adam@1528: # Copyright (c) 2011 Daniel Richard G. adam@1528: # adam@1528: # This program is free software: you can redistribute it and/or modify it adam@1528: # under the terms of the GNU General Public License as published by the adam@1528: # Free Software Foundation, either version 3 of the License, or (at your adam@1528: # option) any later version. adam@1528: # adam@1528: # This program is distributed in the hope that it will be useful, but adam@1528: # WITHOUT ANY WARRANTY; without even the implied warranty of adam@1528: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General adam@1528: # Public License for more details. adam@1528: # adam@1528: # You should have received a copy of the GNU General Public License along adam@1528: # with this program. If not, see . adam@1528: # adam@1528: # As a special exception, the respective Autoconf Macro's copyright owner adam@1528: # gives unlimited permission to copy, distribute and modify the configure adam@1528: # scripts that are the output of Autoconf when processing the Macro. You adam@1528: # need not follow the terms of the GNU General Public License when using adam@1528: # or distributing such scripts, even though portions of the text of the adam@1528: # Macro appear in them. The GNU General Public License (GPL) does govern adam@1528: # all other use of the material that constitutes the Autoconf Macro. adam@1528: # adam@1528: # This special exception to the GPL applies to versions of the Autoconf adam@1528: # Macro released by the Autoconf Archive. When you make and distribute a adam@1528: # modified version of the Autoconf Macro, you may extend this special adam@1528: # exception to the GPL to apply to your modified version as well. adam@1528: adam@1528: #serial 16 adam@1528: adam@1528: AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD]) adam@1528: AC_DEFUN([AX_PTHREAD], [ adam@1528: AC_REQUIRE([AC_CANONICAL_HOST]) adam@1528: AC_LANG_PUSH([C]) adam@1528: ax_pthread_ok=no adam@1528: adam@1528: # We used to check for pthread.h first, but this fails if pthread.h adam@1528: # requires special compiler flags (e.g. on True64 or Sequent). adam@1528: # It gets checked for in the link test anyway. adam@1528: adam@1528: # First of all, check if the user has set any of the PTHREAD_LIBS, adam@1528: # etcetera environment variables, and if threads linking works using adam@1528: # them: adam@1528: if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then adam@1528: save_CFLAGS="$CFLAGS" adam@1528: CFLAGS="$CFLAGS $PTHREAD_CFLAGS" adam@1528: save_LIBS="$LIBS" adam@1528: LIBS="$PTHREAD_LIBS $LIBS" adam@1528: AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS]) adam@1528: AC_TRY_LINK_FUNC(pthread_join, ax_pthread_ok=yes) adam@1528: AC_MSG_RESULT($ax_pthread_ok) adam@1528: if test x"$ax_pthread_ok" = xno; then adam@1528: PTHREAD_LIBS="" adam@1528: PTHREAD_CFLAGS="" adam@1528: fi adam@1528: LIBS="$save_LIBS" adam@1528: CFLAGS="$save_CFLAGS" adam@1528: fi adam@1528: adam@1528: # We must check for the threads library under a number of different adam@1528: # names; the ordering is very important because some systems adam@1528: # (e.g. DEC) have both -lpthread and -lpthreads, where one of the adam@1528: # libraries is broken (non-POSIX). adam@1528: adam@1528: # Create a list of thread flags to try. Items starting with a "-" are adam@1528: # C compiler flags, and other items are library names, except for "none" adam@1528: # which indicates that we try without any flags at all, and "pthread-config" adam@1528: # which is a program returning the flags for the Pth emulation library. adam@1528: adam@1528: ax_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" adam@1528: adam@1528: # The ordering *is* (sometimes) important. Some notes on the adam@1528: # individual items follow: adam@1528: adam@1528: # pthreads: AIX (must check this before -lpthread) adam@1528: # none: in case threads are in libc; should be tried before -Kthread and adam@1528: # other compiler flags to prevent continual compiler warnings adam@1528: # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) adam@1528: # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) adam@1528: # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) adam@1528: # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads) adam@1528: # -pthreads: Solaris/gcc adam@1528: # -mthreads: Mingw32/gcc, Lynx/gcc adam@1528: # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it adam@1528: # doesn't hurt to check since this sometimes defines pthreads too; adam@1528: # also defines -D_REENTRANT) adam@1528: # ... -mt is also the pthreads flag for HP/aCC adam@1528: # pthread: Linux, etcetera adam@1528: # --thread-safe: KAI C++ adam@1528: # pthread-config: use pthread-config program (for GNU Pth library) adam@1528: adam@1528: case "${host_cpu}-${host_os}" in adam@1528: *solaris*) adam@1528: adam@1528: # On Solaris (at least, for some versions), libc contains stubbed adam@1528: # (non-functional) versions of the pthreads routines, so link-based adam@1528: # tests will erroneously succeed. (We need to link with -pthreads/-mt/ adam@1528: # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather adam@1528: # a function called by this macro, so we could check for that, but adam@1528: # who knows whether they'll stub that too in a future libc.) So, adam@1528: # we'll just look for -pthreads and -lpthread first: adam@1528: adam@1528: ax_pthread_flags="-pthreads pthread -mt -pthread $ax_pthread_flags" adam@1528: ;; adam@1528: adam@1528: *-darwin*) adam@1528: ax_pthread_flags="-pthread $ax_pthread_flags" adam@1528: ;; adam@1528: esac adam@1528: adam@1528: if test x"$ax_pthread_ok" = xno; then adam@1528: for flag in $ax_pthread_flags; do adam@1528: adam@1528: case $flag in adam@1528: none) adam@1528: AC_MSG_CHECKING([whether pthreads work without any flags]) adam@1528: ;; adam@1528: adam@1528: -*) adam@1528: AC_MSG_CHECKING([whether pthreads work with $flag]) adam@1528: PTHREAD_CFLAGS="$flag" adam@1528: ;; adam@1528: adam@1528: pthread-config) adam@1528: AC_CHECK_PROG(ax_pthread_config, pthread-config, yes, no) adam@1528: if test x"$ax_pthread_config" = xno; then continue; fi adam@1528: PTHREAD_CFLAGS="`pthread-config --cflags`" adam@1528: PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" adam@1528: ;; adam@1528: adam@1528: *) adam@1528: AC_MSG_CHECKING([for the pthreads library -l$flag]) adam@1528: PTHREAD_LIBS="-l$flag" adam@1528: ;; adam@1528: esac adam@1528: adam@1528: save_LIBS="$LIBS" adam@1528: save_CFLAGS="$CFLAGS" adam@1528: LIBS="$PTHREAD_LIBS $LIBS" adam@1528: CFLAGS="$CFLAGS $PTHREAD_CFLAGS" adam@1528: adam@1528: # Check for various functions. We must include pthread.h, adam@1528: # since some functions may be macros. (On the Sequent, we adam@1528: # need a special flag -Kthread to make this header compile.) adam@1528: # We check for pthread_join because it is in -lpthread on IRIX adam@1528: # while pthread_create is in libc. We check for pthread_attr_init adam@1528: # due to DEC craziness with -lpthreads. We check for adam@1528: # pthread_cleanup_push because it is one of the few pthread adam@1528: # functions on Solaris that doesn't have a non-functional libc stub. adam@1528: # We try pthread_create on general principles. adam@1528: AC_LINK_IFELSE([AC_LANG_PROGRAM([#include adam@1528: static void routine(void *a) { a = 0; } adam@1528: static void *start_routine(void *a) { return a; }], adam@1528: [pthread_t th; pthread_attr_t attr; adam@1528: pthread_create(&th, 0, start_routine, 0); adam@1528: pthread_join(th, 0); adam@1528: pthread_attr_init(&attr); adam@1528: pthread_cleanup_push(routine, 0); adam@1528: pthread_cleanup_pop(0) /* ; */])], adam@1528: [ax_pthread_ok=yes], adam@1528: []) adam@1528: adam@1528: LIBS="$save_LIBS" adam@1528: CFLAGS="$save_CFLAGS" adam@1528: adam@1528: AC_MSG_RESULT($ax_pthread_ok) adam@1528: if test "x$ax_pthread_ok" = xyes; then adam@1528: break; adam@1528: fi adam@1528: adam@1528: PTHREAD_LIBS="" adam@1528: PTHREAD_CFLAGS="" adam@1528: done adam@1528: fi adam@1528: adam@1528: # Various other checks: adam@1528: if test "x$ax_pthread_ok" = xyes; then adam@1528: save_LIBS="$LIBS" adam@1528: LIBS="$PTHREAD_LIBS $LIBS" adam@1528: save_CFLAGS="$CFLAGS" adam@1528: CFLAGS="$CFLAGS $PTHREAD_CFLAGS" adam@1528: adam@1528: # Detect AIX lossage: JOINABLE attribute is called UNDETACHED. adam@1528: AC_MSG_CHECKING([for joinable pthread attribute]) adam@1528: attr_name=unknown adam@1528: for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do adam@1528: AC_LINK_IFELSE([AC_LANG_PROGRAM([#include ], adam@1528: [int attr = $attr; return attr /* ; */])], adam@1528: [attr_name=$attr; break], adam@1528: []) adam@1528: done adam@1528: AC_MSG_RESULT($attr_name) adam@1528: if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then adam@1528: AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name, adam@1528: [Define to necessary symbol if this constant adam@1528: uses a non-standard name on your system.]) adam@1528: fi adam@1528: adam@1528: AC_MSG_CHECKING([if more special flags are required for pthreads]) adam@1528: flag=no adam@1528: case "${host_cpu}-${host_os}" in adam@1528: *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";; adam@1528: *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";; adam@1528: esac adam@1528: AC_MSG_RESULT(${flag}) adam@1528: if test "x$flag" != xno; then adam@1528: PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS" adam@1528: fi adam@1528: adam@1528: AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT], adam@1528: ax_cv_PTHREAD_PRIO_INHERIT, [ adam@1528: AC_LINK_IFELSE([ adam@1528: AC_LANG_PROGRAM([[#include ]], [[int i = PTHREAD_PRIO_INHERIT;]])], adam@1528: [ax_cv_PTHREAD_PRIO_INHERIT=yes], adam@1528: [ax_cv_PTHREAD_PRIO_INHERIT=no]) adam@1528: ]) adam@1528: AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes"], adam@1528: AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], 1, [Have PTHREAD_PRIO_INHERIT.])) adam@1528: adam@1528: LIBS="$save_LIBS" adam@1528: CFLAGS="$save_CFLAGS" adam@1528: adam@1528: # More AIX lossage: must compile with xlc_r or cc_r adam@1528: if test x"$GCC" != xyes; then adam@1528: AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC}) adam@1528: else adam@1528: PTHREAD_CC=$CC adam@1528: fi adam@1528: else adam@1528: PTHREAD_CC="$CC" adam@1528: fi adam@1528: adam@1528: AC_SUBST(PTHREAD_LIBS) adam@1528: AC_SUBST(PTHREAD_CFLAGS) adam@1528: AC_SUBST(PTHREAD_CC) adam@1528: adam@1528: # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: adam@1528: if test x"$ax_pthread_ok" = xyes; then adam@1528: ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1]) adam@1528: : adam@1528: else adam@1528: ax_pthread_ok=no adam@1528: $2 adam@1528: fi adam@1528: AC_LANG_POP adam@1528: ])dnl AX_PTHREAD