From dfe8dac6197ad47821e5fade5fd300ce461a4a65 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Thu, 23 Jul 2009 17:50:02 +0000 Subject: [PATCH] Refactoring: moved semaphore initialization and cleanup code to two new functions. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10563 --- drd/tests/pth_inconsistent_cond_wait.c | 27 +++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/drd/tests/pth_inconsistent_cond_wait.c b/drd/tests/pth_inconsistent_cond_wait.c index 843d109a59..30bfed4f7f 100644 --- a/drd/tests/pth_inconsistent_cond_wait.c +++ b/drd/tests/pth_inconsistent_cond_wait.c @@ -9,6 +9,7 @@ #include #include #include +#include // malloc() #include // memset() #include // gettimeofday() #include // struct timespec @@ -35,16 +36,30 @@ pthread_cond_t s_cond; pthread_mutex_t s_mutex1; pthread_mutex_t s_mutex2; -sem_t s_sem; +sem_t* s_sem; +static sem_t* create_semaphore() +{ + sem_t* p = malloc(sizeof(*p)); + if (p) + sem_init(p, 0, 0); + return p; +} + +static void destroy_semaphore(sem_t* p) +{ + sem_destroy(p); + free(p); +} + static void* thread_func(void* mutex) { struct timeval now; struct timespec deadline; PTH_CALL(pthread_mutex_lock(mutex)); - sem_post(&s_sem); + sem_post(s_sem); gettimeofday(&now, 0); memset(&deadline, 0, sizeof(deadline)); deadline.tv_sec = now.tv_sec + 2; @@ -60,7 +75,7 @@ int main(int argc, char** argv) pthread_t tid2; /* Initialize synchronization objects. */ - sem_init(&s_sem, 0, 0); + s_sem = create_semaphore(); PTH_CALL(pthread_cond_init(&s_cond, 0)); PTH_CALL(pthread_mutex_init(&s_mutex1, 0)); PTH_CALL(pthread_mutex_init(&s_mutex2, 0)); @@ -70,8 +85,10 @@ int main(int argc, char** argv) 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); + sem_wait(s_sem); + sem_wait(s_sem); + destroy_semaphore(s_sem); + s_sem = 0; /* Wait until both threads are waiting inside pthread_cond_wait(). */ PTH_CALL(pthread_mutex_lock(&s_mutex1)); -- 2.47.3