acx_pthread.m4 7.1 KB

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