glob: Declare variables at the very start of their scope.
[gnulib/ericb.git] / m4 / threadlib.m4
blobf79fde20f5bf3b04b4c3247664ca540b422beb9f
1 # threadlib.m4 serial 12
2 dnl Copyright (C) 2005-2017 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.
7 dnl From Bruno Haible.
9 dnl gl_THREADLIB
10 dnl ------------
11 dnl Tests for a multithreading library to be used.
12 dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO
13 dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the
14 dnl default is 'no', otherwise it is system dependent. In both cases, the user
15 dnl can change the choice through the options --enable-threads=choice or
16 dnl --disable-threads.
17 dnl Defines at most one of the macros USE_POSIX_THREADS, USE_SOLARIS_THREADS,
18 dnl USE_PTH_THREADS, USE_WINDOWS_THREADS
19 dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use
20 dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with
21 dnl libtool).
22 dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for
23 dnl programs that really need multithread functionality. The difference
24 dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak
25 dnl symbols, typically LIBTHREAD is empty whereas LIBMULTITHREAD is not.
26 dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
27 dnl multithread-safe programs.
29 AC_DEFUN([gl_THREADLIB_EARLY],
31   AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
34 dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once.
36 AC_DEFUN([gl_THREADLIB_EARLY_BODY],
38   dnl Ordering constraints: This macro modifies CPPFLAGS in a way that
39   dnl influences the result of the autoconf tests that test for *_unlocked
40   dnl declarations, on AIX 5 at least. Therefore it must come early.
41   AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl
42   AC_BEFORE([$0], [gl_ARGP])dnl
44   AC_REQUIRE([AC_CANONICAL_HOST])
45   dnl _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems.
46   dnl AC_USE_SYSTEM_EXTENSIONS was introduced in autoconf 2.60 and obsoletes
47   dnl AC_GNU_SOURCE.
48   m4_ifdef([AC_USE_SYSTEM_EXTENSIONS],
49     [AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])],
50     [AC_REQUIRE([AC_GNU_SOURCE])])
51   dnl Check for multithreading.
52   m4_ifdef([gl_THREADLIB_DEFAULT_NO],
53     [m4_divert_text([DEFAULTS], [gl_use_threads_default=no])],
54     [m4_divert_text([DEFAULTS], [gl_use_threads_default=])])
55   AC_ARG_ENABLE([threads],
56 AC_HELP_STRING([--enable-threads={posix|solaris|pth|windows}], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [
57 AC_HELP_STRING([--disable-threads], [build without multithread safety])]),
58     [gl_use_threads=$enableval],
59     [if test -n "$gl_use_threads_default"; then
60        gl_use_threads="$gl_use_threads_default"
61      else
62 changequote(,)dnl
63        case "$host_os" in
64          dnl Disable multithreading by default on OSF/1, because it interferes
65          dnl with fork()/exec(): When msgexec is linked with -lpthread, its
66          dnl child process gets an endless segmentation fault inside execvp().
67          dnl Disable multithreading by default on Cygwin 1.5.x, because it has
68          dnl bugs that lead to endless loops or crashes. See
69          dnl <http://cygwin.com/ml/cygwin/2009-08/msg00283.html>.
70          osf*) gl_use_threads=no ;;
71          cygwin*)
72                case `uname -r` in
73                  1.[0-5].*) gl_use_threads=no ;;
74                  *)         gl_use_threads=yes ;;
75                esac
76                ;;
77          *)    gl_use_threads=yes ;;
78        esac
79 changequote([,])dnl
80      fi
81     ])
82   if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
83     # For using <pthread.h>:
84     case "$host_os" in
85       osf*)
86         # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
87         # groks <pthread.h>. cc also understands the flag -pthread, but
88         # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
89         # 2. putting a flag into CPPFLAGS that has an effect on the linker
90         # causes the AC_LINK_IFELSE test below to succeed unexpectedly,
91         # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
92         CPPFLAGS="$CPPFLAGS -D_REENTRANT"
93         ;;
94     esac
95     # Some systems optimize for single-threaded programs by default, and
96     # need special flags to disable these optimizations. For example, the
97     # definition of 'errno' in <errno.h>.
98     case "$host_os" in
99       aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
100       solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
101     esac
102   fi
105 dnl The guts of gl_THREADLIB. Needs to be expanded only once.
107 AC_DEFUN([gl_THREADLIB_BODY],
109   AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
110   gl_threads_api=none
111   LIBTHREAD=
112   LTLIBTHREAD=
113   LIBMULTITHREAD=
114   LTLIBMULTITHREAD=
115   if test "$gl_use_threads" != no; then
116     dnl Check whether the compiler and linker support weak declarations.
117     AC_CACHE_CHECK([whether imported symbols can be declared weak],
118       [gl_cv_have_weak],
119       [gl_cv_have_weak=no
120        dnl First, test whether the compiler accepts it syntactically.
121        AC_LINK_IFELSE(
122          [AC_LANG_PROGRAM(
123             [[extern void xyzzy ();
124 #pragma weak xyzzy]],
125             [[xyzzy();]])],
126          [gl_cv_have_weak=maybe])
127        if test $gl_cv_have_weak = maybe; then
128          dnl Second, test whether it actually works. On Cygwin 1.7.2, with
129          dnl gcc 4.3, symbols declared weak always evaluate to the address 0.
130          AC_RUN_IFELSE(
131            [AC_LANG_SOURCE([[
132 #include <stdio.h>
133 #pragma weak fputs
134 int main ()
136   return (fputs == NULL);
137 }]])],
138            [gl_cv_have_weak=yes],
139            [gl_cv_have_weak=no],
140            [dnl When cross-compiling, assume that only ELF platforms support
141             dnl weak symbols.
142             AC_EGREP_CPP([Extensible Linking Format],
143               [#ifdef __ELF__
144                Extensible Linking Format
145                #endif
146               ],
147               [gl_cv_have_weak="guessing yes"],
148               [gl_cv_have_weak="guessing no"])
149            ])
150        fi
151       ])
152     if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
153       # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
154       # it groks <pthread.h>. It's added above, in gl_THREADLIB_EARLY_BODY.
155       AC_CHECK_HEADER([pthread.h],
156         [gl_have_pthread_h=yes], [gl_have_pthread_h=no])
157       if test "$gl_have_pthread_h" = yes; then
158         # Other possible tests:
159         #   -lpthreads (FSU threads, PCthreads)
160         #   -lgthreads
161         gl_have_pthread=
162         # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
163         # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
164         # the second one only in libpthread, and lock.c needs it.
165         #
166         # If -pthread works, prefer it to -lpthread, since Ubuntu 14.04
167         # needs -pthread for some reason.  See:
168         # http://lists.gnu.org/archive/html/bug-gnulib/2014-09/msg00023.html
169         save_LIBS=$LIBS
170         for gl_pthread in '' '-pthread'; do
171           LIBS="$LIBS $gl_pthread"
172           AC_LINK_IFELSE(
173             [AC_LANG_PROGRAM(
174                [[#include <pthread.h>
175                  pthread_mutex_t m;
176                  pthread_mutexattr_t ma;
177                ]],
178                [[pthread_mutex_lock (&m);
179                  pthread_mutexattr_init (&ma);]])],
180             [gl_have_pthread=yes
181              LIBTHREAD=$gl_pthread LTLIBTHREAD=$gl_pthread
182              LIBMULTITHREAD=$gl_pthread LTLIBMULTITHREAD=$gl_pthread])
183           LIBS=$save_LIBS
184           test -n "$gl_have_pthread" && break
185         done
187         # Test for libpthread by looking for pthread_kill. (Not pthread_self,
188         # since it is defined as a macro on OSF/1.)
189         if test -n "$gl_have_pthread" && test -z "$LIBTHREAD"; then
190           # The program links fine without libpthread. But it may actually
191           # need to link with libpthread in order to create multiple threads.
192           AC_CHECK_LIB([pthread], [pthread_kill],
193             [LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread
194              # On Solaris and HP-UX, most pthread functions exist also in libc.
195              # Therefore pthread_in_use() needs to actually try to create a
196              # thread: pthread_create from libc will fail, whereas
197              # pthread_create will actually create a thread.
198              # On Solaris 10 or newer, this test is no longer needed, because
199              # libc contains the fully functional pthread functions.
200              case "$host_os" in
201                solaris | solaris2.[1-9] | solaris2.[1-9].* | hpux*)
202                  AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
203                    [Define if the pthread_in_use() detection is hard.])
204              esac
205             ])
206         elif test -z "$gl_have_pthread"; then
207           # Some library is needed. Try libpthread and libc_r.
208           AC_CHECK_LIB([pthread], [pthread_kill],
209             [gl_have_pthread=yes
210              LIBTHREAD=-lpthread LTLIBTHREAD=-lpthread
211              LIBMULTITHREAD=-lpthread LTLIBMULTITHREAD=-lpthread])
212           if test -z "$gl_have_pthread"; then
213             # For FreeBSD 4.
214             AC_CHECK_LIB([c_r], [pthread_kill],
215               [gl_have_pthread=yes
216                LIBTHREAD=-lc_r LTLIBTHREAD=-lc_r
217                LIBMULTITHREAD=-lc_r LTLIBMULTITHREAD=-lc_r])
218           fi
219         fi
220         if test -n "$gl_have_pthread"; then
221           gl_threads_api=posix
222           AC_DEFINE([USE_POSIX_THREADS], [1],
223             [Define if the POSIX multithreading library can be used.])
224           if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
225             if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
226               AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
227                 [Define if references to the POSIX multithreading library should be made weak.])
228               LIBTHREAD=
229               LTLIBTHREAD=
230             fi
231           fi
232         fi
233       fi
234     fi
235     if test -z "$gl_have_pthread"; then
236       if test "$gl_use_threads" = yes || test "$gl_use_threads" = solaris; then
237         gl_have_solaristhread=
238         gl_save_LIBS="$LIBS"
239         LIBS="$LIBS -lthread"
240         AC_LINK_IFELSE(
241           [AC_LANG_PROGRAM(
242              [[
243 #include <thread.h>
244 #include <synch.h>
245              ]],
246              [[thr_self();]])],
247           [gl_have_solaristhread=yes])
248         LIBS="$gl_save_LIBS"
249         if test -n "$gl_have_solaristhread"; then
250           gl_threads_api=solaris
251           LIBTHREAD=-lthread
252           LTLIBTHREAD=-lthread
253           LIBMULTITHREAD="$LIBTHREAD"
254           LTLIBMULTITHREAD="$LTLIBTHREAD"
255           AC_DEFINE([USE_SOLARIS_THREADS], [1],
256             [Define if the old Solaris multithreading library can be used.])
257           if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
258             AC_DEFINE([USE_SOLARIS_THREADS_WEAK], [1],
259               [Define if references to the old Solaris multithreading library should be made weak.])
260             LIBTHREAD=
261             LTLIBTHREAD=
262           fi
263         fi
264       fi
265     fi
266     if test "$gl_use_threads" = pth; then
267       gl_save_CPPFLAGS="$CPPFLAGS"
268       AC_LIB_LINKFLAGS([pth])
269       gl_have_pth=
270       gl_save_LIBS="$LIBS"
271       LIBS="$LIBS $LIBPTH"
272       AC_LINK_IFELSE(
273         [AC_LANG_PROGRAM([[#include <pth.h>]], [[pth_self();]])],
274         [gl_have_pth=yes])
275       LIBS="$gl_save_LIBS"
276       if test -n "$gl_have_pth"; then
277         gl_threads_api=pth
278         LIBTHREAD="$LIBPTH"
279         LTLIBTHREAD="$LTLIBPTH"
280         LIBMULTITHREAD="$LIBTHREAD"
281         LTLIBMULTITHREAD="$LTLIBTHREAD"
282         AC_DEFINE([USE_PTH_THREADS], [1],
283           [Define if the GNU Pth multithreading library can be used.])
284         if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
285           if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
286             AC_DEFINE([USE_PTH_THREADS_WEAK], [1],
287               [Define if references to the GNU Pth multithreading library should be made weak.])
288             LIBTHREAD=
289             LTLIBTHREAD=
290           fi
291         fi
292       else
293         CPPFLAGS="$gl_save_CPPFLAGS"
294       fi
295     fi
296     if test -z "$gl_have_pthread"; then
297       case "$gl_use_threads" in
298         yes | windows | win32) # The 'win32' is for backward compatibility.
299           if { case "$host_os" in
300                  mingw*) true;;
301                  *) false;;
302                esac
303              }; then
304             gl_threads_api=windows
305             AC_DEFINE([USE_WINDOWS_THREADS], [1],
306               [Define if the native Windows multithreading API can be used.])
307           fi
308           ;;
309       esac
310     fi
311   fi
312   AC_MSG_CHECKING([for multithread API to use])
313   AC_MSG_RESULT([$gl_threads_api])
314   AC_SUBST([LIBTHREAD])
315   AC_SUBST([LTLIBTHREAD])
316   AC_SUBST([LIBMULTITHREAD])
317   AC_SUBST([LTLIBMULTITHREAD])
320 AC_DEFUN([gl_THREADLIB],
322   AC_REQUIRE([gl_THREADLIB_EARLY])
323   AC_REQUIRE([gl_THREADLIB_BODY])
327 dnl gl_DISABLE_THREADS
328 dnl ------------------
329 dnl Sets the gl_THREADLIB default so that threads are not used by default.
330 dnl The user can still override it at installation time, by using the
331 dnl configure option '--enable-threads'.
333 AC_DEFUN([gl_DISABLE_THREADS], [
334   m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
338 dnl Survey of platforms:
340 dnl Platform           Available  Compiler    Supports   test-lock
341 dnl                    flavours   option      weak       result
342 dnl ---------------    ---------  ---------   --------   ---------
343 dnl Linux 2.4/glibc    posix      -lpthread       Y      OK
345 dnl GNU Hurd/glibc     posix
347 dnl Ubuntu 14.04       posix      -pthread        Y      OK
349 dnl FreeBSD 5.3        posix      -lc_r           Y
350 dnl                    posix      -lkse ?         Y
351 dnl                    posix      -lpthread ?     Y
352 dnl                    posix      -lthr           Y
354 dnl FreeBSD 5.2        posix      -lc_r           Y
355 dnl                    posix      -lkse           Y
356 dnl                    posix      -lthr           Y
358 dnl FreeBSD 4.0,4.10   posix      -lc_r           Y      OK
360 dnl NetBSD 1.6         --
362 dnl OpenBSD 3.4        posix      -lpthread       Y      OK
364 dnl Mac OS X 10.[123]  posix      -lpthread       Y      OK
366 dnl Solaris 7,8,9      posix      -lpthread       Y      Sol 7,8: 0.0; Sol 9: OK
367 dnl                    solaris    -lthread        Y      Sol 7,8: 0.0; Sol 9: OK
369 dnl HP-UX 11           posix      -lpthread       N (cc) OK
370 dnl                                               Y (gcc)
372 dnl IRIX 6.5           posix      -lpthread       Y      0.5
374 dnl AIX 4.3,5.1        posix      -lpthread       N      AIX 4: 0.5; AIX 5: OK
376 dnl OSF/1 4.0,5.1      posix      -pthread (cc)   N      OK
377 dnl                               -lpthread (gcc) Y
379 dnl Cygwin             posix      -lpthread       Y      OK
381 dnl Any of the above   pth        -lpth                  0.0
383 dnl Mingw              windows                    N      OK
385 dnl BeOS 5             --
387 dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
388 dnl turned off:
389 dnl   OK if all three tests terminate OK,
390 dnl   0.5 if the first test terminates OK but the second one loops endlessly,
391 dnl   0.0 if the first test already loops endlessly.