Source-Changes-HG archive

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

[src/trunk]: src/regress/lib/libpthread/preempt1 Add a test for kernel preemp...



details:   https://anonhg.NetBSD.org/src/rev/51cb668ec786
branches:  trunk
changeset: 542519:51cb668ec786
user:      skrll <skrll%NetBSD.org@localhost>
date:      Fri Jan 31 20:14:25 2003 +0000

description:
Add a test for kernel preemption during a large uiomove.

Per discussion with Jason.

diffstat:

 regress/lib/libpthread/preempt1/Makefile   |  16 +++++
 regress/lib/libpthread/preempt1/preempt1.c |  89 ++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+), 0 deletions(-)

diffs (113 lines):

diff -r 0da95e4ad8ea -r 51cb668ec786 regress/lib/libpthread/preempt1/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/preempt1/Makefile  Fri Jan 31 20:14:25 2003 +0000
@@ -0,0 +1,16 @@
+#      $NetBSD: Makefile,v 1.1 2003/01/31 20:14:25 skrll Exp $
+
+WARNS=1
+
+PROG=   preempt1
+SRCS=   preempt1.c
+
+LDADD= -lpthread
+
+NOMAN=
+
+regress:
+       @echo Testing kernel preemption during a large uiomove.
+       ./preempt1
+
+.include <bsd.prog.mk>
diff -r 0da95e4ad8ea -r 51cb668ec786 regress/lib/libpthread/preempt1/preempt1.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/regress/lib/libpthread/preempt1/preempt1.c        Fri Jan 31 20:14:25 2003 +0000
@@ -0,0 +1,89 @@
+/*     $NetBSD: preempt1.c,v 1.1 2003/01/31 20:14:25 skrll Exp $       */
+
+#include <assert.h>
+#include <err.h>
+#include <fcntl.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 started;
+
+#define HUGE_BUFFER    1<<20
+#define NTHREADS       1
+
+int
+main(int argc, char *argv[])
+{
+       int ret, i;
+       pthread_t new;
+       void *joinval;
+
+       char *mem;
+       int fd;
+
+       mem = malloc(HUGE_BUFFER);
+       if (mem == NULL)
+               err(1, "malloc");
+
+       fd = open("/dev/urandom", O_RDONLY, 0);
+       if (fd == 0)
+               err(1, "open");
+
+       printf("1: preempt test\n");
+
+       pthread_cond_init(&cond, NULL);
+       pthread_mutex_lock(&mutex);
+
+       started = 0;
+
+       for (i = 0; i < NTHREADS; i++) {
+               ret = pthread_create(&new, NULL, threadfunc, NULL);
+               if (ret != 0)
+                       err(1, "pthread_create");
+       }
+
+       while (started < NTHREADS) {
+               pthread_cond_wait(&cond, &mutex);
+       }
+
+       printf("1: Thread has started.\n");
+
+       pthread_mutex_unlock(&mutex);
+       printf("1: After releasing the mutex.\n");
+
+       ret = read(fd, mem, HUGE_BUFFER);
+       close(fd);
+
+       assert(ret == HUGE_BUFFER);
+
+       ret = pthread_join(new, &joinval);
+       if (ret != 0)
+               err(1, "pthread_join");
+
+       printf("1: Thread joined.\n");
+
+       return 0;
+}
+
+void *
+threadfunc(void *arg)
+{
+       printf("2: Second thread.\n");
+
+       printf("2: Locking mutex\n");
+       pthread_mutex_lock(&mutex);
+       printf("2: Got mutex.\n");
+       started++;
+       
+       pthread_mutex_unlock(&mutex);
+       pthread_cond_signal(&cond);
+       sleep(1);
+
+       return NULL;
+}



Home | Main Index | Thread Index | Old Index