threadlib.m4 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. # threadlib.m4 serial 3 (gettext-0.18)
  2. dnl Copyright (C) 2005-2009 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. case "$host_os" in
  50. dnl Disable multithreading by default on OSF/1, because it interferes
  51. dnl with fork()/exec(): When msgexec is linked with -lpthread, its
  52. dnl child process gets an endless segmentation fault inside execvp().
  53. osf*) gl_use_threads=no ;;
  54. *) gl_use_threads=yes ;;
  55. esac
  56. fi
  57. ])
  58. if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
  59. # For using <pthread.h>:
  60. case "$host_os" in
  61. osf*)
  62. # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
  63. # groks <pthread.h>. cc also understands the flag -pthread, but
  64. # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
  65. # 2. putting a flag into CPPFLAGS that has an effect on the linker
  66. # causes the AC_TRY_LINK test below to succeed unexpectedly,
  67. # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
  68. CPPFLAGS="$CPPFLAGS -D_REENTRANT"
  69. ;;
  70. esac
  71. # Some systems optimize for single-threaded programs by default, and
  72. # need special flags to disable these optimizations. For example, the
  73. # definition of 'errno' in <errno.h>.
  74. case "$host_os" in
  75. aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
  76. solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
  77. esac
  78. fi
  79. ])
  80. dnl The guts of gl_THREADLIB. Needs to be expanded only once.
  81. AC_DEFUN([gl_THREADLIB_BODY],
  82. [
  83. AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
  84. gl_threads_api=none
  85. LIBTHREAD=
  86. LTLIBTHREAD=
  87. LIBMULTITHREAD=
  88. LTLIBMULTITHREAD=
  89. if test "$gl_use_threads" != no; then
  90. dnl Check whether the compiler and linker support weak declarations.
  91. AC_MSG_CHECKING([whether imported symbols can be declared weak])
  92. gl_have_weak=no
  93. AC_TRY_LINK([extern void xyzzy ();
  94. #pragma weak xyzzy], [xyzzy();], [gl_have_weak=yes])
  95. AC_MSG_RESULT([$gl_have_weak])
  96. if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
  97. # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
  98. # it groks <pthread.h>. It's added above, in gl_THREADLIB_EARLY_BODY.
  99. AC_CHECK_HEADER([pthread.h],
  100. [gl_have_pthread_h=yes], [gl_have_pthread_h=no])
  101. if test "$gl_have_pthread_h" = yes; then
  102. # Other possible tests:
  103. # -lpthreads (FSU threads, PCthreads)
  104. # -lgthreads
  105. gl_have_pthread=
  106. # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
  107. # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
  108. # the second one only in libpthread, and lock.c needs it.
  109. AC_TRY_LINK([#include <pthread.h>],
  110. [pthread_mutex_lock((pthread_mutex_t*)0);
  111. pthread_mutexattr_init((pthread_mutexattr_t*)0);],
  112. [gl_have_pthread=yes])
  113. # Test for libpthread by looking for pthread_kill. (Not pthread_self,
  114. # since it is defined as a macro on OSF/1.)
  115. if test -n "$gl_have_pthread"; then
  116. # The program links fine without libpthread. But it may actually
  117. # need to link with libpthread in order to create multiple threads.
  118. AC_CHECK_LIB([pthread], [pthread_kill],
  119. [LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread
  120. # On Solaris and HP-UX, most pthread functions exist also in libc.
  121. # Therefore pthread_in_use() needs to actually try to create a
  122. # thread: pthread_create from libc will fail, whereas
  123. # pthread_create will actually create a thread.
  124. case "$host_os" in
  125. solaris* | hpux*)
  126. AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
  127. [Define if the pthread_in_use() detection is hard.])
  128. esac
  129. ])
  130. else
  131. # Some library is needed. Try libpthread and libc_r.
  132. AC_CHECK_LIB([pthread], [pthread_kill],
  133. [gl_have_pthread=yes
  134. LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
  135. LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
  136. if test -z "$gl_have_pthread"; then
  137. # For FreeBSD 4.
  138. AC_CHECK_LIB([c_r], [pthread_kill],
  139. [gl_have_pthread=yes
  140. LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
  141. LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
  142. fi
  143. fi
  144. if test -n "$gl_have_pthread"; then
  145. gl_threads_api=posix
  146. AC_DEFINE([USE_POSIX_THREADS], [1],
  147. [Define if the POSIX multithreading library can be used.])
  148. if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
  149. if test $gl_have_weak = yes; then
  150. AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
  151. [Define if references to the POSIX multithreading library should be made weak.])
  152. LIBTHREAD=
  153. LTLIBTHREAD=
  154. fi
  155. fi
  156. fi
  157. fi
  158. fi
  159. if test -z "$gl_have_pthread"; then
  160. if test "$gl_use_threads" = yes || test "$gl_use_threads" = solaris; then
  161. gl_have_solaristhread=
  162. gl_save_LIBS="$LIBS"
  163. LIBS="$LIBS -lthread"
  164. AC_TRY_LINK([#include <thread.h>
  165. #include <synch.h>],
  166. [thr_self();],
  167. [gl_have_solaristhread=yes])
  168. LIBS="$gl_save_LIBS"
  169. if test -n "$gl_have_solaristhread"; then
  170. gl_threads_api=solaris
  171. LIBTHREAD=-lthread
  172. LTLIBTHREAD=-lthread
  173. LIBMULTITHREAD="$LIBTHREAD"
  174. LTLIBMULTITHREAD="$LTLIBTHREAD"
  175. AC_DEFINE([USE_SOLARIS_THREADS], [1],
  176. [Define if the old Solaris multithreading library can be used.])
  177. if test $gl_have_weak = yes; then
  178. AC_DEFINE([USE_SOLARIS_THREADS_WEAK], [1],
  179. [Define if references to the old Solaris multithreading library should be made weak.])
  180. LIBTHREAD=
  181. LTLIBTHREAD=
  182. fi
  183. fi
  184. fi
  185. fi
  186. if test "$gl_use_threads" = pth; then
  187. gl_save_CPPFLAGS="$CPPFLAGS"
  188. AC_LIB_LINKFLAGS([pth])
  189. gl_have_pth=
  190. gl_save_LIBS="$LIBS"
  191. LIBS="$LIBS -lpth"
  192. AC_TRY_LINK([#include <pth.h>], [pth_self();], [gl_have_pth=yes])
  193. LIBS="$gl_save_LIBS"
  194. if test -n "$gl_have_pth"; then
  195. gl_threads_api=pth
  196. LIBTHREAD="$LIBPTH"
  197. LTLIBTHREAD="$LTLIBPTH"
  198. LIBMULTITHREAD="$LIBTHREAD"
  199. LTLIBMULTITHREAD="$LTLIBTHREAD"
  200. AC_DEFINE([USE_PTH_THREADS], [1],
  201. [Define if the GNU Pth multithreading library can be used.])
  202. if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
  203. if test $gl_have_weak = yes; then
  204. AC_DEFINE([USE_PTH_THREADS_WEAK], [1],
  205. [Define if references to the GNU Pth multithreading library should be made weak.])
  206. LIBTHREAD=
  207. LTLIBTHREAD=
  208. fi
  209. fi
  210. else
  211. CPPFLAGS="$gl_save_CPPFLAGS"
  212. fi
  213. fi
  214. if test -z "$gl_have_pthread"; then
  215. if test "$gl_use_threads" = yes || test "$gl_use_threads" = win32; then
  216. if { case "$host_os" in
  217. mingw*) true;;
  218. *) false;;
  219. esac
  220. }; then
  221. gl_threads_api=win32
  222. AC_DEFINE([USE_WIN32_THREADS], [1],
  223. [Define if the Win32 multithreading API can be used.])
  224. fi
  225. fi
  226. fi
  227. fi
  228. AC_MSG_CHECKING([for multithread API to use])
  229. AC_MSG_RESULT([$gl_threads_api])
  230. AC_SUBST([LIBTHREAD])
  231. AC_SUBST([LTLIBTHREAD])
  232. AC_SUBST([LIBMULTITHREAD])
  233. AC_SUBST([LTLIBMULTITHREAD])
  234. ])
  235. AC_DEFUN([gl_THREADLIB],
  236. [
  237. AC_REQUIRE([gl_THREADLIB_EARLY])
  238. AC_REQUIRE([gl_THREADLIB_BODY])
  239. ])
  240. dnl gl_DISABLE_THREADS
  241. dnl ------------------
  242. dnl Sets the gl_THREADLIB default so that threads are not used by default.
  243. dnl The user can still override it at installation time, by using the
  244. dnl configure option '--enable-threads'.
  245. AC_DEFUN([gl_DISABLE_THREADS], [
  246. m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
  247. ])
  248. dnl Survey of platforms:
  249. dnl
  250. dnl Platform Available Compiler Supports test-lock
  251. dnl flavours option weak result
  252. dnl --------------- --------- --------- -------- ---------
  253. dnl Linux 2.4/glibc posix -lpthread Y OK
  254. dnl
  255. dnl GNU Hurd/glibc posix
  256. dnl
  257. dnl FreeBSD 5.3 posix -lc_r Y
  258. dnl posix -lkse ? Y
  259. dnl posix -lpthread ? Y
  260. dnl posix -lthr Y
  261. dnl
  262. dnl FreeBSD 5.2 posix -lc_r Y
  263. dnl posix -lkse Y
  264. dnl posix -lthr Y
  265. dnl
  266. dnl FreeBSD 4.0,4.10 posix -lc_r Y OK
  267. dnl
  268. dnl NetBSD 1.6 --
  269. dnl
  270. dnl OpenBSD 3.4 posix -lpthread Y OK
  271. dnl
  272. dnl MacOS X 10.[123] posix -lpthread Y OK
  273. dnl
  274. dnl Solaris 7,8,9 posix -lpthread Y Sol 7,8: 0.0; Sol 9: OK
  275. dnl solaris -lthread Y Sol 7,8: 0.0; Sol 9: OK
  276. dnl
  277. dnl HP-UX 11 posix -lpthread N (cc) OK
  278. dnl Y (gcc)
  279. dnl
  280. dnl IRIX 6.5 posix -lpthread Y 0.5
  281. dnl
  282. dnl AIX 4.3,5.1 posix -lpthread N AIX 4: 0.5; AIX 5: OK
  283. dnl
  284. dnl OSF/1 4.0,5.1 posix -pthread (cc) N OK
  285. dnl -lpthread (gcc) Y
  286. dnl
  287. dnl Cygwin posix -lpthread Y OK
  288. dnl
  289. dnl Any of the above pth -lpth 0.0
  290. dnl
  291. dnl Mingw win32 N OK
  292. dnl
  293. dnl BeOS 5 --
  294. dnl
  295. dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
  296. dnl turned off:
  297. dnl OK if all three tests terminate OK,
  298. dnl 0.5 if the first test terminates OK but the second one loops endlessly,
  299. dnl 0.0 if the first test already loops endlessly.