]> git.ipfire.org Git - thirdparty/util-linux.git/blob - m4/tls.m4
scriptreplay: add --stream to the man page
[thirdparty/util-linux.git] / m4 / tls.m4
1 #
2 # AX_CHECK_TLS -- check whether the target supports TLS (thread-local storage)
3 #
4 # Based on tls.m4 from gcc and extended by TLS link test for cross-compiling
5 # support from http://old.nabble.com/Improve-TLS-link-test-for-cross-compiling-td24312975.html
6 #
7 # Note that AX_TLS from http://autoconf-archive.cryp.to/ax_tls.html supports
8 # more keywords for TLS. We are happy with the "__thread" only.
9 #
10 # -- Karel Zak (04-Dec-2009)
11 #
12 dnl Check whether the target supports TLS.
13 AC_DEFUN([AX_CHECK_TLS], [
14
15 AC_REQUIRE([AC_CANONICAL_HOST])
16
17 AC_ARG_ENABLE([tls],
18 AS_HELP_STRING([--disable-tls], [disable use of thread local support]),
19 [], enable_tls=yes)
20
21 AC_CACHE_CHECK([whether the target supports thread-local storage],
22 ax_cv_have_tls, [
23 AC_RUN_IFELSE([AC_LANG_SOURCE([__thread int a; int b; int main() { return a = b; }])],
24 [dnl If the test case passed with dynamic linking, try again with
25 dnl static linking, but only if static linking is supported (not
26 dnl on Solaris 10). This fails with some older Red Hat releases.
27 chktls_save_LDFLAGS="$LDFLAGS"
28 LDFLAGS="-static $LDFLAGS"
29 AC_LINK_IFELSE([AC_LANG_SOURCE([int main() { return 0; }])],
30 AC_RUN_IFELSE([AC_LANG_SOURCE([__thread int a; int b; int main() { return a = b; }])],
31 [ax_cv_have_tls=yes], [ax_cv_have_tls=no],[]),
32 [ax_cv_have_tls=yes])
33 LDFLAGS="$chktls_save_LDFLAGS"
34 if test $ax_cv_have_tls = yes; then
35 dnl So far, the binutils and the compiler support TLS.
36 dnl Also check whether the libc supports TLS, i.e. whether a variable
37 dnl with __thread linkage has a different address in different threads.
38 dnl First, find the thread_CFLAGS necessary for linking a program that
39 dnl calls pthread_create.
40 chktls_save_CFLAGS="$CFLAGS"
41 thread_CFLAGS=failed
42 for flag in '' '-pthread' '-lpthread'; do
43 CFLAGS="$flag $chktls_save_CFLAGS"
44 AC_LINK_IFELSE(
45 [AC_LANG_PROGRAM(
46 [#include <pthread.h>
47 void *g(void *d) { return NULL; }],
48 [pthread_t t; pthread_create(&t,NULL,g,NULL);])],
49 [thread_CFLAGS="$flag"])
50 if test "X$thread_CFLAGS" != Xfailed; then
51 break
52 fi
53 done
54 CFLAGS="$chktls_save_CFLAGS"
55 if test "X$thread_CFLAGS" != Xfailed; then
56 CFLAGS="$thread_CFLAGS $chktls_save_CFLAGS"
57 AC_RUN_IFELSE(
58 [AC_LANG_PROGRAM(
59 [#include <pthread.h>
60 __thread int a;
61 static int *a_in_other_thread;
62 static void *
63 thread_func (void *arg)
64 {
65 a_in_other_thread = &a;
66 return (void *)0;
67 }],
68 [pthread_t thread;
69 void *thread_retval;
70 int *a_in_main_thread;
71 if (pthread_create (&thread, (pthread_attr_t *)0,
72 thread_func, (void *)0))
73 return 0;
74 a_in_main_thread = &a;
75 if (pthread_join (thread, &thread_retval))
76 return 0;
77 return (a_in_other_thread == a_in_main_thread);])],
78 [ax_cv_have_tls=yes], [ax_cv_have_tls=no], [])
79 CFLAGS="$chktls_save_CFLAGS"
80 fi
81 fi],
82 [ax_cv_have_tls=no],
83 [dnl This is the cross-compiling case. Assume libc supports TLS if the
84 dnl binutils and the compiler do.
85 AC_LINK_IFELSE([AC_LANG_SOURCE([__thread int a; int b; int main() { return a = b; }])],
86 [chktls_save_LDFLAGS="$LDFLAGS"
87 dnl Shared library options may depend on the host; this check
88 dnl is only known to be needed for GNU/Linux.
89 case $host in
90 *-*-linux*)
91 LDFLAGS="-shared -Wl,--no-undefined $LDFLAGS"
92 ;;
93 esac
94 chktls_save_CFLAGS="$CFLAGS"
95 CFLAGS="-fPIC $CFLAGS"
96 dnl If -shared works, test if TLS works in a shared library.
97 AC_LINK_IFELSE([AC_LANG_SOURCE([int f() { return 0; }])],
98 [AC_LINK_IFELSE([AC_LANG_SOURCE([__thread int a; int b; int f() { return a = b; }])],
99 [ax_cv_have_tls=yes],
100 [ax_cv_have_tls=no])],
101 [ax_cv_have_tls=yes])
102 CFLAGS="$chktls_save_CFLAGS"
103 LDFLAGS="$chktls_save_LDFLAGS"], [ax_cv_have_tls=no])
104 ]
105 )])
106
107 if test "$enable_tls $ax_cv_have_tls" = "yes yes"; then
108 AC_DEFINE(HAVE_TLS, 1,
109 [Define to 1 if the target supports thread-local storage.])
110 fi
111 ])