acx_pthread.m4 11 KB

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