1
0

acx_pthread.m4 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
  2. dnl
  3. dnl @summary figure out how to build C programs using POSIX threads
  4. dnl
  5. dnl This macro figures out how to build C programs using POSIX threads.
  6. dnl It sets the PTHREAD_LIBS output variable to the threads library and
  7. dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
  8. dnl C compiler flags that are needed. (The user can also force certain
  9. dnl compiler flags/libs to be tested by setting these environment
  10. dnl variables.)
  11. dnl
  12. dnl Also sets PTHREAD_CC to any special C compiler that is needed for
  13. dnl multi-threaded programs (defaults to the value of CC otherwise).
  14. dnl (This is necessary on AIX to use the special cc_r compiler alias.)
  15. dnl
  16. dnl NOTE: You are assumed to not only compile your program with these
  17. dnl flags, but also link it with them as well. e.g. you should link
  18. dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
  19. dnl $LIBS
  20. dnl
  21. dnl If you are only building threads programs, you may wish to use
  22. dnl these variables in your default LIBS, CFLAGS, and CC:
  23. dnl
  24. dnl LIBS="$PTHREAD_LIBS $LIBS"
  25. dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  26. dnl CC="$PTHREAD_CC"
  27. dnl
  28. dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
  29. dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
  30. dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
  31. dnl
  32. dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
  33. dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
  34. dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
  35. dnl default action will define HAVE_PTHREAD.
  36. dnl
  37. dnl Please let the authors know if this macro fails on any platform, or
  38. dnl if you have any other suggestions or comments. This macro was based
  39. dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
  40. dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
  41. dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
  42. dnl We are also grateful for the helpful feedback of numerous users.
  43. dnl
  44. dnl @category InstalledPackages
  45. dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
  46. dnl @version 2006-05-29
  47. dnl @license GPLWithACException
  48. AC_DEFUN([ACX_PTHREAD], [
  49. AC_REQUIRE([AC_CANONICAL_HOST])
  50. AC_LANG_SAVE
  51. AC_LANG_C
  52. acx_pthread_ok=no
  53. # We used to check for pthread.h first, but this fails if pthread.h
  54. # requires special compiler flags (e.g. on True64 or Sequent).
  55. # It gets checked for in the link test anyway.
  56. # First of all, check if the user has set any of the PTHREAD_LIBS,
  57. # etcetera environment variables, and if threads linking works using
  58. # them:
  59. if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
  60. save_CFLAGS="$CFLAGS"
  61. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  62. save_LIBS="$LIBS"
  63. LIBS="$PTHREAD_LIBS $LIBS"
  64. AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
  65. AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
  66. AC_MSG_RESULT($acx_pthread_ok)
  67. if test x"$acx_pthread_ok" = xno; then
  68. PTHREAD_LIBS=""
  69. PTHREAD_CFLAGS=""
  70. fi
  71. LIBS="$save_LIBS"
  72. CFLAGS="$save_CFLAGS"
  73. fi
  74. # We must check for the threads library under a number of different
  75. # names; the ordering is very important because some systems
  76. # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
  77. # libraries is broken (non-POSIX).
  78. # Create a list of thread flags to try. Items starting with a "-" are
  79. # C compiler flags, and other items are library names, except for "none"
  80. # which indicates that we try without any flags at all, and "pthread-config"
  81. # which is a program returning the flags for the Pth emulation library.
  82. acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
  83. # The ordering *is* (sometimes) important. Some notes on the
  84. # individual items follow:
  85. # pthreads: AIX (must check this before -lpthread)
  86. # none: in case threads are in libc; should be tried before -Kthread and
  87. # other compiler flags to prevent continual compiler warnings
  88. # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
  89. # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
  90. # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
  91. # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
  92. # -pthreads: Solaris/gcc
  93. # -mthreads: Mingw32/gcc, Lynx/gcc
  94. # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
  95. # doesn't hurt to check since this sometimes defines pthreads too;
  96. # also defines -D_REENTRANT)
  97. # ... -mt is also the pthreads flag for HP/aCC
  98. # pthread: Linux, etcetera
  99. # --thread-safe: KAI C++
  100. # pthread-config: use pthread-config program (for GNU Pth library)
  101. case "${host_cpu}-${host_os}" in
  102. *solaris*)
  103. # On Solaris (at least, for some versions), libc contains stubbed
  104. # (non-functional) versions of the pthreads routines, so link-based
  105. # tests will erroneously succeed. (We need to link with -pthreads/-mt/
  106. # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
  107. # a function called by this macro, so we could check for that, but
  108. # who knows whether they'll stub that too in a future libc.) So,
  109. # we'll just look for -pthreads and -lpthread first:
  110. acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
  111. ;;
  112. esac
  113. if test x"$acx_pthread_ok" = xno; then
  114. for flag in $acx_pthread_flags; do
  115. case $flag in
  116. none)
  117. AC_MSG_CHECKING([whether pthreads work without any flags])
  118. ;;
  119. -*)
  120. AC_MSG_CHECKING([whether pthreads work with $flag])
  121. PTHREAD_CFLAGS="$flag"
  122. ;;
  123. pthread-config)
  124. AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
  125. if test x"$acx_pthread_config" = xno; then continue; fi
  126. PTHREAD_CFLAGS="`pthread-config --cflags`"
  127. PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
  128. ;;
  129. *)
  130. AC_MSG_CHECKING([for the pthreads library -l$flag])
  131. PTHREAD_LIBS="-l$flag"
  132. ;;
  133. esac
  134. save_LIBS="$LIBS"
  135. save_CFLAGS="$CFLAGS"
  136. LIBS="$PTHREAD_LIBS $LIBS"
  137. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  138. # Check for various functions. We must include pthread.h,
  139. # since some functions may be macros. (On the Sequent, we
  140. # need a special flag -Kthread to make this header compile.)
  141. # We check for pthread_join because it is in -lpthread on IRIX
  142. # while pthread_create is in libc. We check for pthread_attr_init
  143. # due to DEC craziness with -lpthreads. We check for
  144. # pthread_cleanup_push because it is one of the few pthread
  145. # functions on Solaris that doesn't have a non-functional libc stub.
  146. # We try pthread_create on general principles.
  147. AC_TRY_LINK([#include <pthread.h>],
  148. [pthread_t th; pthread_join(th, 0);
  149. pthread_attr_init(0); pthread_cleanup_push(0, 0);
  150. pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
  151. [acx_pthread_ok=yes])
  152. LIBS="$save_LIBS"
  153. CFLAGS="$save_CFLAGS"
  154. AC_MSG_RESULT($acx_pthread_ok)
  155. if test "x$acx_pthread_ok" = xyes; then
  156. break;
  157. fi
  158. PTHREAD_LIBS=""
  159. PTHREAD_CFLAGS=""
  160. done
  161. fi
  162. # Various other checks:
  163. if test "x$acx_pthread_ok" = xyes; then
  164. save_LIBS="$LIBS"
  165. LIBS="$PTHREAD_LIBS $LIBS"
  166. save_CFLAGS="$CFLAGS"
  167. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  168. # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
  169. AC_MSG_CHECKING([for joinable pthread attribute])
  170. attr_name=unknown
  171. for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
  172. AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
  173. [attr_name=$attr; break])
  174. done
  175. AC_MSG_RESULT($attr_name)
  176. if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
  177. AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
  178. [Define to necessary symbol if this constant
  179. uses a non-standard name on your system.])
  180. fi
  181. AC_MSG_CHECKING([if more special flags are required for pthreads])
  182. flag=no
  183. case "${host_cpu}-${host_os}" in
  184. *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
  185. *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
  186. esac
  187. AC_MSG_RESULT(${flag})
  188. if test "x$flag" != xno; then
  189. PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
  190. fi
  191. LIBS="$save_LIBS"
  192. CFLAGS="$save_CFLAGS"
  193. # More AIX lossage: must compile with xlc_r or cc_r
  194. if test x"$GCC" != xyes; then
  195. AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
  196. else
  197. PTHREAD_CC=$CC
  198. fi
  199. else
  200. PTHREAD_CC="$CC"
  201. fi
  202. AC_SUBST(PTHREAD_LIBS)
  203. AC_SUBST(PTHREAD_CFLAGS)
  204. AC_SUBST(PTHREAD_CC)
  205. # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
  206. if test x"$acx_pthread_ok" = xyes; then
  207. ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
  208. :
  209. else
  210. acx_pthread_ok=no
  211. $2
  212. fi
  213. AC_LANG_RESTORE
  214. ])dnl ACX_PTHREAD