Commit | Line | Data |
---|---|---|
6eee141f ILT |
1 | // tls_test.cc -- test TLS variables for gold, main function |
2 | ||
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. | |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
23 | // This is the main function for the TLS test. See tls_test.cc for | |
24 | // more information. | |
25 | ||
26 | #include <cassert> | |
e9821041 | 27 | #include <cstdio> |
6eee141f ILT |
28 | #include <pthread.h> |
29 | ||
30 | #include "tls_test.h" | |
31 | ||
32 | struct Mutex_set | |
33 | { | |
34 | pthread_mutex_t mutex1; | |
35 | pthread_mutex_t mutex2; | |
36 | pthread_mutex_t mutex3; | |
37 | }; | |
38 | ||
39 | Mutex_set mutexes1 = { PTHREAD_MUTEX_INITIALIZER, | |
40 | PTHREAD_MUTEX_INITIALIZER, | |
41 | PTHREAD_MUTEX_INITIALIZER }; | |
42 | ||
43 | Mutex_set mutexes2 = { PTHREAD_MUTEX_INITIALIZER, | |
44 | PTHREAD_MUTEX_INITIALIZER, | |
45 | PTHREAD_MUTEX_INITIALIZER } ; | |
46 | ||
e9821041 ILT |
47 | bool failed = false; |
48 | ||
49 | void | |
50 | check(const char* name, bool val) | |
51 | { | |
52 | if (!val) | |
53 | { | |
54 | fprintf(stderr, "Test %s failed\n", name); | |
55 | failed = true; | |
56 | } | |
57 | } | |
58 | ||
6eee141f ILT |
59 | // The body of the thread function. This gets a lock on the first |
60 | // mutex, runs the tests, and then unlocks the second mutex. Then it | |
61 | // locks the third mutex, and the runs the verification test again. | |
62 | ||
63 | void* | |
64 | thread_routine(void* arg) | |
65 | { | |
66 | Mutex_set* pms = static_cast<Mutex_set*>(arg); | |
67 | ||
68 | // Lock the first mutex. | |
69 | int err = pthread_mutex_lock(&pms->mutex1); | |
70 | assert(err == 0); | |
71 | ||
72 | // Run the tests. | |
e9821041 ILT |
73 | check("t1", t1()); |
74 | check("t2", t2()); | |
75 | check("t3", t3()); | |
76 | check("t4", t4()); | |
6eee141f | 77 | f5b(f5a()); |
e9821041 | 78 | check("t5", t5()); |
6eee141f | 79 | f6b(f6a()); |
e9821041 | 80 | check("t6", t6()); |
e0374858 ILT |
81 | check("t8", t8()); |
82 | check("t9", t9()); | |
83 | f10b(f10a()); | |
84 | check("t10", t10()); | |
85 | check("t_last", t_last()); | |
6eee141f ILT |
86 | |
87 | // Unlock the second mutex. | |
88 | err = pthread_mutex_unlock(&pms->mutex2); | |
89 | assert(err == 0); | |
90 | ||
91 | // Lock the third mutex. | |
92 | err = pthread_mutex_lock(&pms->mutex3); | |
93 | assert(err == 0); | |
94 | ||
e0374858 | 95 | check("t_last", t_last()); |
6eee141f ILT |
96 | |
97 | return 0; | |
98 | } | |
99 | ||
100 | // The main function. | |
101 | ||
102 | int | |
103 | main() | |
104 | { | |
105 | // Set up the mutex locks. We want the first thread to start right | |
106 | // away, tell us when it is done with the first part, and wait for | |
107 | // us to release it. We want the second thread to wait to start, | |
108 | // tell us when it is done with the first part, and wait for us to | |
109 | // release it. | |
110 | int err = pthread_mutex_lock(&mutexes1.mutex2); | |
111 | assert(err == 0); | |
112 | err = pthread_mutex_lock(&mutexes1.mutex3); | |
113 | assert(err == 0); | |
114 | ||
115 | err = pthread_mutex_lock(&mutexes2.mutex1); | |
116 | assert(err == 0); | |
117 | err = pthread_mutex_lock(&mutexes2.mutex2); | |
118 | assert(err == 0); | |
119 | err = pthread_mutex_lock(&mutexes2.mutex3); | |
120 | assert(err == 0); | |
121 | ||
122 | pthread_t thread1; | |
123 | err = pthread_create(&thread1, NULL, thread_routine, &mutexes1); | |
124 | assert(err == 0); | |
125 | ||
126 | pthread_t thread2; | |
127 | err = pthread_create(&thread2, NULL, thread_routine, &mutexes2); | |
128 | assert(err == 0); | |
129 | ||
130 | // Wait for the first thread to complete the first part. | |
131 | err = pthread_mutex_lock(&mutexes1.mutex2); | |
132 | assert(err == 0); | |
133 | ||
134 | // Tell the second thread to start. | |
135 | err = pthread_mutex_unlock(&mutexes2.mutex1); | |
136 | assert(err == 0); | |
137 | ||
138 | // Wait for the second thread to complete the first part. | |
139 | err = pthread_mutex_lock(&mutexes2.mutex2); | |
140 | assert(err == 0); | |
141 | ||
142 | // Tell the first thread to continue and finish. | |
143 | err = pthread_mutex_unlock(&mutexes1.mutex3); | |
144 | assert(err == 0); | |
145 | ||
146 | // Wait for the first thread to finish. | |
147 | void* thread_val; | |
148 | err = pthread_join(thread1, &thread_val); | |
149 | assert(err == 0); | |
150 | assert(thread_val == 0); | |
151 | ||
152 | // Tell the second thread to continue and finish. | |
153 | err = pthread_mutex_unlock(&mutexes2.mutex3); | |
154 | assert(err == 0); | |
155 | ||
156 | // Wait for the second thread to finish. | |
157 | err = pthread_join(thread2, &thread_val); | |
158 | assert(err == 0); | |
159 | assert(thread_val == 0); | |
160 | ||
161 | // All done. | |
e9821041 | 162 | return failed ? 1 : 0; |
6eee141f | 163 | } |