fitzhardinge | 47735af | 2004-01-21 01:27:27 +0000 | [diff] [blame] | 1 | #include <pthread.h> |
| 2 | #include <stdio.h> |
| 3 | #include <unistd.h> |
| 4 | #include <time.h> |
| 5 | |
| 6 | #define COUNT 10 |
| 7 | |
| 8 | static int race; |
| 9 | static __thread int local; |
| 10 | __thread int global; |
| 11 | extern __thread int static_extern; |
| 12 | extern __thread int so_extern; |
| 13 | |
| 14 | /* deliberate failure */ |
| 15 | static int *test_race(void) |
| 16 | { |
| 17 | return ∽̱ |
| 18 | } |
| 19 | |
| 20 | static int *test_local(void) |
| 21 | { |
| 22 | return &local; |
| 23 | } |
| 24 | |
| 25 | static int *test_global(void) |
| 26 | { |
| 27 | return &global; |
| 28 | } |
| 29 | |
| 30 | static int *test_static_extern(void) |
| 31 | { |
| 32 | return &static_extern; |
| 33 | } |
| 34 | |
| 35 | static int *test_so_extern(void) |
| 36 | { |
| 37 | return &so_extern; |
| 38 | } |
| 39 | |
| 40 | static const struct timespec awhile = { 0, 100000000 }; |
| 41 | |
| 42 | typedef int *(*func_t)(void); |
| 43 | struct testcase { |
| 44 | const char *name; |
| 45 | func_t func; |
| 46 | }; |
| 47 | |
| 48 | static 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 | |
| 67 | int *test_so_extern(void); |
| 68 | int *test_so_local(void); |
| 69 | int *test_so_global(void); |
| 70 | |
| 71 | static 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 | |
| 85 | int 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 | } |