1 // tls_test.cc -- test TLS variables for gold, main function
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 // This is the main function for the TLS test. See tls_test.cc for
32 // We make these macros so the assert() will give useful line-numbers.
33 #define safe_lock(muptr) \
36 int err = pthread_mutex_lock(muptr); \
41 #define safe_unlock(muptr) \
44 int err = pthread_mutex_unlock(muptr); \
51 pthread_mutex_t mutex1;
52 pthread_mutex_t mutex2;
53 pthread_mutex_t mutex3;
56 Mutex_set mutexes1 = { PTHREAD_MUTEX_INITIALIZER,
57 PTHREAD_MUTEX_INITIALIZER,
58 PTHREAD_MUTEX_INITIALIZER };
60 Mutex_set mutexes2 = { PTHREAD_MUTEX_INITIALIZER,
61 PTHREAD_MUTEX_INITIALIZER,
62 PTHREAD_MUTEX_INITIALIZER } ;
67 check(const char* name, bool val)
71 fprintf(stderr, "Test %s failed\n", name);
76 // The body of the thread function. This gets a lock on the first
77 // mutex, runs the tests, and then unlocks the second mutex. Then it
78 // locks the third mutex, and the runs the verification test again.
81 thread_routine(void* arg)
83 Mutex_set* pms = static_cast<Mutex_set*>(arg);
85 // Lock the first mutex.
87 safe_lock(&pms->mutex1);
102 check("t11", t11() != 0);
104 check("t_last", t_last());
106 // Unlock the second mutex.
108 safe_unlock(&pms->mutex2);
110 // Lock the third mutex.
112 safe_lock(&pms->mutex3);
114 check("t_last", t_last());
119 // The main function.
124 // First, as a sanity check, run through the tests in the "main" thread.
127 // Set up the mutex locks. We want the first thread to start right
128 // away, tell us when it is done with the first part, and wait for
129 // us to release it. We want the second thread to wait to start,
130 // tell us when it is done with the first part, and wait for us to
132 safe_lock(&mutexes1.mutex2);
133 safe_lock(&mutexes1.mutex3);
135 safe_lock(&mutexes2.mutex1);
136 safe_lock(&mutexes2.mutex2);
137 safe_lock(&mutexes2.mutex3);
140 int err = pthread_create(&thread1, NULL, thread_routine, &mutexes1);
144 err = pthread_create(&thread2, NULL, thread_routine, &mutexes2);
147 // Wait for the first thread to complete the first part.
148 safe_lock(&mutexes1.mutex2);
150 // Tell the second thread to start.
151 safe_unlock(&mutexes2.mutex1);
153 // Wait for the second thread to complete the first part.
154 safe_lock(&mutexes2.mutex2);
156 // Tell the first thread to continue and finish.
157 safe_unlock(&mutexes1.mutex3);
159 // Wait for the first thread to finish.
161 err = pthread_join(thread1, &thread_val);
163 assert(thread_val == 0);
165 // Tell the second thread to continue and finish.
166 safe_unlock(&mutexes2.mutex3);
168 // Wait for the second thread to finish.
169 err = pthread_join(thread2, &thread_val);
171 assert(thread_val == 0);
174 return failed ? 1 : 0;