4787
|
1 |
/*
|
|
2 |
* CDDL HEADER START
|
|
3 |
*
|
|
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.
|
|
7 |
*
|
|
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.
|
|
12 |
*
|
|
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]
|
|
18 |
*
|
|
19 |
* CDDL HEADER END
|
|
20 |
*/
|
|
21 |
/*
|
|
22 |
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
|
|
23 |
* Use is subject to license terms.
|
|
24 |
*/
|
|
25 |
|
|
26 |
#pragma ident "%Z%%M% %I% %E% SMI"
|
|
27 |
|
|
28 |
#include <sys/zfs_context.h>
|
|
29 |
#include <sys/refcount.h>
|
|
30 |
#include <sys/rprwlock.h>
|
|
31 |
|
|
32 |
void
|
|
33 |
rprw_init(rprwlock_t *rwl)
|
|
34 |
{
|
|
35 |
mutex_init(&rwl->rw_lock, NULL, MUTEX_DEFAULT, NULL);
|
|
36 |
rwl->rw_writer = NULL;
|
|
37 |
cv_init(&rwl->rw_cv, NULL, CV_DEFAULT, NULL);
|
|
38 |
refcount_create(&rwl->rw_count);
|
|
39 |
}
|
|
40 |
|
|
41 |
void
|
|
42 |
rprw_destroy(rprwlock_t *rwl)
|
|
43 |
{
|
|
44 |
mutex_destroy(&rwl->rw_lock);
|
|
45 |
ASSERT(rwl->rw_writer == NULL);
|
|
46 |
cv_destroy(&rwl->rw_cv);
|
|
47 |
refcount_destroy(&rwl->rw_count);
|
|
48 |
}
|
|
49 |
|
|
50 |
void
|
|
51 |
rprw_enter_read(rprwlock_t *rwl, void *tag)
|
|
52 |
{
|
|
53 |
mutex_enter(&rwl->rw_lock);
|
|
54 |
|
|
55 |
if (rwl->rw_writer != curthread) {
|
|
56 |
while (rwl->rw_writer != NULL)
|
|
57 |
cv_wait(&rwl->rw_cv, &rwl->rw_lock);
|
|
58 |
}
|
|
59 |
|
|
60 |
(void) refcount_add(&rwl->rw_count, tag);
|
|
61 |
|
|
62 |
mutex_exit(&rwl->rw_lock);
|
|
63 |
}
|
|
64 |
|
|
65 |
void
|
|
66 |
rprw_enter_write(rprwlock_t *rwl, void *tag)
|
|
67 |
{
|
|
68 |
mutex_enter(&rwl->rw_lock);
|
|
69 |
|
|
70 |
if (rwl->rw_writer != curthread) {
|
|
71 |
while (!refcount_is_zero(&rwl->rw_count))
|
|
72 |
cv_wait(&rwl->rw_cv, &rwl->rw_lock);
|
|
73 |
rwl->rw_writer = curthread;
|
|
74 |
}
|
|
75 |
|
|
76 |
(void) refcount_add(&rwl->rw_count, tag);
|
|
77 |
|
|
78 |
mutex_exit(&rwl->rw_lock);
|
|
79 |
}
|
|
80 |
|
|
81 |
void
|
|
82 |
rprw_enter(rprwlock_t *rwl, krw_t rw, void *tag)
|
|
83 |
{
|
|
84 |
if (rw == RW_READER)
|
|
85 |
rprw_enter_read(rwl, tag);
|
|
86 |
else
|
|
87 |
rprw_enter_write(rwl, tag);
|
|
88 |
}
|
|
89 |
|
|
90 |
void
|
|
91 |
rprw_exit(rprwlock_t *rwl, void *tag)
|
|
92 |
{
|
|
93 |
mutex_enter(&rwl->rw_lock);
|
|
94 |
|
|
95 |
ASSERT(!refcount_is_zero(&rwl->rw_count));
|
|
96 |
ASSERT(rwl->rw_writer == NULL || curthread == rwl->rw_writer);
|
|
97 |
if (refcount_remove(&rwl->rw_count, tag) == 0) {
|
|
98 |
cv_broadcast(&rwl->rw_cv);
|
|
99 |
rwl->rw_writer = NULL; /* OK in either case */
|
|
100 |
}
|
|
101 |
|
|
102 |
mutex_exit(&rwl->rw_lock);
|
|
103 |
}
|
|
104 |
|
|
105 |
boolean_t
|
|
106 |
rprw_held(rprwlock_t *rwl, krw_t rw)
|
|
107 |
{
|
|
108 |
boolean_t held;
|
|
109 |
|
|
110 |
mutex_enter(&rwl->rw_lock);
|
|
111 |
if (rw == RW_WRITER)
|
|
112 |
held = (rwl->rw_writer == curthread);
|
|
113 |
else
|
|
114 |
held = !rwl->rw_writer && !refcount_is_zero(&rwl->rw_count);
|
|
115 |
mutex_exit(&rwl->rw_lock);
|
|
116 |
|
|
117 |
return (held);
|
|
118 |
}
|