Sync usage with man page.
[netbsd-mini2440.git] / regress / sys / kern / mutex1 / test_mutex1.c
blob2eb2328164585fa9751366a97ea29c1eb8f090c5
1 /* $NetBSD: test_mutex1.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_mutex1.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 9
43 #define SECONDS 60
45 int testcall(struct lwp *, void *, register_t *);
46 void thread1(void *);
47 void thread2(void *);
48 void thread3(void *);
49 void thread_exit(void);
51 lwp_t *test_threads[NTHREADS];
52 kmutex_t test_mutex;
53 kcondvar_t test_cv;
54 int test_count;
55 int test_exit;
57 void
58 thread_exit(void)
61 mutex_enter(&test_mutex);
62 if (--test_count == 0)
63 cv_signal(&test_cv);
64 mutex_exit(&test_mutex);
65 kthread_exit(0);
69 * test_mutex -> kernel_lock
71 void
72 thread1(void *cookie)
74 int count;
76 for (count = 0; !test_exit; count++) {
77 mutex_enter(&test_mutex);
78 KERNEL_LOCK(1, curlwp);
79 if ((count % 11) == 0)
80 yield();
81 mutex_exit(&test_mutex);
82 KERNEL_UNLOCK_ONE(curlwp);
83 if ((count % 17) == 0)
84 yield();
87 thread_exit();
91 * kernel_lock -> test_mutex
93 void
94 thread2(void *cookie)
96 int count;
98 for (count = 0; !test_exit; count++) {
99 KERNEL_LOCK(1, curlwp);
100 mutex_enter(&test_mutex);
101 if ((count % 401) == 0)
102 yield();
103 KERNEL_UNLOCK_ONE(curlwp);
104 mutex_exit(&test_mutex);
105 if ((count % 19) == 0)
106 yield();
109 thread_exit();
113 * test_mutex without kernel_lock, to provide pressure
115 void
116 thread3(void *cookie)
118 int count;
120 for (count = 0; !test_exit; count++) {
121 mutex_enter(&test_mutex);
122 DELAY(10);
123 if ((count % 101) == 0)
124 yield();
125 mutex_exit(&test_mutex);
126 if ((count % 23) == 0)
127 yield();
130 thread_exit();
134 testcall(struct lwp *l, void *uap, register_t *retval)
136 void (*func)(void *);
137 int i;
139 mutex_init(&test_mutex, MUTEX_DEFAULT, IPL_NONE);
140 cv_init(&test_cv, "testcv");
142 printf("test: creating threads\n");
144 test_count = NTHREADS;
145 test_exit = 0;
147 for (i = 0; i < test_count; i++) {
148 switch (i % 3) {
149 case 0:
150 func = thread1;
151 break;
152 case 1:
153 func = thread2;
154 break;
155 case 2:
156 func = thread3;
157 break;
159 kthread_create(0, KTHREAD_MPSAFE, NULL, func, NULL,
160 &test_threads[i], "thread%d", i);
163 printf("test: sleeping\n");
165 mutex_enter(&test_mutex);
166 while (test_count != 0) {
167 (void)cv_timedwait(&test_cv, &test_mutex, hz * SECONDS);
168 test_exit = NTHREADS;
170 mutex_exit(&test_mutex);
172 printf("test: finished\n");
174 cv_destroy(&test_cv);
175 mutex_destroy(&test_mutex);
177 return 0;