acx_pthread.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html lang="en">
  4. <head>
  5. <title>
  6. Autoconf Macro: acx_pthread
  7. </title>
  8. <link rel="stylesheet" type="text/css" href="autoconf-archive.css">
  9. </head>
  10. <body>
  11. <table summary="web navigation" style="width:100%;">
  12. <tbody>
  13. <tr>
  14. <td style="width:25%;" align="center" valign="top">
  15. <a href="http://autoconf-archive.cryp.to/acx_pthread.m4">Download M4
  16. Source</a>
  17. </td>
  18. <td style="width:25%;" align="center" valign="top">
  19. <a href=
  20. "http://git.cryp.to/autoconf-archive?a=history;f=acx_pthread.m4">Macro
  21. History</a>
  22. </td>
  23. <td style="width:25%;" align="center" valign="top">
  24. <a href="macros-by-category.html">Category Index</a>
  25. </td>
  26. <td style="width:25%;" align="center" valign="top">
  27. <form method="get" action="http://www.google.com/search">
  28. <div>
  29. <input name="sitesearch" value="autoconf-archive.cryp.to" type=
  30. "hidden"><a href="http://www.google.com/">Search</a>: <input name="q"
  31. size="10" maxlength="255" type="text">
  32. </div>
  33. </form>
  34. </td>
  35. </tr>
  36. </tbody>
  37. </table>
  38. <hr>
  39. <h1>
  40. acx_pthread
  41. </h1>
  42. <h2>
  43. Synopsis
  44. </h2>
  45. <p class="indent" style="white-space:nowrap;">
  46. <code>ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])</code>
  47. </p>
  48. <h2>
  49. Description
  50. </h2>
  51. <div class="indent">
  52. <p>
  53. This macro figures out how to build C programs using POSIX threads. It sets
  54. the PTHREAD_LIBS output variable to the threads library and linker flags,
  55. and the PTHREAD_CFLAGS output variable to any special C compiler flags that
  56. are needed. (The user can also force certain compiler flags/libs to be
  57. tested by setting these environment variables.)
  58. </p>
  59. <p>
  60. Also sets PTHREAD_CC to any special C compiler that is needed for
  61. multi-threaded programs (defaults to the value of CC otherwise). (This is
  62. necessary on AIX to use the special cc_r compiler alias.)
  63. </p>
  64. <p>
  65. NOTE: You are assumed to not only compile your program with these flags,
  66. but also link it with them as well. e.g. you should link with $PTHREAD_CC
  67. $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
  68. </p>
  69. <p>
  70. If you are only building threads programs, you may wish to use these
  71. variables in your default LIBS, CFLAGS, and CC:
  72. </p>
  73. <pre>
  74. LIBS="$PTHREAD_LIBS $LIBS"
  75. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  76. CC="$PTHREAD_CC"
  77. </pre>
  78. <p>
  79. In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant has a
  80. nonstandard name, defines PTHREAD_CREATE_JOINABLE to that name (e.g.
  81. PTHREAD_CREATE_UNDETACHED on AIX).
  82. </p>
  83. <p>
  84. ACTION-IF-FOUND is a list of shell commands to run if a threads library is
  85. found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it is not
  86. found. If ACTION-IF-FOUND is not specified, the default action will define
  87. HAVE_PTHREAD.
  88. </p>
  89. <p>
  90. Please let the authors know if this macro fails on any platform, or if you
  91. have any other suggestions or comments. This macro was based on work by SGJ
  92. on autoconf scripts for FFTW (<a href=
  93. "http://www.fftw.org/">http://www.fftw.org/</a>) (with help from M. Frigo),
  94. as well as ac_pthread and hb_pthread macros posted by Alejandro Forero
  95. Cuervo to the autoconf macro repository. We are also grateful for the
  96. helpful feedback of numerous users.
  97. </p>
  98. </div>
  99. <h2>
  100. Author
  101. </h2>
  102. <p class="indent">
  103. Steven G. Johnson &lt;stevenj@alum.mit.edu&gt;
  104. </p>
  105. <h2>
  106. Last Modified
  107. </h2>
  108. <p class="indent">
  109. 2008-04-12
  110. </p>
  111. <h2>
  112. Cross References
  113. </h2>
  114. <p class="indent">
  115. <img src="group19-xrefs.png" alt="group19-xrefs.png" usemap=
  116. "#group19"><map id="group19" name="group19">
  117. <area shape="poly" href="acx_pthread.html" title="acx_pthread" alt=""
  118. coords=
  119. "944,101 940,94 928,87 909,82 886,79 860,77 834,79 811,82 792,87 780,94 776,101 780,109 792,115 811,121 834,124 860,125 886,124 909,121 928,115 940,109">
  120. <area shape="poly" href="ax_check_gl.html" title="ax_check_gl" alt=""
  121. coords=
  122. "661,173 656,166 644,159 625,154 601,151 575,149 548,151 524,154 505,159 493,166 489,173 493,181 505,187 524,193 548,196 575,197 601,196 625,193 644,187 656,181">
  123. <area shape="poly" href="ax_lang_compiler_ms.html" title=
  124. "ax_lang_compiler_ms" alt="" coords=
  125. "1000,173 993,166 973,159 942,154 903,151 860,149 817,151 778,154 747,159 727,166 720,173 727,181 747,187 778,193 817,196 860,197 903,196 942,193 973,187 993,181">
  126. <area shape="poly" href="ax_check_glu.html" title="ax_check_glu" alt=""
  127. coords=
  128. "429,173 425,166 412,159 391,154 366,151 337,149 309,151 283,154 263,159 250,166 245,173 250,181 263,187 283,193 309,196 337,197 366,196 391,193 412,187 425,181">
  129. <area shape="poly" href="ax_check_glut.html" title="ax_check_glut" alt=""
  130. coords=
  131. "197,173 192,166 179,159 158,154 131,151 101,149 72,151 45,154 24,159 10,166 6,173 10,181 24,187 45,193 72,196 101,197 131,196 158,193 179,187 192,181">
  132. <area shape="poly" href="ax_lib_xerces.html" title="ax_lib_xerces" alt=""
  133. coords=
  134. "668,29 663,22 650,15 629,10 603,7 575,5 546,7 520,10 499,15 486,22 482,29 486,37 499,43 520,49 546,52 575,53 603,52 629,49 650,43 663,37">
  135. <area shape="poly" href="ax_path_milter.html" title="ax_path_milter" alt=""
  136. coords=
  137. "671,101 667,94 653,87 631,82 605,79 575,77 545,79 518,82 497,87 483,94 478,101 483,109 497,115 518,121 545,124 575,125 605,124 631,121 653,115 667,109">
  138. </map>
  139. </p>
  140. <h2>
  141. M4 Source Code
  142. </h2>
  143. <div class="indent">
  144. <pre class="m4source">
  145. AC_DEFUN([ACX_PTHREAD], [
  146. AC_REQUIRE([AC_CANONICAL_HOST])
  147. AC_LANG_SAVE
  148. AC_LANG_C
  149. acx_pthread_ok=no
  150. # We used to check for pthread.h first, but this fails if pthread.h
  151. # requires special compiler flags (e.g. on True64 or Sequent).
  152. # It gets checked for in the link test anyway.
  153. # First of all, check if the user has set any of the PTHREAD_LIBS,
  154. # etcetera environment variables, and if threads linking works using
  155. # them:
  156. if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
  157. save_CFLAGS="$CFLAGS"
  158. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  159. save_LIBS="$LIBS"
  160. LIBS="$PTHREAD_LIBS $LIBS"
  161. AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
  162. AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
  163. AC_MSG_RESULT($acx_pthread_ok)
  164. if test x"$acx_pthread_ok" = xno; then
  165. PTHREAD_LIBS=""
  166. PTHREAD_CFLAGS=""
  167. fi
  168. LIBS="$save_LIBS"
  169. CFLAGS="$save_CFLAGS"
  170. fi
  171. # We must check for the threads library under a number of different
  172. # names; the ordering is very important because some systems
  173. # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
  174. # libraries is broken (non-POSIX).
  175. # Create a list of thread flags to try. Items starting with a "-" are
  176. # C compiler flags, and other items are library names, except for "none"
  177. # which indicates that we try without any flags at all, and "pthread-config"
  178. # which is a program returning the flags for the Pth emulation library.
  179. acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
  180. # The ordering *is* (sometimes) important. Some notes on the
  181. # individual items follow:
  182. # pthreads: AIX (must check this before -lpthread)
  183. # none: in case threads are in libc; should be tried before -Kthread and
  184. # other compiler flags to prevent continual compiler warnings
  185. # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
  186. # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
  187. # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
  188. # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
  189. # -pthreads: Solaris/gcc
  190. # -mthreads: Mingw32/gcc, Lynx/gcc
  191. # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
  192. # doesn't hurt to check since this sometimes defines pthreads too;
  193. # also defines -D_REENTRANT)
  194. # ... -mt is also the pthreads flag for HP/aCC
  195. # pthread: Linux, etcetera
  196. # --thread-safe: KAI C++
  197. # pthread-config: use pthread-config program (for GNU Pth library)
  198. case "${host_cpu}-${host_os}" in
  199. *solaris*)
  200. # On Solaris (at least, for some versions), libc contains stubbed
  201. # (non-functional) versions of the pthreads routines, so link-based
  202. # tests will erroneously succeed. (We need to link with -pthreads/-mt/
  203. # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
  204. # a function called by this macro, so we could check for that, but
  205. # who knows whether they'll stub that too in a future libc.) So,
  206. # we'll just look for -pthreads and -lpthread first:
  207. acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
  208. ;;
  209. esac
  210. if test x"$acx_pthread_ok" = xno; then
  211. for flag in $acx_pthread_flags; do
  212. case $flag in
  213. none)
  214. AC_MSG_CHECKING([whether pthreads work without any flags])
  215. ;;
  216. -*)
  217. AC_MSG_CHECKING([whether pthreads work with $flag])
  218. PTHREAD_CFLAGS="$flag"
  219. ;;
  220. pthread-config)
  221. AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
  222. if test x"$acx_pthread_config" = xno; then continue; fi
  223. PTHREAD_CFLAGS="`pthread-config --cflags`"
  224. PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
  225. ;;
  226. *)
  227. AC_MSG_CHECKING([for the pthreads library -l$flag])
  228. PTHREAD_LIBS="-l$flag"
  229. ;;
  230. esac
  231. save_LIBS="$LIBS"
  232. save_CFLAGS="$CFLAGS"
  233. LIBS="$PTHREAD_LIBS $LIBS"
  234. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  235. # Check for various functions. We must include pthread.h,
  236. # since some functions may be macros. (On the Sequent, we
  237. # need a special flag -Kthread to make this header compile.)
  238. # We check for pthread_join because it is in -lpthread on IRIX
  239. # while pthread_create is in libc. We check for pthread_attr_init
  240. # due to DEC craziness with -lpthreads. We check for
  241. # pthread_cleanup_push because it is one of the few pthread
  242. # functions on Solaris that doesn't have a non-functional libc stub.
  243. # We try pthread_create on general principles.
  244. AC_TRY_LINK([#include &lt;pthread.h&gt;],
  245. [pthread_t th; pthread_join(th, 0);
  246. pthread_attr_init(0); pthread_cleanup_push(0, 0);
  247. pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
  248. [acx_pthread_ok=yes])
  249. LIBS="$save_LIBS"
  250. CFLAGS="$save_CFLAGS"
  251. AC_MSG_RESULT($acx_pthread_ok)
  252. if test "x$acx_pthread_ok" = xyes; then
  253. break;
  254. fi
  255. PTHREAD_LIBS=""
  256. PTHREAD_CFLAGS=""
  257. done
  258. fi
  259. # Various other checks:
  260. if test "x$acx_pthread_ok" = xyes; then
  261. save_LIBS="$LIBS"
  262. LIBS="$PTHREAD_LIBS $LIBS"
  263. save_CFLAGS="$CFLAGS"
  264. CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
  265. # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
  266. AC_MSG_CHECKING([for joinable pthread attribute])
  267. attr_name=unknown
  268. for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
  269. AC_TRY_LINK([#include &lt;pthread.h&gt;], [int attr=$attr; return attr;],
  270. [attr_name=$attr; break])
  271. done
  272. AC_MSG_RESULT($attr_name)
  273. if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
  274. AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
  275. [Define to necessary symbol if this constant
  276. uses a non-standard name on your system.])
  277. fi
  278. AC_MSG_CHECKING([if more special flags are required for pthreads])
  279. flag=no
  280. case "${host_cpu}-${host_os}" in
  281. *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
  282. *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
  283. esac
  284. AC_MSG_RESULT(${flag})
  285. if test "x$flag" != xno; then
  286. PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
  287. fi
  288. LIBS="$save_LIBS"
  289. CFLAGS="$save_CFLAGS"
  290. # More AIX lossage: must compile with xlc_r or cc_r
  291. if test x"$GCC" != xyes; then
  292. AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
  293. else
  294. PTHREAD_CC=$CC
  295. fi
  296. else
  297. PTHREAD_CC="$CC"
  298. fi
  299. AC_SUBST(PTHREAD_LIBS)
  300. AC_SUBST(PTHREAD_CFLAGS)
  301. AC_SUBST(PTHREAD_CC)
  302. # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
  303. if test x"$acx_pthread_ok" = xyes; then
  304. ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
  305. :
  306. else
  307. acx_pthread_ok=no
  308. $2
  309. fi
  310. AC_LANG_RESTORE
  311. ])dnl ACX_PTHREAD
  312. </pre>
  313. </div>
  314. <h2>
  315. License
  316. </h2>
  317. <div class="indent">
  318. <span style="white-space:nowrap;">Copyright &copy; 2008 Steven G. Johnson
  319. &lt;stevenj@alum.mit.edu&gt;</span>
  320. <p>
  321. This program is free software: you can redistribute it and/or modify it
  322. under the terms of the GNU General Public License as published by the Free
  323. Software Foundation, either version 3 of the License, or (at your option)
  324. any later version.
  325. </p>
  326. <p>
  327. This program is distributed in the hope that it will be useful, but WITHOUT
  328. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  329. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  330. more details.
  331. </p>
  332. <p>
  333. You should have received a copy of the GNU General Public License along
  334. with this program. If not, see &lt;<a href=
  335. "http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>&gt;.
  336. </p>
  337. <p>
  338. As a special exception, the respective Autoconf Macro's copyright owner
  339. gives unlimited permission to copy, distribute and modify the configure
  340. scripts that are the output of Autoconf when processing the Macro. You need
  341. not follow the terms of the GNU General Public License when using or
  342. distributing such scripts, even though portions of the text of the Macro
  343. appear in them. The GNU General Public License (GPL) does govern all other
  344. use of the material that constitutes the Autoconf Macro.
  345. </p>
  346. <p>
  347. This special exception to the GPL applies to versions of the Autoconf Macro
  348. released by the Autoconf Macro Archive. When you make and distribute a
  349. modified version of the Autoconf Macro, you may extend this special
  350. exception to the GPL to apply to your modified version as well.
  351. </p>
  352. </div>
  353. </body>
  354. </html>