annotate m4/m4_ax_pthread.m4 @ 1614:efe1a2e9e4de

Support 'safeGet' in demos
author Adam Chlipala <adam@chlipala.net>
date Fri, 25 Nov 2011 17:19:34 -0500
parents 7770ef82c463
children
rev   line source
adam@1528 1 # ===========================================================================
adam@1528 2 # http://www.gnu.org/software/autoconf-archive/ax_pthread.html
adam@1528 3 # ===========================================================================
adam@1528 4 #
adam@1528 5 # SYNOPSIS
adam@1528 6 #
adam@1528 7 # AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
adam@1528 8 #
adam@1528 9 # DESCRIPTION
adam@1528 10 #
adam@1528 11 # This macro figures out how to build C programs using POSIX threads. It
adam@1528 12 # sets the PTHREAD_LIBS output variable to the threads library and linker
adam@1528 13 # flags, and the PTHREAD_CFLAGS output variable to any special C compiler
adam@1528 14 # flags that are needed. (The user can also force certain compiler
adam@1528 15 # flags/libs to be tested by setting these environment variables.)
adam@1528 16 #
adam@1528 17 # Also sets PTHREAD_CC to any special C compiler that is needed for
adam@1528 18 # multi-threaded programs (defaults to the value of CC otherwise). (This
adam@1528 19 # is necessary on AIX to use the special cc_r compiler alias.)
adam@1528 20 #
adam@1528 21 # NOTE: You are assumed to not only compile your program with these flags,
adam@1528 22 # but also link it with them as well. e.g. you should link with
adam@1528 23 # $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
adam@1528 24 #
adam@1528 25 # If you are only building threads programs, you may wish to use these
adam@1528 26 # variables in your default LIBS, CFLAGS, and CC:
adam@1528 27 #
adam@1528 28 # LIBS="$PTHREAD_LIBS $LIBS"
adam@1528 29 # CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
adam@1528 30 # CC="$PTHREAD_CC"
adam@1528 31 #
adam@1528 32 # In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant
adam@1528 33 # has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to that name
adam@1528 34 # (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
adam@1528 35 #
adam@1528 36 # Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the
adam@1528 37 # PTHREAD_PRIO_INHERIT symbol is defined when compiling with
adam@1528 38 # PTHREAD_CFLAGS.
adam@1528 39 #
adam@1528 40 # ACTION-IF-FOUND is a list of shell commands to run if a threads library
adam@1528 41 # is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it
adam@1528 42 # is not found. If ACTION-IF-FOUND is not specified, the default action
adam@1528 43 # will define HAVE_PTHREAD.
adam@1528 44 #
adam@1528 45 # Please let the authors know if this macro fails on any platform, or if
adam@1528 46 # you have any other suggestions or comments. This macro was based on work
adam@1528 47 # by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help
adam@1528 48 # from M. Frigo), as well as ac_pthread and hb_pthread macros posted by
adam@1528 49 # Alejandro Forero Cuervo to the autoconf macro repository. We are also
adam@1528 50 # grateful for the helpful feedback of numerous users.
adam@1528 51 #
adam@1528 52 # Updated for Autoconf 2.68 by Daniel Richard G.
adam@1528 53 #
adam@1528 54 # LICENSE
adam@1528 55 #
adam@1528 56 # Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
adam@1528 57 # Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG>
adam@1528 58 #
adam@1528 59 # This program is free software: you can redistribute it and/or modify it
adam@1528 60 # under the terms of the GNU General Public License as published by the
adam@1528 61 # Free Software Foundation, either version 3 of the License, or (at your
adam@1528 62 # option) any later version.
adam@1528 63 #
adam@1528 64 # This program is distributed in the hope that it will be useful, but
adam@1528 65 # WITHOUT ANY WARRANTY; without even the implied warranty of
adam@1528 66 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
adam@1528 67 # Public License for more details.
adam@1528 68 #
adam@1528 69 # You should have received a copy of the GNU General Public License along
adam@1528 70 # with this program. If not, see <http://www.gnu.org/licenses/>.
adam@1528 71 #
adam@1528 72 # As a special exception, the respective Autoconf Macro's copyright owner
adam@1528 73 # gives unlimited permission to copy, distribute and modify the configure
adam@1528 74 # scripts that are the output of Autoconf when processing the Macro. You
adam@1528 75 # need not follow the terms of the GNU General Public License when using
adam@1528 76 # or distributing such scripts, even though portions of the text of the
adam@1528 77 # Macro appear in them. The GNU General Public License (GPL) does govern
adam@1528 78 # all other use of the material that constitutes the Autoconf Macro.
adam@1528 79 #
adam@1528 80 # This special exception to the GPL applies to versions of the Autoconf
adam@1528 81 # Macro released by the Autoconf Archive. When you make and distribute a
adam@1528 82 # modified version of the Autoconf Macro, you may extend this special
adam@1528 83 # exception to the GPL to apply to your modified version as well.
adam@1528 84
adam@1528 85 #serial 16
adam@1528 86
adam@1528 87 AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
adam@1528 88 AC_DEFUN([AX_PTHREAD], [
adam@1528 89 AC_REQUIRE([AC_CANONICAL_HOST])
adam@1528 90 AC_LANG_PUSH([C])
adam@1528 91 ax_pthread_ok=no
adam@1528 92
adam@1528 93 # We used to check for pthread.h first, but this fails if pthread.h
adam@1528 94 # requires special compiler flags (e.g. on True64 or Sequent).
adam@1528 95 # It gets checked for in the link test anyway.
adam@1528 96
adam@1528 97 # First of all, check if the user has set any of the PTHREAD_LIBS,
adam@1528 98 # etcetera environment variables, and if threads linking works using
adam@1528 99 # them:
adam@1528 100 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
adam@1528 101 save_CFLAGS="$CFLAGS"
adam@1528 102 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
adam@1528 103 save_LIBS="$LIBS"
adam@1528 104 LIBS="$PTHREAD_LIBS $LIBS"
adam@1528 105 AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
adam@1528 106 AC_TRY_LINK_FUNC(pthread_join, ax_pthread_ok=yes)
adam@1528 107 AC_MSG_RESULT($ax_pthread_ok)
adam@1528 108 if test x"$ax_pthread_ok" = xno; then
adam@1528 109 PTHREAD_LIBS=""
adam@1528 110 PTHREAD_CFLAGS=""
adam@1528 111 fi
adam@1528 112 LIBS="$save_LIBS"
adam@1528 113 CFLAGS="$save_CFLAGS"
adam@1528 114 fi
adam@1528 115
adam@1528 116 # We must check for the threads library under a number of different
adam@1528 117 # names; the ordering is very important because some systems
adam@1528 118 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
adam@1528 119 # libraries is broken (non-POSIX).
adam@1528 120
adam@1528 121 # Create a list of thread flags to try. Items starting with a "-" are
adam@1528 122 # C compiler flags, and other items are library names, except for "none"
adam@1528 123 # which indicates that we try without any flags at all, and "pthread-config"
adam@1528 124 # which is a program returning the flags for the Pth emulation library.
adam@1528 125
adam@1528 126 ax_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
adam@1528 127
adam@1528 128 # The ordering *is* (sometimes) important. Some notes on the
adam@1528 129 # individual items follow:
adam@1528 130
adam@1528 131 # pthreads: AIX (must check this before -lpthread)
adam@1528 132 # none: in case threads are in libc; should be tried before -Kthread and
adam@1528 133 # other compiler flags to prevent continual compiler warnings
adam@1528 134 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
adam@1528 135 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
adam@1528 136 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
adam@1528 137 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
adam@1528 138 # -pthreads: Solaris/gcc
adam@1528 139 # -mthreads: Mingw32/gcc, Lynx/gcc
adam@1528 140 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
adam@1528 141 # doesn't hurt to check since this sometimes defines pthreads too;
adam@1528 142 # also defines -D_REENTRANT)
adam@1528 143 # ... -mt is also the pthreads flag for HP/aCC
adam@1528 144 # pthread: Linux, etcetera
adam@1528 145 # --thread-safe: KAI C++
adam@1528 146 # pthread-config: use pthread-config program (for GNU Pth library)
adam@1528 147
adam@1528 148 case "${host_cpu}-${host_os}" in
adam@1528 149 *solaris*)
adam@1528 150
adam@1528 151 # On Solaris (at least, for some versions), libc contains stubbed
adam@1528 152 # (non-functional) versions of the pthreads routines, so link-based
adam@1528 153 # tests will erroneously succeed. (We need to link with -pthreads/-mt/
adam@1528 154 # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
adam@1528 155 # a function called by this macro, so we could check for that, but
adam@1528 156 # who knows whether they'll stub that too in a future libc.) So,
adam@1528 157 # we'll just look for -pthreads and -lpthread first:
adam@1528 158
adam@1528 159 ax_pthread_flags="-pthreads pthread -mt -pthread $ax_pthread_flags"
adam@1528 160 ;;
adam@1528 161
adam@1528 162 *-darwin*)
adam@1528 163 ax_pthread_flags="-pthread $ax_pthread_flags"
adam@1528 164 ;;
adam@1528 165 esac
adam@1528 166
adam@1528 167 if test x"$ax_pthread_ok" = xno; then
adam@1528 168 for flag in $ax_pthread_flags; do
adam@1528 169
adam@1528 170 case $flag in
adam@1528 171 none)
adam@1528 172 AC_MSG_CHECKING([whether pthreads work without any flags])
adam@1528 173 ;;
adam@1528 174
adam@1528 175 -*)
adam@1528 176 AC_MSG_CHECKING([whether pthreads work with $flag])
adam@1528 177 PTHREAD_CFLAGS="$flag"
adam@1528 178 ;;
adam@1528 179
adam@1528 180 pthread-config)
adam@1528 181 AC_CHECK_PROG(ax_pthread_config, pthread-config, yes, no)
adam@1528 182 if test x"$ax_pthread_config" = xno; then continue; fi
adam@1528 183 PTHREAD_CFLAGS="`pthread-config --cflags`"
adam@1528 184 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
adam@1528 185 ;;
adam@1528 186
adam@1528 187 *)
adam@1528 188 AC_MSG_CHECKING([for the pthreads library -l$flag])
adam@1528 189 PTHREAD_LIBS="-l$flag"
adam@1528 190 ;;
adam@1528 191 esac
adam@1528 192
adam@1528 193 save_LIBS="$LIBS"
adam@1528 194 save_CFLAGS="$CFLAGS"
adam@1528 195 LIBS="$PTHREAD_LIBS $LIBS"
adam@1528 196 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
adam@1528 197
adam@1528 198 # Check for various functions. We must include pthread.h,
adam@1528 199 # since some functions may be macros. (On the Sequent, we
adam@1528 200 # need a special flag -Kthread to make this header compile.)
adam@1528 201 # We check for pthread_join because it is in -lpthread on IRIX
adam@1528 202 # while pthread_create is in libc. We check for pthread_attr_init
adam@1528 203 # due to DEC craziness with -lpthreads. We check for
adam@1528 204 # pthread_cleanup_push because it is one of the few pthread
adam@1528 205 # functions on Solaris that doesn't have a non-functional libc stub.
adam@1528 206 # We try pthread_create on general principles.
adam@1528 207 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>
adam@1528 208 static void routine(void *a) { a = 0; }
adam@1528 209 static void *start_routine(void *a) { return a; }],
adam@1528 210 [pthread_t th; pthread_attr_t attr;
adam@1528 211 pthread_create(&th, 0, start_routine, 0);
adam@1528 212 pthread_join(th, 0);
adam@1528 213 pthread_attr_init(&attr);
adam@1528 214 pthread_cleanup_push(routine, 0);
adam@1528 215 pthread_cleanup_pop(0) /* ; */])],
adam@1528 216 [ax_pthread_ok=yes],
adam@1528 217 [])
adam@1528 218
adam@1528 219 LIBS="$save_LIBS"
adam@1528 220 CFLAGS="$save_CFLAGS"
adam@1528 221
adam@1528 222 AC_MSG_RESULT($ax_pthread_ok)
adam@1528 223 if test "x$ax_pthread_ok" = xyes; then
adam@1528 224 break;
adam@1528 225 fi
adam@1528 226
adam@1528 227 PTHREAD_LIBS=""
adam@1528 228 PTHREAD_CFLAGS=""
adam@1528 229 done
adam@1528 230 fi
adam@1528 231
adam@1528 232 # Various other checks:
adam@1528 233 if test "x$ax_pthread_ok" = xyes; then
adam@1528 234 save_LIBS="$LIBS"
adam@1528 235 LIBS="$PTHREAD_LIBS $LIBS"
adam@1528 236 save_CFLAGS="$CFLAGS"
adam@1528 237 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
adam@1528 238
adam@1528 239 # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
adam@1528 240 AC_MSG_CHECKING([for joinable pthread attribute])
adam@1528 241 attr_name=unknown
adam@1528 242 for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
adam@1528 243 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],
adam@1528 244 [int attr = $attr; return attr /* ; */])],
adam@1528 245 [attr_name=$attr; break],
adam@1528 246 [])
adam@1528 247 done
adam@1528 248 AC_MSG_RESULT($attr_name)
adam@1528 249 if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
adam@1528 250 AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
adam@1528 251 [Define to necessary symbol if this constant
adam@1528 252 uses a non-standard name on your system.])
adam@1528 253 fi
adam@1528 254
adam@1528 255 AC_MSG_CHECKING([if more special flags are required for pthreads])
adam@1528 256 flag=no
adam@1528 257 case "${host_cpu}-${host_os}" in
adam@1528 258 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
adam@1528 259 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
adam@1528 260 esac
adam@1528 261 AC_MSG_RESULT(${flag})
adam@1528 262 if test "x$flag" != xno; then
adam@1528 263 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
adam@1528 264 fi
adam@1528 265
adam@1528 266 AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],
adam@1528 267 ax_cv_PTHREAD_PRIO_INHERIT, [
adam@1528 268 AC_LINK_IFELSE([
adam@1528 269 AC_LANG_PROGRAM([[#include <pthread.h>]], [[int i = PTHREAD_PRIO_INHERIT;]])],
adam@1528 270 [ax_cv_PTHREAD_PRIO_INHERIT=yes],
adam@1528 271 [ax_cv_PTHREAD_PRIO_INHERIT=no])
adam@1528 272 ])
adam@1528 273 AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes"],
adam@1528 274 AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], 1, [Have PTHREAD_PRIO_INHERIT.]))
adam@1528 275
adam@1528 276 LIBS="$save_LIBS"
adam@1528 277 CFLAGS="$save_CFLAGS"
adam@1528 278
adam@1528 279 # More AIX lossage: must compile with xlc_r or cc_r
adam@1528 280 if test x"$GCC" != xyes; then
adam@1528 281 AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
adam@1528 282 else
adam@1528 283 PTHREAD_CC=$CC
adam@1528 284 fi
adam@1528 285 else
adam@1528 286 PTHREAD_CC="$CC"
adam@1528 287 fi
adam@1528 288
adam@1528 289 AC_SUBST(PTHREAD_LIBS)
adam@1528 290 AC_SUBST(PTHREAD_CFLAGS)
adam@1528 291 AC_SUBST(PTHREAD_CC)
adam@1528 292
adam@1528 293 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
adam@1528 294 if test x"$ax_pthread_ok" = xyes; then
adam@1528 295 ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
adam@1528 296 :
adam@1528 297 else
adam@1528 298 ax_pthread_ok=no
adam@1528 299 $2
adam@1528 300 fi
adam@1528 301 AC_LANG_POP
adam@1528 302 ])dnl AX_PTHREAD