vgdb: Handle EAGAIN in read_buf
[valgrind.git] / none / tests / tls.c
blob1810790774eb4b4d426cd87da26665810d860b14
1 #include <config.h>
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <time.h>
7 #ifdef HAVE_TLS
9 #define COUNT 10
11 static int race;
12 static __thread int local;
13 __thread int global;
14 extern __thread int static_extern;
15 extern __thread int so_extern;
17 /* deliberate failure */
18 static int *test_race(void)
20 return &race;
23 static int *test_local(void)
25 return &local;
28 static int *test_global(void)
30 return &global;
33 static int *test_static_extern(void)
35 return &static_extern;
38 static int *test_so_extern(void)
40 return &so_extern;
43 static const struct timespec awhile = { 0, 200000000 };
45 typedef int *(*func_t)(void);
46 struct testcase {
47 const char *name;
48 func_t func;
49 char pad[2 * (8 - sizeof(void*))];
51 static void *tls_ptr(void *p)
53 struct testcase *test = (struct testcase *)p;
54 int *ip = (*test->func)();
55 int here = 0;
56 int i;
58 for(i = 0; i < COUNT; i++) {
59 int a = (*ip)++;
60 int b = here++;
61 if (a != b)
62 printf("tls_ptr: case \"%s\" has mismatch: *ip=%d here=%d\n",
63 test->name, a, b);
64 nanosleep(&awhile, 0);
67 return 0;
70 int *test_so_extern(void);
71 int *test_so_local(void);
72 int *test_so_global(void);
74 static const struct testcase tests[] = {
75 #define T(t) { #t, test_##t }
76 T(race),
77 T(local),
78 T(global),
79 T(static_extern),
80 T(so_extern),
81 T(so_local),
82 T(so_global),
83 #undef T
86 #define NTESTS (sizeof(tests)/sizeof(*tests))
88 int main()
90 pthread_t threads[NTESTS*2];
91 int curthread = 0;
92 static
93 int i;
95 for(i = 0; i < NTESTS; i++) {
96 pthread_create(&threads[curthread++], NULL, tls_ptr, (void *)&tests[i]);
97 pthread_create(&threads[curthread++], NULL, tls_ptr, (void *)&tests[i]);
100 for(i = 0; i < curthread; i++)
101 pthread_join(threads[i], NULL);
103 return 0;
105 #else
106 int main()
108 printf("FAILED: no compiler support for __thread\n");
109 return 1;
111 #endif