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 tests for do-once control from Na...



details:   https://anonhg.NetBSD.org/src/rev/57b7fc33700f
branches:  trunk
changeset: 542488:57b7fc33700f
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Thu Jan 30 19:31:59 2003 +0000

description:
Add tests for do-once control from Nathan's testsuite.

diffstat:

 regress/lib/libpthread/Makefile       |   4 +-
 regress/lib/libpthread/once1/Makefile |  15 ++++++++
 regress/lib/libpthread/once1/once1.c  |  35 ++++++++++++++++++++
 regress/lib/libpthread/once2/Makefile |  15 ++++++++
 regress/lib/libpthread/once2/once2.c  |  61 +++++++++++++++++++++++++++++++++++
 5 files changed, 128 insertions(+), 2 deletions(-)

diffs (154 lines):

diff -r be21899d8eab -r 57b7fc33700f regress/lib/libpthread/Makefile
--- a/regress/lib/libpthread/Makefile   Thu Jan 30 19:14:18 2003 +0000
+++ b/regress/lib/libpthread/Makefile   Thu Jan 30 19:31:59 2003 +0000
@@ -1,6 +1,6 @@
-#      $NetBSD: Makefile,v 1.5 2003/01/30 18:57:06 thorpej Exp $
+#      $NetBSD: Makefile,v 1.6 2003/01/30 19:31:59 thorpej Exp $
 
 SUBDIR+= barrier1 cond1 cond2 cond3 cond4 cond5 \
-        mutex1 mutex2 mutex3 mutex4 sem
+        mutex1 mutex2 mutex3 mutex4 once1 once2 sem
 
 .include <bsd.subdir.mk>
diff -r be21899d8eab -r 57b7fc33700f regress/lib/libpthread/once1/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/once1/Makefile     Thu Jan 30 19:31:59 2003 +0000
@@ -0,0 +1,15 @@
+#      $NetBSD: Makefile,v 1.1 2003/01/30 19:31:59 thorpej Exp $
+
+WARNS=1
+
+PROG=   once1
+SRCS=   once1.c
+
+LDADD= -lpthread
+
+NOMAN=
+
+regress:
+       ./once1
+
+.include <bsd.prog.mk>
diff -r be21899d8eab -r 57b7fc33700f regress/lib/libpthread/once1/once1.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/once1/once1.c      Thu Jan 30 19:31:59 2003 +0000
@@ -0,0 +1,35 @@
+/*     $NetBSD: once1.c,v 1.1 2003/01/30 19:32:00 thorpej Exp $        */
+
+#include <assert.h>
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+pthread_once_t once = PTHREAD_ONCE_INIT;
+static int x;
+
+void ofunc(void);
+
+int
+main(int argc, char *argv[])
+{
+
+       printf("1: Test 1 of pthread_once()\n");
+
+       pthread_once(&once, ofunc);
+       pthread_once(&once, ofunc);
+
+       printf("1: X has value %d\n",x );
+       assert(x == 1);
+
+       return 0;
+}
+
+void
+ofunc(void)
+{
+
+       printf("Variable x has value %d\n", x);
+       x++;
+}
diff -r be21899d8eab -r 57b7fc33700f regress/lib/libpthread/once2/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/once2/Makefile     Thu Jan 30 19:31:59 2003 +0000
@@ -0,0 +1,15 @@
+#      $NetBSD: Makefile,v 1.1 2003/01/30 19:32:00 thorpej Exp $
+
+WARNS=1
+
+PROG=   once2
+SRCS=   once2.c
+
+LDADD= -lpthread
+
+NOMAN=
+
+regress:
+       ./once2
+
+.include <bsd.prog.mk>
diff -r be21899d8eab -r 57b7fc33700f regress/lib/libpthread/once2/once2.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/once2/once2.c      Thu Jan 30 19:31:59 2003 +0000
@@ -0,0 +1,61 @@
+/*     $NetBSD: once2.c,v 1.1 2003/01/30 19:32:00 thorpej Exp $        */
+
+#include <assert.h>
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+pthread_once_t once = PTHREAD_ONCE_INIT;
+static int x;
+
+#define NTHREADS 25
+
+void ofunc(void);
+void* threadfunc(void *);
+
+int
+main(int argc, char *argv[])
+{
+       pthread_t  threads[NTHREADS];
+       int id[NTHREADS];
+       int i;
+
+       printf("1: Test 2 of pthread_once()\n");
+
+       for (i=0; i < NTHREADS; i++) {
+               id[i] = i;
+               pthread_create(&threads[i], NULL, threadfunc, &id[i]);
+       }
+
+       for (i=0; i < NTHREADS; i++)
+               pthread_join(threads[i], NULL);
+
+       printf("1: X has value %d\n",x );
+       assert(x == 2);
+
+       return 0;
+}
+
+
+void
+ofunc(void)
+{
+       x++;
+       printf("ofunc: Variable x has value %d\n", x);
+       x++;
+}
+
+void *
+threadfunc(void *arg)
+{
+       int num;
+
+       pthread_once(&once, ofunc);
+
+       num = *(int *)arg;
+       printf("Thread %d sees x with value %d\n", num, x);
+       assert(x == 2);
+
+       return NULL;
+}



Home | Main Index | Thread Index | Old Index