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

drd/tests/pth_inconsistent_cond_wait.c

index 41a24545245ef076afff38df7023c983994f1448..843d109a59a7f83718c75169da56887e03dea73c 100644 (file)
 #include <time.h>      // struct timespec
 #include <unistd.h>
 
+
+#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)
+
+
 pthread_cond_t  s_cond;
 pthread_mutex_t s_mutex1;
 pthread_mutex_t s_mutex2;
 sem_t           s_sem;
 
+
 static void* thread_func(void* mutex)
 {
-  int err;
   struct timeval now;
   struct timespec deadline;
 
-  pthread_mutex_lock(mutex);
+  PTH_CALL(pthread_mutex_lock(mutex));
   sem_post(&s_sem);
   gettimeofday(&now, 0);
   memset(&deadline, 0, sizeof(deadline));
   deadline.tv_sec  = now.tv_sec + 2;
   deadline.tv_nsec = now.tv_usec * 1000;
-  err = pthread_cond_timedwait(&s_cond, mutex, &deadline);
-  if (err != 0)
-    fprintf(stderr,
-           "pthread_cond_timedwait() call returned error code %d (%s)\n",
-           err,
-           strerror(err));
-  pthread_mutex_unlock(mutex);
+  PTH_CALL(pthread_cond_timedwait(&s_cond, mutex, &deadline));
+  PTH_CALL(pthread_mutex_unlock(mutex));
   return 0;
 }
 
@@ -48,31 +61,31 @@ int main(int argc, char** argv)
 
   /* Initialize synchronization objects. */
   sem_init(&s_sem, 0, 0);
-  pthread_cond_init(&s_cond, 0);
-  pthread_mutex_init(&s_mutex1, 0);
-  pthread_mutex_init(&s_mutex2, 0);
+  PTH_CALL(pthread_cond_init(&s_cond, 0));
+  PTH_CALL(pthread_mutex_init(&s_mutex1, 0));
+  PTH_CALL(pthread_mutex_init(&s_mutex2, 0));
 
   /* Create two threads. */
-  pthread_create(&tid1, 0, &thread_func, &s_mutex1);
-  pthread_create(&tid2, 0, &thread_func, &s_mutex2);
+  PTH_CALL(pthread_create(&tid1, 0, &thread_func, &s_mutex1));
+  PTH_CALL(pthread_create(&tid2, 0, &thread_func, &s_mutex2));
 
   /* Wait until both threads have called sem_post(). */
   sem_wait(&s_sem);
   sem_wait(&s_sem);
 
   /* Wait until both threads are waiting inside pthread_cond_wait(). */
-  pthread_mutex_lock(&s_mutex1);
-  pthread_mutex_lock(&s_mutex2);
-  pthread_mutex_unlock(&s_mutex2);
-  pthread_mutex_unlock(&s_mutex1);
+  PTH_CALL(pthread_mutex_lock(&s_mutex1));
+  PTH_CALL(pthread_mutex_lock(&s_mutex2));
+  PTH_CALL(pthread_mutex_unlock(&s_mutex2));
+  PTH_CALL(pthread_mutex_unlock(&s_mutex1));
 
   /* Signal s_cond twice. */
-  pthread_cond_signal(&s_cond);
-  pthread_cond_signal(&s_cond);
+  PTH_CALL(pthread_cond_signal(&s_cond));
+  PTH_CALL(pthread_cond_signal(&s_cond));
 
   /* Join both threads. */
-  pthread_join(tid1, 0);
-  pthread_join(tid2, 0);
+  PTH_CALL(pthread_join(tid1, 0));
+  PTH_CALL(pthread_join(tid2, 0));
 
   return 0;
 }