From Craig Silverstein: TLS test cleanups.
authorIan Lance Taylor <iant@google.com>
Sun, 14 Oct 2007 03:23:38 +0000 (03:23 +0000)
committerIan Lance Taylor <iant@google.com>
Sun, 14 Oct 2007 03:23:38 +0000 (03:23 +0000)
gold/testsuite/tls_test.cc
gold/testsuite/tls_test.h
gold/testsuite/tls_test_file2.cc
gold/testsuite/tls_test_main.cc

index 995e20b53d6cc235f74cd007aa98d79f654458db..ca3d9c59814767f5ea17898b32a147b09460b27f 100644 (file)
 // last  Verify that the above tests left the variables set correctly.
 
 
+#include <cstdio>
 #include "tls_test.h"
 
+#define CHECK_EQ_OR_RETURN(var, expected)                              \
+  do                                                                   \
+    {                                                                  \
+      if ((var) != (expected))                                         \
+       {                                                               \
+         printf(#var ": expected %d, found %d\n", expected, var);      \
+         return false;                                                 \
+       }                                                               \
+    }                                                                  \
+  while (0)
+
 __thread int v1;
 static __thread int v2;
 __thread int v3 = 3;
@@ -53,16 +65,10 @@ static __thread int v4 = 4;
 __thread int v5;
 static __thread int v6;
 
-// These variables are defined in tls_test_file2.cc
-extern __thread int o1;
-extern __thread int o2;
-extern __thread int o3;
-
 bool
 t1()
 {
-  if (v1 != 0)
-    return false;
+  CHECK_EQ_OR_RETURN(v1, 0);
   v1 = 10;
   return true;
 }
@@ -70,8 +76,7 @@ t1()
 bool
 t2()
 {
-  if (v2 != 0)
-    return false;
+  CHECK_EQ_OR_RETURN(v2, 0);
   v2 = 20;
   return true;
 }
@@ -79,8 +84,7 @@ t2()
 bool
 t3()
 {
-  if (v3 != 3)
-    return false;
+  CHECK_EQ_OR_RETURN(v3, 3);
   v3 = 30;
   return true;
 }
@@ -88,8 +92,7 @@ t3()
 bool
 t4()
 {
-  if (v4 != 4)
-    return false;
+  CHECK_EQ_OR_RETURN(v4, 4);
   v4 = 40;
   return true;
 }
@@ -111,7 +114,8 @@ f5b(int* p)
 bool
 t5()
 {
-  return v5 == 50;
+  CHECK_EQ_OR_RETURN(v5, 50);
+  return true;
 }
 
 // For test 6 the main function calls f6b(f6a()), then calls t6().
@@ -131,7 +135,8 @@ f6b(int* p)
 bool
 t6()
 {
-  return v6 == 60;
+  CHECK_EQ_OR_RETURN(v6, 60);
+  return true;
 }
 
 // The slot for t7() is unused.
@@ -139,18 +144,16 @@ t6()
 bool
 t8()
 {
-  if (o1 != 0)
-    return false;
-  o1 = 10;
+  CHECK_EQ_OR_RETURN(o1, 0);
+  o1 = -10;
   return true;
 }
 
 bool
 t9()
 {
-  if (o2 != 2)
-    return false;
-  o2 = 20;
+  CHECK_EQ_OR_RETURN(o2, -2);
+  o2 = -20;
   return true;
 }
 
@@ -165,25 +168,27 @@ f10a()
 void
 f10b(int* p)
 {
-  *p = 30;
+  *p = -30;
 }
 
 bool
 t10()
 {
-  return o3 == 30;
+  CHECK_EQ_OR_RETURN(o3, -30);
+  return true;
 }
 
 bool
 t_last()
 {
-  return (v1 == 10
-         && v2 == 20
-         && v3 == 30
-         && v4 == 40
-         && v5 == 50
-         && v6 == 60
-          && o1 == 10
-          && o2 == 20
-          && o3 == 30);
+  CHECK_EQ_OR_RETURN(v1, 10);
+  CHECK_EQ_OR_RETURN(v2, 20);
+  CHECK_EQ_OR_RETURN(v3, 30);
+  CHECK_EQ_OR_RETURN(v4, 40);
+  CHECK_EQ_OR_RETURN(v5, 50);
+  CHECK_EQ_OR_RETURN(v6, 60);
+  CHECK_EQ_OR_RETURN(o1, -10);
+  CHECK_EQ_OR_RETURN(o2, -20);
+  CHECK_EQ_OR_RETURN(o3, -30);
+  return true;
 }
index 1a1b6fd080bd322460720621dd86bd46d5c6a43c..b78e49eedfe919b5d687d8c21b1c260b75d6a5a2 100644 (file)
@@ -44,3 +44,8 @@ extern void f10b(int*);
 extern bool t10();
 
 extern bool t_last();
+
+// These variables are defined in tls_test_file2.cc
+extern __thread int o1;
+extern __thread int o2;
+extern __thread int o3;
index 103681a079d7852fb9e0263eb28b967aada96eb1..150a13925a6b9bb2eac2e0eeb7bb670a0b91cb2c 100644 (file)
 // MA 02110-1301, USA.
 
 // This is the definition of a thread-local variable in another file.
+// See tls_test.cc for more information.
+
+#include "tls_test.h"
 
 __thread int o1;
-__thread int o2 = 2;
+__thread int o2 = -2;
 __thread int o3;
index 8b2d585c103d343afb07c24d21288afa2d536134..805ffcb2d2d6bffcce451044adee9e2f0bd15b94 100644 (file)
 
 #include "tls_test.h"
 
+// We make these macros so the assert() will give useful line-numbers.
+#define safe_lock(muptr)                       \
+  do                                           \
+    {                                          \
+      int err = pthread_mutex_lock(muptr);     \
+      assert(err == 0);                                \
+    }                                          \
+  while (0)
+
+#define safe_unlock(muptr)                     \
+  do                                           \
+    {                                          \
+      int err = pthread_mutex_unlock(muptr);   \
+      assert(err == 0);                                \
+    }                                          \
+  while (0)
+
 struct Mutex_set
 {
   pthread_mutex_t mutex1;
@@ -66,8 +83,8 @@ thread_routine(void* arg)
   Mutex_set* pms = static_cast<Mutex_set*>(arg);
 
   // Lock the first mutex.
-  int err = pthread_mutex_lock(&pms->mutex1);
-  assert(err == 0);
+  if (pms)
+    safe_lock(&pms->mutex1);
 
   // Run the tests.
   check("t1", t1());
@@ -85,12 +102,12 @@ thread_routine(void* arg)
   check("t_last", t_last());
 
   // Unlock the second mutex.
-  err = pthread_mutex_unlock(&pms->mutex2);
-  assert(err == 0);
+  if (pms)
+    safe_unlock(&pms->mutex2);
 
   // Lock the third mutex.
-  err = pthread_mutex_lock(&pms->mutex3);
-  assert(err == 0);
+  if (pms)
+    safe_lock(&pms->mutex3);
 
   check("t_last", t_last());
 
@@ -102,25 +119,23 @@ thread_routine(void* arg)
 int
 main()
 {
+  // First, as a sanity check, run through the tests in the "main" thread.
+  thread_routine(0);
+
   // Set up the mutex locks.  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);
+  safe_lock(&mutexes1.mutex2);
+  safe_lock(&mutexes1.mutex3);
 
-  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);
+  safe_lock(&mutexes2.mutex1);
+  safe_lock(&mutexes2.mutex2);
+  safe_lock(&mutexes2.mutex3);
 
   pthread_t thread1;
-  err = pthread_create(&thread1, NULL, thread_routine, &mutexes1);
+  int err = pthread_create(&thread1, NULL, thread_routine, &mutexes1);
   assert(err == 0);
 
   pthread_t thread2;
@@ -128,20 +143,16 @@ main()
   assert(err == 0);
 
   // Wait for the first thread to complete the first part.
-  err = pthread_mutex_lock(&mutexes1.mutex2);
-  assert(err == 0);
+  safe_lock(&mutexes1.mutex2);
 
   // Tell the second thread to start.
-  err = pthread_mutex_unlock(&mutexes2.mutex1);
-  assert(err == 0);
+  safe_unlock(&mutexes2.mutex1);
 
   // Wait for the second thread to complete the first part.
-  err = pthread_mutex_lock(&mutexes2.mutex2);
-  assert(err == 0);
+  safe_lock(&mutexes2.mutex2);
 
   // Tell the first thread to continue and finish.
-  err = pthread_mutex_unlock(&mutexes1.mutex3);
-  assert(err == 0);
+  safe_unlock(&mutexes1.mutex3);
 
   // Wait for the first thread to finish.
   void* thread_val;
@@ -150,8 +161,7 @@ main()
   assert(thread_val == 0);
 
   // Tell the second thread to continue and finish.
-  err = pthread_mutex_unlock(&mutexes2.mutex3);
-  assert(err == 0);
+  safe_unlock(&mutexes2.mutex3);
 
   // Wait for the second thread to finish.
   err = pthread_join(thread2, &thread_val);
This page took 0.028865 seconds and 4 git commands to generate.