threadlib.m4 14 KB

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