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
49 .Nd kernel reader/writer lock
55 .Fn rw_init "struct rwlock *rw" "const char *name"
57 .Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
59 .Fn rw_destroy "struct rwlock *rw"
61 .Fn rw_rlock "struct rwlock *rw"
63 .Fn rw_wlock "struct rwlock *rw"
65 .Fn rw_try_rlock "struct rwlock *rw"
67 .Fn rw_try_wlock "struct rwlock *rw"
69 .Fn rw_runlock "struct rwlock *rw"
71 .Fn rw_wunlock "struct rwlock *rw"
73 .Fn rw_unlock "struct rwlock *rw"
75 .Fn rw_try_upgrade "struct rwlock *rw"
77 .Fn rw_downgrade "struct rwlock *rw"
79 .Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
81 .Fn rw_initialized "const struct rwlock *rw"
83 .Fn rw_wowned "const struct rwlock *rw"
85 .Cd "options INVARIANTS"
86 .Cd "options INVARIANT_SUPPORT"
88 .Fn rw_assert "const struct rwlock *rw" "int what"
90 .Fn RW_SYSINIT "name" "struct rwlock *rw" "const char *desc"
92 Reader/writer locks allow shared access to protected data by multiple threads,
93 or exclusive access by a single thread.
94 The threads with shared access are known as
96 since they only read the protected data.
97 A thread with exclusive access is known as a
99 since it can modify protected data.
101 Although reader/writer locks look very similar to
103 locks, their usage pattern is different.
104 Reader/writer locks can be treated as mutexes (see
106 with shared/exclusive semantics.
111 can be locked while holding a non-spin mutex, and an
113 cannot be held while sleeping.
116 locks have priority propagation like mutexes, but priority
117 can be propagated only to writers.
118 This limitation comes from the fact that readers
120 Another important property is that readers can always recurse,
121 and exclusive locks can be made recursive selectively.
122 .Ss Macros and Functions
123 .Bl -tag -width indent
124 .It Fn rw_init "struct rwlock *rw" "const char *name"
125 Initialize structure located at
127 as reader/writer lock, described by name
129 The description is used solely for debugging purposes.
130 This function must be called before any other operations
132 .It Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
133 Initialize the rw lock just like the
135 function, but specifying a set of optional flags to alter the
141 It contains one or more of the following flags:
142 .Bl -tag -width ".Dv RW_NOPROFILE"
144 Witness should not log messages about duplicate locks being acquired.
146 Do not profile this lock.
152 Do not log any operations for this lock via
155 Allow threads to recursively acquire exclusive locks for
158 If the kernel has been compiled with
159 .Cd "option INVARIANTS" ,
163 has not been initialized multiple times without intervening calls to
165 unless this option is specified.
167 .It Fn rw_rlock "struct rwlock *rw"
171 If any thread holds this lock exclusively, the current thread blocks,
172 and its priority is propagated to the exclusive holder.
175 function can be called when the thread has already acquired reader
179 .Dq "recursing on a lock" .
180 .It Fn rw_wlock "struct rwlock *rw"
184 If there are any shared owners of the lock, the current thread blocks.
187 function can be called recursively only if
189 has been initialized with the
192 .It Fn rw_try_rlock "struct rwlock *rw"
196 This function will return true if the operation succeeds, otherwise 0
198 .It Fn rw_try_wlock "struct rwlock *rw"
202 This function will return true if the operation succeeds, otherwise 0
204 .It Fn rw_runlock "struct rwlock *rw"
205 This function releases a shared lock previously acquired by
207 .It Fn rw_wunlock "struct rwlock *rw"
208 This function releases an exclusive lock previously acquired by
210 .It Fn rw_unlock "struct rwlock *rw"
211 This function releases a shared lock previously acquired by
213 or an exclusive lock previously acquired by
215 .It Fn rw_try_upgrade "struct rwlock *rw"
216 Attempt to upgrade a single shared lock to an exclusive lock.
217 The current thread must hold a shared lock of
219 This will only succeed if the current thread holds the only shared lock on
221 and it only holds a single shared lock.
222 If the attempt succeeds
224 will return a non-zero value,
225 and the current thread will hold an exclusive lock.
229 and the current thread will still hold a shared lock.
230 .It Fn rw_downgrade "struct rwlock *rw"
231 Convert an exclusive lock into a single shared lock.
232 The current thread must hold an exclusive lock of
234 .It Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
237 while waiting for an event.
238 For more details on the parameters to this function,
241 .It Fn rw_initialized "const struct rwlock *rw"
242 This function returns non-zero if
244 has been initialized, and zero otherwise.
245 .It Fn rw_destroy "struct rwlock *rw"
246 This functions destroys a lock previously initialized with
250 lock must be unlocked.
251 .It Fn rw_wowned "const struct rwlock *rw"
252 This function returns a non-zero value if the current thread owns an
255 .It Fn rw_assert "const struct rwlock *rw" "int what"
256 This function allows assertions specified in
260 If the assertions are not true and the kernel is compiled
262 .Cd "options INVARIANTS"
264 .Cd "options INVARIANT_SUPPORT" ,
265 the kernel will panic.
266 Currently the following base assertions are supported:
267 .Bl -tag -width ".Dv RA_UNLOCKED"
269 Assert that current thread holds either a shared or exclusive lock
273 Assert that current thread holds a shared lock of
276 Assert that current thread holds an exclusive lock of
279 Assert that current thread holds neither a shared nor exclusive lock of
283 In addition, one of the following optional flags may be specified with
288 .Bl -tag -width ".Dv RA_NOTRECURSED"
290 Assert that the current thread holds a recursive lock of
292 .It Dv RA_NOTRECURSED
293 Assert that the current thread does not hold a recursive lock of
305 functions appeared in
311 facility was written by
313 This manual page was written by
314 .An "Gleb Smirnoff" .
318 is not included in the kernel,
319 then it is impossible to assert that the current thread does or does not
327 assertions merely check that some thread holds a read lock.
329 Reader/writer is a bit of an awkward name.