From b2927ebab0cfd095635be1d889df7ed2a224bf15 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Tue, 21 Jul 2009 16:19:34 +0000 Subject: [PATCH] Added more error checking. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10510 --- drd/tests/pth_inconsistent_cond_wait.c | 57 ++++++++++++++++---------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/drd/tests/pth_inconsistent_cond_wait.c b/drd/tests/pth_inconsistent_cond_wait.c index 41a2454524..843d109a59 100644 --- a/drd/tests/pth_inconsistent_cond_wait.c +++ b/drd/tests/pth_inconsistent_cond_wait.c @@ -14,30 +14,43 @@ #include // struct timespec #include + +#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; } -- 2.47.3