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.

317 lines
13 KiB

  1. # ===========================================================================
  2. # http://www.gnu.org/software/autoconf-archive/ax_pthread.html
  3. # ===========================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
  8. # AX_PTHREAD_CXX([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
  9. #
  10. # DESCRIPTION
  11. #
  12. # This macro figures out how to build C programs using POSIX threads. It
  13. # sets the PTHREAD_LIBS output variable to the threads library and linker
  14. # flags, and the PTHREAD_CFLAGS output variable to any special C compiler
  15. # flags that are needed. (The user can also force certain compiler
  16. # flags/libs to be tested by setting these environment variables.)
  17. #
  18. # NOTE: You are assumed to not only compile your program with these flags,
  19. # but also link it with them as well. e.g. you should link with
  20. # $CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
  21. #
  22. # If you are only building threads programs, you may wish to use these
  23. # variables in your default LIBS, CFLAGS:
  24. #
  25. # LIBS="$PTHREAD_LIBS $LIBS"
  26. # CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  27. #
  28. # In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant
  29. # has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to that name
  30. # (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
  31. #
  32. # Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the
  33. # PTHREAD_PRIO_INHERIT symbol is defined when compiling with
  34. # PTHREAD_CFLAGS.
  35. #
  36. # ACTION-IF-FOUND is a list of shell commands to run if a threads library
  37. # is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it
  38. # is not found. If ACTION-IF-FOUND is not specified, the default action
  39. # will define HAVE_PTHREAD.
  40. #
  41. # Please let the authors know if this macro fails on any platform, or if
  42. # you have any other suggestions or comments. This macro was based on work
  43. # by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help
  44. # from M. Frigo), as well as ac_pthread and hb_pthread macros posted by
  45. # Alejandro Forero Cuervo to the autoconf macro repository. We are also
  46. # grateful for the helpful feedback of numerous users.
  47. #
  48. # Updated for Autoconf 2.68 by Daniel Richard G.
  49. #
  50. # LICENSE
  51. #
  52. # Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
  53. # Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG>
  54. #
  55. # This program is free software: you can redistribute it and/or modify it
  56. # under the terms of the GNU General Public License as published by the
  57. # Free Software Foundation, either version 3 of the License, or (at your
  58. # option) any later version.
  59. #
  60. # This program is distributed in the hope that it will be useful, but
  61. # WITHOUT ANY WARRANTY; without even the implied warranty of
  62. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  63. # Public License for more details.
  64. #
  65. # You should have received a copy of the GNU General Public License along
  66. # with this program. If not, see <http://www.gnu.org/licenses/>.
  67. #
  68. # As a special exception, the respective Autoconf Macro's copyright owner
  69. # gives unlimited permission to copy, distribute and modify the configure
  70. # scripts that are the output of Autoconf when processing the Macro. You
  71. # need not follow the terms of the GNU General Public License when using
  72. # or distributing such scripts, even though portions of the text of the
  73. # Macro appear in them. The GNU General Public License (GPL) does govern
  74. # all other use of the material that constitutes the Autoconf Macro.
  75. #
  76. # This special exception to the GPL applies to versions of the Autoconf
  77. # Macro released by the Autoconf Archive. When you make and distribute a
  78. # modified version of the Autoconf Macro, you may extend this special
  79. # exception to the GPL to apply to your modified version as well.
  80. #serial 19
  81. AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
  82. AC_DEFUN([AX_PTHREAD_ALL], [
  83. AC_REQUIRE([AC_CANONICAL_HOST])
  84. AS_VAR_SET([ax_pthread_ok], [no])
  85. # We used to check for pthread.h first, but this fails if pthread.h
  86. # requires special compiler flags (e.g. on True64 or Sequent).
  87. # It gets checked for in the link test anyway.
  88. # First of all, check if the user has set any of the PTHREAD_LIBS,
  89. # etcetera environment variables, and if threads linking works using
  90. # them:
  91. AS_IF([test ! -z "$PTHREAD_LIBS$PTHREAD_$3"],
  92. [
  93. AS_VAR_COPY([save_$3], [$3])
  94. AS_VAR_SET([$3], ["$$3 $PTHREAD_$3"])
  95. AS_VAR_COPY([save_LIBS], [LIBS])
  96. AS_VAR_SET([LIBS], ["$PTHREAD_LIBS $LIBS"])
  97. AC_MSG_CHECKING(
  98. [for pthread_join in LIBS=$PTHREAD_LIBS with $3=$PTHREAD_$3])
  99. AC_TRY_LINK_FUNC([pthread_join],
  100. [AS_VAR_SET([ax_pthread_ok], [yes])])
  101. AC_MSG_RESULT([$ax_pthread_ok])
  102. AS_IF([test x"$ax_pthread_ok" = xno],
  103. [AS_UNSET([PTHREAD_LIBS])
  104. AS_UNSET([PTHREAD_$3])])
  105. AS_VAR_COPY([LIBS], [save_LIBS])
  106. AS_VAR_COPY([$3], [save_$3])
  107. ])
  108. # We must check for the threads library under a number of different
  109. # names; the ordering is very important because some systems
  110. # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
  111. # libraries is broken (non-POSIX).
  112. # Create a list of thread flags to try. Items starting with a "-" are
  113. # C compiler flags, and other items are library names, except for "none"
  114. # which indicates that we try without any flags at all, and "pthread-config"
  115. # which is a program returning the flags for the Pth emulation library.
  116. AS_VAR_SET([ax_pthread_flags], ["-Kthread -kthread lthread -pthread -pthreads -mt -mthreads --thread-safe pthread-config pthreads pthread"])
  117. # The ordering *is* (sometimes) important. Some notes on the
  118. # individual items follow:
  119. # none: in case threads are in libc; should be tried before -Kthread and
  120. # other compiler flags to prevent continual compiler warnings;
  121. # none will prepended at OS modifications below
  122. # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
  123. # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
  124. # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
  125. # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
  126. # -pthreads: Solaris/gcc
  127. # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
  128. # doesn't hurt to check since this sometimes defines pthreads too;
  129. # also defines -D_REENTRANT)
  130. # ... -mt is also the pthreads flag for HP/aCC
  131. # -mthreads: Mingw32/gcc, Lynx/gcc
  132. # --thread-safe: KAI C++
  133. # pthread-config: use pthread-config program (for GNU Pth library)
  134. # pthread: Linux, etcetera
  135. # pthreads: AIX (very old gcc's on AIX must check this before -lpthread,
  136. # handled below)
  137. AS_CASE([${host_os}],
  138. [solaris*],
  139. [# On Solaris (at least, for some versions), libc contains
  140. # stubbed (non-functional) versions of the pthreads routines,
  141. # so link-based tests will erroneously succeed. (We need to
  142. # link with -pthreads/-mt/ -lpthread.) (The stubs are
  143. # missing pthread_cleanup_push, or rather a function called
  144. # by this macro, so we could check for that, but who knows
  145. # whether they'll stub that too in a future libc.) So, we'll
  146. # just look for -pthreads and -lpthread first:
  147. AS_VAR_SET([ax_pthread_flags],
  148. ["-mt -pthread $ax_pthread_flags"])
  149. AS_IF([test "x$GCC" = "xyes"],
  150. [AS_VAR_SET([ax_pthread_flags],
  151. ["-pthreads pthread -pthread $ax_pthread_flags"])])],
  152. [darwin*],
  153. [AS_VAR_SET([ax_pthread_flags], ["-pthread $ax_pthread_flags"])],
  154. [hp-ux*],
  155. [# On HP-UX compiling with aCC, cc understands -mthreads as
  156. # '-mt' '-hreads' ..., the test succeeds but it fails to run.
  157. AS_IF([test x"$GCC" != xyes],
  158. [AS_VAR_SET([ax_pthread_flags], ["-mt $ax_pthread_flags"])])])
  159. AS_VAR_SET([ax_pthread_flags], ["none $ax_pthread_flags"])
  160. AS_IF([test x"$ax_pthread_ok" = xno], [
  161. for flag in $ax_pthread_flags; do
  162. AS_CASE([$flag],
  163. [none],
  164. [AC_MSG_CHECKING([whether pthreads work without any flags])],
  165. [-*],
  166. [AC_MSG_CHECKING([whether pthreads work with $flag])
  167. AS_VAR_COPY([PTHREAD_$3], [flag])],
  168. [pthread-config],
  169. [AC_CHECK_PROG([ax_pthread_config], [pthread-config],
  170. [yes], [no])
  171. AS_IF([test x"$ax_pthread_config" = xno],
  172. [continue])
  173. AS_VAR_SET([PTHREAD_$3], ["`pthread-config --cflags`"])
  174. AS_VAR_SET([PTHREAD_LIBS],
  175. ["`pthread-config --ldflags` `pthread-config --libs`"])],
  176. [AC_MSG_CHECKING([for the pthreads library -l$flag])
  177. AS_VAR_SET([PTHREAD_LIBS], ["-l$flag"])])
  178. AS_VAR_COPY([save_LIBS], [LIBS])
  179. AS_VAR_COPY([save_$3], [$3])
  180. AS_VAR_SET([LIBS], ["$PTHREAD_LIBS $LIBS"])
  181. AS_VAR_SET([$3], ["$$3 $PTHREAD_$3"])
  182. # Check for various functions. We must include pthread.h,
  183. # since some functions may be macros. (On the Sequent, we
  184. # need a special flag -Kthread to make this header compile.)
  185. # We check for pthread_join because it is in -lpthread on IRIX
  186. # while pthread_create is in libc. We check for pthread_attr_init
  187. # due to DEC craziness with -lpthreads. We check for
  188. # pthread_cleanup_push because it is one of the few pthread
  189. # functions on Solaris that doesn't have a non-functional libc stub.
  190. # We try pthread_create on general principles.
  191. AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>
  192. static void routine(void *a) { a = 0; }
  193. static void *start_routine(void *a) { return a; }]],
  194. [[pthread_t th; pthread_attr_t attr;
  195. pthread_create(&th, 0, start_routine, 0);
  196. pthread_join(th, 0);
  197. pthread_attr_init(&attr);
  198. pthread_cleanup_push(routine, 0);
  199. pthread_cleanup_pop(0) /* ; */]])],
  200. [AS_VAR_SET([ax_pthread_ok], [yes])],
  201. [])
  202. AS_VAR_COPY([LIBS], [save_LIBS])
  203. AS_VAR_COPY([$3], [save_$3])
  204. AC_MSG_RESULT([$ax_pthread_ok])
  205. AS_IF([test "x$ax_pthread_ok" = xyes],
  206. [break])
  207. AS_UNSET([PTHREAD_LIBS])
  208. AS_UNSET([PTHREAD_$3])
  209. done
  210. ])
  211. # Various other checks:
  212. AS_IF([test "x$ax_pthread_ok" = xyes], [
  213. AS_VAR_COPY([save_LIBS], [LIBS])
  214. AS_VAR_SET([LIBS], ["$PTHREAD_LIBS $LIBS"])
  215. AS_VAR_COPY([save_$3], [$3])
  216. AS_VAR_SET([$3], ["$$3 $PTHREAD_$3"])
  217. # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
  218. AC_MSG_CHECKING([for joinable pthread attribute])
  219. AS_VAR_SET([attr_name], [unknown])
  220. for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
  221. AC_LINK_IFELSE(
  222. [AC_LANG_PROGRAM([[#include <pthread.h>
  223. ]],
  224. [[int attr = $attr; return attr /* ; */]])],
  225. [AS_VAR_SET([attr_name], [$attr])
  226. break],
  227. [])
  228. done
  229. AC_MSG_RESULT([$attr_name])
  230. AS_IF([test "$attr_name" != PTHREAD_CREATE_JOINABLE],
  231. [AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE], [$attr_name],
  232. [Define to necessary symbol if this constant
  233. uses a non-standard name on your system.])])
  234. AC_MSG_CHECKING([if more special flags are required for pthreads])
  235. AS_VAR_SET([flag], [no])
  236. AS_CASE([${host_os}],
  237. [aix* | freebsd* | darwin*],
  238. [AS_VAR_SET([flag], ["-D_THREAD_SAFE"])],
  239. [osf* | hpux*],
  240. [AS_VAR_SET([flag], ["-D_REENTRANT"])],
  241. [solaris*],
  242. [AS_IF([test "$GCC" = "yes"],
  243. [AS_VAR_SET([flag], ["-D_REENTRANT"])],
  244. [AS_VAR_SET([flag], ["-mt -D_REENTRANT"])])])
  245. AC_MSG_RESULT(${flag})
  246. AS_IF([test "x$flag" != xno],
  247. [AS_VAR_SET([PTHREAD_$3], ["$flag $PTHREAD_$3"])])
  248. AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],
  249. [ax_cv_PTHREAD_PRIO_INHERIT], [
  250. AC_LINK_IFELSE([
  251. AC_LANG_PROGRAM([[#include <pthread.h>
  252. ]],
  253. [[int i = PTHREAD_PRIO_INHERIT;]])],
  254. [ax_cv_PTHREAD_PRIO_INHERIT=yes],
  255. [ax_cv_PTHREAD_PRIO_INHERIT=no])])
  256. AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes"],
  257. [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], 1,
  258. [Have PTHREAD_PRIO_INHERIT.])])
  259. AS_VAR_COPY([LIBS], [save_LIBS])
  260. AS_VAR_COPY([$3], [save_$3])
  261. ])
  262. AC_SUBST([PTHREAD_LIBS])
  263. AC_SUBST([PTHREAD_$3])
  264. # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
  265. AS_IF([test x"$ax_pthread_ok" = xyes],
  266. [ifelse([$1],
  267. [],
  268. [AC_DEFINE([HAVE_PTHREAD], [1],
  269. [Define if you have POSIX threads libraries and header files.])],
  270. $1)],
  271. [AS_VAR_SET([ax_pthread_ok], [no])]
  272. $2)
  273. ])dnl AX_PTHREAD_ALL
  274. AC_DEFUN([AX_PTHREAD_CXX],[dnl
  275. AC_LANG_PUSH([C++])
  276. AX_PTHREAD_ALL([$1], [$2], [CXXFLAGS])
  277. AC_LANG_POP([C++])])
  278. AC_DEFUN([AX_PTHREAD],[dnl
  279. AC_LANG_PUSH([C])
  280. AX_PTHREAD_ALL([$1], [$2], [CFLAGS])
  281. AC_LANG_POP([C])])