]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/thread-local-variables.dtest/main.c
dyld-750.6.tar.gz
[apple/dyld.git] / testing / test-cases / thread-local-variables.dtest / main.c
1
2
3 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib
4 // BUILD: $CC main.c $BUILD_DIR/libfoo.dylib -o $BUILD_DIR/thread-local-variables.exe
5
6
7 // RUN: ./thread-local-variables.exe
8
9
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <pthread.h>
14 #include <assert.h>
15 #include <unistd.h>
16
17 #include "test_support.h"
18
19 extern __thread int a;
20 extern __thread int b;
21 static __thread int c;
22 static __thread int d = 5;
23
24
25 static int* sAddr1[4];
26 static int* sAddr2[4];
27 static int* sAddr3[4];
28
29 static pthread_t sWorker1;
30 static pthread_t sWorker2;
31
32
33 // verify all initial TLV values are correct
34 static void checkValues()
35 {
36 assert(a == 0);
37 assert(b == 5);
38 assert(c == 0);
39 assert(d == 5);
40 }
41
42
43 // return addresses of all TLVs
44 static void getAddresses(int* array[])
45 {
46 array[0] = &a;
47 array[1] = &b;
48 array[2] = &c;
49 array[3] = &d;
50 }
51
52 static void* work2(void* arg)
53 {
54 checkValues();
55 getAddresses(sAddr2);
56
57 return NULL;
58 }
59
60 static void* work1(void* arg)
61 {
62 checkValues();
63
64 if ( pthread_create(&sWorker2, NULL, work2, NULL) != 0 ) {
65 FAIL("pthread_create");
66 }
67 void* dummy;
68 pthread_join(sWorker2, &dummy);
69
70 getAddresses(sAddr1);
71
72 return NULL;
73 }
74
75 static bool someMatch(int* t1, int* t2, int* t3)
76 {
77 if ( t1 == t2 )
78 return true;
79 if ( t1 == t3 )
80 return true;
81 if ( t2 == t3 )
82 return true;
83 return false;
84 }
85
86 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
87 checkValues();
88
89 if ( pthread_create(&sWorker1, NULL, work1, NULL) != 0 ) {
90 FAIL("pthread_create");
91 }
92
93 getAddresses(sAddr3);
94
95 void* dummy;
96 pthread_join(sWorker1, &dummy);
97
98 // validate each thread had different addresses for all TLVs
99 if ( someMatch(sAddr1[0], sAddr2[0], sAddr3[0]) )
100 FAIL("&a is same across some threads");
101 else if ( someMatch(sAddr1[1], sAddr2[1], sAddr3[1]) )
102 FAIL("&b is same across some threads");
103 else if ( someMatch(sAddr1[2], sAddr2[2], sAddr3[2]) )
104 FAIL("&c is same across some threads");
105 else if ( someMatch(sAddr1[3], sAddr2[3], sAddr3[3]) )
106 FAIL("&d is same across some threads");
107 else
108 PASS("Success");
109 }
110