blob: 78993c2d5ecc9daa4cad70695f11fc049fa850ca [file] [log] [blame]
fitzhardinge47735af2004-01-21 01:27:27 +00001#include <pthread.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <time.h>
5
6#define COUNT 10
7
8static int race;
9static __thread int local;
10__thread int global;
11extern __thread int static_extern;
12extern __thread int so_extern;
13
14/* deliberate failure */
15static int *test_race(void)
16{
17 return &race;
18}
19
20static int *test_local(void)
21{
22 return &local;
23}
24
25static int *test_global(void)
26{
27 return &global;
28}
29
30static int *test_static_extern(void)
31{
32 return &static_extern;
33}
34
35static int *test_so_extern(void)
36{
37 return &so_extern;
38}
39
40static const struct timespec awhile = { 0, 100000000 };
41
42typedef int *(*func_t)(void);
43struct testcase {
44 const char *name;
45 func_t func;
46};
47
48static void *tls_ptr(void *p)
49{
50 struct testcase *test = (struct testcase *)p;
51 int *ip = (*test->func)();
52 int here = 0;
53 int i;
54
55 for(i = 0; i < COUNT; i++) {
56 int a = (*ip)++;
57 int b = here++;
58 if (a != b)
59 printf("tls_ptr: case \"%s\" has mismatch: *ip=%d here=%d\n",
60 test->name, a, b);
61 nanosleep(&awhile, 0);
62 }
63
64 return 0;
65}
66
67int *test_so_extern(void);
68int *test_so_local(void);
69int *test_so_global(void);
70
71static const struct testcase tests[] = {
72#define T(t) { #t, test_##t }
73 T(race),
74 T(local),
75 T(global),
76 T(static_extern),
77 T(so_extern),
78 T(so_local),
79 T(so_global),
80#undef T
81};
82
83#define NTESTS (sizeof(tests)/sizeof(*tests))
84
85int main()
86{
87 pthread_t threads[NTESTS*2];
88 int curthread = 0;
89 static
90 int i;
91
92 for(i = 0; i < NTESTS; i++) {
93 pthread_create(&threads[curthread++], NULL, tls_ptr, (void *)&tests[i]);
94 pthread_create(&threads[curthread++], NULL, tls_ptr, (void *)&tests[i]);
95 }
96
97 for(i = 0; i < curthread; i++)
98 pthread_join(threads[i], NULL);
99
100 return 0;
101}