1 # lock.m4 serial 2 (gettext-0.15)
2 dnl Copyright (C) 2005 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.
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
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.
24 AC_REQUIRE([gl_LOCK_BODY])
27 dnl The guts of gl_LOCK. Needs to be expanded only once.
29 AC_DEFUN([gl_LOCK_BODY],
31 dnl Ordering constraints: This macro modifies CPPFLAGS in a way that
32 dnl influences the result of the autoconf tests that test for *_unlocked
33 dnl declarations, on AIX 5 at least. Therefore it must come early.
34 AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl
35 AC_BEFORE([$0], [gl_ARGP])dnl
37 AC_REQUIRE([AC_CANONICAL_HOST])
38 AC_REQUIRE([AC_GNU_SOURCE]) dnl needed for pthread_rwlock_t on glibc systems
39 dnl Check for multithreading.
40 AC_ARG_ENABLE(threads,
41 AC_HELP_STRING([--enable-threads={posix|solaris|pth|win32}], [specify multithreading API])
42 AC_HELP_STRING([--disable-threads], [build without multithread safety]),
43 gl_use_threads=$enableval, gl_use_threads=yes)
49 if test "$gl_use_threads" != no; then
50 dnl Check whether the compiler and linker support weak declarations.
51 AC_MSG_CHECKING([whether imported symbols can be declared weak])
53 AC_TRY_LINK([extern void xyzzy ();
54 #pragma weak xyzzy], [xyzzy();], [gl_have_weak=yes])
55 AC_MSG_RESULT([$gl_have_weak])
56 if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
57 # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
58 # it groks <pthread.h>.
59 gl_save_CPPFLAGS="$CPPFLAGS"
60 CPPFLAGS="$CPPFLAGS -D_REENTRANT"
61 AC_CHECK_HEADER(pthread.h, gl_have_pthread_h=yes, gl_have_pthread_h=no)
62 CPPFLAGS="$gl_save_CPPFLAGS"
63 if test "$gl_have_pthread_h" = yes; then
64 # Other possible tests:
65 # -lpthreads (FSU threads, PCthreads)
69 # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
70 # groks <pthread.h>. cc also understands the flag -pthread, but
71 # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
72 # 2. putting a flag into CPPFLAGS that has an effect on the linker
73 # causes the AC_TRY_LINK test below to succeed unexpectedly,
74 # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
75 CPPFLAGS="$CPPFLAGS -D_REENTRANT"
79 # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
80 # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
81 # the second one only in libpthread, and lock.c needs it.
82 AC_TRY_LINK([#include <pthread.h>],
83 [pthread_mutex_lock((pthread_mutex_t*)0);
84 pthread_mutexattr_init((pthread_mutexattr_t*)0);],
85 [gl_have_pthread=yes])
86 # Test for libpthread by looking for pthread_kill. (Not pthread_self,
87 # since it is defined as a macro on OSF/1.)
88 if test -n "$gl_have_pthread"; then
89 # The program links fine without libpthread. But it may actually
90 # need to link with libpthread in order to create multiple threads.
91 AC_CHECK_LIB(pthread, pthread_kill,
92 [LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread
93 # On Solaris and HP-UX, most pthread functions exist also in libc.
94 # Therefore pthread_in_use() needs to actually try to create a
95 # thread: pthread_create from libc will fail, whereas
96 # pthread_create will actually create a thread.
99 AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], 1,
100 [Define if the pthread_in_use() detection is hard.])
104 # Some library is needed. Try libpthread and libc_r.
105 AC_CHECK_LIB(pthread, pthread_kill,
107 LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
108 LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
109 if test -z "$gl_have_pthread"; then
111 AC_CHECK_LIB(c_r, pthread_kill,
113 LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
114 LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
117 if test -n "$gl_have_pthread"; then
119 AC_DEFINE([USE_POSIX_THREADS], 1,
120 [Define if the POSIX multithreading library can be used.])
121 if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
122 if test $gl_have_weak = yes; then
123 AC_DEFINE([USE_POSIX_THREADS_WEAK], 1,
124 [Define if references to the POSIX multithreading library should be made weak.])
129 # OSF/1 4.0 and MacOS X 10.1 lack the pthread_rwlock_t type and the
130 # pthread_rwlock_* functions.
131 AC_CHECK_TYPE([pthread_rwlock_t],
132 [AC_DEFINE([HAVE_PTHREAD_RWLOCK], 1,
133 [Define if the POSIX multithreading library has read/write locks.])],
135 [#include <pthread.h>])
136 # glibc defines PTHREAD_MUTEX_RECURSIVE as enum, not as a macro.
137 AC_TRY_COMPILE([#include <pthread.h>],
138 [#if __FreeBSD__ == 4
139 error "No, in FreeBSD 4.0 recursive mutexes actually don't work."
141 int x = (int)PTHREAD_MUTEX_RECURSIVE;
143 [AC_DEFINE([HAVE_PTHREAD_MUTEX_RECURSIVE], 1,
144 [Define if the <pthread.h> defines PTHREAD_MUTEX_RECURSIVE.])])
145 # Some systems optimize for single-threaded programs by default, and
146 # need special flags to disable these optimizations. For example, the
147 # definition of 'errno' in <errno.h>.
149 aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
150 solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
155 if test -z "$gl_have_pthread"; then
156 if test "$gl_use_threads" = yes || test "$gl_use_threads" = solaris; then
157 gl_have_solaristhread=
159 LIBS="$LIBS -lthread"
160 AC_TRY_LINK([#include <thread.h>
163 [gl_have_solaristhread=yes])
165 if test -n "$gl_have_solaristhread"; then
166 gl_threads_api=solaris
169 LIBMULTITHREAD="$LIBTHREAD"
170 LTLIBMULTITHREAD="$LTLIBTHREAD"
171 AC_DEFINE([USE_SOLARIS_THREADS], 1,
172 [Define if the old Solaris multithreading library can be used.])
173 if test $gl_have_weak = yes; then
174 AC_DEFINE([USE_SOLARIS_THREADS_WEAK], 1,
175 [Define if references to the old Solaris multithreading library should be made weak.])
182 if test "$gl_use_threads" = pth; then
183 gl_save_CPPFLAGS="$CPPFLAGS"
184 AC_LIB_LINKFLAGS(pth)
188 AC_TRY_LINK([#include <pth.h>], [pth_self();], gl_have_pth=yes)
190 if test -n "$gl_have_pth"; then
193 LTLIBTHREAD="$LTLIBPTH"
194 LIBMULTITHREAD="$LIBTHREAD"
195 LTLIBMULTITHREAD="$LTLIBTHREAD"
196 AC_DEFINE([USE_PTH_THREADS], 1,
197 [Define if the GNU Pth multithreading library can be used.])
198 if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
199 if test $gl_have_weak = yes; then
200 AC_DEFINE([USE_PTH_THREADS_WEAK], 1,
201 [Define if references to the GNU Pth multithreading library should be made weak.])
207 CPPFLAGS="$gl_save_CPPFLAGS"
210 if test -z "$gl_have_pthread"; then
211 if test "$gl_use_threads" = yes || test "$gl_use_threads" = win32; then
212 if { case "$host_os" in
218 AC_DEFINE([USE_WIN32_THREADS], 1,
219 [Define if the Win32 multithreading API can be used.])
224 AC_MSG_CHECKING([for multithread API to use])
225 AC_MSG_RESULT([$gl_threads_api])
227 AC_SUBST(LTLIBTHREAD)
228 AC_SUBST(LIBMULTITHREAD)
229 AC_SUBST(LTLIBMULTITHREAD)
233 # Prerequisites of lib/lock.c.
234 AC_DEFUN([gl_PREREQ_LOCK], [
235 AC_REQUIRE([AC_C_INLINE])
238 dnl Survey of platforms:
240 dnl Platform Available Compiler Supports test-lock
241 dnl flavours option weak result
242 dnl --------------- --------- --------- -------- ---------
243 dnl Linux 2.4/glibc posix -lpthread Y OK
245 dnl GNU Hurd/glibc posix
247 dnl FreeBSD 5.3 posix -lc_r Y
249 dnl posix -lpthread ? Y
252 dnl FreeBSD 5.2 posix -lc_r Y
256 dnl FreeBSD 4.0,4.10 posix -lc_r Y OK
260 dnl OpenBSD 3.4 posix -lpthread Y OK
262 dnl MacOS X 10.[123] posix -lpthread Y OK
264 dnl Solaris 7,8,9 posix -lpthread Y Sol 7,8: 0.0; Sol 9: OK
265 dnl solaris -lthread Y Sol 7,8: 0.0; Sol 9: OK
267 dnl HP-UX 11 posix -lpthread N (cc) OK
270 dnl IRIX 6.5 posix -lpthread Y 0.5
272 dnl AIX 4.3,5.1 posix -lpthread N AIX 4: 0.5; AIX 5: OK
274 dnl OSF/1 4.0,5.1 posix -pthread (cc) N OK
275 dnl -lpthread (gcc) Y
277 dnl Cygwin posix -lpthread Y OK
279 dnl Any of the above pth -lpth 0.0
285 dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
287 dnl OK if all three tests terminate OK,
288 dnl 0.5 if the first test terminates OK but the second one loops endlessly,
289 dnl 0.0 if the first test already loops endlessly.