threadlib.m4 24 KB

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