1 .\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org>
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 .Nd kernel reader/writer lock
51 .Fn rw_init "struct rwlock *rw" "const char *name"
53 .Fn rw_destroy "struct rwlock *rw"
55 .Fn rw_rlock "struct rwlock *rw"
57 .Fn rw_wlock "struct rwlock *rw"
59 .Fn rw_runlock "struct rwlock *rw"
61 .Fn rw_wunlock "struct rwlock *rw"
63 .Fn rw_try_upgrade "struct rwlock *rw"
65 .Fn rw_downgrade "struct rwlock *rw"
67 .Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
69 .Fn rw_initialized "struct rwlock *rw"
71 .Fn rw_wowned "struct rwlock *rw"
73 .Cd "options INVARIANTS"
74 .Cd "options INVARIANT_SUPPORT"
76 .Fn rw_assert "struct rwlock *rw" "int what"
78 .Fn RW_SYSINIT "name" "struct rwlock *rw" "const char *desc"
80 Reader/writer locks allow shared access to protected data by multiple threads,
81 or exclusive access by a single thread.
82 The threads with shared access are known as
84 since they only read the protected data.
85 A thread with exclusive access is known as a
87 since it can modify protected data.
89 Although reader/writer locks look very similar to
91 locks, their usage pattern is different.
92 Reader/writer locks can be treated as mutexes (see
94 with shared/exclusive semantics.
99 can be locked while holding a non-spin mutex, and an
101 cannot be held while sleeping.
104 locks have priority propagation like mutexes, but priority
105 can be propagated only to an exclusive holder.
106 This limitation comes from the fact that shared owners
108 Another important property is that shared holders of
111 but exclusive locks are not allowed to recurse.
112 .Ss Macros and Functions
113 .Bl -tag -width indent
114 .It Fn rw_init "struct rwlock *rw" "const char *name"
115 Initialize structure located at
117 as reader/writer lock, described by name
119 The description is used solely for debugging purposes.
120 This function must be called before any other operations
122 .It Fn rw_rlock "struct rwlock *rw"
126 If any thread holds this lock exclusively, the current thread blocks,
127 and its priority is propagated to the exclusive holder.
130 function can be called when the thread has already acquired reader
134 .Dq "recursing on a lock" .
135 .It Fn rw_wlock "struct rwlock *rw"
139 If there are any shared owners of the lock, the current thread blocks.
142 function cannot be called recursively.
143 .It Fn rw_runlock "struct rwlock *rw"
144 This function releases a shared lock previously acquired by
146 .It Fn rw_wunlock "struct rwlock *rw"
147 This function releases an exclusive lock previously acquired by
149 .It Fn rw_try_upgrade "struct rwlock *rw"
150 Attempt to upgrade a single shared lock to an exclusive lock.
151 The current thread must hold a shared lock of
153 This will only succeed if the current thread holds the only shared lock on
155 and it only holds a single shared lock.
156 If the attempt succeeds
158 will return a non-zero value,
159 and the current thread will hold an exclusive lock.
163 and the current thread will still hold a shared lock.
164 .It Fn rw_downgrade "struct rwlock *rw"
165 Convert an exclusive lock into a single shared lock.
166 The current thread must hold an exclusive lock of
168 .It Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
171 while waiting for an event.
172 For more details on the parameters to this function,
175 .It Fn rw_initialized "struct rwlock *rw"
176 This function returns non-zero if
178 has been initialized, and zero otherwise.
179 .It Fn rw_destroy "struct rwlock *rw"
180 This functions destroys a lock previously initialized with
184 lock must be unlocked.
185 .It Fn rw_wowned "struct rwlock *rw"
186 This function returns a non-zero value if the current thread owns an
189 .It Fn rw_assert "struct rwlock *rw" "int what"
190 This function allows assertions specified in
194 If the assertions are not true and the kernel is compiled
196 .Cd "options INVARIANTS"
198 .Cd "options INVARIANT_SUPPORT" ,
199 the kernel will panic.
200 Currently the following assertions are supported:
201 .Bl -tag -width ".Dv RA_UNLOCKED"
203 Assert that current thread holds either a shared or exclusive lock
207 Assert that current thread holds a shared lock of
210 Assert that current thread holds an exclusive lock of
213 Assert that current thread holds neither a shared nor exclusive lock of
224 functions appeared in
230 facility was written by
232 This manual page was written by
233 .An "Gleb Smirnoff" .
237 is not included in the kernel,
238 then it is impossible to assert that the current thread does or does not
246 assertions merely check that some thread holds a read lock.
248 Reader/writer is a bit of an awkward name.