1/* $NetBSD: test_rwlock1.c,v 1.5 2008/03/28 20:28:27 ad Exp $ */ 2 3/*- 4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 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. 18 * 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. 30 */ 31 32#include <sys/cdefs.h> 33__KERNEL_RCSID(0, "$NetBSD: test_rwlock1.c,v 1.5 2008/03/28 20:28:27 ad Exp $"); 34 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> 41 42#define NTHREADS 32 43#define SECONDS 60 44 45int testcall(struct lwp *, void *, register_t *); 46void thread1(void *); 47void thread_exit(void); 48 49lwp_t *test_threads[NTHREADS]; 50kmutex_t test_mutex; 51krwlock_t test_rwlock; 52kcondvar_t test_cv; 53int test_count; 54int test_exit; 55 56static 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, 65}; 66 67void 68thread_exit(void) 69{ 70 71 mutex_enter(&test_mutex); 72 if (--test_count == 0) 73 cv_signal(&test_cv); 74 mutex_exit(&test_mutex); 75 kthread_exit(0); 76} 77 78void 79thread1(void *cookie) 80{ 81 int count; 82 krw_t op; 83 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(); 95 } 96 97 thread_exit(); 98} 99 100int 101testcall(struct lwp *l, void *uap, register_t *retval) 102{ 103 int i; 104 105 mutex_init(&test_mutex, MUTEX_DEFAULT, IPL_NONE); 106 rw_init(&test_rwlock); 107 cv_init(&test_cv, "testcv"); 108 109 printf("test: creating threads\n"); 110 111 test_count = NTHREADS; 112 test_exit = 0; 113 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); 117 118 printf("test: sleeping\n"); 119 120 mutex_enter(&test_mutex); 121 while (test_count != 0) { 122 (void)cv_timedwait(&test_cv, &test_mutex, hz * SECONDS); 123 test_exit = 1; 124 } 125 mutex_exit(&test_mutex); 126 127 printf("test: finished\n"); 128 129 cv_destroy(&test_cv); 130 rw_destroy(&test_rwlock); 131 mutex_destroy(&test_mutex); 132 133 return 0; 134} 135