Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/regress/lib/libpthread/condcancel1 Regression test for destr...
details: https://anonhg.NetBSD.org/src/rev/66e341337595
branches: trunk
changeset: 555558:66e341337595
user: nathanw <nathanw%NetBSD.org@localhost>
date: Fri Nov 21 19:24:01 2003 +0000
description:
Regression test for destroying a condition variable that had a cancelled
wait.
diffstat:
regress/lib/libpthread/condcancel1/Makefile | 15 ++++
regress/lib/libpthread/condcancel1/condcancel1.c | 89 ++++++++++++++++++++++++
2 files changed, 104 insertions(+), 0 deletions(-)
diffs (112 lines):
diff -r 302a4513587e -r 66e341337595 regress/lib/libpthread/condcancel1/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/condcancel1/Makefile Fri Nov 21 19:24:01 2003 +0000
@@ -0,0 +1,15 @@
+# $Id: Makefile,v 1.1 2003/11/21 19:24:01 nathanw Exp $
+
+WARNS=2
+
+PROG= condcancel1
+SRCS= condcancel1.c
+
+LDADD= -lpthread
+
+NOMAN=
+
+regress:
+ ./condcancel1
+
+.include <bsd.prog.mk>
diff -r 302a4513587e -r 66e341337595 regress/lib/libpthread/condcancel1/condcancel1.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/condcancel1/condcancel1.c Fri Nov 21 19:24:01 2003 +0000
@@ -0,0 +1,89 @@
+#include <err.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <string.h>
+
+void *threadfunc(void *);
+void unlock(void *);
+
+pthread_mutex_t mutex;
+pthread_cond_t cond;
+int share;
+
+int
+main(int argc, char *argv[])
+{
+ pthread_t thread;
+ int ret;
+
+ printf("Test of CV state after cancelling a wait\n");
+
+ ret = pthread_mutex_init(&mutex, NULL);
+ if (ret) errx(1, "pthread_mutex_init: %s", strerror(ret));
+
+ ret = pthread_cond_init(&cond, NULL);
+ if (ret) errx(1, "pthread_cond_init: %s", strerror(ret));
+
+ ret = pthread_mutex_lock(&mutex);
+ if (ret) errx(1, "pthread_mutex_lock: %s", strerror(ret));
+
+ ret = pthread_create(&thread, NULL, threadfunc, NULL);
+ if (ret) errx(1, "pthread_create: %s", strerror(ret));
+
+ while (share == 0) {
+ ret = pthread_cond_wait(&cond, &mutex);
+ if (ret) errx(1, "pthread_cond_wait: %s", strerror(ret));
+ }
+
+ ret = pthread_mutex_unlock(&mutex);
+ if (ret) errx(1, "pthread_mutex_unlock: %s", strerror(ret));
+
+ ret = pthread_cancel(thread);
+ if (ret) errx(1, "pthread_cancel: %s", strerror(ret));
+
+ ret = pthread_join(thread, NULL);
+ if (ret) errx(1, "pthread_join: %s", strerror(ret));
+
+ ret = pthread_cond_destroy(&cond);
+ if (ret) errx(1, "pthread_cond_destroy: %s", strerror(ret));
+
+ printf("CV successfully destroyed.\n");
+
+ ret = pthread_mutex_destroy(&mutex);
+ if (ret) errx(1, "pthread_mutex_destroy: %s", strerror(ret));
+
+ return 0;
+}
+
+void *
+threadfunc(void *arg)
+{
+ int ret;
+
+ ret = pthread_mutex_lock(&mutex);
+ if (ret) errx(1, "pthread_mutex_lock: %s", strerror(ret));
+
+ pthread_cleanup_push(unlock, &mutex);
+
+ while (1) {
+ share = 1;
+ ret = pthread_cond_broadcast(&cond);
+ if (ret) errx(1, "pthread_cond_broadcast: %s", strerror(ret));
+ ret = pthread_cond_wait(&cond, &mutex);
+ if (ret) errx(1, "pthread_cond_wait: %s", strerror(ret));
+ }
+
+ pthread_cleanup_pop(0);
+ ret = pthread_mutex_unlock(&mutex);
+ if (ret) errx(1, "pthread_mutex_unlock: %s", strerror(ret));
+
+
+
+ return NULL;
+}
+
+void
+unlock(void *arg)
+{
+ pthread_mutex_unlock((pthread_mutex_t *)arg);
+}
Home |
Main Index |
Thread Index |
Old Index