Update Gnulib to the latest git version
[deliverable/binutils-gdb.git] / gnulib / import / m4 / threadlib.m4
CommitLineData
c0c3707f
CB
1# threadlib.m4 serial 24
2dnl Copyright (C) 2005-2019 Free Software Foundation, Inc.
5abebf3c
CB
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7dnl From Bruno Haible.
8
c0c3707f
CB
9AC_PREREQ([2.60])
10
11dnl gl_PTHREADLIB
12dnl -------------
13dnl Tests for the libraries needs for using the POSIX threads API.
14dnl Sets the variable LIBPTHREAD to the linker options for use in a Makefile.
15dnl Sets the variable LIBPMULTITHREAD, for programs that really need
16dnl multithread functionality. The difference between LIBPTHREAD and
17dnl LIBPMULTITHREAD is that on platforms supporting weak symbols, typically
18dnl LIBPTHREAD is empty whereas LIBPMULTITHREAD is not.
19dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
20dnl multithread-safe programs.
21dnl Defines the C macro HAVE_PTHREAD_API if (at least parts of) the POSIX
22dnl threads API is available.
23
5abebf3c
CB
24dnl gl_THREADLIB
25dnl ------------
26dnl Tests for a multithreading library to be used.
27dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO
28dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the
29dnl default is 'no', otherwise it is system dependent. In both cases, the user
30dnl can change the choice through the options --enable-threads=choice or
31dnl --disable-threads.
c0c3707f
CB
32dnl Defines at most one of the macros USE_ISOC_THREADS, USE_POSIX_THREADS,
33dnl USE_ISOC_AND_POSIX_THREADS, USE_WINDOWS_THREADS.
34dnl The choice --enable-threads=isoc+posix is available only on platforms that
35dnl have both the ISO C and the POSIX threads APIs. It has the effect of using
36dnl the ISO C API for most things and the POSIX API only for creating and
37dnl controlling threads (because there is no equivalent to pthread_atfork in
38dnl the ISO C API).
5abebf3c
CB
39dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use
40dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with
41dnl libtool).
42dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for
43dnl programs that really need multithread functionality. The difference
44dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak
45dnl symbols, typically LIBTHREAD is empty whereas LIBMULTITHREAD is not.
46dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for
47dnl multithread-safe programs.
c0c3707f
CB
48dnl Since support for GNU pth was removed, $LTLIBTHREAD and $LIBTHREAD have the
49dnl same value, and similarly $LTLIBMULTITHREAD and $LIBMULTITHREAD have the
50dnl same value. Only system libraries are needed.
51
52AC_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])
5abebf3c
CB
77
78AC_DEFUN([gl_THREADLIB_EARLY],
79[
80 AC_REQUIRE([gl_THREADLIB_EARLY_BODY])
81])
82
83dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once.
84
85AC_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.
c0c3707f 95 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
5abebf3c
CB
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=])])
c0c3707f 100 m4_divert_text([DEFAULTS], [gl_use_winpthreads_default=])
5abebf3c 101 AC_ARG_ENABLE([threads],
c0c3707f 102AC_HELP_STRING([--enable-threads={isoc|posix|isoc+posix|windows}], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [
5abebf3c
CB
103AC_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
108changequote(,)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().
c0c3707f 113 osf*) gl_use_threads=no ;;
5abebf3c
CB
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
c0c3707f 116 dnl <https://cygwin.com/ml/cygwin/2009-08/msg00283.html>.
5abebf3c
CB
117 cygwin*)
118 case `uname -r` in
119 1.[0-5].*) gl_use_threads=no ;;
120 *) gl_use_threads=yes ;;
121 esac
122 ;;
c0c3707f
CB
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 ;;
5abebf3c
CB
131 *) gl_use_threads=yes ;;
132 esac
133changequote([,])dnl
134 fi
135 ])
c0c3707f
CB
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
145dnl Checks whether the compiler and linker support weak declarations of symbols.
146
147AC_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
166int 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
196dnl The guts of gl_PTHREADLIB. Needs to be expanded only once.
197
198AC_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
5abebf3c
CB
281 fi
282])
283
284dnl The guts of gl_THREADLIB. Needs to be expanded only once.
285
286AC_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.
c0c3707f
CB
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
5abebf3c
CB
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.])
c0c3707f 341 LIBTHREAD= LTLIBTHREAD=
5abebf3c
CB
342 fi
343 fi
344 fi
345 fi
346 fi
c0c3707f
CB
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.])
5abebf3c
CB
352 fi
353 fi
c0c3707f 354 if test $gl_threads_api = none; then
5abebf3c
CB
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
c0c3707f
CB
378AC_DEFUN([gl_PTHREADLIB],
379[
380 AC_REQUIRE([gl_ANYTHREADLIB_EARLY])
381 gl_PTHREADLIB_BODY
382])
383
5abebf3c
CB
384AC_DEFUN([gl_THREADLIB],
385[
386 AC_REQUIRE([gl_THREADLIB_EARLY])
387 AC_REQUIRE([gl_THREADLIB_BODY])
388])
389
390
391dnl gl_DISABLE_THREADS
392dnl ------------------
393dnl Sets the gl_THREADLIB default so that threads are not used by default.
394dnl The user can still override it at installation time, by using the
395dnl configure option '--enable-threads'.
396
397AC_DEFUN([gl_DISABLE_THREADS], [
398 m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no])
399])
400
401
c0c3707f
CB
402dnl gl_AVOID_WINPTHREAD
403dnl -------------------
404dnl Sets the gl_THREADLIB default so that on mingw, a dependency to the
405dnl libwinpthread DLL (mingw-w64 winpthreads library) is avoided.
406dnl The user can still override it at installation time, by using the
407dnl configure option '--enable-threads'.
408
409AC_DEFUN([gl_AVOID_WINPTHREAD], [
410 m4_divert_text([INIT_PREPARE], [gl_use_winpthreads_default=no])
411])
412
413
5abebf3c
CB
414dnl Survey of platforms:
415dnl
416dnl Platform Available Compiler Supports test-lock
417dnl flavours option weak result
418dnl --------------- --------- --------- -------- ---------
419dnl Linux 2.4/glibc posix -lpthread Y OK
420dnl
421dnl GNU Hurd/glibc posix
422dnl
423dnl Ubuntu 14.04 posix -pthread Y OK
424dnl
425dnl FreeBSD 5.3 posix -lc_r Y
426dnl posix -lkse ? Y
427dnl posix -lpthread ? Y
428dnl posix -lthr Y
429dnl
430dnl FreeBSD 5.2 posix -lc_r Y
431dnl posix -lkse Y
432dnl posix -lthr Y
433dnl
434dnl FreeBSD 4.0,4.10 posix -lc_r Y OK
435dnl
436dnl NetBSD 1.6 --
437dnl
438dnl OpenBSD 3.4 posix -lpthread Y OK
439dnl
440dnl Mac OS X 10.[123] posix -lpthread Y OK
441dnl
442dnl Solaris 7,8,9 posix -lpthread Y Sol 7,8: 0.0; Sol 9: OK
5abebf3c
CB
443dnl
444dnl HP-UX 11 posix -lpthread N (cc) OK
445dnl Y (gcc)
446dnl
447dnl IRIX 6.5 posix -lpthread Y 0.5
448dnl
449dnl AIX 4.3,5.1 posix -lpthread N AIX 4: 0.5; AIX 5: OK
450dnl
451dnl OSF/1 4.0,5.1 posix -pthread (cc) N OK
452dnl -lpthread (gcc) Y
453dnl
454dnl Cygwin posix -lpthread Y OK
455dnl
5abebf3c
CB
456dnl Mingw windows N OK
457dnl
458dnl BeOS 5 --
459dnl
460dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is
461dnl turned off:
462dnl OK if all three tests terminate OK,
463dnl 0.5 if the first test terminates OK but the second one loops endlessly,
464dnl 0.0 if the first test already loops endlessly.
This page took 0.048896 seconds and 4 git commands to generate.