// tls_test.cc -- test TLS variables for gold, main function
-// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Copyright (C) 2006-2020 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
// more information.
#include <cassert>
+#include <cstdio>
#include <pthread.h>
+#include <semaphore.h>
#include "tls_test.h"
-struct Mutex_set
+// We make these macros so the assert() will give useful line-numbers.
+#define safe_lock(semptr) \
+ do \
+ { \
+ int err = sem_wait(semptr); \
+ assert(err == 0); \
+ } \
+ while (0)
+
+#define safe_unlock(semptr) \
+ do \
+ { \
+ int err = sem_post(semptr); \
+ assert(err == 0); \
+ } \
+ while (0)
+
+struct Sem_set
{
- pthread_mutex_t mutex1;
- pthread_mutex_t mutex2;
- pthread_mutex_t mutex3;
+ sem_t sem1;
+ sem_t sem2;
+ sem_t sem3;
};
-Mutex_set mutexes1 = { PTHREAD_MUTEX_INITIALIZER,
- PTHREAD_MUTEX_INITIALIZER,
- PTHREAD_MUTEX_INITIALIZER };
+Sem_set sems1;
+Sem_set sems2;
-Mutex_set mutexes2 = { PTHREAD_MUTEX_INITIALIZER,
- PTHREAD_MUTEX_INITIALIZER,
- PTHREAD_MUTEX_INITIALIZER } ;
+bool failed = false;
-// The body of the thread function. This gets a lock on the first
-// mutex, runs the tests, and then unlocks the second mutex. Then it
-// locks the third mutex, and the runs the verification test again.
+void
+check(const char* name, bool val)
+{
+ if (!val)
+ {
+ fprintf(stderr, "Test %s failed\n", name);
+ failed = true;
+ }
+}
+
+// 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.
void*
thread_routine(void* arg)
{
- Mutex_set* pms = static_cast<Mutex_set*>(arg);
+ Sem_set* pms = static_cast<Sem_set*>(arg);
- // Lock the first mutex.
- int err = pthread_mutex_lock(&pms->mutex1);
- assert(err == 0);
+ // Acquire the first semaphore.
+ if (pms)
+ safe_lock(&pms->sem1);
// Run the tests.
- assert(t1());
- assert(t2());
- assert(t3());
- assert(t4());
+ check("t1", t1());
+ check("t2", t2());
+ check("t3", t3());
+ check("t4", t4());
f5b(f5a());
- assert(t5());
+ check("t5", t5());
f6b(f6a());
- assert(t6());
- assert(t7());
+ check("t6", t6());
+ check("t8", t8());
+ check("t9", t9());
+ f10b(f10a());
+ check("t10", t10());
+ check("t11", t11() != 0);
+ check("t12", t12());
+ check("t_last", t_last());
- // Unlock the second mutex.
- err = pthread_mutex_unlock(&pms->mutex2);
- assert(err == 0);
+ // Release the second semaphore.
+ if (pms)
+ safe_unlock(&pms->sem2);
- // Lock the third mutex.
- err = pthread_mutex_lock(&pms->mutex3);
- assert(err == 0);
+ // Acquire the third semaphore.
+ if (pms)
+ safe_lock(&pms->sem3);
- assert(t7());
+ check("t_last", t_last());
return 0;
}
int
main()
{
- // Set up the mutex locks. We want the first thread to start right
+ // First, as a sanity check, run through the tests in the "main" thread.
+ thread_routine(0);
+
+ // Set up the semaphores. We want the first thread to start right
// 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.
- int err = pthread_mutex_lock(&mutexes1.mutex2);
- assert(err == 0);
- err = pthread_mutex_lock(&mutexes1.mutex3);
- assert(err == 0);
+ sem_init(&sems1.sem1, 0, 1);
+ sem_init(&sems1.sem2, 0, 0);
+ sem_init(&sems1.sem3, 0, 0);
- err = pthread_mutex_lock(&mutexes2.mutex1);
- assert(err == 0);
- err = pthread_mutex_lock(&mutexes2.mutex2);
- assert(err == 0);
- err = pthread_mutex_lock(&mutexes2.mutex3);
- assert(err == 0);
+ sem_init(&sems2.sem1, 0, 0);
+ sem_init(&sems2.sem2, 0, 0);
+ sem_init(&sems2.sem3, 0, 0);
pthread_t thread1;
- err = pthread_create(&thread1, NULL, thread_routine, &mutexes1);
+ int err = pthread_create(&thread1, NULL, thread_routine, &sems1);
assert(err == 0);
pthread_t thread2;
- err = pthread_create(&thread2, NULL, thread_routine, &mutexes2);
+ err = pthread_create(&thread2, NULL, thread_routine, &sems2);
assert(err == 0);
// Wait for the first thread to complete the first part.
- err = pthread_mutex_lock(&mutexes1.mutex2);
- assert(err == 0);
+ safe_lock(&sems1.sem2);
// Tell the second thread to start.
- err = pthread_mutex_unlock(&mutexes2.mutex1);
- assert(err == 0);
+ safe_unlock(&sems2.sem1);
// Wait for the second thread to complete the first part.
- err = pthread_mutex_lock(&mutexes2.mutex2);
- assert(err == 0);
+ safe_lock(&sems2.sem2);
// Tell the first thread to continue and finish.
- err = pthread_mutex_unlock(&mutexes1.mutex3);
- assert(err == 0);
+ safe_unlock(&sems1.sem3);
// Wait for the first thread to finish.
void* thread_val;
assert(thread_val == 0);
// Tell the second thread to continue and finish.
- err = pthread_mutex_unlock(&mutexes2.mutex3);
- assert(err == 0);
+ safe_unlock(&sems2.sem3);
// Wait for the second thread to finish.
err = pthread_join(thread2, &thread_val);
assert(thread_val == 0);
// All done.
- return 0;
+ return failed ? 1 : 0;
}