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> | |
27 | #include <pthread.h> | |
28 | ||
29 | #include "tls_test.h" | |
30 | ||
31 | struct Mutex_set | |
32 | { | |
33 | pthread_mutex_t mutex1; | |
34 | pthread_mutex_t mutex2; | |
35 | pthread_mutex_t mutex3; | |
36 | }; | |
37 | ||
38 | Mutex_set mutexes1 = { PTHREAD_MUTEX_INITIALIZER, | |
39 | PTHREAD_MUTEX_INITIALIZER, | |
40 | PTHREAD_MUTEX_INITIALIZER }; | |
41 | ||
42 | Mutex_set mutexes2 = { PTHREAD_MUTEX_INITIALIZER, | |
43 | PTHREAD_MUTEX_INITIALIZER, | |
44 | PTHREAD_MUTEX_INITIALIZER } ; | |
45 | ||
46 | // The body of the thread function. This gets a lock on the first | |
47 | // mutex, runs the tests, and then unlocks the second mutex. Then it | |
48 | // locks the third mutex, and the runs the verification test again. | |
49 | ||
50 | void* | |
51 | thread_routine(void* arg) | |
52 | { | |
53 | Mutex_set* pms = static_cast<Mutex_set*>(arg); | |
54 | ||
55 | // Lock the first mutex. | |
56 | int err = pthread_mutex_lock(&pms->mutex1); | |
57 | assert(err == 0); | |
58 | ||
59 | // Run the tests. | |
60 | assert(t1()); | |
61 | assert(t2()); | |
62 | assert(t3()); | |
63 | assert(t4()); | |
64 | f5b(f5a()); | |
65 | assert(t5()); | |
66 | f6b(f6a()); | |
67 | assert(t6()); | |
68 | assert(t7()); | |
69 | ||
70 | // Unlock the second mutex. | |
71 | err = pthread_mutex_unlock(&pms->mutex2); | |
72 | assert(err == 0); | |
73 | ||
74 | // Lock the third mutex. | |
75 | err = pthread_mutex_lock(&pms->mutex3); | |
76 | assert(err == 0); | |
77 | ||
78 | assert(t7()); | |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
83 | // The main function. | |
84 | ||
85 | int | |
86 | main() | |
87 | { | |
88 | // Set up the mutex locks. We want the first thread to start right | |
89 | // away, tell us when it is done with the first part, and wait for | |
90 | // us to release it. We want the second thread to wait to start, | |
91 | // tell us when it is done with the first part, and wait for us to | |
92 | // release it. | |
93 | int err = pthread_mutex_lock(&mutexes1.mutex2); | |
94 | assert(err == 0); | |
95 | err = pthread_mutex_lock(&mutexes1.mutex3); | |
96 | assert(err == 0); | |
97 | ||
98 | err = pthread_mutex_lock(&mutexes2.mutex1); | |
99 | assert(err == 0); | |
100 | err = pthread_mutex_lock(&mutexes2.mutex2); | |
101 | assert(err == 0); | |
102 | err = pthread_mutex_lock(&mutexes2.mutex3); | |
103 | assert(err == 0); | |
104 | ||
105 | pthread_t thread1; | |
106 | err = pthread_create(&thread1, NULL, thread_routine, &mutexes1); | |
107 | assert(err == 0); | |
108 | ||
109 | pthread_t thread2; | |
110 | err = pthread_create(&thread2, NULL, thread_routine, &mutexes2); | |
111 | assert(err == 0); | |
112 | ||
113 | // Wait for the first thread to complete the first part. | |
114 | err = pthread_mutex_lock(&mutexes1.mutex2); | |
115 | assert(err == 0); | |
116 | ||
117 | // Tell the second thread to start. | |
118 | err = pthread_mutex_unlock(&mutexes2.mutex1); | |
119 | assert(err == 0); | |
120 | ||
121 | // Wait for the second thread to complete the first part. | |
122 | err = pthread_mutex_lock(&mutexes2.mutex2); | |
123 | assert(err == 0); | |
124 | ||
125 | // Tell the first thread to continue and finish. | |
126 | err = pthread_mutex_unlock(&mutexes1.mutex3); | |
127 | assert(err == 0); | |
128 | ||
129 | // Wait for the first thread to finish. | |
130 | void* thread_val; | |
131 | err = pthread_join(thread1, &thread_val); | |
132 | assert(err == 0); | |
133 | assert(thread_val == 0); | |
134 | ||
135 | // Tell the second thread to continue and finish. | |
136 | err = pthread_mutex_unlock(&mutexes2.mutex3); | |
137 | assert(err == 0); | |
138 | ||
139 | // Wait for the second thread to finish. | |
140 | err = pthread_join(thread2, &thread_val); | |
141 | assert(err == 0); | |
142 | assert(thread_val == 0); | |
143 | ||
144 | // All done. | |
145 | return 0; | |
146 | } |