1 dnl Check whether the target supports TLS.
2 AC_DEFUN([GCC_CHECK_TLS], [
3 GCC_ENABLE(tls, yes, [], [Use thread-local storage])
4 AC_CACHE_CHECK([whether the target supports thread-local storage],
6 AC_RUN_IFELSE([__thread int a; int b; int main() { return a = b; }],
7 [dnl If the test case passed with dynamic linking, try again with
8 dnl static linking, but only if static linking is supported (not
9 dnl on Solaris 10). This fails with some older Red Hat releases.
10 chktls_save_LDFLAGS="$LDFLAGS"
11 LDFLAGS="-static $LDFLAGS"
12 AC_LINK_IFELSE([int main() { return 0; }],
13 AC_RUN_IFELSE([__thread int a; int b; int main() { return a = b; }],
14 [have_tls=yes], [have_tls=no],[]),
16 LDFLAGS="$chktls_save_LDFLAGS"
17 if test $have_tls = yes; then
18 dnl So far, the binutils and the compiler support TLS.
19 dnl Also check whether the libc supports TLS, i.e. whether a variable
20 dnl with __thread linkage has a different address in different threads.
21 dnl First, find the thread_CFLAGS necessary for linking a program that
22 dnl calls pthread_create.
23 chktls_save_CFLAGS="$CFLAGS"
25 for flag in '' '-pthread' '-lpthread'; do
26 CFLAGS="$flag $chktls_save_CFLAGS"
30 void *g(void *d) { return NULL; }],
31 [pthread_t t; pthread_create(&t,NULL,g,NULL);])],
32 [thread_CFLAGS="$flag"])
33 if test "X$thread_CFLAGS" != Xfailed; then
37 CFLAGS="$chktls_save_CFLAGS"
38 if test "X$thread_CFLAGS" != Xfailed; then
39 CFLAGS="$thread_CFLAGS $chktls_save_CFLAGS"
44 static int *a_in_other_thread;
46 thread_func (void *arg)
48 a_in_other_thread = &a;
53 int *a_in_main_thread;
54 if (pthread_create (&thread, (pthread_attr_t *)0,
55 thread_func, (void *)0))
57 a_in_main_thread = &a;
58 if (pthread_join (thread, &thread_retval))
60 return (a_in_other_thread == a_in_main_thread);])],
61 [have_tls=yes], [have_tls=no], [])
62 CFLAGS="$chktls_save_CFLAGS"
66 [dnl This is the cross-compiling case. Assume libc supports TLS if the
67 dnl binutils and the compiler do.
68 AC_LINK_IFELSE([__thread int a; int b; int main() { return a = b; }],
69 [have_tls=yes], [have_tls=no])
72 if test "$enable_tls $have_tls" = "yes yes"; then
73 AC_DEFINE(HAVE_TLS, 1,
74 [Define to 1 if the target supports thread-local storage.])
77 dnl Check whether the target assembler supports TLS.
78 AC_DEFUN([GCC_CHECK_CC_TLS], [
79 GCC_ENABLE(tls, yes, [], [Use thread-local storage])
80 AC_CACHE_CHECK([whether the target asssembler upports thread-local storage],
82 AC_COMPILE_IFELSE([__thread int a; int b; int main() { return a = b; }],
83 [have_cc_tls=yes], [have_cc_tls=no])]
85 if test "$enable_tls $have_cc_tls" = "yes yes"; then
86 AC_DEFINE(HAVE_CC_TLS, 1,
87 [Define to 1 if the target assembler supports thread-local storage.])