Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / sys / kern / mutex2 / test_mutex2.c
blob34e76cc761ae5d34965dfdfe8b4ff02702e422f6
1 /* $NetBSD: test_mutex2.c,v 1.4 2008/03/28 20:28:27 ad Exp $ */
3 /*-
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: test_mutex2.c,v 1.4 2008/03/28 20:28:27 ad Exp $");
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/kthread.h>
40 #include <sys/kernel.h>
42 #define NTHREADS 32
43 #define SECONDS 60
45 int testcall(struct lwp *, void *, register_t *);
46 void thread1(void *);
47 void thread_enter(void);
49 lwp_t *test_threads[NTHREADS];
50 kmutex_t test_mutex;
51 kcondvar_t test_cv;
52 int test_count;
53 int test_exit;
55 static int primes[NTHREADS] = {
56 131, 137, 139, 149,
57 151, 157, 163, 167,
58 173, 179, 181, 191,
59 193, 197, 199, 211,
60 223, 227, 229, 233,
61 239, 241, 251, 257,
62 263, 269, 271, 277,
63 281, 283, 293, 307,
66 void
67 thread_exit(void)
70 mutex_enter(&test_mutex);
71 if (--test_count == 0)
72 cv_signal(&test_cv);
73 mutex_exit(&test_mutex);
74 kthread_exit(0);
77 void
78 thread1(void *cookie)
80 int count;
82 for (count = 0; !test_exit; count++) {
83 mutex_enter(&test_mutex);
84 if ((count % *(int *)cookie) == 0)
85 yield();
86 mutex_exit(&test_mutex);
87 if ((count % curproc->p_pid) == 0)
88 yield();
92 int
93 testcall(struct lwp *l, void *uap, register_t *retval)
95 int i;
97 mutex_init(&test_mutex, MUTEX_DEFAULT, IPL_NONE);
98 cv_init(&test_cv, "testcv");
100 printf("test: creating threads\n");
102 test_count = NTHREADS;
103 test_exit = 0;
105 for (i = 0; i < test_count; i++)
106 kthread_create(0, KTHREAD_MPSAFE, NULL, thread1, &primes[i],
107 &test_threads[i], "thread%d", i);
109 printf("test: sleeping\n");
111 mutex_enter(&test_mutex);
112 while (test_count != 0) {
113 (void)cv_timedwait(&test_cv, &test_mutex, hz * SECONDS);
114 test_exit = 1;
116 mutex_exit(&test_mutex);
118 printf("test: finished\n");
120 cv_destroy(&test_cv);
121 mutex_destroy(&test_mutex);
123 return 0;