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.

256 lines
9.8 KiB

  1. dnl
  2. dnl NOTE: This file was modified for Xerces-C++. See the comments
  3. dnl starting with 'XERCES' for more information.
  4. dnl
  5. dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
  6. dnl
  7. dnl @summary figure out how to build C programs using POSIX threads
  8. dnl
  9. dnl This macro figures out how to build C programs using POSIX threads.
  10. dnl It sets the PTHREAD_LIBS output variable to the threads library and
  11. dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
  12. dnl C compiler flags that are needed. (The user can also force certain
  13. dnl compiler flags/libs to be tested by setting these environment
  14. dnl variables.)
  15. dnl
  16. dnl Also sets PTHREAD_CC to any special C compiler that is needed for
  17. dnl multi-threaded programs (defaults to the value of CC otherwise).
  18. dnl (This is necessary on AIX to use the special cc_r compiler alias.)
  19. dnl
  20. dnl NOTE: You are assumed to not only compile your program with these
  21. dnl flags, but also link it with them as well. e.g. you should link
  22. dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
  23. dnl $LIBS
  24. dnl
  25. dnl If you are only building threads programs, you may wish to use
  26. dnl these variables in your default LIBS, CFLAGS, and CC:
  27. dnl
  28. dnl LIBS="$PTHREAD_LIBS $LIBS"
  29. dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  30. dnl CC="$PTHREAD_CC"
  31. dnl
  32. dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
  33. dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
  34. dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
  35. dnl
  36. dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
  37. dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
  38. dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
  39. dnl default action will define HAVE_PTHREAD.
  40. dnl
  41. dnl Please let the authors know if this macro fails on any platform, or
  42. dnl if you have any other suggestions or comments. This macro was based
  43. dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
  44. dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
  45. dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
  46. dnl We are also grateful for the helpful feedback of numerous users.
  47. dnl
  48. dnl @category InstalledPackages
  49. dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
  50. dnl @version 2006-05-29
  51. dnl @license GPLWithACException
  52. AC_DEFUN([ACX_PTHREAD], [
  53. AC_REQUIRE([AC_CANONICAL_HOST])
  54. AC_LANG_SAVE
  55. AC_LANG_C
  56. acx_pthread_ok=no
  57. # We used to check for pthread.h first, but this fails if pthread.h
  58. # requires special compiler flags (e.g. on True64 or Sequent).
  59. # It gets checked for in the link test anyway.
  60. # First of all, check if the user has set any of the PTHREAD_LIBS,
  61. # etcetera environment variables, and if threads linking works using
  62. # them:
  63. if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
  64. save_CFLAGS="$CFLAGS"
  65. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  66. save_LIBS="$LIBS"
  67. LIBS="$PTHREAD_LIBS $LIBS"
  68. AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
  69. AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
  70. AC_MSG_RESULT($acx_pthread_ok)
  71. if test x"$acx_pthread_ok" = xno; then
  72. PTHREAD_LIBS=""
  73. PTHREAD_CFLAGS=""
  74. fi
  75. LIBS="$save_LIBS"
  76. CFLAGS="$save_CFLAGS"
  77. fi
  78. # We must check for the threads library under a number of different
  79. # names; the ordering is very important because some systems
  80. # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
  81. # libraries is broken (non-POSIX).
  82. # Create a list of thread flags to try. Items starting with a "-" are
  83. # C compiler flags, and other items are library names, except for "none"
  84. # which indicates that we try without any flags at all, and "pthread-config"
  85. # which is a program returning the flags for the Pth emulation library.
  86. # XERCES: On GNU/Linux with gcc both -pthread and -lpthread are valid.
  87. # However, libtool links libraries with -nostdlib which results in
  88. # -pthread being stripped from the linker command line. To resolve
  89. # this we move pthread from after -mthreads to after pthreads.
  90. #
  91. acx_pthread_flags="pthreads pthread none -Kthread -kthread lthread -pthread -pthreads -mthreads --thread-safe -mt pthread-config"
  92. # The ordering *is* (sometimes) important. Some notes on the
  93. # individual items follow:
  94. # pthreads: AIX (must check this before -lpthread)
  95. # none: in case threads are in libc; should be tried before -Kthread and
  96. # other compiler flags to prevent continual compiler warnings
  97. # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
  98. # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
  99. # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
  100. # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
  101. # -pthreads: Solaris/gcc
  102. # -mthreads: Mingw32/gcc, Lynx/gcc
  103. # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
  104. # doesn't hurt to check since this sometimes defines pthreads too;
  105. # also defines -D_REENTRANT)
  106. # ... -mt is also the pthreads flag for HP/aCC
  107. # pthread: Linux, etcetera
  108. # --thread-safe: KAI C++
  109. # pthread-config: use pthread-config program (for GNU Pth library)
  110. case "${host_cpu}-${host_os}" in
  111. *solaris*)
  112. # On Solaris (at least, for some versions), libc contains stubbed
  113. # (non-functional) versions of the pthreads routines, so link-based
  114. # tests will erroneously succeed. (We need to link with -pthreads/-mt/
  115. # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
  116. # a function called by this macro, so we could check for that, but
  117. # who knows whether they'll stub that too in a future libc.) So,
  118. # we'll just look for -pthreads and -lpthread first:
  119. acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
  120. ;;
  121. esac
  122. if test x"$acx_pthread_ok" = xno; then
  123. for flag in $acx_pthread_flags; do
  124. case $flag in
  125. none)
  126. AC_MSG_CHECKING([whether pthreads work without any flags])
  127. ;;
  128. -*)
  129. AC_MSG_CHECKING([whether pthreads work with $flag])
  130. PTHREAD_CFLAGS="$flag"
  131. ;;
  132. pthread-config)
  133. AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
  134. if test x"$acx_pthread_config" = xno; then continue; fi
  135. PTHREAD_CFLAGS="`pthread-config --cflags`"
  136. PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
  137. ;;
  138. *)
  139. AC_MSG_CHECKING([for the pthreads library -l$flag])
  140. PTHREAD_LIBS="-l$flag"
  141. ;;
  142. esac
  143. save_LIBS="$LIBS"
  144. save_CFLAGS="$CFLAGS"
  145. LIBS="$PTHREAD_LIBS $LIBS"
  146. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  147. # Check for various functions. We must include pthread.h,
  148. # since some functions may be macros. (On the Sequent, we
  149. # need a special flag -Kthread to make this header compile.)
  150. # We check for pthread_join because it is in -lpthread on IRIX
  151. # while pthread_create is in libc. We check for pthread_attr_init
  152. # due to DEC craziness with -lpthreads. We check for
  153. # pthread_cleanup_push because it is one of the few pthread
  154. # functions on Solaris that doesn't have a non-functional libc stub.
  155. # We try pthread_create on general principles.
  156. # XERCES: Add tests for pthread_mutexattr_init and
  157. # pthread_mutexattr_destroy.
  158. #
  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. pthread_mutexattr_init(0); pthread_mutexattr_destroy(0);],
  164. [acx_pthread_ok=yes])
  165. LIBS="$save_LIBS"
  166. CFLAGS="$save_CFLAGS"
  167. AC_MSG_RESULT($acx_pthread_ok)
  168. if test "x$acx_pthread_ok" = xyes; then
  169. break;
  170. fi
  171. PTHREAD_LIBS=""
  172. PTHREAD_CFLAGS=""
  173. done
  174. fi
  175. # Various other checks:
  176. if test "x$acx_pthread_ok" = xyes; then
  177. save_LIBS="$LIBS"
  178. LIBS="$PTHREAD_LIBS $LIBS"
  179. save_CFLAGS="$CFLAGS"
  180. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  181. # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
  182. AC_MSG_CHECKING([for joinable pthread attribute])
  183. attr_name=unknown
  184. for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
  185. AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
  186. [attr_name=$attr; break])
  187. done
  188. AC_MSG_RESULT($attr_name)
  189. if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
  190. AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
  191. [Define to necessary symbol if this constant
  192. uses a non-standard name on your system.])
  193. fi
  194. AC_MSG_CHECKING([if more special flags are required for pthreads])
  195. flag=no
  196. case "${host_cpu}-${host_os}" in
  197. *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
  198. *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
  199. esac
  200. AC_MSG_RESULT(${flag})
  201. if test "x$flag" != xno; then
  202. PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
  203. fi
  204. LIBS="$save_LIBS"
  205. CFLAGS="$save_CFLAGS"
  206. # More AIX lossage: must compile with xlc_r or cc_r
  207. if test x"$GCC" != xyes; then
  208. AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
  209. else
  210. PTHREAD_CC=$CC
  211. fi
  212. else
  213. PTHREAD_CC="$CC"
  214. fi
  215. AC_SUBST(PTHREAD_LIBS)
  216. AC_SUBST(PTHREAD_CFLAGS)
  217. AC_SUBST(PTHREAD_CC)
  218. # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
  219. if test x"$acx_pthread_ok" = xyes; then
  220. ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
  221. :
  222. else
  223. acx_pthread_ok=no
  224. $2
  225. fi
  226. AC_LANG_RESTORE
  227. ])dnl ACX_PTHREAD