acx_pthread.m4 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. # This was retrieved from
  2. # http://svn.0pointer.de/viewvc/trunk/common/acx_pthread.m4?revision=1277&root=avahi
  3. # See also (perhaps for new versions?)
  4. # http://svn.0pointer.de/viewvc/trunk/common/acx_pthread.m4?root=avahi
  5. #
  6. # We've rewritten the inconsistency check code (from avahi), to work
  7. # more broadly. In particular, it no longer assumes ld accepts -zdefs.
  8. # This caused a restructing of the code, but the functionality has only
  9. # changed a little.
  10. dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
  11. dnl
  12. dnl @summary figure out how to build C programs using POSIX threads
  13. dnl
  14. dnl This macro figures out how to build C programs using POSIX threads.
  15. dnl It sets the PTHREAD_LIBS output variable to the threads library and
  16. dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
  17. dnl C compiler flags that are needed. (The user can also force certain
  18. dnl compiler flags/libs to be tested by setting these environment
  19. dnl variables.)
  20. dnl
  21. dnl Also sets PTHREAD_CC to any special C compiler that is needed for
  22. dnl multi-threaded programs (defaults to the value of CC otherwise).
  23. dnl (This is necessary on AIX to use the special cc_r compiler alias.)
  24. dnl
  25. dnl NOTE: You are assumed to not only compile your program with these
  26. dnl flags, but also link it with them as well. e.g. you should link
  27. dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
  28. dnl $LIBS
  29. dnl
  30. dnl If you are only building threads programs, you may wish to use
  31. dnl these variables in your default LIBS, CFLAGS, and CC:
  32. dnl
  33. dnl LIBS="$PTHREAD_LIBS $LIBS"
  34. dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  35. dnl CC="$PTHREAD_CC"
  36. dnl
  37. dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
  38. dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
  39. dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
  40. dnl
  41. dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
  42. dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
  43. dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
  44. dnl default action will define HAVE_PTHREAD.
  45. dnl
  46. dnl Please let the authors know if this macro fails on any platform, or
  47. dnl if you have any other suggestions or comments. This macro was based
  48. dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
  49. dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
  50. dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
  51. dnl We are also grateful for the helpful feedback of numerous users.
  52. dnl
  53. dnl @category InstalledPackages
  54. dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
  55. dnl @version 2006-05-29
  56. dnl @license GPLWithACException
  57. dnl
  58. dnl Checks for GCC shared/pthread inconsistency based on work by
  59. dnl Marcin Owsiany <marcin@owsiany.pl>
  60. AC_DEFUN([ACX_PTHREAD], [
  61. AC_REQUIRE([AC_CANONICAL_HOST])
  62. AC_LANG_SAVE
  63. AC_LANG_C
  64. acx_pthread_ok=no
  65. # We used to check for pthread.h first, but this fails if pthread.h
  66. # requires special compiler flags (e.g. on True64 or Sequent).
  67. # It gets checked for in the link test anyway.
  68. # First of all, check if the user has set any of the PTHREAD_LIBS,
  69. # etcetera environment variables, and if threads linking works using
  70. # them:
  71. if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
  72. save_CFLAGS="$CFLAGS"
  73. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  74. save_LIBS="$LIBS"
  75. LIBS="$PTHREAD_LIBS $LIBS"
  76. AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
  77. AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
  78. AC_MSG_RESULT($acx_pthread_ok)
  79. if test x"$acx_pthread_ok" = xno; then
  80. PTHREAD_LIBS=""
  81. PTHREAD_CFLAGS=""
  82. fi
  83. LIBS="$save_LIBS"
  84. CFLAGS="$save_CFLAGS"
  85. fi
  86. # We must check for the threads library under a number of different
  87. # names; the ordering is very important because some systems
  88. # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
  89. # libraries is broken (non-POSIX).
  90. # Create a list of thread flags to try. Items starting with a "-" are
  91. # C compiler flags, and other items are library names, except for "none"
  92. # which indicates that we try without any flags at all, and "pthread-config"
  93. # which is a program returning the flags for the Pth emulation library.
  94. acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
  95. # The ordering *is* (sometimes) important. Some notes on the
  96. # individual items follow:
  97. # pthreads: AIX (must check this before -lpthread)
  98. # none: in case threads are in libc; should be tried before -Kthread and
  99. # other compiler flags to prevent continual compiler warnings
  100. # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
  101. # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
  102. # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
  103. # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
  104. # -pthreads: Solaris/gcc
  105. # -mthreads: Mingw32/gcc, Lynx/gcc
  106. # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
  107. # doesn't hurt to check since this sometimes defines pthreads too;
  108. # also defines -D_REENTRANT)
  109. # ... -mt is also the pthreads flag for HP/aCC
  110. # pthread: Linux, etcetera
  111. # --thread-safe: KAI C++
  112. # pthread-config: use pthread-config program (for GNU Pth library)
  113. case "${host_cpu}-${host_os}" in
  114. *solaris*)
  115. # On Solaris (at least, for some versions), libc contains stubbed
  116. # (non-functional) versions of the pthreads routines, so link-based
  117. # tests will erroneously succeed. (We need to link with -pthreads/-mt/
  118. # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
  119. # a function called by this macro, so we could check for that, but
  120. # who knows whether they'll stub that too in a future libc.) So,
  121. # we'll just look for -pthreads and -lpthread first:
  122. acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
  123. ;;
  124. esac
  125. if test x"$acx_pthread_ok" = xno; then
  126. for flag in $acx_pthread_flags; do
  127. case $flag in
  128. none)
  129. AC_MSG_CHECKING([whether pthreads work without any flags])
  130. ;;
  131. -*)
  132. AC_MSG_CHECKING([whether pthreads work with $flag])
  133. PTHREAD_CFLAGS="$flag"
  134. ;;
  135. pthread-config)
  136. AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
  137. if test x"$acx_pthread_config" = xno; then continue; fi
  138. PTHREAD_CFLAGS="`pthread-config --cflags`"
  139. PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
  140. ;;
  141. *)
  142. AC_MSG_CHECKING([for the pthreads library -l$flag])
  143. PTHREAD_LIBS="-l$flag"
  144. ;;
  145. esac
  146. save_LIBS="$LIBS"
  147. save_CFLAGS="$CFLAGS"
  148. LIBS="$PTHREAD_LIBS $LIBS"
  149. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  150. # Check for various functions. We must include pthread.h,
  151. # since some functions may be macros. (On the Sequent, we
  152. # need a special flag -Kthread to make this header compile.)
  153. # We check for pthread_join because it is in -lpthread on IRIX
  154. # while pthread_create is in libc. We check for pthread_attr_init
  155. # due to DEC craziness with -lpthreads. We check for
  156. # pthread_cleanup_push because it is one of the few pthread
  157. # functions on Solaris that doesn't have a non-functional libc stub.
  158. # We try pthread_create on general principles.
  159. AC_TRY_LINK([#include <pthread.h>],
  160. [pthread_t th; pthread_join(th, 0);
  161. pthread_attr_init(0); pthread_cleanup_push(0, 0);
  162. pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
  163. [acx_pthread_ok=yes])
  164. LIBS="$save_LIBS"
  165. CFLAGS="$save_CFLAGS"
  166. AC_MSG_RESULT($acx_pthread_ok)
  167. if test "x$acx_pthread_ok" = xyes; then
  168. break;
  169. fi
  170. PTHREAD_LIBS=""
  171. PTHREAD_CFLAGS=""
  172. done
  173. fi
  174. # Various other checks:
  175. if test "x$acx_pthread_ok" = xyes; then
  176. save_LIBS="$LIBS"
  177. LIBS="$PTHREAD_LIBS $LIBS"
  178. save_CFLAGS="$CFLAGS"
  179. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  180. # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
  181. AC_MSG_CHECKING([for joinable pthread attribute])
  182. attr_name=unknown
  183. for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
  184. AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
  185. [attr_name=$attr; break])
  186. done
  187. AC_MSG_RESULT($attr_name)
  188. if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
  189. AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
  190. [Define to necessary symbol if this constant
  191. uses a non-standard name on your system.])
  192. fi
  193. AC_MSG_CHECKING([if more special flags are required for pthreads])
  194. flag=no
  195. case "${host_cpu}-${host_os}" in
  196. *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
  197. *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
  198. esac
  199. AC_MSG_RESULT(${flag})
  200. if test "x$flag" != xno; then
  201. PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
  202. fi
  203. LIBS="$save_LIBS"
  204. CFLAGS="$save_CFLAGS"
  205. # More AIX lossage: must compile with xlc_r or cc_r
  206. if test x"$GCC" != xyes; then
  207. AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
  208. else
  209. PTHREAD_CC=$CC
  210. fi
  211. # The next part tries to detect GCC inconsistency with -shared on some
  212. # architectures and systems. The problem is that in certain
  213. # configurations, when -shared is specified, GCC "forgets" to
  214. # internally use various flags which are still necessary.
  215. #
  216. # Prepare the flags
  217. #
  218. save_CFLAGS="$CFLAGS"
  219. save_LIBS="$LIBS"
  220. save_CC="$CC"
  221. # Try with the flags determined by the earlier checks.
  222. #
  223. # -Wl,-z,defs forces link-time symbol resolution, so that the
  224. # linking checks with -shared actually have any value
  225. #
  226. # FIXME: -fPIC is required for -shared on many architectures,
  227. # so we specify it here, but the right way would probably be to
  228. # properly detect whether it is actually required.
  229. CFLAGS="-shared -fPIC -Wl,-z,defs $CFLAGS $PTHREAD_CFLAGS"
  230. LIBS="$PTHREAD_LIBS $LIBS"
  231. CC="$PTHREAD_CC"
  232. # In order not to create several levels of indentation, we test
  233. # the value of "$done" until we find the cure or run out of ideas.
  234. done="no"
  235. # First, make sure the CFLAGS we added are actually accepted by our
  236. # compiler. If not (and OS X's ld, for instance, does not accept -z),
  237. # then we can't do this test.
  238. if test x"$done" = xno; then
  239. AC_MSG_CHECKING([whether to check for GCC pthread/shared inconsistencies])
  240. AC_TRY_LINK(,, , [done=yes])
  241. if test "x$done" = xyes ; then
  242. AC_MSG_RESULT([no])
  243. else
  244. AC_MSG_RESULT([yes])
  245. fi
  246. fi
  247. if test x"$done" = xno; then
  248. AC_MSG_CHECKING([whether -pthread is sufficient with -shared])
  249. AC_TRY_LINK([#include <pthread.h>],
  250. [pthread_t th; pthread_join(th, 0);
  251. pthread_attr_init(0); pthread_cleanup_push(0, 0);
  252. pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
  253. [done=yes])
  254. if test "x$done" = xyes; then
  255. AC_MSG_RESULT([yes])
  256. else
  257. AC_MSG_RESULT([no])
  258. fi
  259. fi
  260. #
  261. # Linux gcc on some architectures such as mips/mipsel forgets
  262. # about -lpthread
  263. #
  264. if test x"$done" = xno; then
  265. AC_MSG_CHECKING([whether -lpthread fixes that])
  266. LIBS="-lpthread $PTHREAD_LIBS $save_LIBS"
  267. AC_TRY_LINK([#include <pthread.h>],
  268. [pthread_t th; pthread_join(th, 0);
  269. pthread_attr_init(0); pthread_cleanup_push(0, 0);
  270. pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
  271. [done=yes])
  272. if test "x$done" = xyes; then
  273. AC_MSG_RESULT([yes])
  274. PTHREAD_LIBS="-lpthread $PTHREAD_LIBS"
  275. else
  276. AC_MSG_RESULT([no])
  277. fi
  278. fi
  279. #
  280. # FreeBSD 4.10 gcc forgets to use -lc_r instead of -lc
  281. #
  282. if test x"$done" = xno; then
  283. AC_MSG_CHECKING([whether -lc_r fixes that])
  284. LIBS="-lc_r $PTHREAD_LIBS $save_LIBS"
  285. AC_TRY_LINK([#include <pthread.h>],
  286. [pthread_t th; pthread_join(th, 0);
  287. pthread_attr_init(0); pthread_cleanup_push(0, 0);
  288. pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
  289. [done=yes])
  290. if test "x$done" = xyes; then
  291. AC_MSG_RESULT([yes])
  292. PTHREAD_LIBS="-lc_r $PTHREAD_LIBS"
  293. else
  294. AC_MSG_RESULT([no])
  295. fi
  296. fi
  297. if test x"$done" = xno; then
  298. # OK, we have run out of ideas
  299. AC_MSG_WARN([Impossible to determine how to use pthreads with shared libraries])
  300. # so it's not safe to assume that we may use pthreads
  301. acx_pthread_ok=no
  302. fi
  303. CFLAGS="$save_CFLAGS"
  304. LIBS="$save_LIBS"
  305. CC="$save_CC"
  306. else
  307. PTHREAD_CC="$CC"
  308. fi
  309. AC_SUBST(PTHREAD_LIBS)
  310. AC_SUBST(PTHREAD_CFLAGS)
  311. AC_SUBST(PTHREAD_CC)
  312. # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
  313. if test x"$acx_pthread_ok" = xyes; then
  314. ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
  315. :
  316. else
  317. acx_pthread_ok=no
  318. $2
  319. fi
  320. AC_LANG_RESTORE
  321. ])dnl ACX_PTHREAD