threadlib.m4 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. # threadlib.m4 serial 12
  2. dnl Copyright (C) 2005-2017 Free Software Foundation, Inc.
  3. dnl This file is free software; the Free Software Foundation
  4. dnl gives unlimited permission to copy and/or distribute it,
  5. dnl with or without modifications, as long as this notice is preserved.
  6. dnl From Bruno Haible.
  7. dnl gl_THREADLIB
  8. dnl ------------
  9. dnl Tests for a multithreading library to be used.
  10. dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO
  11. dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the
  12. dnl default is 'no', otherwise it is system dependent. In both cases, the user
  13. dnl can change the choice through the options --enable-threads=choice or
  14. dnl --disable-threads.
  15. dnl Defines at most one of the macros USE_POSIX_THREADS, USE_SOLARIS_THREADS,
  16. dnl USE_PTH_THREADS, USE_WINDOWS_THREADS
  17. dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use
  18. dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with
  19. dnl libtool).
  20. dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for
  21. dnl programs that really need multithread functionality. The difference
  22. dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak
  23. dnl symbols, typically LIBTHREAD is empty whereas LIBMULTITHREAD is not.
  24. dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
  25. dnl multithread-safe programs.
  26. AC_DEFUN([gl_THREADLIB_EARLY],
  27. [
  28. AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
  29. ])
  30. dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once.
  31. AC_DEFUN([gl_THREADLIB_EARLY_BODY],
  32. [
  33. dnl Ordering constraints: This macro modifies CPPFLAGS in a way that
  34. dnl influences the result of the autoconf tests that test for *_unlocked
  35. dnl declarations, on AIX 5 at least. Therefore it must come early.
  36. AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl
  37. AC_BEFORE([$0], [gl_ARGP])dnl
  38. AC_REQUIRE([AC_CANONICAL_HOST])
  39. dnl _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems.
  40. dnl AC_USE_SYSTEM_EXTENSIONS was introduced in autoconf 2.60 and obsoletes
  41. dnl AC_GNU_SOURCE.
  42. m4_ifdef([AC_USE_SYSTEM_EXTENSIONS],
  43. [AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])],
  44. [AC_REQUIRE([AC_GNU_SOURCE])])
  45. dnl Check for multithreading.
  46. m4_ifdef([gl_THREADLIB_DEFAULT_NO],
  47. [m4_divert_text([DEFAULTS], [gl_use_threads_default=no])],
  48. [m4_divert_text([DEFAULTS], [gl_use_threads_default=])])
  49. AC_ARG_ENABLE([threads],
  50. AC_HELP_STRING([--enable-threads={posix|solaris|pth|windows}], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [
  51. AC_HELP_STRING([--disable-threads], [build without multithread safety])]),
  52. [gl_use_threads=$enableval],
  53. [if test -n "$gl_use_threads_default"; then
  54. gl_use_threads="$gl_use_threads_default"
  55. else
  56. changequote(,)dnl
  57. case "$host_os" in
  58. dnl Disable multithreading by default on OSF/1, because it interferes
  59. dnl with fork()/exec(): When msgexec is linked with -lpthread, its
  60. dnl child process gets an endless segmentation fault inside execvp().
  61. dnl Disable multithreading by default on Cygwin 1.5.x, because it has
  62. dnl bugs that lead to endless loops or crashes. See
  63. dnl <http://cygwin.com/ml/cygwin/2009-08/msg00283.html>.
  64. osf*) gl_use_threads=no ;;
  65. cygwin*)
  66. case `uname -r` in
  67. 1.[0-5].*) gl_use_threads=no ;;
  68. *) gl_use_threads=yes ;;
  69. esac
  70. ;;
  71. *) gl_use_threads=yes ;;
  72. esac
  73. changequote([,])dnl
  74. fi
  75. ])
  76. if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
  77. # For using <pthread.h>:
  78. case "$host_os" in
  79. osf*)
  80. # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
  81. # groks <pthread.h>. cc also understands the flag -pthread, but
  82. # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
  83. # 2. putting a flag into CPPFLAGS that has an effect on the linker
  84. # causes the AC_LINK_IFELSE test below to succeed unexpectedly,
  85. # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
  86. CPPFLAGS="$CPPFLAGS -D_REENTRANT"
  87. ;;
  88. esac
  89. # Some systems optimize for single-threaded programs by default, and
  90. # need special flags to disable these optimizations. For example, the
  91. # definition of 'errno' in <errno.h>.
  92. case "$host_os" in
  93. aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
  94. solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
  95. esac
  96. fi
  97. ])
  98. dnl The guts of gl_THREADLIB. Needs to be expanded only once.
  99. AC_DEFUN([gl_THREADLIB_BODY],
  100. [
  101. AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
  102. gl_threads_api=none
  103. LIBTHREAD=
  104. LTLIBTHREAD=
  105. LIBMULTITHREAD=
  106. LTLIBMULTITHREAD=
  107. if test "$gl_use_threads" != no; then
  108. dnl Check whether the compiler and linker support weak declarations.
  109. AC_CACHE_CHECK([whether imported symbols can be declared weak],
  110. [gl_cv_have_weak],
  111. [gl_cv_have_weak=no
  112. dnl First, test whether the compiler accepts it syntactically.
  113. AC_LINK_IFELSE(
  114. [AC_LANG_PROGRAM(
  115. [[extern void xyzzy ();
  116. #pragma weak xyzzy]],
  117. [[xyzzy();]])],
  118. [gl_cv_have_weak=maybe])
  119. if test $gl_cv_have_weak = maybe; then
  120. dnl Second, test whether it actually works. On Cygwin 1.7.2, with
  121. dnl gcc 4.3, symbols declared weak always evaluate to the address 0.
  122. AC_RUN_IFELSE(
  123. [AC_LANG_SOURCE([[
  124. #include <stdio.h>
  125. #pragma weak fputs
  126. int main ()
  127. {
  128. return (fputs == NULL);
  129. }]])],
  130. [gl_cv_have_weak=yes],
  131. [gl_cv_have_weak=no],
  132. [dnl When cross-compiling, assume that only ELF platforms support
  133. dnl weak symbols.
  134. AC_EGREP_CPP([Extensible Linking Format],
  135. [#ifdef __ELF__
  136. Extensible Linking Format
  137. #endif
  138. ],
  139. [gl_cv_have_weak="guessing yes"],
  140. [gl_cv_have_weak="guessing no"])
  141. ])
  142. fi
  143. ])
  144. if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
  145. # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
  146. # it groks <pthread.h>. It's added above, in gl_THREADLIB_EARLY_BODY.
  147. AC_CHECK_HEADER([pthread.h],
  148. [gl_have_pthread_h=yes], [gl_have_pthread_h=no])
  149. if test "$gl_have_pthread_h" = yes; then
  150. # Other possible tests:
  151. # -lpthreads (FSU threads, PCthreads)
  152. # -lgthreads
  153. gl_have_pthread=
  154. # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
  155. # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
  156. # the second one only in libpthread, and lock.c needs it.
  157. #
  158. # If -pthread works, prefer it to -lpthread, since Ubuntu 14.04
  159. # needs -pthread for some reason. See:
  160. # http://lists.gnu.org/archive/html/bug-gnulib/2014-09/msg00023.html
  161. save_LIBS=$LIBS
  162. for gl_pthread in '' '-pthread'; do
  163. LIBS="$LIBS $gl_pthread"
  164. AC_LINK_IFELSE(
  165. [AC_LANG_PROGRAM(
  166. [[#include <pthread.h>
  167. pthread_mutex_t m;
  168. pthread_mutexattr_t ma;
  169. ]],
  170. [[pthread_mutex_lock (&m);
  171. pthread_mutexattr_init (&ma);]])],
  172. [gl_have_pthread=yes
  173. LIBTHREAD=$gl_pthread LTLIBTHREAD=$gl_pthread
  174. LIBMULTITHREAD=$gl_pthread LTLIBMULTITHREAD=$gl_pthread])
  175. LIBS=$save_LIBS
  176. test -n "$gl_have_pthread" && break
  177. done
  178. # Test for libpthread by looking for pthread_kill. (Not pthread_self,
  179. # since it is defined as a macro on OSF/1.)
  180. if test -n "$gl_have_pthread" && test -z "$LIBTHREAD"; then
  181. # The program links fine without libpthread. But it may actually
  182. # need to link with libpthread in order to create multiple threads.
  183. AC_CHECK_LIB([pthread], [pthread_kill],
  184. [LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread
  185. # On Solaris and HP-UX, most pthread functions exist also in libc.
  186. # Therefore pthread_in_use() needs to actually try to create a
  187. # thread: pthread_create from libc will fail, whereas
  188. # pthread_create will actually create a thread.
  189. # On Solaris 10 or newer, this test is no longer needed, because
  190. # libc contains the fully functional pthread functions.
  191. case "$host_os" in
  192. solaris | solaris2.[1-9] | solaris2.[1-9].* | hpux*)
  193. AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
  194. [Define if the pthread_in_use() detection is hard.])
  195. esac
  196. ])
  197. elif test -z "$gl_have_pthread"; then
  198. # Some library is needed. Try libpthread and libc_r.
  199. AC_CHECK_LIB([pthread], [pthread_kill],
  200. [gl_have_pthread=yes
  201. LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
  202. LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
  203. if test -z "$gl_have_pthread"; then
  204. # For FreeBSD 4.
  205. AC_CHECK_LIB([c_r], [pthread_kill],
  206. [gl_have_pthread=yes
  207. LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
  208. LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
  209. fi
  210. fi
  211. if test -n "$gl_have_pthread"; then
  212. gl_threads_api=posix
  213. AC_DEFINE([USE_POSIX_THREADS], [1],
  214. [Define if the POSIX multithreading library can be used.])
  215. if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
  216. if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
  217. AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
  218. [Define if references to the POSIX multithreading library should be made weak.])
  219. LIBTHREAD=
  220. LTLIBTHREAD=
  221. fi
  222. fi
  223. fi
  224. fi
  225. fi
  226. if test -z "$gl_have_pthread"; then
  227. if test "$gl_use_threads" = yes || test "$gl_use_threads" = solaris; then
  228. gl_have_solaristhread=
  229. gl_save_LIBS="$LIBS"
  230. LIBS="$LIBS -lthread"
  231. AC_LINK_IFELSE(
  232. [AC_LANG_PROGRAM(
  233. [[
  234. #include <thread.h>
  235. #include <synch.h>
  236. ]],
  237. [[thr_self();]])],
  238. [gl_have_solaristhread=yes])
  239. LIBS="$gl_save_LIBS"
  240. if test -n "$gl_have_solaristhread"; then
  241. gl_threads_api=solaris
  242. LIBTHREAD=-lthread
  243. LTLIBTHREAD=-lthread
  244. LIBMULTITHREAD="$LIBTHREAD"
  245. LTLIBMULTITHREAD="$LTLIBTHREAD"
  246. AC_DEFINE([USE_SOLARIS_THREADS], [1],
  247. [Define if the old Solaris multithreading library can be used.])
  248. if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
  249. AC_DEFINE([USE_SOLARIS_THREADS_WEAK], [1],
  250. [Define if references to the old Solaris multithreading library should be made weak.])
  251. LIBTHREAD=
  252. LTLIBTHREAD=
  253. fi
  254. fi
  255. fi
  256. fi
  257. if test "$gl_use_threads" = pth; then
  258. gl_save_CPPFLAGS="$CPPFLAGS"
  259. AC_LIB_LINKFLAGS([pth])
  260. gl_have_pth=
  261. gl_save_LIBS="$LIBS"
  262. LIBS="$LIBS $LIBPTH"
  263. AC_LINK_IFELSE(
  264. [AC_LANG_PROGRAM([[#include <pth.h>]], [[pth_self();]])],
  265. [gl_have_pth=yes])
  266. LIBS="$gl_save_LIBS"
  267. if test -n "$gl_have_pth"; then
  268. gl_threads_api=pth
  269. LIBTHREAD="$LIBPTH"
  270. LTLIBTHREAD="$LTLIBPTH"
  271. LIBMULTITHREAD="$LIBTHREAD"
  272. LTLIBMULTITHREAD="$LTLIBTHREAD"
  273. AC_DEFINE([USE_PTH_THREADS], [1],
  274. [Define if the GNU Pth multithreading library can be used.])
  275. if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
  276. if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
  277. AC_DEFINE([USE_PTH_THREADS_WEAK], [1],
  278. [Define if references to the GNU Pth multithreading library should be made weak.])
  279. LIBTHREAD=
  280. LTLIBTHREAD=
  281. fi
  282. fi
  283. else
  284. CPPFLAGS="$gl_save_CPPFLAGS"
  285. fi
  286. fi
  287. if test -z "$gl_have_pthread"; then
  288. case "$gl_use_threads" in
  289. yes | windows | win32) # The 'win32' is for backward compatibility.
  290. if { case "$host_os" in
  291. mingw*) true;;
  292. *) false;;
  293. esac
  294. }; then
  295. gl_threads_api=windows
  296. AC_DEFINE([USE_WINDOWS_THREADS], [1],
  297. [Define if the native Windows multithreading API can be used.])
  298. fi
  299. ;;
  300. esac
  301. fi
  302. fi
  303. AC_MSG_CHECKING([for multithread API to use])
  304. AC_MSG_RESULT([$gl_threads_api])
  305. AC_SUBST([LIBTHREAD])
  306. AC_SUBST([LTLIBTHREAD])
  307. AC_SUBST([LIBMULTITHREAD])
  308. AC_SUBST([LTLIBMULTITHREAD])
  309. ])
  310. AC_DEFUN([gl_THREADLIB],
  311. [
  312. AC_REQUIRE([gl_THREADLIB_EARLY])
  313. AC_REQUIRE([gl_THREADLIB_BODY])
  314. ])
  315. dnl gl_DISABLE_THREADS
  316. dnl ------------------
  317. dnl Sets the gl_THREADLIB default so that threads are not used by default.
  318. dnl The user can still override it at installation time, by using the
  319. dnl configure option '--enable-threads'.
  320. AC_DEFUN([gl_DISABLE_THREADS], [
  321. m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
  322. ])
  323. dnl Survey of platforms:
  324. dnl
  325. dnl Platform Available Compiler Supports test-lock
  326. dnl flavours option weak result
  327. dnl --------------- --------- --------- -------- ---------
  328. dnl Linux 2.4/glibc posix -lpthread Y OK
  329. dnl
  330. dnl GNU Hurd/glibc posix
  331. dnl
  332. dnl Ubuntu 14.04 posix -pthread Y OK
  333. dnl
  334. dnl FreeBSD 5.3 posix -lc_r Y
  335. dnl posix -lkse ? Y
  336. dnl posix -lpthread ? Y
  337. dnl posix -lthr Y
  338. dnl
  339. dnl FreeBSD 5.2 posix -lc_r Y
  340. dnl posix -lkse Y
  341. dnl posix -lthr Y
  342. dnl
  343. dnl FreeBSD 4.0,4.10 posix -lc_r Y OK
  344. dnl
  345. dnl NetBSD 1.6 --
  346. dnl
  347. dnl OpenBSD 3.4 posix -lpthread Y OK
  348. dnl
  349. dnl Mac OS X 10.[123] posix -lpthread Y OK
  350. dnl
  351. dnl Solaris 7,8,9 posix -lpthread Y Sol 7,8: 0.0; Sol 9: OK
  352. dnl solaris -lthread Y Sol 7,8: 0.0; Sol 9: OK
  353. dnl
  354. dnl HP-UX 11 posix -lpthread N (cc) OK
  355. dnl Y (gcc)
  356. dnl
  357. dnl IRIX 6.5 posix -lpthread Y 0.5
  358. dnl
  359. dnl AIX 4.3,5.1 posix -lpthread N AIX 4: 0.5; AIX 5: OK
  360. dnl
  361. dnl OSF/1 4.0,5.1 posix -pthread (cc) N OK
  362. dnl -lpthread (gcc) Y
  363. dnl
  364. dnl Cygwin posix -lpthread Y OK
  365. dnl
  366. dnl Any of the above pth -lpth 0.0
  367. dnl
  368. dnl Mingw windows N OK
  369. dnl
  370. dnl BeOS 5 --
  371. dnl
  372. dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
  373. dnl turned off:
  374. dnl OK if all three tests terminate OK,
  375. dnl 0.5 if the first test terminates OK but the second one loops endlessly,
  376. dnl 0.0 if the first test already loops endlessly.