Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/regress/lib/libpthread Add one more condition variable test ...



details:   https://anonhg.NetBSD.org/src/rev/06896ec13df7
branches:  trunk
changeset: 542486:06896ec13df7
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Thu Jan 30 18:57:06 2003 +0000

description:
Add one more condition variable test from Nathan's testsuite.

diffstat:

 regress/lib/libpthread/Makefile       |   5 +-
 regress/lib/libpthread/cond5/Makefile |  15 ++++++
 regress/lib/libpthread/cond5/cond5.c  |  86 +++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+), 2 deletions(-)

diffs (121 lines):

diff -r d1c4ecf9d75b -r 06896ec13df7 regress/lib/libpthread/Makefile
--- a/regress/lib/libpthread/Makefile   Thu Jan 30 18:53:44 2003 +0000
+++ b/regress/lib/libpthread/Makefile   Thu Jan 30 18:57:06 2003 +0000
@@ -1,5 +1,6 @@
-#      $NetBSD: Makefile,v 1.4 2003/01/30 18:53:44 thorpej Exp $
+#      $NetBSD: Makefile,v 1.5 2003/01/30 18:57:06 thorpej Exp $
 
-SUBDIR+= barrier1 cond1 cond2 cond3 cond4 mutex1 mutex2 mutex3 mutex4 sem
+SUBDIR+= barrier1 cond1 cond2 cond3 cond4 cond5 \
+        mutex1 mutex2 mutex3 mutex4 sem
 
 .include <bsd.subdir.mk>
diff -r d1c4ecf9d75b -r 06896ec13df7 regress/lib/libpthread/cond5/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/cond5/Makefile     Thu Jan 30 18:57:06 2003 +0000
@@ -0,0 +1,15 @@
+#      $NetBSD: Makefile,v 1.1 2003/01/30 18:57:07 thorpej Exp $
+
+WARNS=1
+
+PROG=   cond5
+SRCS=   cond5.c
+
+LDADD= -lpthread
+
+NOMAN=
+
+regress:
+       ./cond5
+
+.include <bsd.prog.mk>
diff -r d1c4ecf9d75b -r 06896ec13df7 regress/lib/libpthread/cond5/cond5.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/cond5/cond5.c      Thu Jan 30 18:57:06 2003 +0000
@@ -0,0 +1,86 @@
+/*     $NetBSD: cond5.c,v 1.1 2003/01/30 18:57:07 thorpej Exp $        */
+
+#include <assert.h>
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+void *threadfunc(void *arg);
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
+int count, total, toggle;
+
+int main(void)
+{
+       int ret;
+       pthread_t new;
+       void *joinval;
+       int sharedval;
+
+       printf("1: condition variable test 5\n");
+
+       ret = pthread_mutex_lock(&mutex);
+       if (ret)
+               err(1, "pthread_mutex_lock(1)");
+
+       count = 5000000;
+       toggle = 0;
+
+       ret = pthread_create(&new, NULL, threadfunc, &sharedval);
+       if (ret != 0)
+               err(1, "pthread_create");
+
+       printf("1: Before waiting.\n");
+       while (count>0) {
+               count--;
+               total++;
+               toggle = 1;
+               pthread_cond_broadcast(&cond);
+               do {
+                       pthread_cond_wait(&cond, &mutex);
+               } while (toggle != 0);
+       }
+       printf("1: After the loop.\n");
+
+       toggle = 1;
+       pthread_mutex_unlock(&mutex);
+       pthread_cond_signal(&cond);
+       
+       printf("1: After releasing the mutex.\n");
+       ret = pthread_join(new, &joinval);
+       if (ret != 0)
+               err(1, "pthread_join");
+
+       printf("1: Thread joined. Final count = %d, total = %d\n",
+           count, total);
+       assert(count == 0);
+       assert(total == 5000000);
+
+       return 0;
+}
+
+void *
+threadfunc(void *arg)
+{
+#if 0
+       int *share = (int *) arg;
+#endif
+
+       printf("2: Second thread.\n");
+       pthread_mutex_lock(&mutex);
+       while (count>0) {
+               count--;
+               total++;
+               toggle = 0;
+               pthread_cond_signal(&cond);
+               do {
+                       pthread_cond_wait(&cond, &mutex);
+               } while (toggle != 1);
+       }
+       printf("2: After the loop.\n");
+       pthread_mutex_unlock(&mutex);
+
+       return NULL;
+}



Home | Main Index | Thread Index | Old Index