xref: /titanic_54/usr/src/uts/common/os/rwstlock.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/rwstlock.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/lockstat.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /*
37*7c478bd9Sstevel@tonic-gate  * Alternate rwlock that is interruptible and can be released by a thread
38*7c478bd9Sstevel@tonic-gate  * other than the one that acquired the lock.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * There is no priority inheritance mechanism for these locks.
41*7c478bd9Sstevel@tonic-gate  * Writers have priority over readers, so reader starvation is possible.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * Common code to grab a lock.  There are three cases:
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * (1) If RWST_TRYENTER is set, we try the lock without blocking.
48*7c478bd9Sstevel@tonic-gate  *     In this case we return 1 on success, 0 on failure.
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * (2) If RWST_SIG is set, we block interruptibly until we get the lock.
51*7c478bd9Sstevel@tonic-gate  *     In this case we return 0 on success, EINTR if we're interrupted.
52*7c478bd9Sstevel@tonic-gate  *
53*7c478bd9Sstevel@tonic-gate  * (3) If neither flag is set, we block uninterruptibly until we get the lock.
54*7c478bd9Sstevel@tonic-gate  *     In this case we return 0 (we always succeed).
55*7c478bd9Sstevel@tonic-gate  */
56*7c478bd9Sstevel@tonic-gate static int
57*7c478bd9Sstevel@tonic-gate rwst_enter_common(rwstlock_t *l, krw_t rw, int flags)
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	hrtime_t sleep_time;
60*7c478bd9Sstevel@tonic-gate 	int writer;
61*7c478bd9Sstevel@tonic-gate 	intptr_t readers;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	mutex_enter(&l->rwst_lock);
64*7c478bd9Sstevel@tonic-gate 	if (rw == RW_READER) {
65*7c478bd9Sstevel@tonic-gate 		while (RWST_WRITE_HELD(l) || RWST_WRITE_WANTED(l)) {
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 			if (flags & RWST_TRYENTER) {
68*7c478bd9Sstevel@tonic-gate 				mutex_exit(&l->rwst_lock);
69*7c478bd9Sstevel@tonic-gate 				return (0);
70*7c478bd9Sstevel@tonic-gate 			}
71*7c478bd9Sstevel@tonic-gate 			if (panicstr)
72*7c478bd9Sstevel@tonic-gate 				return (0);
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 			if (RWST_WRITE_HELD(l)) {
75*7c478bd9Sstevel@tonic-gate 				writer = 1;
76*7c478bd9Sstevel@tonic-gate 				readers = 0;
77*7c478bd9Sstevel@tonic-gate 			} else {
78*7c478bd9Sstevel@tonic-gate 				writer = 0;
79*7c478bd9Sstevel@tonic-gate 				readers = l->rwst_count;
80*7c478bd9Sstevel@tonic-gate 			}
81*7c478bd9Sstevel@tonic-gate 			sleep_time = -gethrtime();
82*7c478bd9Sstevel@tonic-gate 			if (!RWST_READ_WAIT(l, flags)) {
83*7c478bd9Sstevel@tonic-gate 				mutex_exit(&l->rwst_lock);
84*7c478bd9Sstevel@tonic-gate 				return (EINTR);
85*7c478bd9Sstevel@tonic-gate 			}
86*7c478bd9Sstevel@tonic-gate 			sleep_time += gethrtime();
87*7c478bd9Sstevel@tonic-gate 			LOCKSTAT_RECORD4(LS_RW_ENTER_BLOCK, l, sleep_time, rw,
88*7c478bd9Sstevel@tonic-gate 			    writer, readers);
89*7c478bd9Sstevel@tonic-gate 		}
90*7c478bd9Sstevel@tonic-gate 		RWST_READ_ENTER(l);
91*7c478bd9Sstevel@tonic-gate 		LOCKSTAT_RECORD(LS_RW_ENTER_ACQUIRE, l, rw);
92*7c478bd9Sstevel@tonic-gate 	} else {
93*7c478bd9Sstevel@tonic-gate 		ASSERT(rw == RW_WRITER);
94*7c478bd9Sstevel@tonic-gate 		while (RWST_HELD(l)) {
95*7c478bd9Sstevel@tonic-gate 			if (flags & RWST_TRYENTER) {
96*7c478bd9Sstevel@tonic-gate 				mutex_exit(&l->rwst_lock);
97*7c478bd9Sstevel@tonic-gate 				return (0);
98*7c478bd9Sstevel@tonic-gate 			}
99*7c478bd9Sstevel@tonic-gate 			if (panicstr)
100*7c478bd9Sstevel@tonic-gate 				return (0);
101*7c478bd9Sstevel@tonic-gate 			if (RWST_WRITE_HELD(l)) {
102*7c478bd9Sstevel@tonic-gate 				writer = 1;
103*7c478bd9Sstevel@tonic-gate 				readers = 0;
104*7c478bd9Sstevel@tonic-gate 			} else {
105*7c478bd9Sstevel@tonic-gate 				writer = 0;
106*7c478bd9Sstevel@tonic-gate 				readers = l->rwst_count;
107*7c478bd9Sstevel@tonic-gate 			}
108*7c478bd9Sstevel@tonic-gate 			sleep_time = -gethrtime();
109*7c478bd9Sstevel@tonic-gate 			if (!RWST_WRITE_WAIT(l, flags)) {
110*7c478bd9Sstevel@tonic-gate 				if (!RWST_WRITE_HELD(l) &&
111*7c478bd9Sstevel@tonic-gate 				    !RWST_WRITE_WANTED(l))
112*7c478bd9Sstevel@tonic-gate 					RWST_READ_WAKE_ALL(l);
113*7c478bd9Sstevel@tonic-gate 				mutex_exit(&l->rwst_lock);
114*7c478bd9Sstevel@tonic-gate 				return (EINTR);
115*7c478bd9Sstevel@tonic-gate 			}
116*7c478bd9Sstevel@tonic-gate 			sleep_time += gethrtime();
117*7c478bd9Sstevel@tonic-gate 			LOCKSTAT_RECORD4(LS_RW_ENTER_BLOCK, l, sleep_time, rw,
118*7c478bd9Sstevel@tonic-gate 			    writer, readers);
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 		RWST_WRITE_ENTER(l);
121*7c478bd9Sstevel@tonic-gate 		LOCKSTAT_RECORD(LS_RW_ENTER_ACQUIRE, l, rw);
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 	mutex_exit(&l->rwst_lock);
124*7c478bd9Sstevel@tonic-gate 	return (flags & RWST_TRYENTER);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate void
128*7c478bd9Sstevel@tonic-gate rwst_exit(rwstlock_t *l)
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	mutex_enter(&l->rwst_lock);
131*7c478bd9Sstevel@tonic-gate 	if (RWST_WRITE_HELD(l)) {
132*7c478bd9Sstevel@tonic-gate 		LOCKSTAT_RECORD(LS_RW_EXIT_RELEASE, l, RW_WRITER);
133*7c478bd9Sstevel@tonic-gate 		RWST_WRITE_EXIT(l);
134*7c478bd9Sstevel@tonic-gate 	} else {
135*7c478bd9Sstevel@tonic-gate 		ASSERT(RWST_READ_HELD(l));
136*7c478bd9Sstevel@tonic-gate 		LOCKSTAT_RECORD(LS_RW_EXIT_RELEASE, l, RW_READER);
137*7c478bd9Sstevel@tonic-gate 		RWST_READ_EXIT(l);
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	if (!RWST_WRITE_WANTED(l))
140*7c478bd9Sstevel@tonic-gate 		RWST_READ_WAKE_ALL(l);
141*7c478bd9Sstevel@tonic-gate 	else if (!RWST_HELD(l))
142*7c478bd9Sstevel@tonic-gate 		RWST_WRITE_WAKE_ONE(l);
143*7c478bd9Sstevel@tonic-gate 	mutex_exit(&l->rwst_lock);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate void
147*7c478bd9Sstevel@tonic-gate rwst_enter(rwstlock_t *l, krw_t rw)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	(void) rwst_enter_common(l, rw, 0);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate int
153*7c478bd9Sstevel@tonic-gate rwst_enter_sig(rwstlock_t *l, krw_t rw)
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	return (rwst_enter_common(l, rw, RWST_SIG));
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate int
159*7c478bd9Sstevel@tonic-gate rwst_tryenter(rwstlock_t *l, krw_t rw)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	return (rwst_enter_common(l, rw, RWST_TRYENTER));
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate int
165*7c478bd9Sstevel@tonic-gate rwst_lock_held(rwstlock_t *l, krw_t rw)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	if (rw == RW_READER)
168*7c478bd9Sstevel@tonic-gate 		return (RWST_READ_HELD(l));
169*7c478bd9Sstevel@tonic-gate 	ASSERT(rw == RW_WRITER);
170*7c478bd9Sstevel@tonic-gate 	return (RWST_WRITE_OWNER(l));
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
174*7c478bd9Sstevel@tonic-gate void
175*7c478bd9Sstevel@tonic-gate rwst_init(rwstlock_t *l, char *name, krw_type_t krw_t, void *arg)
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	l->rwst_count = 0;
178*7c478bd9Sstevel@tonic-gate 	mutex_init(&l->rwst_lock, NULL, MUTEX_DEFAULT, NULL);
179*7c478bd9Sstevel@tonic-gate 	cv_init(&l->rwst_rcv, NULL, CV_DEFAULT, NULL);
180*7c478bd9Sstevel@tonic-gate 	cv_init(&l->rwst_wcv, NULL, CV_DEFAULT, NULL);
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate void
184*7c478bd9Sstevel@tonic-gate rwst_destroy(rwstlock_t *l)
185*7c478bd9Sstevel@tonic-gate {
186*7c478bd9Sstevel@tonic-gate 	ASSERT(l->rwst_count == 0);
187*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&l->rwst_lock);
188*7c478bd9Sstevel@tonic-gate 	cv_destroy(&l->rwst_rcv);
189*7c478bd9Sstevel@tonic-gate 	cv_destroy(&l->rwst_wcv);
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate struct _kthread *
193*7c478bd9Sstevel@tonic-gate rwst_owner(rwstlock_t *l)
194*7c478bd9Sstevel@tonic-gate {
195*7c478bd9Sstevel@tonic-gate 	return (RWST_OWNER(l));
196*7c478bd9Sstevel@tonic-gate }
197