Sync usage with man page.
[netbsd-mini2440.git] / regress / sys / kern / rwlock1 / test_rwlock1.c
blob66d36e061546bcf599b81110fc45f57a1cf1c1c7
1 /* $NetBSD: test_rwlock1.c,v 1.5 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_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>
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_exit(void);
49 lwp_t *test_threads[NTHREADS];
50 kmutex_t test_mutex;
51 krwlock_t test_rwlock;
52 kcondvar_t test_cv;
53 int test_count;
54 int test_exit;
56 static int primes[NTHREADS] = {
57 17, 19, 23, 29,
58 31, 37, 41, 43,
59 47, 53, 59, 61,
60 67, 71, 73, 79,
61 83, 89, 97, 101,
62 103, 107, 109, 113,
63 127, 131, 137, 139,
64 149, 151, 157, 163,
67 void
68 thread_exit(void)
71 mutex_enter(&test_mutex);
72 if (--test_count == 0)
73 cv_signal(&test_cv);
74 mutex_exit(&test_mutex);
75 kthread_exit(0);
78 void
79 thread1(void *cookie)
81 int count;
82 krw_t op;
84 for (count = 0; !test_exit; count++) {
85 if (count % *(int *)cookie == 0)
86 op = RW_WRITER;
87 else
88 op = RW_READER;
89 rw_enter(&test_rwlock, op);
90 if ((arc4random() % 11) == 0)
91 yield();
92 rw_exit(&test_rwlock);
93 if ((arc4random() % 23) == 0)
94 yield();
97 thread_exit();
101 testcall(struct lwp *l, void *uap, register_t *retval)
103 int i;
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;
112 test_exit = 0;
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);
123 test_exit = 1;
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);
133 return 0;