1 /* $NetBSD: test_mutex1.c,v 1.4 2008/03/28 20:28:27 ad Exp $ */
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
39 #include <sys/kthread.h>
40 #include <sys/kernel.h>
45 int testcall(struct lwp
*, void *, register_t
*);
49 void thread_exit(void);
51 lwp_t
*test_threads
[NTHREADS
];
61 mutex_enter(&test_mutex
);
62 if (--test_count
== 0)
64 mutex_exit(&test_mutex
);
69 * test_mutex -> kernel_lock
76 for (count
= 0; !test_exit
; count
++) {
77 mutex_enter(&test_mutex
);
78 KERNEL_LOCK(1, curlwp
);
79 if ((count
% 11) == 0)
81 mutex_exit(&test_mutex
);
82 KERNEL_UNLOCK_ONE(curlwp
);
83 if ((count
% 17) == 0)
91 * kernel_lock -> test_mutex
98 for (count
= 0; !test_exit
; count
++) {
99 KERNEL_LOCK(1, curlwp
);
100 mutex_enter(&test_mutex
);
101 if ((count
% 401) == 0)
103 KERNEL_UNLOCK_ONE(curlwp
);
104 mutex_exit(&test_mutex
);
105 if ((count
% 19) == 0)
113 * test_mutex without kernel_lock, to provide pressure
116 thread3(void *cookie
)
120 for (count
= 0; !test_exit
; count
++) {
121 mutex_enter(&test_mutex
);
123 if ((count
% 101) == 0)
125 mutex_exit(&test_mutex
);
126 if ((count
% 23) == 0)
134 testcall(struct lwp
*l
, void *uap
, register_t
*retval
)
136 void (*func
)(void *);
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
;
147 for (i
= 0; i
< test_count
; i
++) {
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
);