Index: gcc/testsuite/lib/target-supports.exp =================================================================== --- gcc/testsuite/lib/target-supports.exp (revision 161974) +++ gcc/testsuite/lib/target-supports.exp (working copy) @@ -596,6 +596,23 @@ proc check_effective_target_tls_native {} { }] } +# Return 1 if *emulated* thread local storage (TLS) is supported, 0 otherwise. + +proc check_effective_target_tls_emulated {} { + # VxWorks uses emulated TLS machinery, but with non-standard helper + # functions, so we fail to automatically detect it. + global target_triplet + if { [regexp ".*-.*-vxworks.*" $target_triplet] } { + return 1 + } + + return [check_no_messages_and_pattern tls_emulated "emutls" assembly { + __thread int i; + int f (void) { return i; } + void g (int j) { i = j; } + }] +} + # Return 1 if TLS executables can run correctly, 0 otherwise. proc check_effective_target_tls_runtime {} { Index: gcc/testsuite/gcc.dg/tls/thr-cse-1.c =================================================================== --- gcc/testsuite/gcc.dg/tls/thr-cse-1.c (revision 0) +++ gcc/testsuite/gcc.dg/tls/thr-cse-1.c (revision 0) @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O1" } */ +/* { dg-require-effective-target tls_emulated } */ + +/* Test that we only get one call to emutls_get_address when CSE is + active. Note that the var _must_ be initialized for the scan asm + to work, since otherwise there will be an initializer which will, + correctly, call emutls_get_address. */ +int foo (int b, int c, int d) +{ + static __thread int a=1; + a += b; + a -= c; + a += d; + return a; +} + +/* { dg-final { scan-assembler-not "emutls_get_address.*emutls_get_address.*" { target { ! *-wrs-vxworks } } } } */ +/* { dg-final { scan-assembler-not "tls_lookup.*tls_lookup.*" { target *-wrs-vxworks } } } */ + Index: gcc/testsuite/gcc.dg/tls/thr-init-1.c =================================================================== --- gcc/testsuite/gcc.dg/tls/thr-init-1.c (revision 0) +++ gcc/testsuite/gcc.dg/tls/thr-init-1.c (revision 0) @@ -0,0 +1,8 @@ +/* { dg-require-effective-target tls } */ +/* { dg-do compile } */ + +static __thread int fstat ; +static __thread int fstat = 1 ; +static __thread int fstat ; +static __thread int fstat = 2; /* { dg-error "redefinition of 'fstat'" } */ + /* { dg-message "note: previous definition of 'fstat' was here" "" { target *-*-* } 5 } */ Index: gcc/testsuite/gcc.dg/tls/thr-init-2.c =================================================================== --- gcc/testsuite/gcc.dg/tls/thr-init-2.c (revision 0) +++ gcc/testsuite/gcc.dg/tls/thr-init-2.c (revision 0) @@ -0,0 +1,23 @@ +/* { dg-require-effective-target tls } */ +/* { dg-do run } */ + +extern void abort() ; + +static __thread int fstat ; +static __thread int fstat = 1; + +int test_code(int b) +{ + fstat += b ; + return fstat; +} + +int main (int ac, char *av[]) +{ + int a = test_code(1); + + if ((a != 2) || (fstat != 2)) + abort () ; + + return 0; +} Index: gcc/testsuite/gcc.dg/torture/tls/tls-test.c =================================================================== --- gcc/testsuite/gcc.dg/torture/tls/tls-test.c (revision 0) +++ gcc/testsuite/gcc.dg/torture/tls/tls-test.c (revision 0) @@ -0,0 +1,52 @@ +/* { dg-do run } */ +/* { dg-require-effective-target tls } */ +/* { dg-require-effective-target pthread } */ +/* { dg-options "-pthread" } */ + +#include +extern int printf (char *,...); +__thread int a = 5; +int *volatile a_in_other_thread = (int *) 12345; + +static void * +thread_func (void *arg) +{ + a_in_other_thread = &a; + a+=5; + *((int *) arg) = a; + return (void *)0; +} + +int +main () +{ + pthread_t thread; + void *thread_retval; + int *volatile a_in_main_thread; + int *volatile again ; + int thr_a; + + a_in_main_thread = &a; + + if (pthread_create (&thread, (pthread_attr_t *)0, thread_func, &thr_a)) + return 0; + + if (pthread_join (thread, &thread_retval)) + return 0; + + again = &a; + if (again != a_in_main_thread) + { + printf ("FAIL: main thread addy changed from 0x%0x to 0x%0x\n", + a_in_other_thread, again); + return 1; + } + + if (a != 5 || thr_a != 10 || (a_in_other_thread == a_in_main_thread)) + { + printf ("FAIL: a= %d, thr_a = %d Addr = 0x%0x\n", + a, thr_a, a_in_other_thread); + return 1; + } + return 0; +} Index: gcc/testsuite/gcc.dg/torture/tls/thr-init-1.c =================================================================== --- gcc/testsuite/gcc.dg/torture/tls/thr-init-1.c (revision 0) +++ gcc/testsuite/gcc.dg/torture/tls/thr-init-1.c (revision 0) @@ -0,0 +1,25 @@ +/* { dg-do run } */ +/* { dg-require-effective-target tls } */ + +extern int printf (char *,...); +extern void abort() ; + +int test_code(int b) +{ +static __thread int fstat = 1; + fstat += b ; + return fstat; +} + +int main (int ac, char *av[]) +{ + int a = test_code(1); + + if ( a != 2 ) + { + printf ("a=%d\n", a) ; + abort (); + } + + return 0; +} Index: gcc/testsuite/gcc.dg/torture/tls/tls.exp =================================================================== --- gcc/testsuite/gcc.dg/torture/tls/tls.exp (revision 0) +++ gcc/testsuite/gcc.dg/torture/tls/tls.exp (revision 0) @@ -0,0 +1,36 @@ +# Copyright (C) 2010 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GCC; see the file COPYING3. If not see +# . + +# GCC testsuite that uses the `dg.exp' driver. + +# Load support procs. +load_lib gcc-dg.exp + +# If a testcase doesn't have special options, use these. +global DEFAULT_CFLAGS +if ![info exists DEFAULT_CFLAGS] then { + set DEFAULT_CFLAGS " -ansi -pedantic-errors" +} + +# Initialize `dg'. +dg-init + +# Main loop. +gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[cS\]]] \ + $DEFAULT_CFLAGS + +# All done. +dg-finish Index: gcc/testsuite/gcc.dg/torture/tls/thr-init-2.c =================================================================== --- gcc/testsuite/gcc.dg/torture/tls/thr-init-2.c (revision 0) +++ gcc/testsuite/gcc.dg/torture/tls/thr-init-2.c (revision 0) @@ -0,0 +1,28 @@ +/* { dg-do run } */ +/* { dg-require-effective-target tls } */ + +extern int printf (char *,...); +extern void abort() ; + +static __thread int fstat ; +static __thread int fstat = 1; +static __thread int fstat ; + +int test_code(int b) +{ + fstat += b ; + return fstat; +} + +int main (int ac, char *av[]) +{ + int a = test_code(1); + + if ( a != 2 || fstat != 2 ) + { + printf ("a=%d fstat=%d\n", a, fstat) ; + abort (); + } + + return 0; +}