New gnulib module 'lock'.
[gnulib.git] / m4 / lock.m4
1 # lock.m4 serial 1 (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
20 AC_DEFUN([gl_LOCK],
21 [
22   AC_REQUIRE([AC_CANONICAL_HOST])
23   AC_REQUIRE([AC_GNU_SOURCE]) dnl needed for pthread_rwlock_t on glibc systems
24   dnl Check for multithreading.
25   AC_ARG_ENABLE(threads,
26 AC_HELP_STRING([--enable-threads={posix|solaris|pth|win32}], [specify multithreading API])
27 AC_HELP_STRING([--disable-threads], [build without multithread safety]),
28     gl_use_threads=$enableval, gl_use_threads=yes)
29   gl_threads_api=none
30   LIBTHREAD=
31   LTLIBTHREAD=
32   LIBMULTITHREAD=
33   LTLIBMULTITHREAD=
34   if test "$gl_use_threads" != no; then
35     dnl Check whether the compiler and linker support weak declarations.
36     AC_MSG_CHECKING([whether imported symbols can be declared weak])
37     gl_have_weak=no
38     AC_TRY_LINK([extern void xyzzy ();
39 #pragma weak xyzzy], [xyzzy();], [gl_have_weak=yes])
40     AC_MSG_RESULT([$gl_have_weak])
41     if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
42       # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
43       # it groks <pthread.h>.
44       gl_save_CPPFLAGS="$CPPFLAGS"
45       CPPFLAGS="$CPPFLAGS -D_REENTRANT"
46       AC_CHECK_HEADER(pthread.h, gl_have_pthread_h=yes, gl_have_pthread_h=no)
47       CPPFLAGS="$gl_save_CPPFLAGS"
48       if test "$gl_have_pthread_h" = yes; then
49         # Other possible tests:
50         #   -lpthreads (FSU threads, PCthreads)
51         #   -lgthreads
52         case "$host_os" in
53           osf*)
54             # On OSF/1, the compiler needs the flag -pthread so that it groks
55             # <pthread.h>. For the linker, it is equivalent to -lpthread.
56             if test -n "$GCC"; then
57               # gcc-2.95 doesn't understand -pthread, only -D_REENTRANT.
58               CPPFLAGS="$CPPFLAGS -D_REENTRANT"
59             else
60               CPPFLAGS="$CPPFLAGS -pthread"
61             fi
62             ;;
63         esac
64         gl_have_pthread=
65         AC_TRY_LINK([#include <pthread.h>],
66           [pthread_mutex_lock((pthread_mutex_t*)0);],
67           [gl_have_pthread=yes])
68         # Test for libpthread by looking for pthread_kill. (Not pthread_self,
69         # since it is defined as a macro on OSF/1.)
70         if test -n "$gl_have_pthread"; then
71           # The program links fine without libpthread. But it may actually
72           # need to link with libpthread in order to create multiple threads.
73           AC_CHECK_LIB(pthread, pthread_kill,
74             [LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
75         else
76           # Some library is needed. Try libpthread and libc_r.
77           AC_CHECK_LIB(pthread, pthread_kill,
78             [gl_have_pthread=yes
79              LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
80              LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
81           if test -z "$gl_have_pthread"; then
82             # For FreeBSD 4.
83             AC_CHECK_LIB(c_r, pthread_kill,
84               [gl_have_pthread=yes
85                LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
86                LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
87           fi
88         fi
89         if test -n "$gl_have_pthread"; then
90           gl_threads_api=posix
91           AC_DEFINE([USE_POSIX_THREADS], 1,
92             [Define if the POSIX multithreading library can be used.])
93           if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
94             if test $gl_have_weak = yes; then
95               AC_DEFINE([USE_POSIX_THREADS_WEAK], 1,
96                 [Define if references to the POSIX multithreading library should be made weak.])
97               LIBTHREAD=
98               LTLIBTHREAD=
99             fi
100           fi
101           # OSF/1 4.0 and MacOS X 10.1 lack the pthread_rwlock_t type and the
102           # pthread_rwlock_* functions.
103           AC_CHECK_TYPE([pthread_rwlock_t],
104             [AC_DEFINE([HAVE_PTHREAD_RWLOCK], 1,
105                [Define if the POSIX multithreading library has read/write locks.])],
106             [],
107             [#include <pthread.h>])
108           # glibc defines PTHREAD_MUTEX_RECURSIVE as enum, not as a macro.
109           AC_TRY_COMPILE([#include <pthread.h>],
110             [#if __FreeBSD__ == 4
111 error "No, in FreeBSD 4.0 recursive mutexes actually don't work."
112 #else
113 int x = (int)PTHREAD_MUTEX_RECURSIVE;
114 #endif],
115             [AC_DEFINE([HAVE_PTHREAD_MUTEX_RECURSIVE], 1,
116                [Define if the <pthread.h> defines PTHREAD_MUTEX_RECURSIVE.])])
117           # Some systems optimize for single-threaded programs by default, and
118           # need special flags to disable these optimizations.
119           case "$host_os" in
120             aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
121             solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
122           esac
123         fi
124       fi
125     fi
126     if test -z "$gl_have_pthread"; then
127       if test "$gl_use_threads" = yes || test "$gl_use_threads" = solaris; then
128         gl_have_solaristhread=
129         gl_save_LIBS="$LIBS"
130         LIBS="$LIBS -lthread"
131         AC_TRY_LINK([#include <thread.h>
132 #include <synch.h>],
133           [thr_self();],
134           [gl_have_solaristhread=yes])
135         LIBS="$gl_save_LIBS"
136         if test -n "$gl_have_solaristhread"; then
137           gl_threads_api=solaris
138           LIBTHREAD=-lthread
139           LTLIBTHREAD=-lthread
140           LIBMULTITHREAD="$LIBTHREAD"
141           LTLIBMULTITHREAD="$LTLIBTHREAD"
142           AC_DEFINE([USE_SOLARIS_THREADS], 1,
143             [Define if the old Solaris multithreading library can be used.])
144           if test $gl_have_weak = yes; then
145             AC_DEFINE([USE_SOLARIS_THREADS_WEAK], 1,
146               [Define if references to the old Solaris multithreading library should be made weak.])
147             LIBTHREAD=
148             LTLIBTHREAD=
149           fi
150         fi
151       fi
152     fi
153     if test "$gl_use_threads" = pth; then
154       gl_save_CPPFLAGS="$CPPFLAGS"
155       AC_LIB_LINKFLAGS(pth)
156       gl_have_pth=
157       gl_save_LIBS="$LIBS"
158       LIBS="$LIBS -lpth"
159       AC_TRY_LINK([#include <pth.h>], [pth_self();], gl_have_pth=yes)
160       LIBS="$gl_save_LIBS"
161       if test -n "$gl_have_pth"; then
162         gl_threads_api=pth
163         LIBTHREAD="$LIBPTH"
164         LTLIBTHREAD="$LTLIBPTH"
165         LIBMULTITHREAD="$LIBTHREAD"
166         LTLIBMULTITHREAD="$LTLIBTHREAD"
167         AC_DEFINE([USE_PTH_THREADS], 1,
168           [Define if the GNU Pth multithreading library can be used.])
169         if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
170           if test $gl_have_weak = yes; then
171             AC_DEFINE([USE_PTH_THREADS_WEAK], 1,
172               [Define if references to the GNU Pth multithreading library should be made weak.])
173             LIBTHREAD=
174             LTLIBTHREAD=
175           fi
176         fi
177       else
178         CPPFLAGS="$gl_save_CPPFLAGS"
179       fi
180     fi
181     if test -z "$gl_have_pthread"; then
182       if test "$gl_use_threads" = yes || test "$gl_use_threads" = win32; then
183         if { case "$host_os" in
184                mingw*) true;;
185                *) false;;
186              esac
187            }; then
188           gl_threads_api=win32
189           AC_DEFINE([USE_WIN32_THREADS], 1,
190             [Define if the Win32 multithreading API can be used.])
191         fi
192       fi
193     fi
194   fi
195   AC_MSG_CHECKING([for multithread API to use])
196   AC_MSG_RESULT([$gl_threads_api])
197   AC_SUBST(LIBTHREAD)
198   AC_SUBST(LTLIBTHREAD)
199   AC_SUBST(LIBMULTITHREAD)
200   AC_SUBST(LTLIBMULTITHREAD)
201   gl_PREREQ_LOCK
202 ])
203
204 # Prerequisites of lib/lock.c.
205 AC_DEFUN([gl_PREREQ_LOCK], [
206   AC_REQUIRE([AC_C_INLINE])
207 ])
208
209 dnl Survey of platforms:
210 dnl
211 dnl Platform          Available   Compiler    Supports   test-lock
212 dnl                   flavours    option      weak       result
213 dnl ---------------   ---------   ---------   --------   ---------
214 dnl Linux 2.4/glibc   posix       -lpthread       Y      OK
215 dnl
216 dnl GNU Hurd/glibc    posix
217 dnl
218 dnl FreeBSD 5.3       posix       -lc_r           Y
219 dnl                   posix       -lkse ?         Y
220 dnl                   posix       -lpthread ?     Y
221 dnl                   posix       -lthr           Y
222 dnl
223 dnl FreeBSD 5.2       posix       -lc_r           Y
224 dnl                   posix       -lkse           Y
225 dnl                   posix       -lthr           Y
226 dnl
227 dnl FreeBSD 4.0,4.10  posix       -lc_r           Y      OK
228 dnl
229 dnl NetBSD 1.6        --
230 dnl
231 dnl OpenBSD 3.4       posix       -lpthread       Y      OK
232 dnl
233 dnl MacOS X 10.[123]  posix       -lpthread       Y      OK
234 dnl
235 dnl Solaris 7,8,9     posix       -lpthread       Y      Sol 7,8: 0.0; Sol 9: OK
236 dnl                   solaris     -lthread        Y      Sol 7,8: 0.0; Sol 9: OK
237 dnl
238 dnl HP-UX 11          posix       -lpthread       Y      OK
239 dnl
240 dnl IRIX 6.5          posix       -lpthread       Y      0.5
241 dnl
242 dnl AIX 4.3,5.1       posix       -lpthread       N      AIX 4: 0.5; AIX 5: OK
243 dnl
244 dnl OSF/1 4.0,5.1     posix       -pthread (cc)   Y      OK
245 dnl                               -lpthread (gcc) Y
246 dnl
247 dnl Cygwin            posix       -lpthread       Y      OK
248 dnl
249 dnl Any of the above  pth         -lpth                  0.0
250 dnl
251 dnl Mingw             win32                       N      OK
252 dnl
253 dnl BeOS 5            --
254 dnl
255 dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
256 dnl turned off:
257 dnl   OK if all three tests terminate OK,
258 dnl   0.5 if the first test terminates OK but the second one loops endlessly,
259 dnl   0.0 if the first test already loops endlessly.