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 condition variable tests from Nat...



details:   https://anonhg.NetBSD.org/src/rev/d1c4ecf9d75b
branches:  trunk
changeset: 542485:d1c4ecf9d75b
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Thu Jan 30 18:53:44 2003 +0000

description:
Add condition variable tests from Nathan's testsuite.

diffstat:

 regress/lib/libpthread/Makefile       |   4 +-
 regress/lib/libpthread/cond1/Makefile |  15 ++++++
 regress/lib/libpthread/cond1/cond1.c  |  74 ++++++++++++++++++++++++++++++
 regress/lib/libpthread/cond2/Makefile |  15 ++++++
 regress/lib/libpthread/cond2/cond2.c  |  75 ++++++++++++++++++++++++++++++
 regress/lib/libpthread/cond3/Makefile |  15 ++++++
 regress/lib/libpthread/cond3/cond3.c  |  72 +++++++++++++++++++++++++++++
 regress/lib/libpthread/cond4/Makefile |  15 ++++++
 regress/lib/libpthread/cond4/cond4.c  |  85 +++++++++++++++++++++++++++++++++++
 9 files changed, 368 insertions(+), 2 deletions(-)

diffs (truncated from 409 to 300 lines):

diff -r 23e08d9d549b -r d1c4ecf9d75b regress/lib/libpthread/Makefile
--- a/regress/lib/libpthread/Makefile   Thu Jan 30 18:23:09 2003 +0000
+++ b/regress/lib/libpthread/Makefile   Thu Jan 30 18:53:44 2003 +0000
@@ -1,5 +1,5 @@
-#      $NetBSD: Makefile,v 1.3 2003/01/30 18:23:09 thorpej Exp $
+#      $NetBSD: Makefile,v 1.4 2003/01/30 18:53:44 thorpej Exp $
 
-SUBDIR+= barrier1 mutex1 mutex2 mutex3 mutex4 sem
+SUBDIR+= barrier1 cond1 cond2 cond3 cond4 mutex1 mutex2 mutex3 mutex4 sem
 
 .include <bsd.subdir.mk>
