acx_pthread.m4 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. # pthread: Linux, etcetera
  51. # --thread-safe: KAI C++
  52. # pthread-config: use pthread-config program (for GNU Pth library)
  53. case "${host_cpu}-${host_os}" in
  54. *solaris*)
  55. # On Solaris (at least, for some versions), libc contains stubbed
  56. # (non-functional) versions of the pthreads routines, so link-based
  57. # tests will erroneously succeed. (We need to link with -pthread or
  58. # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
  59. # a function called by this macro, so we could check for that, but
  60. # who knows whether they'll stub that too in a future libc.) So,
  61. # we'll just look for -pthreads and -lpthread first:
  62. acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
  63. ;;
  64. esac
  65. if test x"$acx_pthread_ok" = xno; then
  66. for flag in $acx_pthread_flags; do
  67. case $flag in
  68. none)
  69. AC_MSG_CHECKING([whether pthreads work without any flags])
  70. ;;
  71. -*)
  72. AC_MSG_CHECKING([whether pthreads work with $flag])
  73. PTHREAD_CFLAGS="$flag"
  74. ;;
  75. pthread-config)
  76. AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
  77. if test x"$acx_pthread_config" = xno; then continue; fi
  78. PTHREAD_CFLAGS="`pthread-config --cflags`"
  79. PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
  80. ;;
  81. *)
  82. AC_MSG_CHECKING([for the pthreads library -l$flag])
  83. PTHREAD_LIBS="-l$flag"
  84. ;;
  85. esac
  86. save_LIBS="$LIBS"
  87. save_CFLAGS="$CFLAGS"
  88. LIBS="$PTHREAD_LIBS $LIBS"
  89. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  90. # Check for various functions. We must include pthread.h,
  91. # since some functions may be macros. (On the Sequent, we
  92. # need a special flag -Kthread to make this header compile.)
  93. # We check for pthread_join because it is in -lpthread on IRIX
  94. # while pthread_create is in libc. We check for pthread_attr_init
  95. # due to DEC craziness with -lpthreads. We check for
  96. # pthread_cleanup_push because it is one of the few pthread
  97. # functions on Solaris that doesn't have a non-functional libc stub.
  98. # We try pthread_create on general principles.
  99. AC_TRY_LINK([#include <pthread.h>],
  100. [pthread_t th; pthread_join(th, 0);
  101. pthread_attr_init(0); pthread_cleanup_push(0, 0);
  102. pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
  103. [acx_pthread_ok=yes])
  104. LIBS="$save_LIBS"
  105. CFLAGS="$save_CFLAGS"
  106. AC_MSG_RESULT($acx_pthread_ok)
  107. if test "x$acx_pthread_ok" = xyes; then
  108. break;
  109. fi
  110. PTHREAD_LIBS=""
  111. PTHREAD_CFLAGS=""
  112. done
  113. fi
  114. # Various other checks:
  115. if test "x$acx_pthread_ok" = xyes; then
  116. save_LIBS="$LIBS"
  117. LIBS="$PTHREAD_LIBS $LIBS"
  118. save_CFLAGS="$CFLAGS"
  119. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  120. # Detect AIX lossage: threads are created detached by default
  121. # and the JOINABLE attribute has a nonstandard name (UNDETACHED).
  122. AC_MSG_CHECKING([for joinable pthread attribute])
  123. AC_TRY_LINK([#include <pthread.h>],
  124. [int attr=PTHREAD_CREATE_JOINABLE;],
  125. ok=PTHREAD_CREATE_JOINABLE, ok=unknown)
  126. if test x"$ok" = xunknown; then
  127. AC_TRY_LINK([#include <pthread.h>],
  128. [int attr=PTHREAD_CREATE_UNDETACHED;],
  129. ok=PTHREAD_CREATE_UNDETACHED, ok=unknown)
  130. fi
  131. if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then
  132. AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok,
  133. [Define to the necessary symbol if this constant
  134. uses a non-standard name on your system.])
  135. fi
  136. AC_MSG_RESULT(${ok})
  137. if test x"$ok" = xunknown; then
  138. AC_MSG_WARN([we do not know how to create joinable pthreads])
  139. fi
  140. AC_MSG_CHECKING([if more special flags are required for pthreads])
  141. flag=no
  142. case "${host_cpu}-${host_os}" in
  143. *-aix* | *-freebsd*) flag="-D_THREAD_SAFE";;
  144. *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
  145. esac
  146. AC_MSG_RESULT(${flag})
  147. if test "x$flag" != xno; then
  148. PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
  149. fi
  150. LIBS="$save_LIBS"
  151. CFLAGS="$save_CFLAGS"
  152. # More AIX lossage: must compile with cc_r
  153. AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
  154. else
  155. PTHREAD_CC="$CC"
  156. fi
  157. AC_SUBST(PTHREAD_LIBS)
  158. AC_SUBST(PTHREAD_CFLAGS)
  159. AC_SUBST(PTHREAD_CC)
  160. # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
  161. if test x"$acx_pthread_ok" = xyes; then
  162. ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
  163. :
  164. else
  165. acx_pthread_ok=no
  166. $2
  167. fi
  168. AC_LANG_RESTORE
  169. ])dnl ACX_PTHREAD