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
48 .Nd kernel reader/writer lock
54 .Fn rw_init "struct rwlock *rw" "const char *name"
56 .Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
58 .Fn rw_destroy "struct rwlock *rw"
60 .Fn rw_rlock "struct rwlock *rw"
62 .Fn rw_wlock "struct rwlock *rw"
64 .Fn rw_try_rlock "struct rwlock *rw"
66 .Fn rw_try_wlock "struct rwlock *rw"
68 .Fn rw_runlock "struct rwlock *rw"
70 .Fn rw_wunlock "struct rwlock *rw"
72 .Fn rw_try_upgrade "struct rwlock *rw"
74 .Fn rw_downgrade "struct rwlock *rw"
76 .Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
78 .Fn rw_initialized "struct rwlock *rw"
80 .Fn rw_wowned "struct rwlock *rw"
82 .Cd "options INVARIANTS"
83 .Cd "options INVARIANT_SUPPORT"
85 .Fn rw_assert "struct rwlock *rw" "int what"
87 .Fn RW_SYSINIT "name" "struct rwlock *rw" "const char *desc"
89 Reader/writer locks allow shared access to protected data by multiple threads,
90 or exclusive access by a single thread.
91 The threads with shared access are known as
93 since they only read the protected data.
94 A thread with exclusive access is known as a
96 since it can modify protected data.
98 Although reader/writer locks look very similar to
100 locks, their usage pattern is different.
101 Reader/writer locks can be treated as mutexes (see
103 with shared/exclusive semantics.
108 can be locked while holding a non-spin mutex, and an
110 cannot be held while sleeping.
113 locks have priority propagation like mutexes, but priority
114 can be propagated only to an exclusive holder.
115 This limitation comes from the fact that shared owners
117 Another important property is that shared holders of
120 and exclusive locks can be made recursive selectively.
121 .Ss Macros and Functions
122 .Bl -tag -width indent
123 .It Fn rw_init "struct rwlock *rw" "const char *name"
124 Initialize structure located at
126 as reader/writer lock, described by name
128 The description is used solely for debugging purposes.
129 This function must be called before any other operations
131 .It Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
132 Initialize the rw lock just like the
134 function, but specifying a set of optional flags to alter the
140 It contains one or more of the following flags:
141 .Bl -tag -width ".Dv RW_NOPROFILE"
143 Witness should not log messages about duplicate locks being acquired.
145 Do not profile this lock.
151 Do not log any operations for this lock via
154 Allow threads to recursively acquire exclusive locks for
157 .It Fn rw_rlock "struct rwlock *rw"
161 If any thread holds this lock exclusively, the current thread blocks,
162 and its priority is propagated to the exclusive holder.
165 function can be called when the thread has already acquired reader
169 .Dq "recursing on a lock" .
170 .It Fn rw_wlock "struct rwlock *rw"
174 If there are any shared owners of the lock, the current thread blocks.
177 function can be called recursively only if
179 has been initialized with the
182 .It Fn rw_try_rlock "struct rwlock *rw"
186 This function will return true if the operation succeeds, otherwise 0
188 .It Fn rw_try_wlock "struct rwlock *rw"
192 This function will return true if the operation succeeds, otherwise 0
194 .It Fn rw_runlock "struct rwlock *rw"
195 This function releases a shared lock previously acquired by
197 .It Fn rw_wunlock "struct rwlock *rw"
198 This function releases an exclusive lock previously acquired by
200 .It Fn rw_try_upgrade "struct rwlock *rw"
201 Attempt to upgrade a single shared lock to an exclusive lock.
202 The current thread must hold a shared lock of
204 This will only succeed if the current thread holds the only shared lock on
206 and it only holds a single shared lock.
207 If the attempt succeeds
209 will return a non-zero value,
210 and the current thread will hold an exclusive lock.
214 and the current thread will still hold a shared lock.
215 .It Fn rw_downgrade "struct rwlock *rw"
216 Convert an exclusive lock into a single shared lock.
217 The current thread must hold an exclusive lock of
219 .It Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
222 while waiting for an event.
223 For more details on the parameters to this function,
226 .It Fn rw_initialized "struct rwlock *rw"
227 This function returns non-zero if
229 has been initialized, and zero otherwise.
230 .It Fn rw_destroy "struct rwlock *rw"
231 This functions destroys a lock previously initialized with
235 lock must be unlocked.
236 .It Fn rw_wowned "struct rwlock *rw"
237 This function returns a non-zero value if the current thread owns an
240 .It Fn rw_assert "struct rwlock *rw" "int what"
241 This function allows assertions specified in
245 If the assertions are not true and the kernel is compiled
247 .Cd "options INVARIANTS"
249 .Cd "options INVARIANT_SUPPORT" ,
250 the kernel will panic.
251 Currently the following assertions are supported:
252 .Bl -tag -width ".Dv RA_UNLOCKED"
254 Assert that current thread holds either a shared or exclusive lock
258 Assert that current thread holds a shared lock of
261 Assert that current thread holds an exclusive lock of
264 Assert that current thread holds neither a shared nor exclusive lock of
276 functions appeared in
282 facility was written by
284 This manual page was written by
285 .An "Gleb Smirnoff" .
289 is not included in the kernel,
290 then it is impossible to assert that the current thread does or does not
298 assertions merely check that some thread holds a read lock.
300 Reader/writer is a bit of an awkward name.