diff -r 23e08d9d549b -r d1c4ecf9d75b regress/lib/libpthread/cond1/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/cond1/Makefile     Thu Jan 30 18:53:44 2003 +0000
@@ -0,0 +1,15 @@
+#      $NetBSD: Makefile,v 1.1 2003/01/30 18:53:45 thorpej Exp $
+
+WARNS=1
+
+PROG=   cond1
+SRCS=   cond1.c
+
+LDADD= -lpthread
+
+NOMAN=
+
+regress:
+       ./cond1
+
+.include <bsd.prog.mk>
diff -r 23e08d9d549b -r d1c4ecf9d75b regress/lib/libpthread/cond1/cond1.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/cond1/cond1.c      Thu Jan 30 18:53:44 2003 +0000
@@ -0,0 +1,74 @@
+/*     $NetBSD: cond1.c,v 1.1 2003/01/30 18:53:46 thorpej Exp $        */
+
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void *threadfunc(void *arg);
+
+pthread_mutex_t mutex;
+pthread_cond_t cond;
+
+int
+main(int argc, char *argv[])
+{
+       int x,ret;
+       pthread_t new;
+       void *joinval;
+       int sharedval;
+
+       printf("1: condition variable test 1\n");
+
+       pthread_mutex_init(&mutex, NULL);
+       pthread_cond_init(&cond, NULL);
+
+       x = 20;
+       pthread_mutex_lock(&mutex);
+
+       sharedval = 1;
+
+       ret = pthread_create(&new, NULL, threadfunc, &sharedval);
+       if (ret != 0)
+               err(1, "pthread_create");
+
+       printf("1: Before waiting.\n");
+       do {
+               sleep(2);
+               pthread_cond_wait(&cond, &mutex);
+               printf("1: After waiting, in loop.\n");
+       } while (sharedval != 0);
+
+       printf("1: After the loop.\n");
+
+       pthread_mutex_unlock(&mutex);
+
+       printf("1: After releasing the mutex.\n");
+       ret = pthread_join(new, &joinval);
+       if (ret != 0)
+               err(1, "pthread_join");
+
+       printf("1: Thread joined.\n");
+
+       return 0;
+}
+
+void *
+threadfunc(void *arg)
+{
+       int *share = (int *) arg;
+
+       printf("2: Second thread.\n");
+
+       printf("2: Locking mutex\n");
+       pthread_mutex_lock(&mutex);
+       printf("2: Got mutex.\n");
+       printf("Shared value: %d. Changing to 0.\n", *share);
+       *share = 0;
+       
+       pthread_mutex_unlock(&mutex);
+       pthread_cond_signal(&cond);
+
+       return NULL;
+}
diff -r 23e08d9d549b -r d1c4ecf9d75b regress/lib/libpthread/cond2/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/cond2/Makefile     Thu Jan 30 18:53:44 2003 +0000
@@ -0,0 +1,15 @@
+#      $NetBSD: Makefile,v 1.1 2003/01/30 18:53:46 thorpej Exp $
+
+WARNS=1
+
+PROG=   cond2
+SRCS=   cond2.c
+
+LDADD= -lpthread
+
+NOMAN=
+
+regress:
+       ./cond2
+
+.include <bsd.prog.mk>
diff -r 23e08d9d549b -r d1c4ecf9d75b regress/lib/libpthread/cond2/cond2.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/cond2/cond2.c      Thu Jan 30 18:53:44 2003 +0000
@@ -0,0 +1,75 @@
+/*     $NetBSD: cond2.c,v 1.1 2003/01/30 18:53:46 thorpej Exp $        */
+
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void *threadfunc(void *arg);
+
+pthread_mutex_t mutex;
+pthread_cond_t cond;
+
+int
+main(int argc, char *argv[])
+{
+       int x,ret;
+       pthread_t new;
+       void *joinval;
+       int sharedval;
+
+       printf("1: condition variable test 2\n");
+
+       pthread_mutex_init(&mutex, NULL);
+       pthread_cond_init(&cond, NULL);
+
+       x = 20;
+       pthread_mutex_lock(&mutex);
+
+       sharedval = 1;
+
+       ret = pthread_create(&new, NULL, threadfunc, &sharedval);
+       if (ret != 0)
+               err(1, "pthread_create");
+
+       printf("1: Before waiting.\n");
+       do {
+               sleep(2);
+               pthread_cond_wait(&cond, &mutex);
+               printf("1: After waiting, in loop.\n");
+       } while (sharedval != 0);
+
+       printf("1: After the loop.\n");
+
+       pthread_mutex_unlock(&mutex);
+
+       printf("1: After releasing the mutex.\n");
+       ret = pthread_join(new, &joinval);
+       if (ret != 0)
+               err(1, "pthread_join");
+
+       printf("1: Thread joined.\n");
+
+       return 0;
+}
+
+void *
+threadfunc(void *arg)
+{
+       int *share = (int *) arg;
+
+       printf("2: Second thread.\n");
+
+       printf("2: Locking mutex\n");
+       pthread_mutex_lock(&mutex);
+       printf("2: Got mutex.\n");
+       printf("Shared value: %d. Changing to 0.\n", *share);
+       *share = 0;
+       
+       /* Signal first, then unlock, for a different test than #1. */
+       pthread_cond_signal(&cond);
+       pthread_mutex_unlock(&mutex);
+
+       return NULL;
+}
diff -r 23e08d9d549b -r d1c4ecf9d75b regress/lib/libpthread/cond3/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/cond3/Makefile     Thu Jan 30 18:53:44 2003 +0000
@@ -0,0 +1,15 @@
+#      $NetBSD: Makefile,v 1.1 2003/01/30 18:53:47 thorpej Exp $
+
+WARNS=1
+
+PROG=   cond3
+SRCS=   cond3.c
+
+LDADD= -lpthread
+
+NOMAN=
+
+regress:
+       ./cond3
+
+.include <bsd.prog.mk>
diff -r 23e08d9d549b -r d1c4ecf9d75b regress/lib/libpthread/cond3/cond3.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/cond3/cond3.c      Thu Jan 30 18:53:44 2003 +0000
@@ -0,0 +1,72 @@
+/*     $NetBSD: cond3.c,v 1.1 2003/01/30 18:53:47 thorpej Exp $        */
+
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void *threadfunc(void *arg);
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
+
+int
+main(int argc, char *argv[])
+{
+       int x,ret;
+       pthread_t new;
+       void *joinval;
+       int sharedval;
+
+       printf("1: condition variable test 3\n");
+
+       x = 20;
+       pthread_mutex_lock(&mutex);
+
+       sharedval = 1;
+
+       ret = pthread_create(&new, NULL, threadfunc, &sharedval);
+       if (ret != 0)
+               err(1, "pthread_create");
+
+       printf("1: Before waiting.\n");
+       do {
+               sleep(2);
+               pthread_cond_wait(&cond, &mutex);
+               printf("1: After waiting, in loop.\n");
+       } while (sharedval != 0);
+
+       printf("1: After the loop.\n");
+
+       pthread_mutex_unlock(&mutex);
+
+       printf("1: After releasing the mutex.\n");
+       ret = pthread_join(new, &joinval);
+       if (ret != 0)
+               err(1, "pthread_join");
+
+       printf("1: Thread joined.\n");
+
+       return 0;
+}
+
+void *
+threadfunc(void *arg)
+{
+       int *share = (int *) arg;
+
+       printf("2: Second thread.\n");
+
+       printf("2: Locking mutex\n");
+       pthread_mutex_lock(&mutex);
+       printf("2: Got mutex.\n");
+       printf("Shared value: %d. Changing to 0.\n", *share);
+       *share = 0;
+       
+       /* Signal first, then unlock, for a different test than #1. */
+       pthread_cond_signal(&cond);
+       pthread_mutex_unlock(&mutex);
+
+       return NULL;



Home | Main Index | Thread Index | Old Index