Move the JOIN and TOUCH keys to more accessible key combinations.
[m17n/m17n-contrib.git] / m4 / lock.m4
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.
6
7 dnl From Bruno Haible.
8
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
22 AC_DEFUN([gl_LOCK],
23 [
24   AC_REQUIRE([gl_LOCK_BODY])
25 ])
26
27 dnl The guts of gl_LOCK. Needs to be expanded only once.
28
29 AC_DEFUN([gl_LOCK_BODY],
30 [
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
36
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)
44   gl_threads_api=none
45   LIBTHREAD=
46   LTLIBTHREAD=
47   LIBMULTITHREAD=
48   LTLIBMULTITHREAD=
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])
52     gl_have_weak=no
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)
66         #   -lgthreads
67         case "$host_os" in
68           osf*)
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"
76             ;;
77         esac
78         gl_have_pthread=
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.
97              case "$host_os" in
98                solaris* | hpux*)
99                  AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], 1,
100                    [Define if the pthread_in_use() detection is hard.])
101              esac
102             ])
103         else
104           # Some library is needed. Try libpthread and libc_r.
105           AC_CHECK_LIB(pthread, pthread_kill,
106             [gl_have_pthread=yes
107              LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
108              LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
109           if test -z "$gl_have_pthread"; then
110             # For FreeBSD 4.
111             AC_CHECK_LIB(c_r, pthread_kill,
112               [gl_have_pthread=yes
113                LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
114                LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
115           fi
116         fi
117         if test -n "$gl_have_pthread"; then
118           gl_threads_api=posix
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.])
125               LIBTHREAD=
126               LTLIBTHREAD=
127             fi
128           fi
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.])],
134             [],
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."
140 #else
141 int x = (int)PTHREAD_MUTEX_RECURSIVE;
142 #endif],
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>.
148           case "$host_os" in
149             aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
150             solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
151           esac
152         fi
153       fi
154     fi
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=
158         gl_save_LIBS="$LIBS"
159         LIBS="$LIBS -lthread"
160         AC_TRY_LINK([#include <thread.h>
161 #include <synch.h>],
162           [thr_self();],
163           [gl_have_solaristhread=yes])
164         LIBS="$gl_save_LIBS"
165         if test -n "$gl_have_solaristhread"; then
166           gl_threads_api=solaris
167           LIBTHREAD=-lthread
168           LTLIBTHREAD=-lthread
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.])
176             LIBTHREAD=
177             LTLIBTHREAD=
178           fi
179         fi
180       fi
181     fi
182     if test "$gl_use_threads" = pth; then
183       gl_save_CPPFLAGS="$CPPFLAGS"
184       AC_LIB_LINKFLAGS(pth)
185       gl_have_pth=
186       gl_save_LIBS="$LIBS"
187       LIBS="$LIBS -lpth"
188       AC_TRY_LINK([#include <pth.h>], [pth_self();], gl_have_pth=yes)
189       LIBS="$gl_save_LIBS"
190       if test -n "$gl_have_pth"; then
191         gl_threads_api=pth
192         LIBTHREAD="$LIBPTH"
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.])
202             LIBTHREAD=
203             LTLIBTHREAD=
204           fi
205         fi
206       else
207         CPPFLAGS="$gl_save_CPPFLAGS"
208       fi
209     fi
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
213                mingw*) true;;
214                *) false;;
215              esac
216            }; then
217           gl_threads_api=win32
218           AC_DEFINE([USE_WIN32_THREADS], 1,
219             [Define if the Win32 multithreading API can be used.])
220         fi
221       fi
222     fi
223   fi
224   AC_MSG_CHECKING([for multithread API to use])
225   AC_MSG_RESULT([$gl_threads_api])
226   AC_SUBST(LIBTHREAD)
227   AC_SUBST(LTLIBTHREAD)
228   AC_SUBST(LIBMULTITHREAD)
229   AC_SUBST(LTLIBMULTITHREAD)
230   gl_PREREQ_LOCK
231 ])
232
233 # Prerequisites of lib/lock.c.
234 AC_DEFUN([gl_PREREQ_LOCK], [
235   AC_REQUIRE([AC_C_INLINE])
236 ])
237
238 dnl Survey of platforms:
239 dnl
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
244 dnl
245 dnl GNU Hurd/glibc    posix
246 dnl
247 dnl FreeBSD 5.3       posix       -lc_r           Y
248 dnl                   posix       -lkse ?         Y
249 dnl                   posix       -lpthread ?     Y
250 dnl                   posix       -lthr           Y
251 dnl
252 dnl FreeBSD 5.2       posix       -lc_r           Y
253 dnl                   posix       -lkse           Y
254 dnl                   posix       -lthr           Y
255 dnl
256 dnl FreeBSD 4.0,4.10  posix       -lc_r           Y      OK
257 dnl
258 dnl NetBSD 1.6        --
259 dnl
260 dnl OpenBSD 3.4       posix       -lpthread       Y      OK
261 dnl
262 dnl MacOS X 10.[123]  posix       -lpthread       Y      OK
263 dnl
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
266 dnl
267 dnl HP-UX 11          posix       -lpthread       N (cc) OK
268 dnl                                               Y (gcc)
269 dnl
270 dnl IRIX 6.5          posix       -lpthread       Y      0.5
271 dnl
272 dnl AIX 4.3,5.1       posix       -lpthread       N      AIX 4: 0.5; AIX 5: OK
273 dnl
274 dnl OSF/1 4.0,5.1     posix       -pthread (cc)   N      OK
275 dnl                               -lpthread (gcc) Y
276 dnl
277 dnl Cygwin            posix       -lpthread       Y      OK
278 dnl
279 dnl Any of the above  pth         -lpth                  0.0
280 dnl
281 dnl Mingw             win32                       N      OK
282 dnl
283 dnl BeOS 5            --
284 dnl
285 dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
286 dnl turned off:
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.