]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
rwlock_test: added more internal error checking.
authorBart Van Assche <bvanassche@acm.org>
Fri, 24 Jul 2009 11:10:05 +0000 (11:10 +0000)
committerBart Van Assche <bvanassche@acm.org>
Fri, 24 Jul 2009 11:10:05 +0000 (11:10 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10592

drd/tests/rwlock_test.c
drd/tests/rwlock_test.stderr.exp
drd/tests/rwlock_test.vgtest

index c182f894084bb1ba652d088f1b830eb9aaace6cf..65cfd5ba1935363f4f26b1a8e863e603f8971451 100644 (file)
 #include <string.h>  /* strerror() */
 #include <unistd.h>  /* getopt() */
 
-static pthread_rwlock_t s_rwlock;
-static int s_counter;
 static int s_num_threads = 10;
 static int s_num_iterations = 1000;
+static pthread_mutex_t s_mutex;
+static long long s_grand_sum; /* protected by s_mutex. */
+static pthread_rwlock_t s_rwlock;
+static int s_counter; /* protected by s_rwlock. */
 
 static void* thread_func(void* arg)
 {
-  int i;
-  int sum = 0;
+  int i, r;
+  int sum1 = 0, sum2 = 0;
 
   for (i = s_num_iterations; i > 0; i--)
   {
-    pthread_rwlock_rdlock(&s_rwlock);
-    sum += s_counter;
-    pthread_rwlock_unlock(&s_rwlock);
-    pthread_rwlock_wrlock(&s_rwlock);
-    s_counter++;
-    pthread_rwlock_unlock(&s_rwlock);
+    r = pthread_rwlock_rdlock(&s_rwlock);
+    assert(! r);
+    sum1 += s_counter;
+    r = pthread_rwlock_unlock(&s_rwlock);
+    assert(! r);
+    r = pthread_rwlock_wrlock(&s_rwlock);
+    assert(! r);
+    sum2 += s_counter++;
+    r = pthread_rwlock_unlock(&s_rwlock);
+    assert(! r);
   }
 
+  pthread_mutex_lock(&s_mutex);
+  s_grand_sum += sum2;
+  pthread_mutex_unlock(&s_mutex);
+
   return 0;
 }
 
@@ -47,6 +57,8 @@ int main(int argc, char** argv)
   int optchar;
   int err;
   int i;
+  int expected_counter;
+  long long expected_grand_sum;
 
   while ((optchar = getopt(argc, argv, "i:t:")) != EOF)
   {
@@ -64,6 +76,7 @@ int main(int argc, char** argv)
     }
   }
 
+  pthread_mutex_init(&s_mutex, NULL);
   pthread_rwlock_init(&s_rwlock, NULL);
 
   pthread_attr_init(&attr);
@@ -90,8 +103,12 @@ int main(int argc, char** argv)
   }
   free(tid);
 
-  fprintf(stderr, "s_counter - thread_count * iterations = %d\n",
-          s_counter - threads_created * s_num_iterations);
+  expected_counter = threads_created * s_num_iterations;
+  fprintf(stderr, "s_counter - expected_counter = %d\n",
+          s_counter - expected_counter);
+  expected_grand_sum = 1ULL * expected_counter * (expected_counter - 1) / 2;
+  fprintf(stderr, "s_grand_sum - expected_grand_sum = %lld\n",
+          s_grand_sum - expected_grand_sum);
   fprintf(stderr, "Finished.\n");
 
   return 0;
index 3545f07d19bc4ad73f60f7e0df35ae28b917474c..3063f40adca61dd857d8f56b93ab7ce3cdf3b5b2 100644 (file)
@@ -1,5 +1,6 @@
 
-s_counter - thread_count * iterations = 0
+s_counter - expected_counter = 0
+s_grand_sum - expected_grand_sum = 0
 Finished.
 
 ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
index 6ce02632662d7627a7b7e0d8ab0be6feed4ad90a..b8c874ea04a22317cc2e0611f767cd88549606cc 100644 (file)
@@ -1 +1,2 @@
+vgopts: --check-stack-var=yes --read-var-info=yes --show-confl-seg=no
 prog: rwlock_test