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
50 .Nd kernel reader/writer lock
56 .Fn rw_init "struct rwlock *rw" "const char *name"
58 .Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
60 .Fn rw_destroy "struct rwlock *rw"
62 .Fn rw_rlock "struct rwlock *rw"
64 .Fn rw_wlock "struct rwlock *rw"
66 .Fn rw_try_rlock "struct rwlock *rw"
68 .Fn rw_try_wlock "struct rwlock *rw"
70 .Fn rw_runlock "struct rwlock *rw"
72 .Fn rw_wunlock "struct rwlock *rw"
74 .Fn rw_unlock "struct rwlock *rw"
76 .Fn rw_try_upgrade "struct rwlock *rw"
78 .Fn rw_downgrade "struct rwlock *rw"
80 .Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
82 .Fn rw_initialized "const struct rwlock *rw"
84 .Fn rw_wowned "const struct rwlock *rw"
86 .Cd "options INVARIANTS"
87 .Cd "options INVARIANT_SUPPORT"
89 .Fn rw_assert "const struct rwlock *rw" "int what"
91 .Fn RW_SYSINIT "name" "struct rwlock *rw" "const char *desc"
92 .Fn RW_SYSINIT_FLAGS "name" "struct rwlock *rw" "const char *desc" "int flags"
94 Reader/writer locks allow shared access to protected data by multiple threads,
95 or exclusive access by a single thread.
96 The threads with shared access are known as
98 since they only read the protected data.
99 A thread with exclusive access is known as a
101 since it can modify protected data.
103 Although reader/writer locks look very similar to
105 locks, their usage pattern is different.
106 Reader/writer locks can be treated as mutexes (see
108 with shared/exclusive semantics.
113 can be locked while holding a non-spin mutex, and an
115 cannot be held while sleeping.
118 locks have priority propagation like mutexes, but priority
119 can be propagated only to writers.
120 This limitation comes from the fact that readers
122 Another important property is that readers can always recurse,
123 and exclusive locks can be made recursive selectively.
124 .Ss Macros and Functions
125 .Bl -tag -width indent
126 .It Fn rw_init "struct rwlock *rw" "const char *name"
127 Initialize structure located at
129 as reader/writer lock, described by name
131 The description is used solely for debugging purposes.
132 This function must be called before any other operations
134 .It Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
135 Initialize the rw lock just like the
137 function, but specifying a set of optional flags to alter the
143 It contains one or more of the following flags:
144 .Bl -tag -width ".Dv RW_NOPROFILE"
146 Witness should not log messages about duplicate locks being acquired.
148 Do not profile this lock.
154 Do not log any operations for this lock via
157 Allow threads to recursively acquire exclusive locks for
160 If the kernel has been compiled with
161 .Cd "option INVARIANTS" ,
165 has not been initialized multiple times without intervening calls to
167 unless this option is specified.
169 .It Fn rw_rlock "struct rwlock *rw"
173 If any thread holds this lock exclusively, the current thread blocks,
174 and its priority is propagated to the exclusive holder.
177 function can be called when the thread has already acquired reader
181 .Dq "recursing on a lock" .
182 .It Fn rw_wlock "struct rwlock *rw"
186 If there are any shared owners of the lock, the current thread blocks.
189 function can be called recursively only if
191 has been initialized with the
194 .It Fn rw_try_rlock "struct rwlock *rw"
198 This function will return true if the operation succeeds, otherwise 0
200 .It Fn rw_try_wlock "struct rwlock *rw"
204 This function will return true if the operation succeeds, otherwise 0
206 .It Fn rw_runlock "struct rwlock *rw"
207 This function releases a shared lock previously acquired by
209 .It Fn rw_wunlock "struct rwlock *rw"
210 This function releases an exclusive lock previously acquired by
212 .It Fn rw_unlock "struct rwlock *rw"
213 This function releases a shared lock previously acquired by
215 or an exclusive lock previously acquired by
217 .It Fn rw_try_upgrade "struct rwlock *rw"
218 Attempt to upgrade a single shared lock to an exclusive lock.
219 The current thread must hold a shared lock of
221 This will only succeed if the current thread holds the only shared lock on
223 and it only holds a single shared lock.
224 If the attempt succeeds
226 will return a non-zero value,
227 and the current thread will hold an exclusive lock.
231 and the current thread will still hold a shared lock.
232 .It Fn rw_downgrade "struct rwlock *rw"
233 Convert an exclusive lock into a single shared lock.
234 The current thread must hold an exclusive lock of
236 .It Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
239 while waiting for an event.
240 For more details on the parameters to this function,
243 .It Fn rw_initialized "const struct rwlock *rw"
244 This function returns non-zero if
246 has been initialized, and zero otherwise.
247 .It Fn rw_destroy "struct rwlock *rw"
248 This functions destroys a lock previously initialized with
252 lock must be unlocked.
253 .It Fn rw_wowned "const struct rwlock *rw"
254 This function returns a non-zero value if the current thread owns an
257 .It Fn rw_assert "const struct rwlock *rw" "int what"
258 This function allows assertions specified in
262 If the assertions are not true and the kernel is compiled
264 .Cd "options INVARIANTS"
266 .Cd "options INVARIANT_SUPPORT" ,
267 the kernel will panic.
268 Currently the following base assertions are supported:
269 .Bl -tag -width ".Dv RA_UNLOCKED"
271 Assert that current thread holds either a shared or exclusive lock
275 Assert that current thread holds a shared lock of
278 Assert that current thread holds an exclusive lock of
281 Assert that current thread holds neither a shared nor exclusive lock of
285 In addition, one of the following optional flags may be specified with
290 .Bl -tag -width ".Dv RA_NOTRECURSED"
292 Assert that the current thread holds a recursive lock of
294 .It Dv RA_NOTRECURSED
295 Assert that the current thread does not hold a recursive lock of
307 functions appeared in
313 facility was written by
315 This manual page was written by
316 .An "Gleb Smirnoff" .
320 cannot assert whether the current thread does or does not hold a read lock.
326 thread holds a read lock.
327 They cannot ensure that the current thread holds a read lock.
330 can only assert that the current thread does not hold a write lock.
332 Reader/writer is a bit of an awkward name.