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 a test for pthread barriers.



details:   https://anonhg.NetBSD.org/src/rev/23e08d9d549b
branches:  trunk
changeset: 542484:23e08d9d549b
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Thu Jan 30 18:23:09 2003 +0000

description:
Add a test for pthread barriers.

diffstat:

 regress/lib/libpthread/Makefile            |   4 +-
 regress/lib/libpthread/barrier1/Makefile   |  15 ++++++
 regress/lib/libpthread/barrier1/barrier1.c |  72 ++++++++++++++++++++++++++++++
 3 files changed, 89 insertions(+), 2 deletions(-)

diffs (106 lines):

diff -r ef7b2482c532 -r 23e08d9d549b regress/lib/libpthread/Makefile
--- a/regress/lib/libpthread/Makefile   Thu Jan 30 18:05:25 2003 +0000
+++ b/regress/lib/libpthread/Makefile   Thu Jan 30 18:23:09 2003 +0000
@@ -1,5 +1,5 @@
-#      $NetBSD: Makefile,v 1.2 2003/01/30 18:05:25 thorpej Exp $
+#      $NetBSD: Makefile,v 1.3 2003/01/30 18:23:09 thorpej Exp $
 
-SUBDIR+= mutex1 mutex2 mutex3 mutex4 sem
+SUBDIR+= barrier1 mutex1 mutex2 mutex3 mutex4 sem
 
 .include <bsd.subdir.mk>
diff -r ef7b2482c532 -r 23e08d9d549b regress/lib/libpthread/barrier1/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/barrier1/Makefile  Thu Jan 30 18:23:09 2003 +0000
@@ -0,0 +1,15 @@
+#      $NetBSD: Makefile,v 1.1 2003/01/30 18:23:09 thorpej Exp $
+
+WARNS=1
+
+PROG=   barrier1
+SRCS=   barrier1.c
+
+LDADD= -lpthread
+
+NOMAN=
+
+regress:
+       ./barrier1
+
+.include <bsd.prog.mk>
diff -r ef7b2482c532 -r 23e08d9d549b regress/lib/libpthread/barrier1/barrier1.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/barrier1/barrier1.c        Thu Jan 30 18:23:09 2003 +0000
@@ -0,0 +1,72 @@
+/*     $NetBSD: barrier1.c,v 1.1 2003/01/30 18:23:09 thorpej Exp $     */
+
+#include <assert.h>
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void *threadfunc(void *arg);
+
+pthread_barrier_t barrier;
+pthread_mutex_t mutex;
+int serial_count;
+int after_barrier_count;
+
+#define        COUNT   10
+
+int
+main(int argc, char *argv[])
+{
+       int i, ret;
+       pthread_t new[COUNT];
+       void *joinval;
+
+       pthread_mutex_init(&mutex, NULL);
+
+       ret = pthread_barrier_init(&barrier, NULL, COUNT);
+       if (ret != 0)
+               err(1, "pthread_barrier_init");
+
+       for (i = 0; i < COUNT; i++) {
+               ret = pthread_create(&new[i], NULL, threadfunc,
+                   (void *)(long)i);
+               if (ret != 0)
+                       err(1, "pthread_create");
+               sleep(2);
+       }
+
+       for (i = 0; i < COUNT; i++) {
+               pthread_mutex_lock(&mutex);
+               assert(after_barrier_count >= (COUNT - i));
+               pthread_mutex_unlock(&mutex);
+               ret = pthread_join(new[i], &joinval);
+               if (ret != 0)
+                       err(1, "pthread_join");
+               printf("main joined with thread %d\n", i);
+       }
+
+       assert(serial_count == 1);
+
+       return 0;
+}
+
+void *
+threadfunc(void *arg)
+{
+       int which = (int)(long)arg;
+       int ret;
+
+       printf("thread %d entering barrier\n", which);
+       ret = pthread_barrier_wait(&barrier);
+       printf("thread %d leaving barrier -> %d\n", which, ret);
+       
+       pthread_mutex_lock(&mutex);
+       after_barrier_count++;
+       if (ret == PTHREAD_BARRIER_SERIAL_THREAD)
+               serial_count++;
+       pthread_mutex_unlock(&mutex);
+
+       return NULL;
+}



Home | Main Index | Thread Index | Old Index