1 // tls_test.cc -- test TLS variables for gold, main function
3 // Copyright (C) 2006-2023 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
29 #include <semaphore.h>
33 // We make these macros so the assert() will give useful line-numbers.
34 #define safe_lock(semptr) \
37 int err = sem_wait(semptr); \
42 #define safe_unlock(semptr) \
45 int err = sem_post(semptr); \
63 check(const char* name
, bool val
)
67 fprintf(stderr
, "Test %s failed\n", name
);
72 // The body of the thread function. This acquires the first
73 // semaphore, runs the tests, and then releases the second semaphore.
74 // Then it acquires the third semaphore, and the runs the verification
78 thread_routine(void* arg
)
80 Sem_set
* pms
= static_cast<Sem_set
*>(arg
);
82 // Acquire the first semaphore.
84 safe_lock(&pms
->sem1
);
99 check("t11", t11() != 0);
101 check("t_last", t_last());
103 // Release the second semaphore.
105 safe_unlock(&pms
->sem2
);
107 // Acquire the third semaphore.
109 safe_lock(&pms
->sem3
);
111 check("t_last", t_last());
116 // The main function.
121 // First, as a sanity check, run through the tests in the "main" thread.
124 // Set up the semaphores. We want the first thread to start right
125 // away, tell us when it is done with the first part, and wait for
126 // us to release it. We want the second thread to wait to start,
127 // tell us when it is done with the first part, and wait for us to
129 sem_init(&sems1
.sem1
, 0, 1);
130 sem_init(&sems1
.sem2
, 0, 0);
131 sem_init(&sems1
.sem3
, 0, 0);
133 sem_init(&sems2
.sem1
, 0, 0);
134 sem_init(&sems2
.sem2
, 0, 0);
135 sem_init(&sems2
.sem3
, 0, 0);
138 int err
= pthread_create(&thread1
, NULL
, thread_routine
, &sems1
);
142 err
= pthread_create(&thread2
, NULL
, thread_routine
, &sems2
);
145 // Wait for the first thread to complete the first part.
146 safe_lock(&sems1
.sem2
);
148 // Tell the second thread to start.
149 safe_unlock(&sems2
.sem1
);
151 // Wait for the second thread to complete the first part.
152 safe_lock(&sems2
.sem2
);
154 // Tell the first thread to continue and finish.
155 safe_unlock(&sems1
.sem3
);
157 // Wait for the first thread to finish.
159 err
= pthread_join(thread1
, &thread_val
);
161 assert(thread_val
== 0);
163 // Tell the second thread to continue and finish.
164 safe_unlock(&sems2
.sem3
);
166 // Wait for the second thread to finish.
167 err
= pthread_join(thread2
, &thread_val
);
169 assert(thread_val
== 0);
172 return failed
? 1 : 0;