From 41ba001828dd0a8fd75848ff17684b920ddfbf75 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Mon, 2 Feb 2015 13:13:59 -0800 Subject: [PATCH] pthread condition variable tests --- lib/libc/posix/pthread.c | 2 +- tests/pthreadtest.c | 52 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/libc/posix/pthread.c b/lib/libc/posix/pthread.c index d37e768..82a575d 100644 --- a/lib/libc/posix/pthread.c +++ b/lib/libc/posix/pthread.c @@ -339,7 +339,7 @@ pthread_cond_destroy(pthread_cond_t *cond) *cond = NULL; free(cnd); - return EINVAL; + return 0; } int diff --git a/tests/pthreadtest.c b/tests/pthreadtest.c index e931279..0c84038 100644 --- a/tests/pthreadtest.c +++ b/tests/pthreadtest.c @@ -7,6 +7,7 @@ #include pthread_mutex_t mtx; +pthread_cond_t cnd; void * thread_simple(void *arg) @@ -33,6 +34,22 @@ thread_lock(void *arg) return NULL; } +void * +thread_cond(void *arg) +{ + int i; + int status; + + for (i = 0; i < 100; i++) { + status = pthread_cond_wait(&cnd, NULL); + assert(status == 0); + status = pthread_cond_signal(&cnd); + assert(status == 0); + } + + return NULL; +} + int main(int argc, const char *argv[]) { @@ -43,23 +60,25 @@ main(int argc, const char *argv[]) printf("PThread Test\n"); - // Simple thread test + // Simple thread Test printf("simple test\n"); status = pthread_create(&thr, NULL, thread_simple, NULL); assert(status == 0); status = pthread_join(thr, &result); assert(status == 0); assert(result == NULL); + printf("\n"); - // Return value test + // Return value Test printf("return value test\n"); status = pthread_create(&thr, NULL, thread_simple, (void *)1); assert(status == 0); status = pthread_join(thr, &result); assert(status == 0); assert(result == (void *)1); + printf("\n"); - // Mutex test + // Mutex Test printf("simple mutex lock test\n"); status = pthread_mutex_init(&mtx, NULL); assert(status == 0); @@ -69,6 +88,7 @@ main(int argc, const char *argv[]) assert(status == 0); status = pthread_mutex_destroy(&mtx); assert(status == 0); + printf("\n"); // Mutex Contention Test printf("contended mutex lock test\n"); @@ -86,6 +106,32 @@ main(int argc, const char *argv[]) assert(status == 0); status = pthread_mutex_destroy(&mtx); assert(status == 0); + printf("\n"); + + // Condition Variable Test + printf("simple condition variable test\n"); + status = pthread_cond_init(&cnd, NULL); + assert(status == 0); + status = pthread_cond_signal(&cnd); + assert(status == 0); + status = pthread_cond_wait(&cnd, NULL); + assert(status == 0); + status = pthread_cond_destroy(&cnd); + assert(status == 0); + printf("\n"); + + printf("threaded condition variable test\n"); + status = pthread_cond_init(&cnd, NULL); + assert(status == 0); + for (i = 0; i < 100; i++) { + status = pthread_cond_signal(&cnd); + assert(status == 0); + status = pthread_cond_wait(&cnd, NULL); + assert(status == 0); + } + status = pthread_cond_destroy(&cnd); + assert(status == 0); + printf("\n"); printf("Success!\n");