threadlib.m4 13 KB

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