|
1 | 1 | #include <stdio.h> |
2 | 2 | #include <pthread.h> |
3 | 3 |
|
4 | | -__thread int tls_init = 42; |
| 4 | +#define CHECK(var,fmt,val) printf(fmt "\n",var); if (var != val) errors = 1 |
| 5 | + |
| 6 | +typedef enum { tls_a, tls_b, tls_c } tls_enum_type; |
| 7 | +typedef struct { int tls_d, tls_e, tls_f; } tls_struct_type; |
| 8 | + |
| 9 | +__thread char tls_char = 42; |
| 10 | +__thread short tls_short = 43; |
| 11 | +__thread int tls_init = 44; |
| 12 | +__thread long long tls_long_long = 45; |
5 | 13 | __thread int tls_zero; |
| 14 | +__thread float tls_float = 46.0; |
| 15 | +__thread double tls_double = 47.0; |
| 16 | +__thread long double tls_long_double = 48.0; |
| 17 | +__thread int *tls_ptr = (int *)49; |
| 18 | +__thread tls_enum_type tls_enum = tls_b; |
| 19 | +__thread tls_struct_type tls_struct = { 50, 51, 52 }; |
6 | 20 |
|
7 | | -static void *thread_func(void *arg) |
| 21 | +static int check(void) |
8 | 22 | { |
9 | | - (void)arg; |
| 23 | + int errors = 0; |
10 | 24 |
|
11 | | - printf("%d\n", tls_init); |
12 | | - if (tls_init != 42) return (void *)1; |
| 25 | + CHECK(tls_char, "%d", 42); |
| 26 | + CHECK(tls_short, "%d", 43); |
| 27 | + CHECK(tls_init, "%d", 44); |
| 28 | + CHECK(tls_long_long, "%lld", 45); |
| 29 | + CHECK(tls_zero, "%d", 0); |
| 30 | + CHECK(tls_float, "%g", 46.0); |
| 31 | + CHECK(tls_double, "%g", 47.0); |
| 32 | + CHECK(tls_long_double, "%Lg", 48.0); |
| 33 | + CHECK(tls_ptr, "%p", (int *)49); |
| 34 | + CHECK(tls_enum, "%d", tls_b); |
| 35 | + CHECK(tls_struct.tls_d, "%d", 50); |
| 36 | + CHECK(tls_struct.tls_e, "%d", 51); |
| 37 | + CHECK(tls_struct.tls_f, "%d", 52); |
| 38 | + return errors; |
| 39 | +} |
13 | 40 |
|
14 | | - printf("%d\n", tls_zero); |
15 | | - if (tls_zero != 0) return (void *)1; |
| 41 | +static void *thread_func(void *arg) |
| 42 | +{ |
| 43 | + (void)arg; |
| 44 | + long errors = check(); |
16 | 45 |
|
17 | | - tls_init = 100; |
18 | | - tls_zero = 200; |
| 46 | + tls_char = 10; |
| 47 | + tls_short = 20; |
| 48 | + tls_init = 30; |
| 49 | + tls_long_long = 40; |
| 50 | + tls_zero = 50; |
| 51 | + tls_float = 60.0; |
| 52 | + tls_double = 70.0; |
| 53 | + tls_long_double = 80.0; |
| 54 | + tls_ptr = (int *)90; |
| 55 | + tls_enum = tls_c; |
| 56 | + tls_struct.tls_d = 100; |
| 57 | + tls_struct.tls_e = 110; |
| 58 | + tls_struct.tls_f = 120; |
19 | 59 |
|
20 | | - printf("%d\n", tls_init); |
21 | | - printf("%d\n", tls_zero); |
| 60 | + CHECK(tls_char, "%d", 10); |
| 61 | + CHECK(tls_short, "%d", 20); |
| 62 | + CHECK(tls_init, "%d", 30); |
| 63 | + CHECK(tls_long_long, "%lld", 40); |
| 64 | + CHECK(tls_zero, "%d", 50); |
| 65 | + CHECK(tls_float, "%g", 60.0); |
| 66 | + CHECK(tls_double, "%g", 70.0); |
| 67 | + CHECK(tls_long_double, "%Lg", 80.0); |
| 68 | + CHECK(tls_enum, "%d", tls_c); |
| 69 | + CHECK(tls_ptr, "%p", (int *)90); |
| 70 | + CHECK(tls_struct.tls_d, "%d", 100); |
| 71 | + CHECK(tls_struct.tls_e, "%d", 110); |
| 72 | + CHECK(tls_struct.tls_f, "%d", 120); |
22 | 73 |
|
23 | | - return (void *)0; |
| 74 | + return (void *)errors; |
24 | 75 | } |
25 | 76 |
|
26 | 77 | int main() |
27 | 78 | { |
28 | 79 | pthread_t t; |
29 | 80 | void *ret; |
30 | | - int errors = 0; |
31 | | - |
32 | | - printf("%d\n", tls_init); |
33 | | - if (tls_init != 42) errors = 1; |
34 | | - |
35 | | - printf("%d\n", tls_zero); |
36 | | - if (tls_zero != 0) errors = 1; |
| 81 | + int errors = check(); |
37 | 82 |
|
38 | 83 | pthread_create(&t, NULL, thread_func, NULL); |
39 | 84 | pthread_join(t, &ret); |
40 | 85 |
|
41 | | - if (ret) errors = 1; |
42 | | - |
43 | | - printf("%d\n", tls_init); |
44 | | - if (tls_init != 42) errors = 1; |
45 | | - |
46 | | - printf("%d\n", tls_zero); |
47 | | - if (tls_zero != 0) errors = 1; |
48 | | - |
49 | | - return errors; |
| 86 | + return (ret ? 1 : 0) | errors | check(); |
50 | 87 | } |
0 commit comments