4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/refcount.h>
29 #include <sys/rrwlock.h>
32 * This file contains the implementation of a re-entrant read
33 * reader/writer lock (aka "rrwlock").
35 * This is a normal reader/writer lock with the additional feature
36 * of allowing threads who have already obtained a read lock to
37 * re-enter another read lock (re-entrant read) - even if there are
40 * Callers who have not obtained a read lock give waiting writers priority.
42 * The rrwlock_t lock does not allow re-entrant writers, nor does it
43 * allow a re-entrant mix of reads and writes (that is, it does not
44 * allow a caller who has already obtained a read lock to be able to
45 * then grab a write lock without first dropping all read locks, and
48 * The rrwlock_t uses tsd (thread specific data) to keep a list of
49 * nodes (rrw_node_t), where each node keeps track of which specific
50 * lock (rrw_node_t::rn_rrl) the thread has grabbed. Since re-entering
51 * should be rare, a thread that grabs multiple reads on the same rrwlock_t
52 * will store multiple rrw_node_ts of the same 'rrn_rrl'. Nodes on the
53 * tsd list can represent a different rrwlock_t. This allows a thread
54 * to enter multiple and unique rrwlock_ts for read locks at the same time.
56 * Since using tsd exposes some overhead, the rrwlock_t only needs to
57 * keep tsd data when writers are waiting. If no writers are waiting, then
58 * a reader just bumps the anonymous read count (rr_anon_rcount) - no tsd
59 * is needed. Once a writer attempts to grab the lock, readers then
60 * keep tsd data and bump the linked readers count (rr_linked_rcount).
62 * If there are waiting writers and there are anonymous readers, then a
63 * reader doesn't know if it is a re-entrant lock. But since it may be one,
64 * we allow the read to proceed (otherwise it could deadlock). Since once
65 * waiting writers are active, readers no longer bump the anonymous count,
66 * the anonymous readers will eventually flush themselves out. At this point,
67 * readers will be able to tell if they are a re-entrant lock (have a
68 * rrw_node_t entry for the lock) or not. If they are a re-entrant lock, then
69 * we must let the proceed. If they are not, then the reader blocks for the
70 * waiting writers. Hence, we do not starve writers.
73 /* global key for TSD */
76 typedef struct rrw_node
{
77 struct rrw_node
*rn_next
;
82 rrn_find(rrwlock_t
*rrl
)
86 if (refcount_count(&rrl
->rr_linked_rcount
) == 0)
89 for (rn
= tsd_get(rrw_tsd_key
); rn
!= NULL
; rn
= rn
->rn_next
) {
90 if (rn
->rn_rrl
== rrl
)
97 * Add a node to the head of the singly linked list.
100 rrn_add(rrwlock_t
*rrl
)
104 rn
= kmem_alloc(sizeof (*rn
), KM_SLEEP
);
106 rn
->rn_next
= tsd_get(rrw_tsd_key
);
107 VERIFY(tsd_set(rrw_tsd_key
, rn
) == 0);
111 * If a node is found for 'rrl', then remove the node from this
112 * thread's list and return TRUE; otherwise return FALSE.
115 rrn_find_and_remove(rrwlock_t
*rrl
)
118 rrw_node_t
*prev
= NULL
;
120 if (refcount_count(&rrl
->rr_linked_rcount
) == 0)
123 for (rn
= tsd_get(rrw_tsd_key
); rn
!= NULL
; rn
= rn
->rn_next
) {
124 if (rn
->rn_rrl
== rrl
) {
126 prev
->rn_next
= rn
->rn_next
;
128 VERIFY(tsd_set(rrw_tsd_key
, rn
->rn_next
) == 0);
129 kmem_free(rn
, sizeof (*rn
));
138 rrw_init(rrwlock_t
*rrl
)
140 mutex_init(&rrl
->rr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
141 cv_init(&rrl
->rr_cv
, NULL
, CV_DEFAULT
, NULL
);
142 rrl
->rr_writer
= NULL
;
143 refcount_create(&rrl
->rr_anon_rcount
);
144 refcount_create(&rrl
->rr_linked_rcount
);
145 rrl
->rr_writer_wanted
= B_FALSE
;
149 rrw_destroy(rrwlock_t
*rrl
)
151 mutex_destroy(&rrl
->rr_lock
);
152 cv_destroy(&rrl
->rr_cv
);
153 ASSERT(rrl
->rr_writer
== NULL
);
154 refcount_destroy(&rrl
->rr_anon_rcount
);
155 refcount_destroy(&rrl
->rr_linked_rcount
);
159 rrw_enter_read(rrwlock_t
*rrl
, void *tag
)
161 mutex_enter(&rrl
->rr_lock
);
162 ASSERT(rrl
->rr_writer
!= curthread
);
163 ASSERT(refcount_count(&rrl
->rr_anon_rcount
) >= 0);
165 while (rrl
->rr_writer
|| (rrl
->rr_writer_wanted
&&
166 refcount_is_zero(&rrl
->rr_anon_rcount
) &&
167 rrn_find(rrl
) == NULL
))
168 cv_wait(&rrl
->rr_cv
, &rrl
->rr_lock
);
170 if (rrl
->rr_writer_wanted
) {
171 /* may or may not be a re-entrant enter */
173 (void) refcount_add(&rrl
->rr_linked_rcount
, tag
);
175 (void) refcount_add(&rrl
->rr_anon_rcount
, tag
);
177 ASSERT(rrl
->rr_writer
== NULL
);
178 mutex_exit(&rrl
->rr_lock
);
182 rrw_enter_write(rrwlock_t
*rrl
)
184 mutex_enter(&rrl
->rr_lock
);
185 ASSERT(rrl
->rr_writer
!= curthread
);
187 while (refcount_count(&rrl
->rr_anon_rcount
) > 0 ||
188 refcount_count(&rrl
->rr_linked_rcount
) > 0 ||
189 rrl
->rr_writer
!= NULL
) {
190 rrl
->rr_writer_wanted
= B_TRUE
;
191 cv_wait(&rrl
->rr_cv
, &rrl
->rr_lock
);
193 rrl
->rr_writer_wanted
= B_FALSE
;
194 rrl
->rr_writer
= curthread
;
195 mutex_exit(&rrl
->rr_lock
);
199 rrw_enter(rrwlock_t
*rrl
, krw_t rw
, void *tag
)
202 rrw_enter_read(rrl
, tag
);
204 rrw_enter_write(rrl
);
208 rrw_exit(rrwlock_t
*rrl
, void *tag
)
210 mutex_enter(&rrl
->rr_lock
);
211 ASSERT(!refcount_is_zero(&rrl
->rr_anon_rcount
) ||
212 !refcount_is_zero(&rrl
->rr_linked_rcount
) ||
213 rrl
->rr_writer
!= NULL
);
215 if (rrl
->rr_writer
== NULL
) {
216 if (rrn_find_and_remove(rrl
)) {
217 if (refcount_remove(&rrl
->rr_linked_rcount
, tag
) == 0)
218 cv_broadcast(&rrl
->rr_cv
);
221 if (refcount_remove(&rrl
->rr_anon_rcount
, tag
) == 0)
222 cv_broadcast(&rrl
->rr_cv
);
225 ASSERT(rrl
->rr_writer
== curthread
);
226 ASSERT(refcount_is_zero(&rrl
->rr_anon_rcount
) &&
227 refcount_is_zero(&rrl
->rr_linked_rcount
));
228 rrl
->rr_writer
= NULL
;
229 cv_broadcast(&rrl
->rr_cv
);
231 mutex_exit(&rrl
->rr_lock
);
235 rrw_held(rrwlock_t
*rrl
, krw_t rw
)
239 mutex_enter(&rrl
->rr_lock
);
240 if (rw
== RW_WRITER
) {
241 held
= (rrl
->rr_writer
== curthread
);
243 held
= (!refcount_is_zero(&rrl
->rr_anon_rcount
) ||
244 !refcount_is_zero(&rrl
->rr_linked_rcount
));
246 mutex_exit(&rrl
->rr_lock
);