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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
26 * All rights reserved.
29 #pragma ident "%Z%%M% %I% %E% SMI"
32 * A homegrown reader/writer lock implementation. It addresses
33 * two requirements not addressed by the system primitives. They
34 * are that the `enter" operation is optionally interruptible and
35 * that that they can be re`enter'ed by writers without deadlock.
37 * All of this was borrowed from NFS.
38 * See: kernel/fs/nfs/nfs_subr.c
40 * XXX: Could we make this serve our needs instead?
41 * See: kernel/os/rwstlock.c
42 * (and then use it for NFS too)
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/vnode.h>
50 #include <smbfs/smbfs.h>
51 #include <smbfs/smbfs_node.h>
52 #include <smbfs/smbfs_subr.h>
56 * Only can return non-zero if intr != 0.
59 smbfs_rw_enter_sig(smbfs_rwlock_t
*l
, krw_t rw
, int intr
)
62 mutex_enter(&l
->lock
);
65 * If this is a nested enter, then allow it. There
66 * must be as many exits as enters through.
68 if (l
->owner
== curthread
) {
69 /* lock is held for writing by current thread */
70 ASSERT(rw
== RW_READER
|| rw
== RW_WRITER
);
72 } else if (rw
== RW_READER
) {
74 * While there is a writer active or writers waiting,
75 * then wait for them to finish up and move on. Then,
76 * increment the count to indicate that a reader is
79 while (l
->count
< 0 || l
->waiters
> 0) {
81 klwp_t
*lwp
= ttolwp(curthread
);
85 if (!cv_wait_sig(&l
->cv
, &l
->lock
)) {
94 cv_wait(&l
->cv
, &l
->lock
);
96 ASSERT(l
->count
< INT_MAX
);
98 if ((l
->count
% 10000) == 9999)
99 cmn_err(CE_WARN
, "smbfs_rw_enter_sig: count %d on"
100 "rwlock @ %p\n", l
->count
, (void *)&l
);
104 ASSERT(rw
== RW_WRITER
);
106 * While there are readers active or a writer
107 * active, then wait for all of the readers
108 * to finish or for the writer to finish.
109 * Then, set the owner field to curthread and
110 * decrement count to indicate that a writer
113 while (l
->count
> 0 || l
->owner
!= NULL
) {
116 klwp_t
*lwp
= ttolwp(curthread
);
120 if (!cv_wait_sig(&l
->cv
, &l
->lock
)) {
124 cv_broadcast(&l
->cv
);
125 mutex_exit(&l
->lock
);
131 cv_wait(&l
->cv
, &l
->lock
);
134 l
->owner
= curthread
;
138 mutex_exit(&l
->lock
);
144 * If the lock is available, obtain it and return non-zero. If there is
145 * already a conflicting lock, return 0 immediately.
149 smbfs_rw_tryenter(smbfs_rwlock_t
*l
, krw_t rw
)
151 mutex_enter(&l
->lock
);
154 * If this is a nested enter, then allow it. There
155 * must be as many exits as enters through.
157 if (l
->owner
== curthread
) {
158 /* lock is held for writing by current thread */
159 ASSERT(rw
== RW_READER
|| rw
== RW_WRITER
);
161 } else if (rw
== RW_READER
) {
163 * If there is a writer active or writers waiting, deny the
164 * lock. Otherwise, bump the count of readers.
166 if (l
->count
< 0 || l
->waiters
> 0) {
167 mutex_exit(&l
->lock
);
172 ASSERT(rw
== RW_WRITER
);
174 * If there are readers active or a writer active, deny the
175 * lock. Otherwise, set the owner field to curthread and
176 * decrement count to indicate that a writer is active.
178 if (l
->count
> 0 || l
->owner
!= NULL
) {
179 mutex_exit(&l
->lock
);
182 l
->owner
= curthread
;
186 mutex_exit(&l
->lock
);
192 smbfs_rw_exit(smbfs_rwlock_t
*l
)
195 mutex_enter(&l
->lock
);
197 * If this is releasing a writer lock, then increment count to
198 * indicate that there is one less writer active. If this was
199 * the last of possibly nested writer locks, then clear the owner
200 * field as well to indicate that there is no writer active
201 * and wakeup any possible waiting writers or readers.
203 * If releasing a reader lock, then just decrement count to
204 * indicate that there is one less reader active. If this was
205 * the last active reader and there are writer(s) waiting,
206 * then wake up the first.
208 if (l
->owner
!= NULL
) {
209 ASSERT(l
->owner
== curthread
);
213 cv_broadcast(&l
->cv
);
216 ASSERT(l
->count
> 0);
218 if (l
->count
== 0 && l
->waiters
> 0)
219 cv_broadcast(&l
->cv
);
221 mutex_exit(&l
->lock
);
225 smbfs_rw_lock_held(smbfs_rwlock_t
*l
, krw_t rw
)
229 return (l
->count
> 0);
230 ASSERT(rw
== RW_WRITER
);
231 return (l
->count
< 0);
236 smbfs_rw_init(smbfs_rwlock_t
*l
, char *name
, krw_type_t type
, void *arg
)
242 mutex_init(&l
->lock
, NULL
, MUTEX_DEFAULT
, NULL
);
243 cv_init(&l
->cv
, NULL
, CV_DEFAULT
, NULL
);
247 smbfs_rw_destroy(smbfs_rwlock_t
*l
)
250 mutex_destroy(&l
->lock
);