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.
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".
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)
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])
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)
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"
60 CPPFLAGS="$CPPFLAGS -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])
76 # Some library is needed. Try libpthread and libc_r.
77 AC_CHECK_LIB(pthread, pthread_kill,
79 LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
80 LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
81 if test -z "$gl_have_pthread"; then
83 AC_CHECK_LIB(c_r, pthread_kill,
85 LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
86 LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
89 if test -n "$gl_have_pthread"; then
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.])
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.])],
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."
113 int x = (int)PTHREAD_MUTEX_RECURSIVE;
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.
120 aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
121 solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
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=
130 LIBS="$LIBS -lthread"
131 AC_TRY_LINK([#include <thread.h>
134 [gl_have_solaristhread=yes])
136 if test -n "$gl_have_solaristhread"; then
137 gl_threads_api=solaris
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.])
153 if test "$gl_use_threads" = pth; then
154 gl_save_CPPFLAGS="$CPPFLAGS"
155 AC_LIB_LINKFLAGS(pth)
159 AC_TRY_LINK([#include <pth.h>], [pth_self();], gl_have_pth=yes)
161 if test -n "$gl_have_pth"; then
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.])
178 CPPFLAGS="$gl_save_CPPFLAGS"
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
189 AC_DEFINE([USE_WIN32_THREADS], 1,
190 [Define if the Win32 multithreading API can be used.])
195 AC_MSG_CHECKING([for multithread API to use])
196 AC_MSG_RESULT([$gl_threads_api])
198 AC_SUBST(LTLIBTHREAD)
199 AC_SUBST(LIBMULTITHREAD)
200 AC_SUBST(LTLIBMULTITHREAD)
204 # Prerequisites of lib/lock.c.
205 AC_DEFUN([gl_PREREQ_LOCK], [
206 AC_REQUIRE([AC_C_INLINE])
209 dnl Survey of platforms:
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
216 dnl GNU Hurd/glibc posix
218 dnl FreeBSD 5.3 posix -lc_r Y
220 dnl posix -lpthread ? Y
223 dnl FreeBSD 5.2 posix -lc_r Y
227 dnl FreeBSD 4.0,4.10 posix -lc_r Y OK
231 dnl OpenBSD 3.4 posix -lpthread Y OK
233 dnl MacOS X 10.[123] posix -lpthread Y OK
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
238 dnl HP-UX 11 posix -lpthread Y OK
240 dnl IRIX 6.5 posix -lpthread Y 0.5
242 dnl AIX 4.3,5.1 posix -lpthread N AIX 4: 0.5; AIX 5: OK
244 dnl OSF/1 4.0,5.1 posix -pthread (cc) Y OK
245 dnl -lpthread (gcc) Y
247 dnl Cygwin posix -lpthread Y OK
249 dnl Any of the above pth -lpth 0.0
255 dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
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.