You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.4 KiB

11 years ago
  1. dnl Available from the GNU Autoconf Macro Archive at:
  2. dnl http://www.gnu.org/software/ac-archive/htmldoc/acx_pthread.html
  3. dnl
  4. AC_DEFUN([ACX_PTHREAD], [
  5. AC_REQUIRE([AC_CANONICAL_HOST])
  6. AC_LANG_SAVE
  7. AC_LANG_C
  8. acx_pthread_ok=no
  9. # We used to check for pthread.h first, but this fails if pthread.h
  10. # requires special compiler flags (e.g. on True64 or Sequent).
  11. # It gets checked for in the link test anyway.
  12. # First of all, check if the user has set any of the PTHREAD_LIBS,
  13. # etcetera environment variables, and if threads linking works using
  14. # them:
  15. if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
  16. save_CFLAGS="$CFLAGS"
  17. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  18. save_LIBS="$LIBS"
  19. LIBS="$PTHREAD_LIBS $LIBS"
  20. AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
  21. AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
  22. AC_MSG_RESULT($acx_pthread_ok)
  23. if test x"$acx_pthread_ok" = xno; then
  24. PTHREAD_LIBS=""
  25. PTHREAD_CFLAGS=""
  26. fi
  27. LIBS="$save_LIBS"
  28. CFLAGS="$save_CFLAGS"
  29. fi
  30. # We must check for the threads library under a number of different
  31. # names; the ordering is very important because some systems
  32. # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
  33. # libraries is broken (non-POSIX).
  34. # Create a list of thread flags to try. Items starting with a "-" are
  35. # C compiler flags, and other items are library names, except for "none"
  36. # which indicates that we try without any flags at all, and "pthread-config"
  37. # which is a program returning the flags for the Pth emulation library.
  38. acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
  39. # The ordering *is* (sometimes) important. Some notes on the
  40. # individual items follow:
  41. # pthreads: AIX (must check this before -lpthread)
  42. # none: in case threads are in libc; should be tried before -Kthread and
  43. # other compiler flags to prevent continual compiler warnings
  44. # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
  45. # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
  46. # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
  47. # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
  48. # -pthreads: Solaris/gcc
  49. # -mthreads: Mingw32/gcc, Lynx/gcc
  50. # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
  51. # doesn't hurt to check since this sometimes defines pthreads too;
  52. # also defines -D_REENTRANT)
  53. # pthread: Linux, etcetera
  54. # --thread-safe: KAI C++
  55. # pthread-config: use pthread-config program (for GNU Pth library)
  56. case "${host_cpu}-${host_os}" in
  57. *solaris*)
  58. # On Solaris (at least, for some versions), libc contains stubbed
  59. # (non-functional) versions of the pthreads routines, so link-based
  60. # tests will erroneously succeed. (We need to link with -pthread or
  61. # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
  62. # a function called by this macro, so we could check for that, but
  63. # who knows whether they'll stub that too in a future libc.) So,
  64. # we'll just look for -pthreads and -lpthread first:
  65. acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
  66. ;;
  67. esac
  68. if test x"$acx_pthread_ok" = xno; then
  69. for flag in $acx_pthread_flags; do
  70. case $flag in
  71. none)
  72. AC_MSG_CHECKING([whether pthreads work without any flags])
  73. ;;
  74. -*)
  75. AC_MSG_CHECKING([whether pthreads work with $flag])
  76. PTHREAD_CFLAGS="$flag"
  77. ;;
  78. pthread-config)
  79. AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
  80. if test x"$acx_pthread_config" = xno; then continue; fi
  81. PTHREAD_CFLAGS="`pthread-config --cflags`"
  82. PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
  83. ;;
  84. *)
  85. AC_MSG_CHECKING([for the pthreads library -l$flag])
  86. PTHREAD_LIBS="-l$flag"
  87. ;;
  88. esac
  89. save_LIBS="$LIBS"
  90. save_CFLAGS="$CFLAGS"
  91. LIBS="$PTHREAD_LIBS $LIBS"
  92. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  93. # Check for various functions. We must include pthread.h,
  94. # since some functions may be macros. (On the Sequent, we
  95. # need a special flag -Kthread to make this header compile.)
  96. # We check for pthread_join because it is in -lpthread on IRIX
  97. # while pthread_create is in libc. We check for pthread_attr_init
  98. # due to DEC craziness with -lpthreads. We check for
  99. # pthread_cleanup_push because it is one of the few pthread
  100. # functions on Solaris that doesn't have a non-functional libc stub.
  101. # We try pthread_create on general principles.
  102. AC_TRY_LINK([#include <pthread.h>],
  103. [pthread_t th; pthread_join(th, 0);
  104. pthread_attr_init(0); pthread_cleanup_push(0, 0);
  105. pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
  106. [acx_pthread_ok=yes])
  107. LIBS="$save_LIBS"
  108. CFLAGS="$save_CFLAGS"
  109. AC_MSG_RESULT($acx_pthread_ok)
  110. if test "x$acx_pthread_ok" = xyes; then
  111. break;
  112. fi
  113. PTHREAD_LIBS=""
  114. PTHREAD_CFLAGS=""
  115. done
  116. fi
  117. # Various other checks:
  118. if test "x$acx_pthread_ok" = xyes; then
  119. save_LIBS="$LIBS"
  120. LIBS="$PTHREAD_LIBS $LIBS"
  121. save_CFLAGS="$CFLAGS"
  122. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  123. # Detect AIX lossage: threads are created detached by default
  124. # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
  125. AC_MSG_CHECKING([for joinable pthread attribute])
  126. AC_TRY_LINK([#include <pthread.h>],
  127. [int attr=PTHREAD_CREATE_JOINABLE;],
  128. ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
  129. if test x"$ok" = xunknown; then
  130. AC_TRY_LINK([#include <pthread.h>],
  131. [int attr=PTHREAD_CREATE_UNDETACHED;],
  132. ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
  133. fi
  134. if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
  135. AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
  136. [Define to the necessary symbol if this constant
  137. uses a non-standard name on your system.])
  138. fi
  139. AC_MSG_RESULT(${ok})
  140. if test x"$ok" = xunknown; then
  141. AC_MSG_WARN([we do not know how to create joinable pthreads])
  142. fi
  143. AC_MSG_CHECKING([if more special flags are required for pthreads])
  144. flag=no
  145. case "${host_cpu}-${host_os}" in
  146. *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
  147. *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
  148. esac
  149. AC_MSG_RESULT(${flag})
  150. if test "x$flag" != xno; then
  151. PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
  152. fi
  153. LIBS="$save_LIBS"
  154. CFLAGS="$save_CFLAGS"
  155. # More AIX lossage: must compile with cc_r
  156. AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
  157. else
  158. PTHREAD_CC="$CC"
  159. fi
  160. AC_SUBST(PTHREAD_LIBS)
  161. AC_SUBST(PTHREAD_CFLAGS)
  162. AC_SUBST(PTHREAD_CC)
  163. # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
  164. if test x"$acx_pthread_ok" = xyes; then
  165. ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
  166. :
  167. else
  168. acx_pthread_ok=no
  169. $2
  170. fi
  171. AC_LANG_RESTORE
  172. ])dnl ACX_PTHREAD