threadlib.m4 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. # threadlib.m4 serial 11 (gettext-0.18.2)
  2. dnl Copyright (C) 2005-2014 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. case "$host_os" in
  190. solaris* | hpux*)
  191. AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
  192. [Define if the pthread_in_use() detection is hard.])
  193. esac
  194. ])
  195. elif test -z "$gl_have_pthread"; then
  196. # Some library is needed. Try libpthread and libc_r.
  197. AC_CHECK_LIB([pthread], [pthread_kill],
  198. [gl_have_pthread=yes
  199. LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
  200. LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
  201. if test -z "$gl_have_pthread"; then
  202. # For FreeBSD 4.
  203. AC_CHECK_LIB([c_r], [pthread_kill],
  204. [gl_have_pthread=yes
  205. LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
  206. LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
  207. fi
  208. fi
  209. if test -n "$gl_have_pthread"; then
  210. gl_threads_api=posix
  211. AC_DEFINE([USE_POSIX_THREADS], [1],
  212. [Define if the POSIX multithreading library can be used.])
  213. if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
  214. if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
  215. AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
  216. [Define if references to the POSIX multithreading library should be made weak.])
  217. LIBTHREAD=
  218. LTLIBTHREAD=
  219. fi
  220. fi
  221. fi
  222. fi
  223. fi
  224. if test -z "$gl_have_pthread"; then
  225. if test "$gl_use_threads" = yes || test "$gl_use_threads" = solaris; then
  226. gl_have_solaristhread=
  227. gl_save_LIBS="$LIBS"
  228. LIBS="$LIBS -lthread"
  229. AC_LINK_IFELSE(
  230. [AC_LANG_PROGRAM(
  231. [[
  232. #include <thread.h>
  233. #include <synch.h>
  234. ]],
  235. [[thr_self();]])],
  236. [gl_have_solaristhread=yes])
  237. LIBS="$gl_save_LIBS"
  238. if test -n "$gl_have_solaristhread"; then
  239. gl_threads_api=solaris
  240. LIBTHREAD=-lthread
  241. LTLIBTHREAD=-lthread
  242. LIBMULTITHREAD="$LIBTHREAD"
  243. LTLIBMULTITHREAD="$LTLIBTHREAD"
  244. AC_DEFINE([USE_SOLARIS_THREADS], [1],
  245. [Define if the old Solaris multithreading library can be used.])
  246. if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
  247. AC_DEFINE([USE_SOLARIS_THREADS_WEAK], [1],
  248. [Define if references to the old Solaris multithreading library should be made weak.])
  249. LIBTHREAD=
  250. LTLIBTHREAD=
  251. fi
  252. fi
  253. fi
  254. fi
  255. if test "$gl_use_threads" = pth; then
  256. gl_save_CPPFLAGS="$CPPFLAGS"
  257. AC_LIB_LINKFLAGS([pth])
  258. gl_have_pth=
  259. gl_save_LIBS="$LIBS"
  260. LIBS="$LIBS $LIBPTH"
  261. AC_LINK_IFELSE(
  262. [AC_LANG_PROGRAM([[#include <pth.h>]], [[pth_self();]])],
  263. [gl_have_pth=yes])
  264. LIBS="$gl_save_LIBS"
  265. if test -n "$gl_have_pth"; then
  266. gl_threads_api=pth
  267. LIBTHREAD="$LIBPTH"
  268. LTLIBTHREAD="$LTLIBPTH"
  269. LIBMULTITHREAD="$LIBTHREAD"
  270. LTLIBMULTITHREAD="$LTLIBTHREAD"
  271. AC_DEFINE([USE_PTH_THREADS], [1],
  272. [Define if the GNU Pth multithreading library can be used.])
  273. if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
  274. if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
  275. AC_DEFINE([USE_PTH_THREADS_WEAK], [1],
  276. [Define if references to the GNU Pth multithreading library should be made weak.])
  277. LIBTHREAD=
  278. LTLIBTHREAD=
  279. fi
  280. fi
  281. else
  282. CPPFLAGS="$gl_save_CPPFLAGS"
  283. fi
  284. fi
  285. if test -z "$gl_have_pthread"; then
  286. case "$gl_use_threads" in
  287. yes | windows | win32) # The 'win32' is for backward compatibility.
  288. if { case "$host_os" in
  289. mingw*) true;;
  290. *) false;;
  291. esac
  292. }; then
  293. gl_threads_api=windows
  294. AC_DEFINE([USE_WINDOWS_THREADS], [1],
  295. [Define if the native Windows multithreading API can be used.])
  296. fi
  297. ;;
  298. esac
  299. fi
  300. fi
  301. AC_MSG_CHECKING([for multithread API to use])
  302. AC_MSG_RESULT([$gl_threads_api])
  303. AC_SUBST([LIBTHREAD])
  304. AC_SUBST([LTLIBTHREAD])
  305. AC_SUBST([LIBMULTITHREAD])
  306. AC_SUBST([LTLIBMULTITHREAD])
  307. ])
  308. AC_DEFUN([gl_THREADLIB],
  309. [
  310. AC_REQUIRE([gl_THREADLIB_EARLY])
  311. AC_REQUIRE([gl_THREADLIB_BODY])
  312. ])
  313. dnl gl_DISABLE_THREADS
  314. dnl ------------------
  315. dnl Sets the gl_THREADLIB default so that threads are not used by default.
  316. dnl The user can still override it at installation time, by using the
  317. dnl configure option '--enable-threads'.
  318. AC_DEFUN([gl_DISABLE_THREADS], [
  319. m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
  320. ])
  321. dnl Survey of platforms:
  322. dnl
  323. dnl Platform Available Compiler Supports test-lock
  324. dnl flavours option weak result
  325. dnl --------------- --------- --------- -------- ---------
  326. dnl Linux 2.4/glibc posix -lpthread Y OK
  327. dnl
  328. dnl GNU Hurd/glibc posix
  329. dnl
  330. dnl Ubuntu 14.04 posix -pthread Y OK
  331. dnl
  332. dnl FreeBSD 5.3 posix -lc_r Y
  333. dnl posix -lkse ? Y
  334. dnl posix -lpthread ? Y
  335. dnl posix -lthr Y
  336. dnl
  337. dnl FreeBSD 5.2 posix -lc_r Y
  338. dnl posix -lkse Y
  339. dnl posix -lthr Y
  340. dnl
  341. dnl FreeBSD 4.0,4.10 posix -lc_r Y OK
  342. dnl
  343. dnl NetBSD 1.6 --
  344. dnl
  345. dnl OpenBSD 3.4 posix -lpthread Y OK
  346. dnl
  347. dnl Mac OS X 10.[123] posix -lpthread Y OK
  348. dnl
  349. dnl Solaris 7,8,9 posix -lpthread Y Sol 7,8: 0.0; Sol 9: OK
  350. dnl solaris -lthread Y Sol 7,8: 0.0; Sol 9: OK
  351. dnl
  352. dnl HP-UX 11 posix -lpthread N (cc) OK
  353. dnl Y (gcc)
  354. dnl
  355. dnl IRIX 6.5 posix -lpthread Y 0.5
  356. dnl
  357. dnl AIX 4.3,5.1 posix -lpthread N AIX 4: 0.5; AIX 5: OK
  358. dnl
  359. dnl OSF/1 4.0,5.1 posix -pthread (cc) N OK
  360. dnl -lpthread (gcc) Y
  361. dnl
  362. dnl Cygwin posix -lpthread Y OK
  363. dnl
  364. dnl Any of the above pth -lpth 0.0
  365. dnl
  366. dnl Mingw windows N OK
  367. dnl
  368. dnl BeOS 5 --
  369. dnl
  370. dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
  371. dnl turned off:
  372. dnl OK if all three tests terminate OK,
  373. dnl 0.5 if the first test terminates OK but the second one loops endlessly,
  374. dnl 0.0 if the first test already loops endlessly.