| 1 |
# threadlib.m4 |
| 2 |
# serial 42 |
| 3 |
dnl Copyright (C) 2005-2024 Free Software Foundation, Inc. |
| 4 |
dnl This file is free software; the Free Software Foundation |
| 5 |
dnl gives unlimited permission to copy and/or distribute it, |
| 6 |
dnl with or without modifications, as long as this notice is preserved. |
| 7 |
dnl This file is offered as-is, without any warranty. |
| 8 |
|
| 9 |
dnl From Bruno Haible. |
| 10 |
|
| 11 |
AC_PREREQ([2.60]) |
| 12 |
|
| 13 |
dnl The general structure of the multithreading modules in gnulib is that we |
| 14 |
dnl have three set of modules: |
| 15 |
dnl |
| 16 |
dnl * POSIX API: |
| 17 |
dnl pthread, which combines |
| 18 |
dnl pthread-h |
| 19 |
dnl pthread-thread |
| 20 |
dnl pthread-once |
| 21 |
dnl pthread-mutex |
| 22 |
dnl pthread-rwlock |
| 23 |
dnl pthread-cond |
| 24 |
dnl pthread-tss |
| 25 |
dnl pthread-spin |
| 26 |
dnl sched_yield |
| 27 |
dnl |
| 28 |
dnl * ISO C API: |
| 29 |
dnl threads, which combines |
| 30 |
dnl threads-h |
| 31 |
dnl thrd |
| 32 |
dnl mtx |
| 33 |
dnl cnd |
| 34 |
dnl tss |
| 35 |
dnl |
| 36 |
dnl * Gnulib API, with an implementation that can be chosen at configure |
| 37 |
dnl time through the option --enable-threads=... |
| 38 |
dnl thread |
| 39 |
dnl lock |
| 40 |
dnl cond |
| 41 |
dnl tls |
| 42 |
dnl yield |
| 43 |
dnl |
| 44 |
dnl They are independent, except for the fact that |
| 45 |
dnl - the implementation of the ISO C API may use the POSIX (or some other |
| 46 |
dnl platform dependent) API, |
| 47 |
dnl - the implementation of the Gnulib API may use the POSIX or ISO C or |
| 48 |
dnl some other platform dependent API, depending on the --enable-threads |
| 49 |
dnl option. |
| 50 |
dnl |
| 51 |
dnl This file contains macros for all of these APIs! |
| 52 |
|
| 53 |
dnl ============================================================================ |
| 54 |
dnl Macros for all thread APIs |
| 55 |
|
| 56 |
AC_DEFUN([gl_ANYTHREADLIB_EARLY], |
| 57 |
[ |
| 58 |
AC_REQUIRE([AC_CANONICAL_HOST]) |
| 59 |
if test -z "$gl_anythreadlib_early_done"; then |
| 60 |
case "$host_os" in |
| 61 |
osf*) |
| 62 |
# On OSF/1, the compiler needs the flag -D_REENTRANT so that it |
| 63 |
# groks <pthread.h>. cc also understands the flag -pthread, but |
| 64 |
# we don't use it because 1. gcc-2.95 doesn't understand -pthread, |
| 65 |
# 2. putting a flag into CPPFLAGS that has an effect on the linker |
| 66 |
# causes the AC_LINK_IFELSE test below to succeed unexpectedly, |
| 67 |
# leading to wrong values of LIBTHREAD and LTLIBTHREAD. |
| 68 |
CPPFLAGS="$CPPFLAGS -D_REENTRANT" |
| 69 |
;; |
| 70 |
esac |
| 71 |
# Some systems optimize for single-threaded programs by default, and |
| 72 |
# need special flags to disable these optimizations. For example, the |
| 73 |
# definition of 'errno' in <errno.h>. |
| 74 |
case "$host_os" in |
| 75 |
aix* | freebsd*) CPPFLAGS="$CPPFLAGS -D_THREAD_SAFE" ;; |
| 76 |
solaris*) CPPFLAGS="$CPPFLAGS -D_REENTRANT" ;; |
| 77 |
esac |
| 78 |
gl_anythreadlib_early_done=done |
| 79 |
fi |
| 80 |
]) |
| 81 |
|
| 82 |
dnl Checks whether the compiler and linker support weak declarations of symbols. |
| 83 |
|
| 84 |
AC_DEFUN([gl_WEAK_SYMBOLS], |
| 85 |
[ |
| 86 |
AC_REQUIRE([AC_CANONICAL_HOST]) |
| 87 |
AC_CACHE_CHECK([whether imported symbols can be declared weak], |
| 88 |
[gl_cv_have_weak], |
| 89 |
[case "$host_os" in |
| 90 |
cygwin* | mingw* | windows*) |
| 91 |
dnl On Cygwin 3.2.0 with gcc 10.2, and likewise on mingw 10.0.0 with |
| 92 |
dnl gcc 11.3, the test below would succeed, but programs that use |
| 93 |
dnl pthread_in_use() with weak symbol references crash miserably at |
| 94 |
dnl runtime. |
| 95 |
gl_cv_have_weak="guessing no" |
| 96 |
;; |
| 97 |
*) |
| 98 |
gl_cv_have_weak=no |
| 99 |
dnl First, test whether the compiler accepts it syntactically. |
| 100 |
AC_LINK_IFELSE( |
| 101 |
[AC_LANG_PROGRAM( |
| 102 |
[[extern void xyzzy (); |
| 103 |
#pragma weak xyzzy]], |
| 104 |
[[xyzzy();]])], |
| 105 |
[gl_cv_have_weak=maybe]) |
| 106 |
if test $gl_cv_have_weak = maybe; then |
| 107 |
dnl Second, test whether it actually works. On Cygwin 1.7.2, with |
| 108 |
dnl gcc 4.3, symbols declared weak always evaluate to the address 0. |
| 109 |
AC_RUN_IFELSE( |
| 110 |
[AC_LANG_SOURCE([[ |
| 111 |
#include <stdio.h> |
| 112 |
#pragma weak fputs |
| 113 |
int main () |
| 114 |
{ |
| 115 |
return (fputs == NULL); |
| 116 |
}]])], |
| 117 |
[gl_cv_have_weak=yes], |
| 118 |
[gl_cv_have_weak=no], |
| 119 |
[dnl When cross-compiling, assume that only ELF platforms support |
| 120 |
dnl weak symbols. |
| 121 |
AC_EGREP_CPP([Extensible Linking Format], |
| 122 |
[#ifdef __ELF__ |
| 123 |
Extensible Linking Format |
| 124 |
#endif |
| 125 |
], |
| 126 |
[gl_cv_have_weak="guessing yes"], |
| 127 |
[gl_cv_have_weak="guessing no"]) |
| 128 |
]) |
| 129 |
fi |
| 130 |
;; |
| 131 |
esac |
| 132 |
dnl But when linking statically, weak symbols don't work. |
| 133 |
case " $LDFLAGS " in |
| 134 |
*" -static "*) gl_cv_have_weak=no ;; |
| 135 |
esac |
| 136 |
dnl Test for a bug in FreeBSD 11: A link error occurs when using a weak |
| 137 |
dnl symbol and linking against a shared library that has a dependency on |
| 138 |
dnl the shared library that defines the symbol. |
| 139 |
case "$gl_cv_have_weak" in |
| 140 |
*yes) |
| 141 |
case "$host_os" in |
| 142 |
freebsd* | dragonfly* | midnightbsd*) |
| 143 |
: > conftest1.c |
| 144 |
$CC $CPPFLAGS $CFLAGS $LDFLAGS -fPIC -shared -o libempty.so conftest1.c -lpthread >&AS_MESSAGE_LOG_FD 2>&1 |
| 145 |
cat <<EOF > conftest2.c |
| 146 |
#include <pthread.h> |
| 147 |
#pragma weak pthread_mutexattr_gettype |
| 148 |
int main () |
| 149 |
{ |
| 150 |
return (pthread_mutexattr_gettype != NULL); |
| 151 |
} |
| 152 |
EOF |
| 153 |
$CC $CPPFLAGS $CFLAGS $LDFLAGS -o conftest conftest2.c libempty.so >&AS_MESSAGE_LOG_FD 2>&1 \ |
| 154 |
|| gl_cv_have_weak=no |
| 155 |
rm -f conftest1.c libempty.so conftest2.c conftest |
| 156 |
;; |
| 157 |
esac |
| 158 |
;; |
| 159 |
esac |
| 160 |
]) |
| 161 |
case "$gl_cv_have_weak" in |
| 162 |
*yes) |
| 163 |
AC_DEFINE([HAVE_WEAK_SYMBOLS], [1], |
| 164 |
[Define to 1 if the compiler and linker support weak declarations of symbols.]) |
| 165 |
;; |
| 166 |
esac |
| 167 |
]) |
| 168 |
|
| 169 |
dnl ============================================================================ |
| 170 |
dnl Macros for the POSIX API |
| 171 |
|
| 172 |
dnl gl_PTHREADLIB |
| 173 |
dnl ------------- |
| 174 |
dnl Tests for the libraries needs for using the POSIX threads API. |
| 175 |
dnl Sets the variable LIBPTHREAD to the linker options for use in a Makefile. |
| 176 |
dnl Sets the variable LIBPMULTITHREAD, for programs that really need |
| 177 |
dnl multithread functionality. The difference between LIBPTHREAD and |
| 178 |
dnl LIBPMULTITHREAD is that on platforms supporting weak symbols, typically |
| 179 |
dnl LIBPTHREAD is empty whereas LIBPMULTITHREAD is not. |
| 180 |
dnl Sets the variable SCHED_YIELD_LIB to the linker options needed to use the |
| 181 |
dnl sched_yield() function. |
| 182 |
dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for |
| 183 |
dnl multithread-safe programs. |
| 184 |
dnl Defines the C macro HAVE_PTHREAD_API if (at least parts of) the POSIX |
| 185 |
dnl threads API is available. |
| 186 |
|
| 187 |
dnl The guts of gl_PTHREADLIB. Needs to be expanded only once. |
| 188 |
|
| 189 |
AC_DEFUN([gl_PTHREADLIB_BODY], |
| 190 |
[ |
| 191 |
AC_REQUIRE([gl_ANYTHREADLIB_EARLY]) |
| 192 |
if test -z "$gl_pthreadlib_body_done"; then |
| 193 |
gl_pthread_api=no |
| 194 |
LIBPTHREAD= |
| 195 |
LIBPMULTITHREAD= |
| 196 |
# On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that |
| 197 |
# it groks <pthread.h>. It's added above, in gl_ANYTHREADLIB_EARLY. |
| 198 |
AC_CHECK_HEADER([pthread.h], |
| 199 |
[gl_have_pthread_h=yes], [gl_have_pthread_h=no]) |
| 200 |
if test "$gl_have_pthread_h" = yes; then |
| 201 |
# Other possible tests: |
| 202 |
# -lpthreads (FSU threads, PCthreads) |
| 203 |
# -lgthreads |
| 204 |
# Test whether both pthread_mutex_lock and pthread_mutexattr_init exist |
| 205 |
# in libc. IRIX 6.5 has the first one in both libc and libpthread, but |
| 206 |
# the second one only in libpthread, and lock.c needs it. |
| 207 |
# |
| 208 |
# If -pthread works, prefer it to -lpthread, since Ubuntu 14.04 |
| 209 |
# needs -pthread for some reason. See: |
| 210 |
# https://lists.gnu.org/r/bug-gnulib/2014-09/msg00023.html |
| 211 |
saved_LIBS="$LIBS" |
| 212 |
for gl_pthread in '' '-pthread'; do |
| 213 |
LIBS="$LIBS $gl_pthread" |
| 214 |
AC_LINK_IFELSE( |
| 215 |
[AC_LANG_PROGRAM( |
| 216 |
[[#include <pthread.h> |
| 217 |
pthread_mutex_t m; |
| 218 |
pthread_mutexattr_t ma; |
| 219 |
]], |
| 220 |
[[pthread_mutex_lock (&m); |
| 221 |
pthread_mutexattr_init (&ma);]])], |
| 222 |
[gl_pthread_api=yes |
| 223 |
LIBPTHREAD=$gl_pthread |
| 224 |
LIBPMULTITHREAD=$gl_pthread]) |
| 225 |
LIBS="$saved_LIBS" |
| 226 |
test $gl_pthread_api = yes && break |
| 227 |
done |
| 228 |
echo "$as_me:__oline__: gl_pthread_api=$gl_pthread_api" >&AS_MESSAGE_LOG_FD |
| 229 |
echo "$as_me:__oline__: LIBPTHREAD=$LIBPTHREAD" >&AS_MESSAGE_LOG_FD |
| 230 |
|
| 231 |
gl_pthread_in_glibc=no |
| 232 |
# On Linux with glibc >= 2.34, libc contains the fully functional |
| 233 |
# pthread functions. |
| 234 |
case "$host_os" in |
| 235 |
linux*) |
| 236 |
AC_EGREP_CPP([Lucky user], |
| 237 |
[#include <features.h> |
| 238 |
#ifdef __GNU_LIBRARY__ |
| 239 |
#if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 34) || (__GLIBC__ > 2) |
| 240 |
Lucky user |
| 241 |
#endif |
| 242 |
#endif |
| 243 |
], |
| 244 |
[gl_pthread_in_glibc=yes], |
| 245 |
[]) |
| 246 |
;; |
| 247 |
esac |
| 248 |
echo "$as_me:__oline__: gl_pthread_in_glibc=$gl_pthread_in_glibc" >&AS_MESSAGE_LOG_FD |
| 249 |
|
| 250 |
# Test for libpthread by looking for pthread_kill. (Not pthread_self, |
| 251 |
# since it is defined as a macro on OSF/1.) |
| 252 |
if test $gl_pthread_api = yes && test -z "$LIBPTHREAD"; then |
| 253 |
# The program links fine without libpthread. But it may actually |
| 254 |
# need to link with libpthread in order to create multiple threads. |
| 255 |
AC_CHECK_LIB([pthread], [pthread_kill], |
| 256 |
[if test $gl_pthread_in_glibc = yes; then |
| 257 |
LIBPMULTITHREAD= |
| 258 |
else |
| 259 |
LIBPMULTITHREAD=-lpthread |
| 260 |
# On Solaris and HP-UX, most pthread functions exist also in libc. |
| 261 |
# Therefore pthread_in_use() needs to actually try to create a |
| 262 |
# thread: pthread_create from libc will fail, whereas |
| 263 |
# pthread_create will actually create a thread. |
| 264 |
# On Solaris 10 or newer, this test is no longer needed, because |
| 265 |
# libc contains the fully functional pthread functions. |
| 266 |
case "$host_os" in |
| 267 |
changequote(,)dnl |
| 268 |
solaris | solaris2.[1-9] | solaris2.[1-9].* | hpux*) |
| 269 |
changequote([,])dnl |
| 270 |
AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1], |
| 271 |
[Define if the pthread_in_use() detection is hard.]) |
| 272 |
esac |
| 273 |
fi |
| 274 |
], |
| 275 |
[dnl This is needed on FreeBSD 5.2.1. |
| 276 |
AC_CHECK_LIB([thr], [pthread_kill], |
| 277 |
[if test $gl_pthread_in_glibc = yes; then |
| 278 |
LIBPMULTITHREAD= |
| 279 |
else |
| 280 |
LIBPMULTITHREAD=-lthr |
| 281 |
fi |
| 282 |
]) |
| 283 |
]) |
| 284 |
elif test $gl_pthread_api != yes; then |
| 285 |
# Some library is needed. Try libpthread and libc_r. |
| 286 |
AC_CHECK_LIB([pthread], [pthread_kill], |
| 287 |
[gl_pthread_api=yes |
| 288 |
LIBPTHREAD=-lpthread |
| 289 |
LIBPMULTITHREAD=-lpthread]) |
| 290 |
if test $gl_pthread_api != yes; then |
| 291 |
# For FreeBSD 4. |
| 292 |
AC_CHECK_LIB([c_r], [pthread_kill], |
| 293 |
[gl_pthread_api=yes |
| 294 |
LIBPTHREAD=-lc_r |
| 295 |
LIBPMULTITHREAD=-lc_r]) |
| 296 |
fi |
| 297 |
fi |
| 298 |
echo "$as_me:__oline__: LIBPMULTITHREAD=$LIBPMULTITHREAD" >&AS_MESSAGE_LOG_FD |
| 299 |
fi |
| 300 |
AC_MSG_CHECKING([whether POSIX threads API is available]) |
| 301 |
AC_MSG_RESULT([$gl_pthread_api]) |
| 302 |
AC_SUBST([LIBPTHREAD]) |
| 303 |
AC_SUBST([LIBPMULTITHREAD]) |
| 304 |
if test $gl_pthread_api = yes; then |
| 305 |
AC_DEFINE([HAVE_PTHREAD_API], [1], |
| 306 |
[Define if you have the <pthread.h> header and the POSIX threads API.]) |
| 307 |
fi |
| 308 |
|
| 309 |
dnl On some systems, sched_yield is in librt, rather than in libpthread. |
| 310 |
AC_LINK_IFELSE( |
| 311 |
[AC_LANG_PROGRAM( |
| 312 |
[[#include <sched.h>]], |
| 313 |
[[sched_yield ();]])], |
| 314 |
[SCHED_YIELD_LIB= |
| 315 |
], |
| 316 |
[dnl Solaris 7...10 has sched_yield in librt, not in libpthread or libc. |
| 317 |
AC_CHECK_LIB([rt], [sched_yield], [SCHED_YIELD_LIB=-lrt], |
| 318 |
[dnl Solaris 2.5.1, 2.6 has sched_yield in libposix4, not librt. |
| 319 |
AC_CHECK_LIB([posix4], [sched_yield], [SCHED_YIELD_LIB=-lposix4])]) |
| 320 |
]) |
| 321 |
AC_SUBST([SCHED_YIELD_LIB]) |
| 322 |
dnl For backward compatibility. |
| 323 |
LIB_SCHED_YIELD="$SCHED_YIELD_LIB" |
| 324 |
AC_SUBST([LIB_SCHED_YIELD]) |
| 325 |
|
| 326 |
gl_pthreadlib_body_done=done |
| 327 |
fi |
| 328 |
]) |
| 329 |
|
| 330 |
AC_DEFUN([gl_PTHREADLIB], |
| 331 |
[ |
| 332 |
AC_REQUIRE([gl_ANYTHREADLIB_EARLY]) |
| 333 |
gl_PTHREADLIB_BODY |
| 334 |
]) |
| 335 |
|
| 336 |
dnl ============================================================================ |
| 337 |
dnl Macros for the ISO C API |
| 338 |
|
| 339 |
dnl gl_STDTHREADLIB |
| 340 |
dnl --------------- |
| 341 |
dnl Tests for the libraries needs for using the ISO C threads API. |
| 342 |
dnl Sets the variable LIBSTDTHREAD to the linker options for use in a Makefile. |
| 343 |
dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for |
| 344 |
dnl multithread-safe programs. |
| 345 |
dnl Defines the C macro HAVE_THREADS_H if (at least parts of) the ISO C threads |
| 346 |
dnl API is available. |
| 347 |
|
| 348 |
dnl The guts of gl_STDTHREADLIB. Needs to be expanded only once. |
| 349 |
|
| 350 |
AC_DEFUN([gl_STDTHREADLIB_BODY], |
| 351 |
[ |
| 352 |
AC_REQUIRE([gl_ANYTHREADLIB_EARLY]) |
| 353 |
AC_REQUIRE([AC_CANONICAL_HOST]) |
| 354 |
if test -z "$gl_stdthreadlib_body_done"; then |
| 355 |
AC_CHECK_HEADERS_ONCE([threads.h]) |
| 356 |
|
| 357 |
case "$host_os" in |
| 358 |
mingw* | windows*) |
| 359 |
LIBSTDTHREAD= |
| 360 |
;; |
| 361 |
*) |
| 362 |
gl_PTHREADLIB_BODY |
| 363 |
if test $ac_cv_header_threads_h = yes; then |
| 364 |
dnl glibc >= 2.29 has thrd_create in libpthread. |
| 365 |
dnl FreeBSD >= 10 has thrd_create in libstdthreads; this library depends |
| 366 |
dnl on libpthread (for the symbol 'pthread_mutexattr_gettype'). |
| 367 |
dnl glibc >= 2.34, AIX >= 7.1, and Solaris >= 11.4 have thrd_create in |
| 368 |
dnl libc. |
| 369 |
gl_CHECK_FUNCS_ANDROID([thrd_create], [[#include <threads.h>]]) |
| 370 |
if test $ac_cv_func_thrd_create = yes; then |
| 371 |
LIBSTDTHREAD= |
| 372 |
else |
| 373 |
AC_CHECK_LIB([stdthreads], [thrd_create], [ |
| 374 |
LIBSTDTHREAD='-lstdthreads -lpthread' |
| 375 |
], [ |
| 376 |
dnl Guess that thrd_create is in libpthread. |
| 377 |
LIBSTDTHREAD="$LIBPMULTITHREAD" |
| 378 |
]) |
| 379 |
fi |
| 380 |
else |
| 381 |
dnl Libraries needed by thrd.c, mtx.c, cnd.c, tss.c. |
| 382 |
LIBSTDTHREAD="$LIBPMULTITHREAD $SCHED_YIELD_LIB" |
| 383 |
fi |
| 384 |
;; |
| 385 |
esac |
| 386 |
AC_SUBST([LIBSTDTHREAD]) |
| 387 |
|
| 388 |
AC_MSG_CHECKING([whether ISO C threads API is available]) |
| 389 |
AC_MSG_RESULT([$ac_cv_header_threads_h]) |
| 390 |
gl_stdthreadlib_body_done=done |
| 391 |
fi |
| 392 |
]) |
| 393 |
|
| 394 |
AC_DEFUN([gl_STDTHREADLIB], |
| 395 |
[ |
| 396 |
AC_REQUIRE([gl_ANYTHREADLIB_EARLY]) |
| 397 |
gl_STDTHREADLIB_BODY |
| 398 |
]) |
| 399 |
|
| 400 |
dnl ============================================================================ |
| 401 |
dnl Macros for the Gnulib API |
| 402 |
|
| 403 |
dnl gl_THREADLIB |
| 404 |
dnl ------------ |
| 405 |
dnl Tests for a multithreading library to be used. |
| 406 |
dnl If the configure.ac contains a definition of the gl_THREADLIB_DEFAULT_NO |
| 407 |
dnl (it must be placed before the invocation of gl_THREADLIB_EARLY!), then the |
| 408 |
dnl default is 'no', otherwise it is system dependent. In both cases, the user |
| 409 |
dnl can change the choice through the options --enable-threads=choice or |
| 410 |
dnl --disable-threads. |
| 411 |
dnl Defines at most one of the macros USE_ISOC_THREADS, USE_POSIX_THREADS, |
| 412 |
dnl USE_ISOC_AND_POSIX_THREADS, USE_WINDOWS_THREADS. |
| 413 |
dnl The choice --enable-threads=isoc+posix is available only on platforms that |
| 414 |
dnl have both the ISO C and the POSIX threads APIs. It has the effect of using |
| 415 |
dnl the ISO C API for most things and the POSIX API only for creating and |
| 416 |
dnl controlling threads (because there is no equivalent to pthread_atfork in |
| 417 |
dnl the ISO C API). |
| 418 |
dnl Sets the variables LIBTHREAD and LTLIBTHREAD to the linker options for use |
| 419 |
dnl in a Makefile (LIBTHREAD for use without libtool, LTLIBTHREAD for use with |
| 420 |
dnl libtool). |
| 421 |
dnl Sets the variables LIBMULTITHREAD and LTLIBMULTITHREAD similarly, for |
| 422 |
dnl programs that really need multithread functionality. The difference |
| 423 |
dnl between LIBTHREAD and LIBMULTITHREAD is that on platforms supporting weak |
| 424 |
dnl symbols, typically LIBTHREAD is empty whereas LIBMULTITHREAD is not. |
| 425 |
dnl Adds to CPPFLAGS the flag -D_REENTRANT or -D_THREAD_SAFE if needed for |
| 426 |
dnl multithread-safe programs. |
| 427 |
dnl Since support for GNU pth was removed, $LTLIBTHREAD and $LIBTHREAD have the |
| 428 |
dnl same value, and similarly $LTLIBMULTITHREAD and $LIBMULTITHREAD have the |
| 429 |
dnl same value. Only system libraries are needed. |
| 430 |
|
| 431 |
AC_DEFUN([gl_THREADLIB_EARLY], |
| 432 |
[ |
| 433 |
AC_REQUIRE([gl_THREADLIB_EARLY_BODY]) |
| 434 |
]) |
| 435 |
|
| 436 |
dnl The guts of gl_THREADLIB_EARLY. Needs to be expanded only once. |
| 437 |
|
| 438 |
AC_DEFUN([gl_THREADLIB_EARLY_BODY], |
| 439 |
[ |
| 440 |
dnl Ordering constraints: This macro modifies CPPFLAGS in a way that |
| 441 |
dnl influences the result of the autoconf tests that test for *_unlocked |
| 442 |
dnl declarations, on AIX 5 at least. Therefore it must come early. |
| 443 |
AC_BEFORE([$0], [gl_FUNC_GLIBC_UNLOCKED_IO])dnl |
| 444 |
AC_BEFORE([$0], [gl_ARGP])dnl |
| 445 |
|
| 446 |
AC_REQUIRE([AC_CANONICAL_HOST]) |
| 447 |
dnl _GNU_SOURCE is needed for pthread_rwlock_t on glibc systems. |
| 448 |
AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) |
| 449 |
dnl Check for multithreading. |
| 450 |
m4_ifdef([gl_THREADLIB_DEFAULT_NO], |
| 451 |
[m4_divert_text([DEFAULTS], [gl_use_threads_default=no])], |
| 452 |
[m4_divert_text([DEFAULTS], [gl_use_threads_default=])]) |
| 453 |
dnl gl_use_winpthreads_default defaults to 'no', because in mingw 10, like |
| 454 |
dnl in mingw 5, the use of libwinpthread still makes test-pthread-tss crash. |
| 455 |
m4_divert_text([DEFAULTS], [gl_use_winpthreads_default=no]) |
| 456 |
AC_ARG_ENABLE([threads], |
| 457 |
AS_HELP_STRING([[--enable-threads={isoc|posix|isoc+posix|windows}]], [specify multithreading API])m4_ifdef([gl_THREADLIB_DEFAULT_NO], [], [ |
| 458 |
AS_HELP_STRING([[--disable-threads]], [build without multithread safety])]), |
| 459 |
[gl_use_threads=$enableval], |
| 460 |
[if test -n "$gl_use_threads_default"; then |
| 461 |
gl_use_threads="$gl_use_threads_default" |
| 462 |
else |
| 463 |
changequote(,)dnl |
| 464 |
case "$host_os" in |
| 465 |
dnl Disable multithreading by default on OSF/1, because it interferes |
| 466 |
dnl with fork()/exec(): When msgexec is linked with -lpthread, its |
| 467 |
dnl child process gets an endless segmentation fault inside execvp(). |
| 468 |
osf*) gl_use_threads=no ;; |
| 469 |
dnl Disable multithreading by default on Cygwin 1.5.x, because it has |
| 470 |
dnl bugs that lead to endless loops or crashes. See |
| 471 |
dnl <https://cygwin.com/ml/cygwin/2009-08/msg00283.html>. |
| 472 |
cygwin*) |
| 473 |
case `uname -r` in |
| 474 |
1.[0-5].*) gl_use_threads=no ;; |
| 475 |
*) gl_use_threads=yes ;; |
| 476 |
esac |
| 477 |
;; |
| 478 |
dnl Obey gl_AVOID_WINPTHREAD on mingw. |
| 479 |
mingw* | windows*) |
| 480 |
case "$gl_use_winpthreads_default" in |
| 481 |
yes) gl_use_threads=posix ;; |
| 482 |
no) gl_use_threads=windows ;; |
| 483 |
*) gl_use_threads=yes ;; |
| 484 |
esac |
| 485 |
;; |
| 486 |
*) gl_use_threads=yes ;; |
| 487 |
esac |
| 488 |
changequote([,])dnl |
| 489 |
fi |
| 490 |
]) |
| 491 |
if test "$gl_use_threads" = yes \ |
| 492 |
|| test "$gl_use_threads" = isoc \ |
| 493 |
|| test "$gl_use_threads" = posix \ |
| 494 |
|| test "$gl_use_threads" = isoc+posix; then |
| 495 |
# For using <threads.h> or <pthread.h>: |
| 496 |
gl_ANYTHREADLIB_EARLY |
| 497 |
fi |
| 498 |
]) |
| 499 |
|
| 500 |
dnl The guts of gl_THREADLIB. Needs to be expanded only once. |
| 501 |
|
| 502 |
AC_DEFUN([gl_THREADLIB_BODY], |
| 503 |
[ |
| 504 |
AC_REQUIRE([gl_THREADLIB_EARLY_BODY]) |
| 505 |
gl_threads_api=none |
| 506 |
LIBTHREAD= |
| 507 |
LTLIBTHREAD= |
| 508 |
LIBMULTITHREAD= |
| 509 |
LTLIBMULTITHREAD= |
| 510 |
if test "$gl_use_threads" != no; then |
| 511 |
dnl Check whether the compiler and linker support weak declarations. |
| 512 |
gl_WEAK_SYMBOLS |
| 513 |
if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then |
| 514 |
dnl If we use weak symbols to implement pthread_in_use / pth_in_use / |
| 515 |
dnl thread_in_use, we also need to test whether the ISO C 11 thrd_create |
| 516 |
dnl facility is in use. |
| 517 |
AC_CHECK_HEADERS_ONCE([threads.h]) |
| 518 |
: |
| 519 |
fi |
| 520 |
if test "$gl_use_threads" = isoc || test "$gl_use_threads" = isoc+posix; then |
| 521 |
AC_CHECK_HEADERS_ONCE([threads.h]) |
| 522 |
gl_have_isoc_threads="$ac_cv_header_threads_h" |
| 523 |
fi |
| 524 |
if test "$gl_use_threads" = yes \ |
| 525 |
|| test "$gl_use_threads" = posix \ |
| 526 |
|| test "$gl_use_threads" = isoc+posix; then |
| 527 |
gl_PTHREADLIB_BODY |
| 528 |
LIBTHREAD=$LIBPTHREAD LTLIBTHREAD=$LIBPTHREAD |
| 529 |
LIBMULTITHREAD=$LIBPMULTITHREAD LTLIBMULTITHREAD=$LIBPMULTITHREAD |
| 530 |
if test $gl_pthread_api = yes; then |
| 531 |
if test "$gl_use_threads" = isoc+posix && test "$gl_have_isoc_threads" = yes; then |
| 532 |
gl_threads_api='isoc+posix' |
| 533 |
AC_DEFINE([USE_ISOC_AND_POSIX_THREADS], [1], |
| 534 |
[Define if the combination of the ISO C and POSIX multithreading APIs can be used.]) |
| 535 |
LIBTHREAD= LTLIBTHREAD= |
| 536 |
else |
| 537 |
gl_threads_api=posix |
| 538 |
AC_DEFINE([USE_POSIX_THREADS], [1], |
| 539 |
[Define if the POSIX multithreading library can be used.]) |
| 540 |
if test -z "$LIBMULTITHREAD" && test -z "$LTLIBMULTITHREAD"; then |
| 541 |
AC_DEFINE([USE_POSIX_THREADS_FROM_LIBC], [1], |
| 542 |
[Define if references to the POSIX multithreading library are satisfied by libc.]) |
| 543 |
else |
| 544 |
if case "$gl_cv_have_weak" in *yes) true;; *) false;; esac; then |
| 545 |
AC_DEFINE([USE_POSIX_THREADS_WEAK], [1], |
| 546 |
[Define if references to the POSIX multithreading library should be made weak.]) |
| 547 |
LIBTHREAD= LTLIBTHREAD= |
| 548 |
else |
| 549 |
case "$host_os" in |
| 550 |
freebsd* | dragonfly* | midnightbsd*) |
| 551 |
if test "x$LIBTHREAD" != "x$LIBMULTITHREAD"; then |
| 552 |
dnl If weak symbols can't tell whether pthread_create(), pthread_key_create() |
| 553 |
dnl etc. will succeed, we need a runtime test. |
| 554 |
AC_DEFINE([PTHREAD_IN_USE_DETECTION_HARD], [1], |
| 555 |
[Define if the pthread_in_use() detection is hard.]) |
| 556 |
fi |
| 557 |
;; |
| 558 |
esac |
| 559 |
fi |
| 560 |
fi |
| 561 |
fi |
| 562 |
fi |
| 563 |
fi |
| 564 |
if test $gl_threads_api = none; then |
| 565 |
if test "$gl_use_threads" = isoc && test "$gl_have_isoc_threads" = yes; then |
| 566 |
gl_STDTHREADLIB_BODY |
| 567 |
LIBTHREAD=$LIBSTDTHREAD LTLIBTHREAD=$LIBSTDTHREAD |
| 568 |
LIBMULTITHREAD=$LIBSTDTHREAD LTLIBMULTITHREAD=$LIBSTDTHREAD |
| 569 |
gl_threads_api=isoc |
| 570 |
AC_DEFINE([USE_ISOC_THREADS], [1], |
| 571 |
[Define if the ISO C multithreading library can be used.]) |
| 572 |
fi |
| 573 |
fi |
| 574 |
if test $gl_threads_api = none; then |
| 575 |
case "$gl_use_threads" in |
| 576 |
yes | windows | win32) # The 'win32' is for backward compatibility. |
| 577 |
if { case "$host_os" in |
| 578 |
mingw* | windows*) true;; |
| 579 |
*) false;; |
| 580 |
esac |
| 581 |
}; then |
| 582 |
gl_threads_api=windows |
| 583 |
AC_DEFINE([USE_WINDOWS_THREADS], [1], |
| 584 |
[Define if the native Windows multithreading API can be used.]) |
| 585 |
fi |
| 586 |
;; |
| 587 |
esac |
| 588 |
fi |
| 589 |
else |
| 590 |
dnl "$gl_use_threads" is "no". |
| 591 |
AC_DEFINE([AVOID_ANY_THREADS], [1], |
| 592 |
[Define if no multithread safety and no multithreading is desired.]) |
| 593 |
fi |
| 594 |
AC_MSG_CHECKING([for multithread API to use]) |
| 595 |
AC_MSG_RESULT([$gl_threads_api]) |
| 596 |
AC_SUBST([LIBTHREAD]) |
| 597 |
AC_SUBST([LTLIBTHREAD]) |
| 598 |
AC_SUBST([LIBMULTITHREAD]) |
| 599 |
AC_SUBST([LTLIBMULTITHREAD]) |
| 600 |
]) |
| 601 |
|
| 602 |
AC_DEFUN([gl_THREADLIB], |
| 603 |
[ |
| 604 |
AC_REQUIRE([gl_THREADLIB_EARLY]) |
| 605 |
AC_REQUIRE([gl_THREADLIB_BODY]) |
| 606 |
]) |
| 607 |
|
| 608 |
|
| 609 |
dnl gl_DISABLE_THREADS |
| 610 |
dnl ------------------ |
| 611 |
dnl Sets the gl_THREADLIB default so that threads are not used by default. |
| 612 |
dnl The user can still override it at installation time, by using the |
| 613 |
dnl configure option '--enable-threads'. |
| 614 |
|
| 615 |
AC_DEFUN([gl_DISABLE_THREADS], [ |
| 616 |
m4_divert_text([INIT_PREPARE], [gl_use_threads_default=no]) |
| 617 |
]) |
| 618 |
|
| 619 |
|
| 620 |
dnl gl_AVOID_WINPTHREAD |
| 621 |
dnl ------------------- |
| 622 |
dnl Sets the gl_THREADLIB default so that on mingw, a dependency to the |
| 623 |
dnl libwinpthread DLL (mingw-w64 winpthreads library) is avoided. |
| 624 |
dnl The user can still override it at installation time, by using the |
| 625 |
dnl configure option '--enable-threads=posix'. |
| 626 |
dnl As of 2023, this is now the default. |
| 627 |
|
| 628 |
AC_DEFUN([gl_AVOID_WINPTHREAD], [ |
| 629 |
m4_divert_text([INIT_PREPARE], [gl_use_winpthreads_default=no]) |
| 630 |
]) |
| 631 |
|
| 632 |
|
| 633 |
dnl ============================================================================ |
| 634 |
|
| 635 |
|
| 636 |
dnl Survey of platforms: |
| 637 |
dnl |
| 638 |
dnl Platform Available Compiler Supports test-lock |
| 639 |
dnl flavours option weak result |
| 640 |
dnl --------------- --------- --------- -------- --------- |
| 641 |
dnl Linux 2.4/glibc posix -lpthread Y OK |
| 642 |
dnl |
| 643 |
dnl Linux/glibc 2.34 posix Y OK |
| 644 |
dnl |
| 645 |
dnl GNU Hurd/glibc posix -lpthread Y OK |
| 646 |
dnl |
| 647 |
dnl Ubuntu 14.04 posix -pthread Y OK |
| 648 |
dnl |
| 649 |
dnl FreeBSD 5.3 posix -lc_r Y |
| 650 |
dnl posix -lkse ? Y |
| 651 |
dnl posix -lpthread ? Y |
| 652 |
dnl posix -lthr Y |
| 653 |
dnl |
| 654 |
dnl FreeBSD 5.2 posix -lc_r Y |
| 655 |
dnl posix -lkse Y |
| 656 |
dnl posix -lthr Y |
| 657 |
dnl |
| 658 |
dnl FreeBSD 4.0,4.10 posix -lc_r Y OK |
| 659 |
dnl |
| 660 |
dnl NetBSD 1.6 -- |
| 661 |
dnl |
| 662 |
dnl OpenBSD 3.4 posix -lpthread Y OK |
| 663 |
dnl |
| 664 |
dnl Mac OS X 10.[123] posix -lpthread Y OK |
| 665 |
dnl |
| 666 |
dnl Solaris 7,8,9 posix -lpthread Y Sol 7,8: 0.0; Sol 9: OK |
| 667 |
dnl |
| 668 |
dnl HP-UX 11 posix -lpthread N (cc) OK |
| 669 |
dnl Y (gcc) |
| 670 |
dnl |
| 671 |
dnl IRIX 6.5 posix -lpthread Y 0.5 |
| 672 |
dnl |
| 673 |
dnl AIX 4.3,5.1 posix -lpthread N AIX 4: 0.5; AIX 5: OK |
| 674 |
dnl |
| 675 |
dnl OSF/1 4.0,5.1 posix -pthread (cc) N OK |
| 676 |
dnl -lpthread (gcc) Y |
| 677 |
dnl |
| 678 |
dnl Cygwin posix -lpthread Y OK |
| 679 |
dnl |
| 680 |
dnl Mingw windows N OK |
| 681 |
dnl |
| 682 |
dnl BeOS 5 -- |
| 683 |
dnl |
| 684 |
dnl The test-lock result shows what happens if in test-lock.c EXPLICIT_YIELD is |
| 685 |
dnl turned off: |
| 686 |
dnl OK if all three tests terminate OK, |
| 687 |
dnl 0.5 if the first test terminates OK but the second one loops endlessly, |
| 688 |
dnl 0.0 if the first test already loops endlessly. |