2007-10-08 01:10:10 +08:00
|
|
|
// tls_test.cc -- test TLS variables for gold, main function
|
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
// Copyright 2006, 2007, 2008, 2011 Free Software Foundation, Inc.
|
2007-10-08 01:10:10 +08:00
|
|
|
// Written by Ian Lance Taylor <iant@google.com>.
|
|
|
|
|
|
|
|
// This file is part of gold.
|
|
|
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
|
|
|
// MA 02110-1301, USA.
|
|
|
|
|
|
|
|
// This is the main function for the TLS test. See tls_test.cc for
|
|
|
|
// more information.
|
|
|
|
|
|
|
|
#include <cassert>
|
2007-10-08 15:24:21 +08:00
|
|
|
#include <cstdio>
|
2007-10-08 01:10:10 +08:00
|
|
|
#include <pthread.h>
|
2011-05-17 07:29:30 +08:00
|
|
|
#include <semaphore.h>
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
#include "tls_test.h"
|
|
|
|
|
2007-10-14 11:23:38 +08:00
|
|
|
// We make these macros so the assert() will give useful line-numbers.
|
2011-05-17 07:29:30 +08:00
|
|
|
#define safe_lock(semptr) \
|
2007-10-14 11:23:38 +08:00
|
|
|
do \
|
|
|
|
{ \
|
2011-05-17 07:29:30 +08:00
|
|
|
int err = sem_wait(semptr); \
|
2009-12-15 03:53:05 +08:00
|
|
|
assert(err == 0); \
|
2007-10-14 11:23:38 +08:00
|
|
|
} \
|
|
|
|
while (0)
|
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
#define safe_unlock(semptr) \
|
2007-10-14 11:23:38 +08:00
|
|
|
do \
|
|
|
|
{ \
|
2011-05-17 07:29:30 +08:00
|
|
|
int err = sem_post(semptr); \
|
2009-12-15 03:53:05 +08:00
|
|
|
assert(err == 0); \
|
2007-10-14 11:23:38 +08:00
|
|
|
} \
|
|
|
|
while (0)
|
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
struct Sem_set
|
2007-10-08 01:10:10 +08:00
|
|
|
{
|
2011-05-17 07:29:30 +08:00
|
|
|
sem_t sem1;
|
|
|
|
sem_t sem2;
|
|
|
|
sem_t sem3;
|
2007-10-08 01:10:10 +08:00
|
|
|
};
|
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
Sem_set sems1;
|
|
|
|
Sem_set sems2;
|
2007-10-08 01:10:10 +08:00
|
|
|
|
2007-10-08 15:24:21 +08:00
|
|
|
bool failed = false;
|
|
|
|
|
|
|
|
void
|
|
|
|
check(const char* name, bool val)
|
|
|
|
{
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Test %s failed\n", name);
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
// The body of the thread function. This acquires the first
|
|
|
|
// semaphore, runs the tests, and then releases the second semaphore.
|
|
|
|
// Then it acquires the third semaphore, and the runs the verification
|
|
|
|
// test again.
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
void*
|
|
|
|
thread_routine(void* arg)
|
|
|
|
{
|
2011-05-17 07:29:30 +08:00
|
|
|
Sem_set* pms = static_cast<Sem_set*>(arg);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
// Acquire the first semaphore.
|
2007-10-14 11:23:38 +08:00
|
|
|
if (pms)
|
2011-05-17 07:29:30 +08:00
|
|
|
safe_lock(&pms->sem1);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
// Run the tests.
|
2007-10-08 15:24:21 +08:00
|
|
|
check("t1", t1());
|
|
|
|
check("t2", t2());
|
|
|
|
check("t3", t3());
|
|
|
|
check("t4", t4());
|
2007-10-08 01:10:10 +08:00
|
|
|
f5b(f5a());
|
2007-10-08 15:24:21 +08:00
|
|
|
check("t5", t5());
|
2007-10-08 01:10:10 +08:00
|
|
|
f6b(f6a());
|
2007-10-08 15:24:21 +08:00
|
|
|
check("t6", t6());
|
2007-10-13 01:32:20 +08:00
|
|
|
check("t8", t8());
|
|
|
|
check("t9", t9());
|
|
|
|
f10b(f10a());
|
|
|
|
check("t10", t10());
|
2008-04-17 15:12:00 +08:00
|
|
|
check("t11", t11() != 0);
|
2008-08-15 12:57:46 +08:00
|
|
|
check("t12", t12());
|
2007-10-13 01:32:20 +08:00
|
|
|
check("t_last", t_last());
|
2007-10-08 01:10:10 +08:00
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
// Release the second semaphore.
|
2007-10-14 11:23:38 +08:00
|
|
|
if (pms)
|
2011-05-17 07:29:30 +08:00
|
|
|
safe_unlock(&pms->sem2);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
// Acquire the third semaphore.
|
2007-10-14 11:23:38 +08:00
|
|
|
if (pms)
|
2011-05-17 07:29:30 +08:00
|
|
|
safe_lock(&pms->sem3);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
2007-10-13 01:32:20 +08:00
|
|
|
check("t_last", t_last());
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The main function.
|
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
2007-10-14 11:23:38 +08:00
|
|
|
// First, as a sanity check, run through the tests in the "main" thread.
|
|
|
|
thread_routine(0);
|
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
// Set up the semaphores. We want the first thread to start right
|
2007-10-08 01:10:10 +08:00
|
|
|
// away, tell us when it is done with the first part, and wait for
|
|
|
|
// us to release it. We want the second thread to wait to start,
|
|
|
|
// tell us when it is done with the first part, and wait for us to
|
|
|
|
// release it.
|
2011-05-17 07:29:30 +08:00
|
|
|
sem_init(&sems1.sem1, 0, 1);
|
|
|
|
sem_init(&sems1.sem2, 0, 0);
|
|
|
|
sem_init(&sems1.sem3, 0, 0);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
2011-05-17 07:29:30 +08:00
|
|
|
sem_init(&sems2.sem1, 0, 0);
|
|
|
|
sem_init(&sems2.sem2, 0, 0);
|
|
|
|
sem_init(&sems2.sem3, 0, 0);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
pthread_t thread1;
|
2011-05-17 07:29:30 +08:00
|
|
|
int err = pthread_create(&thread1, NULL, thread_routine, &sems1);
|
2007-10-08 01:10:10 +08:00
|
|
|
assert(err == 0);
|
|
|
|
|
|
|
|
pthread_t thread2;
|
2011-05-17 07:29:30 +08:00
|
|
|
err = pthread_create(&thread2, NULL, thread_routine, &sems2);
|
2007-10-08 01:10:10 +08:00
|
|
|
assert(err == 0);
|
|
|
|
|
|
|
|
// Wait for the first thread to complete the first part.
|
2011-05-17 07:29:30 +08:00
|
|
|
safe_lock(&sems1.sem2);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
// Tell the second thread to start.
|
2011-05-17 07:29:30 +08:00
|
|
|
safe_unlock(&sems2.sem1);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
// Wait for the second thread to complete the first part.
|
2011-05-17 07:29:30 +08:00
|
|
|
safe_lock(&sems2.sem2);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
// Tell the first thread to continue and finish.
|
2011-05-17 07:29:30 +08:00
|
|
|
safe_unlock(&sems1.sem3);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
// Wait for the first thread to finish.
|
|
|
|
void* thread_val;
|
|
|
|
err = pthread_join(thread1, &thread_val);
|
|
|
|
assert(err == 0);
|
|
|
|
assert(thread_val == 0);
|
|
|
|
|
|
|
|
// Tell the second thread to continue and finish.
|
2011-05-17 07:29:30 +08:00
|
|
|
safe_unlock(&sems2.sem3);
|
2007-10-08 01:10:10 +08:00
|
|
|
|
|
|
|
// Wait for the second thread to finish.
|
|
|
|
err = pthread_join(thread2, &thread_val);
|
|
|
|
assert(err == 0);
|
|
|
|
assert(thread_val == 0);
|
|
|
|
|
|
|
|
// All done.
|
2007-10-08 15:24:21 +08:00
|
|
|
return failed ? 1 : 0;
|
2007-10-08 01:10:10 +08:00
|
|
|
}
|