]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Added more error checking.
authorBart Van Assche <bvanassche@acm.org>
Tue, 21 Jul 2009 16:35:48 +0000 (16:35 +0000)
committerBart Van Assche <bvanassche@acm.org>
Tue, 21 Jul 2009 16:35:48 +0000 (16:35 +0000)
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10511

drd/tests/rwlock_test.c

index b8dcf471f4572178b5bebdf9c5d946b64a204e44..f7cc33c4ba6e74b203eb4c991d59427b1c642132 100644 (file)
 
 #include <pthread.h>
 #include <stdio.h>
+#include <string.h>  // strerror()
+
+
+#define PTH_CALL(expr)                                  \
+  do                                                    \
+  {                                                     \
+    int err = (expr);                                   \
+    if ((err) != 0)                                     \
+    {                                                   \
+      fprintf(stderr,                                   \
+              "%s:%d %s returned error code %d (%s)\n", \
+              __FILE__,                                 \
+              __LINE__,                                 \
+              #expr,                                    \
+              err,                                      \
+              strerror(err));                           \
+    }                                                   \
+  } while (0)
 
 
 static pthread_rwlock_t s_rwlock;
 static int s_counter;
 
+
 static void* thread_func(void* arg)
 {
   int i;
@@ -22,12 +41,12 @@ static void* thread_func(void* arg)
 
   for (i = 0; i < 1000; i++)
   {
-    pthread_rwlock_rdlock(&s_rwlock);
+    PTH_CALL(pthread_rwlock_rdlock(&s_rwlock));
     sum += s_counter;
-    pthread_rwlock_unlock(&s_rwlock);
-    pthread_rwlock_wrlock(&s_rwlock);
+    PTH_CALL(pthread_rwlock_unlock(&s_rwlock));
+    PTH_CALL(pthread_rwlock_wrlock(&s_rwlock));
     s_counter++;
-    pthread_rwlock_unlock(&s_rwlock);
+    PTH_CALL(pthread_rwlock_unlock(&s_rwlock));
   }
 
   return 0;
@@ -39,15 +58,15 @@ int main(int argc, char** argv)
   pthread_t tid[thread_count];
   int i;
 
-  pthread_rwlock_init(&s_rwlock, NULL);
+  PTH_CALL(pthread_rwlock_init(&s_rwlock, NULL));
   for (i = 0; i < thread_count; i++)
   {
-    pthread_create(&tid[i], 0, thread_func, 0);
+    PTH_CALL(pthread_create(&tid[i], 0, thread_func, 0));
   }
 
   for (i = 0; i < thread_count; i++)
   {
-    pthread_join(tid[i], 0);
+    PTH_CALL(pthread_join(tid[i], 0));
   }
 
   fprintf(stderr, "Finished.\n");