diff options
Diffstat (limited to 'lib/libc_r/TEST/test_pthread_cond_timedwait.c')
-rw-r--r-- | lib/libc_r/TEST/test_pthread_cond_timedwait.c | 68 |
1 files changed, 27 insertions, 41 deletions
diff --git a/lib/libc_r/TEST/test_pthread_cond_timedwait.c b/lib/libc_r/TEST/test_pthread_cond_timedwait.c index 9942f3aa26e..95cf2a8d466 100644 --- a/lib/libc_r/TEST/test_pthread_cond_timedwait.c +++ b/lib/libc_r/TEST/test_pthread_cond_timedwait.c @@ -11,28 +11,25 @@ #include <stdio.h> #include <errno.h> #include <unistd.h> - -#ifndef ETIME -#define ETIME ETIMEDOUT -#endif +#include "test.h" pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; void* thread_1(void * new_buf) { - pthread_mutex_lock(&mutex); - pthread_cond_signal(&cond); - pthread_mutex_unlock(&mutex); + CHECKr(pthread_mutex_lock(&mutex)); + CHECKr(pthread_cond_signal(&cond)); + CHECKr(pthread_mutex_unlock(&mutex)); pthread_exit(NULL); } void* thread_2(void * new_buf) { sleep(1); - pthread_mutex_lock(&mutex); - pthread_cond_signal(&cond); - pthread_mutex_unlock(&mutex); + CHECKr(pthread_mutex_lock(&mutex)); + CHECKr(pthread_cond_signal(&cond)); + CHECKr(pthread_mutex_unlock(&mutex)); pthread_exit(NULL); } @@ -42,52 +39,41 @@ main() struct timespec abstime = { 0, 0 }; struct timeval curtime; pthread_t thread; + int ret; printf("pthread_cond_timedwait START\n"); - pthread_mutex_lock(&mutex); - gettimeofday(&curtime, NULL); + CHECKr(pthread_mutex_lock(&mutex)); + CHECKe(gettimeofday(&curtime, NULL)); abstime.tv_sec = curtime.tv_sec + 5; /* Test a condition timeout */ - if (pthread_cond_timedwait(&cond, &mutex, &abstime) != ETIME) { - printf("pthread_cond_timedwait failed to timeout\n"); - printf("pthread_cond_timedwait FAILED\n"); - pthread_mutex_unlock(&mutex); - exit(1); + switch((ret = pthread_cond_timedwait(&cond, &mutex, &abstime))) { + case 0: + PANIC("pthread_cond_timedwait #0 failed to timeout"); + /* NOTREACHED */ + case ETIMEDOUT: + /* expected behaviour */ + printf("Got first timeout ok\n"); /* Added by monty */ + break; + default: + DIE(ret, "pthread_cond_timedwait"); + /* NOTREACHED */ } - printf("Got first timeout ok\n"); /* Added by monty */ + /* Test a normal condition signal */ - if (pthread_create(&thread, NULL, thread_1, NULL)) { - printf("pthread_create failed\n"); - exit(2); - } + CHECKr(pthread_create(&thread, NULL, thread_1, NULL)); abstime.tv_sec = curtime.tv_sec + 10; - if (pthread_cond_timedwait(&cond, &mutex, &abstime)) { - printf("pthread_cond_timedwait #1 timedout\n"); - printf("pthread_cond_timedwait FAILED\n"); - pthread_mutex_unlock(&mutex); - exit(1); - } + CHECKr(pthread_cond_timedwait(&cond, &mutex, &abstime)); /* Test a normal condition signal after a sleep */ - if (pthread_create(&thread, NULL, thread_2, NULL)) { - printf("pthread_create failed\n"); - exit(2); - } + CHECKr(pthread_create(&thread, NULL, thread_2, NULL)); pthread_yield(); abstime.tv_sec = curtime.tv_sec + 10; - if (pthread_cond_timedwait(&cond, &mutex, &abstime)) { - printf("pthread_cond_timedwait #2 timedout\n"); - printf("pthread_cond_timedwait FAILED\n"); - pthread_mutex_unlock(&mutex); - exit(1); - } + CHECKr(pthread_cond_timedwait(&cond, &mutex, &abstime)); - printf("pthread_cond_timedwait PASSED\n"); - pthread_mutex_unlock(&mutex); - exit(0); + SUCCEED; } |