From f861ba831585fe4503f62ada852e55d2269fd2e1 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Tue, 21 Jul 2009 11:36:46 +0000 Subject: [PATCH] - Replaced pthread_cond_wait() by pthread_cond_timedwait() calls. - An error message is now printed in case pthread_cond_timedwait() fails. - Refactoring: joined thread1() and thread2() into one function. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10501 --- drd/tests/pth_inconsistent_cond_wait.c | 37 ++++++++++++------- .../pth_inconsistent_cond_wait.stderr.exp1 | 4 +- .../pth_inconsistent_cond_wait.stderr.exp2 | 4 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/drd/tests/pth_inconsistent_cond_wait.c b/drd/tests/pth_inconsistent_cond_wait.c index 06c476b0ec..41a2454524 100644 --- a/drd/tests/pth_inconsistent_cond_wait.c +++ b/drd/tests/pth_inconsistent_cond_wait.c @@ -5,8 +5,13 @@ */ +#include // ETIMEDOUT #include #include +#include +#include // memset() +#include // gettimeofday() +#include // struct timespec #include pthread_cond_t s_cond; @@ -14,21 +19,25 @@ pthread_mutex_t s_mutex1; pthread_mutex_t s_mutex2; sem_t s_sem; -void* thread1(void* arg) +static void* thread_func(void* mutex) { - pthread_mutex_lock(&s_mutex1); - sem_post(&s_sem); - pthread_cond_wait(&s_cond, &s_mutex1); - pthread_mutex_unlock(&s_mutex1); - return 0; -} + int err; + struct timeval now; + struct timespec deadline; -void* thread2(void* arg) -{ - pthread_mutex_lock(&s_mutex2); + pthread_mutex_lock(mutex); sem_post(&s_sem); - pthread_cond_wait(&s_cond, &s_mutex2); - pthread_mutex_unlock(&s_mutex2); + 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); return 0; } @@ -44,8 +53,8 @@ int main(int argc, char** argv) pthread_mutex_init(&s_mutex2, 0); /* Create two threads. */ - pthread_create(&tid1, 0, &thread1, 0); - pthread_create(&tid2, 0, &thread2, 0); + pthread_create(&tid1, 0, &thread_func, &s_mutex1); + pthread_create(&tid2, 0, &thread_func, &s_mutex2); /* Wait until both threads have called sem_post(). */ sem_wait(&s_sem); diff --git a/drd/tests/pth_inconsistent_cond_wait.stderr.exp1 b/drd/tests/pth_inconsistent_cond_wait.stderr.exp1 index db4c68fa15..ab65a361b0 100644 --- a/drd/tests/pth_inconsistent_cond_wait.stderr.exp1 +++ b/drd/tests/pth_inconsistent_cond_wait.stderr.exp1 @@ -1,8 +1,8 @@ Thread 3: Inconsistent association of condition variable and mutex: condition variable 0x........, mutexes 0x........ and 0x........ - at 0x........: pthread_cond_wait* (drd_pthread_intercepts.c:?) - by 0x........: thread2 (pth_inconsistent_cond_wait.c:?) + at 0x........: pthread_cond_timedwait* (drd_pthread_intercepts.c:?) + by 0x........: thread_func (pth_inconsistent_cond_wait.c:?) by 0x........: vgDrd_thread_wrapper (drd_pthread_intercepts.c:?) by 0x........: (within libpthread-?.?.so) by 0x........: clone (in /...libc...) diff --git a/drd/tests/pth_inconsistent_cond_wait.stderr.exp2 b/drd/tests/pth_inconsistent_cond_wait.stderr.exp2 index f6962c7c03..7ec0471f9e 100644 --- a/drd/tests/pth_inconsistent_cond_wait.stderr.exp2 +++ b/drd/tests/pth_inconsistent_cond_wait.stderr.exp2 @@ -1,8 +1,8 @@ Thread 2: Inconsistent association of condition variable and mutex: condition variable 0x........, mutexes 0x........ and 0x........ - at 0x........: pthread_cond_wait* (drd_pthread_intercepts.c:?) - by 0x........: thread1 (pth_inconsistent_cond_wait.c:?) + at 0x........: pthread_cond_timedwait* (drd_pthread_intercepts.c:?) + by 0x........: thread_func (pth_inconsistent_cond_wait.c:?) by 0x........: vgDrd_thread_wrapper (drd_pthread_intercepts.c:?) by 0x........: (within libpthread-?.?.so) by 0x........: clone (in /...libc...) -- 2.47.3