Update Gnulib to the latest git version
[deliverable/binutils-gdb.git] / gnulib / import / m4 / threadlib.m4
1 # threadlib.m4 serial 24
2 dnl Copyright (C) 2005-2019 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 AC_PREREQ([2.60])
10
11 dnl gl_PTHREADLIB
12 dnl -------------
13 dnl Tests for the libraries needs for using the POSIX threads API.
14 dnl Sets the variable LIBPTHREAD to the linker options for use in a Makefile.
15 dnl Sets the variable LIBPMULTITHREAD, for programs that really need
16 dnl multithread functionality. The difference between LIBPTHREAD and
17 dnl LIBPMULTITHREAD is that on platforms supporting weak symbols, typically
18 dnl LIBPTHREAD is empty whereas LIBPMULTITHREAD is not.
19 dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
20 dnl multithread-safe programs.
21 dnl Defines the C macro HAVE_PTHREAD_API if (at least parts of) the POSIX
22 dnl threads API is available.
23
24 dnl gl_THREADLIB
25 dnl ------------
26 dnl Tests for a multithreading library to be used.
27 dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO
28 dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the
29 dnl default is 'no', otherwise it is system dependent. In both cases, the user
30 dnl can change the choice through the options --enable-threads=choice or
31 dnl --disable-threads.
32 dnl Defines at most one of the macros USE_ISOC_THREADS, USE_POSIX_THREADS,
33 dnl USE_ISOC_AND_POSIX_THREADS, USE_WINDOWS_THREADS.
34 dnl The choice --enable-threads=isoc+posix is available only on platforms that
35 dnl have both the ISO C and the POSIX threads APIs. It has the effect of using
36 dnl the ISO C API for most things and the POSIX API only for creating and
37 dnl controlling threads (because there is no equivalent to pthread_atfork in
38 dnl the ISO C API).
39 dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use
40 dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with
41 dnl libtool).
42 dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for
43 dnl programs that really need multithread functionality. The difference
44 dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak
45 dnl symbols, typically LIBTHREAD is empty whereas LIBMULTITHREAD is not.
46 dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
47 dnl multithread-safe programs.
48 dnl Since support for GNU pth was removed, $LTLIBTHREAD and $LIBTHREAD have the
49 dnl same value, and similarly $LTLIBMULTITHREAD and $LIBMULTITHREAD have the
50 dnl same value. Only system libraries are needed.
51
52 AC_DEFUN([gl_ANYTHREADLIB_EARLY],
53 [
54 AC_REQUIRE([AC_CANONICAL_HOST])
55 if test -z "$gl_anythreadlib_early_done"; then
56 case "$host_os" in
57 osf*)
58 # On OSF/1, the compiler needs the flag -D_REENTRANT so that it
59 # groks <pthread.h>. cc also understands the flag -pthread, but
60 # we don't use it because 1. gcc-2.95 doesn't understand -pthread,
61 # 2. putting a flag into CPPFLAGS that has an effect on the linker
62 # causes the AC_LINK_IFELSE test below to succeed unexpectedly,
63 # leading to wrong values of LIBTHREAD and LTLIBTHREAD.
64 CPPFLAGS="$CPPFLAGS -D_REENTRANT"
65 ;;
66 esac
67 # Some systems optimize for single-threaded programs by default, and
68 # need special flags to disable these optimizations. For example, the
69 # definition of 'errno' in <errno.h>.
70 case "$host_os" in
71 aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;;
72 solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;;
73 esac
74 gl_anythreadlib_early_done=done
75 fi
76 ])
77
78 AC_DEFUN([gl_THREADLIB_EARLY],
79 [
80 AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
81 ])
82
83 dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once.
84
85 AC_DEFUN([gl_THREADLIB_EARLY_BODY],
86 [
87 dnl Ordering constraints: This macro modifies CPPFLAGS in a way that
88 dnl influences the result of the autoconf tests that test for *_unlocked
89 dnl declarations, on AIX 5 at least. Therefore it must come early.
90 AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl
91 AC_BEFORE([$0], [gl_ARGP])dnl
92
93 AC_REQUIRE([AC_CANONICAL_HOST])
94 dnl _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems.
95 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
96 dnl Check for multithreading.
97 m4_ifdef([gl_THREADLIB_DEFAULT_NO],
98 [m4_divert_text([DEFAULTS], [gl_use_threads_default=no])],
99 [m4_divert_text([DEFAULTS], [gl_use_threads_default=])])
100 m4_divert_text([DEFAULTS], [gl_use_winpthreads_default=])
101 AC_ARG_ENABLE([threads],
102 AC_HELP_STRING([--enable-threads={isoc|posix|isoc+posix|windows}], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [
103 AC_HELP_STRING([--disable-threads], [build without multithread safety])]),
104 [gl_use_threads=$enableval],
105 [if test -n "$gl_use_threads_default"; then
106 gl_use_threads="$gl_use_threads_default"
107 else
108 changequote(,)dnl
109 case "$host_os" in
110 dnl Disable multithreading by default on OSF/1, because it interferes
111 dnl with fork()/exec(): When msgexec is linked with -lpthread, its
112 dnl child process gets an endless segmentation fault inside execvp().
113 osf*) gl_use_threads=no ;;
114 dnl Disable multithreading by default on Cygwin 1.5.x, because it has
115 dnl bugs that lead to endless loops or crashes. See
116 dnl <https://cygwin.com/ml/cygwin/2009-08/msg00283.html>.
117 cygwin*)
118 case `uname -r` in
119 1.[0-5].*) gl_use_threads=no ;;
120 *) gl_use_threads=yes ;;
121 esac
122 ;;
123 dnl Obey gl_AVOID_WINPTHREAD on mingw.
124 mingw*)
125 case "$gl_use_winpthreads_default" in
126 yes) gl_use_threads=posix ;;
127 no) gl_use_threads=windows ;;
128 *) gl_use_threads=yes ;;
129 esac
130 ;;
131 *) gl_use_threads=yes ;;
132 esac
133 changequote([,])dnl
134 fi
135 ])
136 if test "$gl_use_threads" = yes \
137 || test "$gl_use_threads" = isoc \
138 || test "$gl_use_threads" = posix \
139 || test "$gl_use_threads" = isoc+posix; then
140 # For using <threads.h> or <pthread.h>:
141 gl_ANYTHREADLIB_EARLY
142 fi
143 ])
144
145 dnl Checks whether the compiler and linker support weak declarations of symbols.
146
147 AC_DEFUN([gl_WEAK_SYMBOLS],
148 [
149 AC_CACHE_CHECK([whether imported symbols can be declared weak],
150 [gl_cv_have_weak],
151 [gl_cv_have_weak=no
152 dnl First, test whether the compiler accepts it syntactically.
153 AC_LINK_IFELSE(
154 [AC_LANG_PROGRAM(
155 [[extern void xyzzy ();
156 #pragma weak xyzzy]],
157 [[xyzzy();]])],
158 [gl_cv_have_weak=maybe])
159 if test $gl_cv_have_weak = maybe; then
160 dnl Second, test whether it actually works. On Cygwin 1.7.2, with
161 dnl gcc 4.3, symbols declared weak always evaluate to the address 0.
162 AC_RUN_IFELSE(
163 [AC_LANG_SOURCE([[
164 #include <stdio.h>
165 #pragma weak fputs
166 int main ()
167 {
168 return (fputs == NULL);
169 }]])],
170 [gl_cv_have_weak=yes],
171 [gl_cv_have_weak=no],
172 [dnl When cross-compiling, assume that only ELF platforms support
173 dnl weak symbols.
174 AC_EGREP_CPP([Extensible Linking Format],
175 [#ifdef __ELF__
176 Extensible Linking Format
177 #endif
178 ],
179 [gl_cv_have_weak="guessing yes"],
180 [gl_cv_have_weak="guessing no"])
181 ])
182 fi
183 dnl But when linking statically, weak symbols don't work.
184 case " $LDFLAGS " in
185 *" -static "*) gl_cv_have_weak=no ;;
186 esac
187 ])
188 case "$gl_cv_have_weak" in
189 *yes)
190 AC_DEFINE([HAVE_WEAK_SYMBOLS], [1],
191 [Define to 1 if the compiler and linker support weak declarations of symbols.])
192 ;;
193 esac
194 ])
195
196 dnl The guts of gl_PTHREADLIB. Needs to be expanded only once.
197
198 AC_DEFUN([gl_PTHREADLIB_BODY],
199 [
200 AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
201 if test -z "$gl_threadlib_body_done"; then
202 gl_pthread_api=no
203 LIBPTHREAD=
204 LIBPMULTITHREAD=
205 # On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that
206 # it groks <pthread.h>. It's added above, in gl_ANYTHREADLIB_EARLY.
207 AC_CHECK_HEADER([pthread.h],
208 [gl_have_pthread_h=yes], [gl_have_pthread_h=no])
209 if test "$gl_have_pthread_h" = yes; then
210 # Other possible tests:
211 # -lpthreads (FSU threads, PCthreads)
212 # -lgthreads
213 # Test whether both pthread_mutex_lock and pthread_mutexattr_init exist
214 # in libc. IRIX 6.5 has the first one in both libc and libpthread, but
215 # the second one only in libpthread, and lock.c needs it.
216 #
217 # If -pthread works, prefer it to -lpthread, since Ubuntu 14.04
218 # needs -pthread for some reason. See:
219 # https://lists.gnu.org/r/bug-gnulib/2014-09/msg00023.html
220 save_LIBS=$LIBS
221 for gl_pthread in '' '-pthread'; do
222 LIBS="$LIBS $gl_pthread"
223 AC_LINK_IFELSE(
224 [AC_LANG_PROGRAM(
225 [[#include <pthread.h>
226 pthread_mutex_t m;
227 pthread_mutexattr_t ma;
228 ]],
229 [[pthread_mutex_lock (&m);
230 pthread_mutexattr_init (&ma);]])],
231 [gl_pthread_api=yes
232 LIBPTHREAD=$gl_pthread
233 LIBPMULTITHREAD=$gl_pthread])
234 LIBS=$save_LIBS
235 test $gl_pthread_api = yes && break
236 done
237
238 # Test for libpthread by looking for pthread_kill. (Not pthread_self,
239 # since it is defined as a macro on OSF/1.)
240 if test $gl_pthread_api = yes && test -z "$LIBPTHREAD"; then
241 # The program links fine without libpthread. But it may actually
242 # need to link with libpthread in order to create multiple threads.
243 AC_CHECK_LIB([pthread], [pthread_kill],
244 [LIBPMULTITHREAD=-lpthread
245 # On Solaris and HP-UX, most pthread functions exist also in libc.
246 # Therefore pthread_in_use() needs to actually try to create a
247 # thread: pthread_create from libc will fail, whereas
248 # pthread_create will actually create a thread.
249 # On Solaris 10 or newer, this test is no longer needed, because
250 # libc contains the fully functional pthread functions.
251 case "$host_os" in
252 solaris | solaris2.[1-9] | solaris2.[1-9].* | hpux*)
253 AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1],
254 [Define if the pthread_in_use() detection is hard.])
255 esac
256 ])
257 elif test $gl_pthread_api != yes; then
258 # Some library is needed. Try libpthread and libc_r.
259 AC_CHECK_LIB([pthread], [pthread_kill],
260 [gl_pthread_api=yes
261 LIBPTHREAD=-lpthread
262 LIBPMULTITHREAD=-lpthread])
263 if test $gl_pthread_api != yes; then
264 # For FreeBSD 4.
265 AC_CHECK_LIB([c_r], [pthread_kill],
266 [gl_pthread_api=yes
267 LIBPTHREAD=-lc_r
268 LIBPMULTITHREAD=-lc_r])
269 fi
270 fi
271 fi
272 AC_MSG_CHECKING([whether POSIX threads API is available])
273 AC_MSG_RESULT([$gl_pthread_api])
274 AC_SUBST([LIBPTHREAD])
275 AC_SUBST([LIBPMULTITHREAD])
276 if test $gl_pthread_api = yes; then
277 AC_DEFINE([HAVE_PTHREAD_API], [1],
278 [Define if you have the <pthread.h> header and the POSIX threads API.])
279 fi
280 gl_threadlib_body_done=done
281 fi
282 ])
283
284 dnl The guts of gl_THREADLIB. Needs to be expanded only once.
285
286 AC_DEFUN([gl_THREADLIB_BODY],
287 [
288 AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
289 gl_threads_api=none
290 LIBTHREAD=
291 LTLIBTHREAD=
292 LIBMULTITHREAD=
293 LTLIBMULTITHREAD=
294 if test "$gl_use_threads" != no; then
295 dnl Check whether the compiler and linker support weak declarations.
296 gl_WEAK_SYMBOLS
297 if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
298 dnl If we use weak symbols to implement pthread_in_use / pth_in_use /
299 dnl thread_in_use, we also need to test whether the ISO C 11 thrd_create
300 dnl facility is in use.
301 AC_CHECK_HEADERS_ONCE([threads.h])
302 :
303 fi
304 if test "$gl_use_threads" = isoc || test "$gl_use_threads" = isoc+posix; then
305 AC_CHECK_HEADERS_ONCE([threads.h])
306 if test $ac_cv_header_threads_h = yes; then
307 gl_have_isoc_threads=
308 # Test whether both mtx_lock and cnd_timedwait exist in libc.
309 AC_LINK_IFELSE(
310 [AC_LANG_PROGRAM(
311 [[#include <threads.h>
312 #include <stddef.h>
313 mtx_t m;
314 cnd_t c;
315 ]],
316 [[mtx_lock (&m);
317 cnd_timedwait (&c, &m, NULL);]])],
318 [gl_have_isoc_threads=yes])
319 fi
320 fi
321 if test "$gl_use_threads" = yes \
322 || test "$gl_use_threads" = posix \
323 || test "$gl_use_threads" = isoc+posix; then
324 gl_PTHREADLIB_BODY
325 LIBTHREAD=$LIBPTHREAD LTLIBTHREAD=$LIBPTHREAD
326 LIBMULTITHREAD=$LIBPMULTITHREAD LTLIBMULTITHREAD=$LIBPMULTITHREAD
327 if test $gl_pthread_api = yes; then
328 if test "$gl_use_threads" = isoc+posix && test "$gl_have_isoc_threads" = yes; then
329 gl_threads_api='isoc+posix'
330 AC_DEFINE([USE_ISOC_AND_POSIX_THREADS], [1],
331 [Define if the combination of the ISO C and POSIX multithreading APIs can be used.])
332 LIBTHREAD= LTLIBTHREAD=
333 else
334 gl_threads_api=posix
335 AC_DEFINE([USE_POSIX_THREADS], [1],
336 [Define if the POSIX multithreading library can be used.])
337 if test -n "$LIBMULTITHREAD" || test -n "$LTLIBMULTITHREAD"; then
338 if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then
339 AC_DEFINE([USE_POSIX_THREADS_WEAK], [1],
340 [Define if references to the POSIX multithreading library should be made weak.])
341 LIBTHREAD= LTLIBTHREAD=
342 fi
343 fi
344 fi
345 fi
346 fi
347 if test $gl_threads_api = none; then
348 if test "$gl_use_threads" = isoc && test "$gl_have_isoc_threads" = yes; then
349 gl_threads_api=isoc
350 AC_DEFINE([USE_ISOC_THREADS], [1],
351 [Define if the ISO C multithreading library can be used.])
352 fi
353 fi
354 if test $gl_threads_api = none; then
355 case "$gl_use_threads" in
356 yes | windows | win32) # The 'win32' is for backward compatibility.
357 if { case "$host_os" in
358 mingw*) true;;
359 *) false;;
360 esac
361 }; then
362 gl_threads_api=windows
363 AC_DEFINE([USE_WINDOWS_THREADS], [1],
364 [Define if the native Windows multithreading API can be used.])
365 fi
366 ;;
367 esac
368 fi
369 fi
370 AC_MSG_CHECKING([for multithread API to use])
371 AC_MSG_RESULT([$gl_threads_api])
372 AC_SUBST([LIBTHREAD])
373 AC_SUBST([LTLIBTHREAD])
374 AC_SUBST([LIBMULTITHREAD])
375 AC_SUBST([LTLIBMULTITHREAD])
376 ])
377
378 AC_DEFUN([gl_PTHREADLIB],
379 [
380 AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
381 gl_PTHREADLIB_BODY
382 ])
383
384 AC_DEFUN([gl_THREADLIB],
385 [
386 AC_REQUIRE([gl_THREADLIB_EARLY])
387 AC_REQUIRE([gl_THREADLIB_BODY])
388 ])
389
390
391 dnl gl_DISABLE_THREADS
392 dnl ------------------
393 dnl Sets the gl_THREADLIB default so that threads are not used by default.
394 dnl The user can still override it at installation time, by using the
395 dnl configure option '--enable-threads'.
396
397 AC_DEFUN([gl_DISABLE_THREADS], [
398 m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
399 ])
400
401
402 dnl gl_AVOID_WINPTHREAD
403 dnl -------------------
404 dnl Sets the gl_THREADLIB default so that on mingw, a dependency to the
405 dnl libwinpthread DLL (mingw-w64 winpthreads library) is avoided.
406 dnl The user can still override it at installation time, by using the
407 dnl configure option '--enable-threads'.
408
409 AC_DEFUN([gl_AVOID_WINPTHREAD], [
410 m4_divert_text([INIT_PREPARE], [gl_use_winpthreads_default=no])
411 ])
412
413
414 dnl Survey of platforms:
415 dnl
416 dnl Platform Available Compiler Supports test-lock
417 dnl flavours option weak result
418 dnl --------------- --------- --------- -------- ---------
419 dnl Linux 2.4/glibc posix -lpthread Y OK
420 dnl
421 dnl GNU Hurd/glibc posix
422 dnl
423 dnl Ubuntu 14.04 posix -pthread Y OK
424 dnl
425 dnl FreeBSD 5.3 posix -lc_r Y
426 dnl posix -lkse ? Y
427 dnl posix -lpthread ? Y
428 dnl posix -lthr Y
429 dnl
430 dnl FreeBSD 5.2 posix -lc_r Y
431 dnl posix -lkse Y
432 dnl posix -lthr Y
433 dnl
434 dnl FreeBSD 4.0,4.10 posix -lc_r Y OK
435 dnl
436 dnl NetBSD 1.6 --
437 dnl
438 dnl OpenBSD 3.4 posix -lpthread Y OK
439 dnl
440 dnl Mac OS X 10.[123] posix -lpthread Y OK
441 dnl
442 dnl Solaris 7,8,9 posix -lpthread Y Sol 7,8: 0.0; Sol 9: OK
443 dnl
444 dnl HP-UX 11 posix -lpthread N (cc) OK
445 dnl Y (gcc)
446 dnl
447 dnl IRIX 6.5 posix -lpthread Y 0.5
448 dnl
449 dnl AIX 4.3,5.1 posix -lpthread N AIX 4: 0.5; AIX 5: OK
450 dnl
451 dnl OSF/1 4.0,5.1 posix -pthread (cc) N OK
452 dnl -lpthread (gcc) Y
453 dnl
454 dnl Cygwin posix -lpthread Y OK
455 dnl
456 dnl Mingw windows N OK
457 dnl
458 dnl BeOS 5 --
459 dnl
460 dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
461 dnl turned off:
462 dnl OK if all three tests terminate OK,
463 dnl 0.5 if the first test terminates OK but the second one loops endlessly,
464 dnl 0.0 if the first test already loops endlessly.
This page took 0.040251 seconds and 4 git commands to generate.