1 /* $NetBSD: test_rwlock1.c,v 1.5 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_rwlock1.c,v 1.5 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
*);
47 void thread_exit(void);
49 lwp_t
*test_threads
[NTHREADS
];
51 krwlock_t test_rwlock
;
56 static int primes
[NTHREADS
] = {
71 mutex_enter(&test_mutex
);
72 if (--test_count
== 0)
74 mutex_exit(&test_mutex
);
84 for (count
= 0; !test_exit
; count
++) {
85 if (count
% *(int *)cookie
== 0)
89 rw_enter(&test_rwlock
, op
);
90 if ((arc4random() % 11) == 0)
92 rw_exit(&test_rwlock
);
93 if ((arc4random() % 23) == 0)
101 testcall(struct lwp
*l
, void *uap
, register_t
*retval
)
105 mutex_init(&test_mutex
, MUTEX_DEFAULT
, IPL_NONE
);
106 rw_init(&test_rwlock
);
107 cv_init(&test_cv
, "testcv");
109 printf("test: creating threads\n");
111 test_count
= NTHREADS
;
114 for (i
= 0; i
< test_count
; i
++)
115 kthread_create(0, KTHREAD_MPSAFE
, NULL
, thread1
, &primes
[i
],
116 &test_threads
[i
], "thread%d", i
);
118 printf("test: sleeping\n");
120 mutex_enter(&test_mutex
);
121 while (test_count
!= 0) {
122 (void)cv_timedwait(&test_cv
, &test_mutex
, hz
* SECONDS
);
125 mutex_exit(&test_mutex
);
127 printf("test: finished\n");
129 cv_destroy(&test_cv
);
130 rw_destroy(&test_rwlock
);
131 mutex_destroy(&test_mutex
);