]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/thread-local-variables.dtest/main.c
dyld-519.2.2.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
18 extern __thread int a;
19 extern __thread int b;
20 static __thread int c;
21 static __thread int d = 5;
22
23
24 static int* sAddr1[4];
25 static int* sAddr2[4];
26 static int* sAddr3[4];
27
28 static pthread_t sWorker1;
29 static pthread_t sWorker2;
30
31
32 // verify all initial TLV values are correct
33 static void checkValues()
34 {
35 assert(a == 0);
36 assert(b == 5);
37 assert(c == 0);
38 assert(d == 5);
39 }
40
41
42 // return addresses of all TLVs
43 static void getAddresses(int* array[])
44 {
45 array[0] = &a;
46 array[1] = &b;
47 array[2] = &c;
48 array[3] = &d;
49 }
50
51 static void* work2(void* arg)
52 {
53 checkValues();
54 getAddresses(sAddr2);
55
56 return NULL;
57 }
58
59 static void* work1(void* arg)
60 {
61 checkValues();
62
63 if ( pthread_create(&sWorker2, NULL, work2, NULL) != 0 ) {
64 printf("[FAIL] thread-local-variables, pthread_create\n");
65 exit(0);
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()
87 {
88 printf("[BEGIN] thread-local-variables\n");
89
90 checkValues();
91
92 if ( pthread_create(&sWorker1, NULL, work1, NULL) != 0 ) {
93 printf("[FAIL] thread-local-variables, pthread_create\n");
94 return 0;
95 }
96
97 getAddresses(sAddr3);
98
99 void* dummy;
100 pthread_join(sWorker1, &dummy);
101
102 // validate each thread had different addresses for all TLVs
103 if ( someMatch(sAddr1[0], sAddr2[0], sAddr3[0]) )
104 printf("[FAIL] thread-local-variables, &a is same across some threads\n");
105 else if ( someMatch(sAddr1[1], sAddr2[1], sAddr3[1]) )
106 printf("[FAIL] thread-local-variables, &b is same across some threads\n");
107 else if ( someMatch(sAddr1[2], sAddr2[2], sAddr3[2]) )
108 printf("[FAIL] thread-local-variables, &c is same across some threads\n");
109 else if ( someMatch(sAddr1[3], sAddr2[3], sAddr3[3]) )
110 printf("[FAIL] thread-local-variables, &d is same across some threads\n");
111 else
112 printf("[PASS] thread-local-variables\n");
113 return 0;
114 }
115