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