]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - share/man/man9/rwlock.9
MFC: r275751
[FreeBSD/stable/10.git] / share / man / man9 / rwlock.9
1 .\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org>
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
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.
12 .\"
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
23 .\" SUCH DAMAGE.
24 .\"
25 .\" $FreeBSD$
26 .\"
27 .Dd December 13, 2014
28 .Dt RWLOCK 9
29 .Os
30 .Sh NAME
31 .Nm rwlock ,
32 .Nm rw_init ,
33 .Nm rw_init_flags,
34 .Nm rw_destroy ,
35 .Nm rw_rlock ,
36 .Nm rw_wlock ,
37 .Nm rw_runlock ,
38 .Nm rw_wunlock ,
39 .Nm rw_unlock ,
40 .Nm rw_try_rlock ,
41 .Nm rw_try_upgrade ,
42 .Nm rw_try_wlock ,
43 .Nm rw_downgrade ,
44 .Nm rw_sleep ,
45 .Nm rw_initialized ,
46 .Nm rw_wowned ,
47 .Nm rw_assert ,
48 .Nm RW_SYSINIT
49 .Nd kernel reader/writer lock
50 .Sh SYNOPSIS
51 .In sys/param.h
52 .In sys/lock.h
53 .In sys/rwlock.h
54 .Ft void
55 .Fn rw_init "struct rwlock *rw" "const char *name"
56 .Ft void
57 .Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
58 .Ft void
59 .Fn rw_destroy "struct rwlock *rw"
60 .Ft void
61 .Fn rw_rlock "struct rwlock *rw"
62 .Ft void
63 .Fn rw_wlock "struct rwlock *rw"
64 .Ft int
65 .Fn rw_try_rlock "struct rwlock *rw"
66 .Ft int
67 .Fn rw_try_wlock "struct rwlock *rw"
68 .Ft void
69 .Fn rw_runlock "struct rwlock *rw"
70 .Ft void
71 .Fn rw_wunlock "struct rwlock *rw"
72 .Ft void
73 .Fn rw_unlock "struct rwlock *rw"
74 .Ft int
75 .Fn rw_try_upgrade "struct rwlock *rw"
76 .Ft void
77 .Fn rw_downgrade "struct rwlock *rw"
78 .Ft int
79 .Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
80 .Ft int
81 .Fn rw_initialized "const struct rwlock *rw"
82 .Ft int
83 .Fn rw_wowned "const struct rwlock *rw"
84 .Pp
85 .Cd "options INVARIANTS"
86 .Cd "options INVARIANT_SUPPORT"
87 .Ft void
88 .Fn rw_assert "const struct rwlock *rw" "int what"
89 .In sys/kernel.h
90 .Fn RW_SYSINIT "name" "struct rwlock *rw" "const char *desc"
91 .Sh DESCRIPTION
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
95 .Em readers
96 since they only read the protected data.
97 A thread with exclusive access is known as a
98 .Em writer
99 since it can modify protected data.
100 .Pp
101 Although reader/writer locks look very similar to
102 .Xr sx 9
103 locks, their usage pattern is different.
104 Reader/writer locks can be treated as mutexes (see
105 .Xr mutex 9 )
106 with shared/exclusive semantics.
107 Unlike
108 .Xr sx 9 ,
109 an
110 .Nm
111 can be locked while holding a non-spin mutex, and an
112 .Nm
113 cannot be held while sleeping.
114 The
115 .Nm
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
119 are anonymous.
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
126 .Fa rw
127 as reader/writer lock, described by name
128 .Fa name .
129 The description is used solely for debugging purposes.
130 This function must be called before any other operations
131 on the lock.
132 .It Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
133 Initialize the rw lock just like the
134 .Fn rw_init
135 function, but specifying a set of optional flags to alter the
136 behaviour of
137 .Fa rw ,
138 through the
139 .Fa opts
140 argument.
141 It contains one or more of the following flags:
142 .Bl -tag -width ".Dv RW_NOPROFILE"
143 .It Dv RW_DUPOK
144 Witness should not log messages about duplicate locks being acquired.
145 .It Dv RW_NOPROFILE
146 Do not profile this lock.
147 .It Dv RW_NOWITNESS
148 Instruct
149 .Xr witness 4
150 to ignore this lock.
151 .It Dv RW_QUIET
152 Do not log any operations for this lock via
153 .Xr ktr 4 .
154 .It Dv RW_RECURSE
155 Allow threads to recursively acquire exclusive locks for
156 .Fa rw .
157 .It Dv RW_NEW
158 If the kernel has been compiled with
159 .Cd "option INVARIANTS" ,
160 .Fn rw_init_flags
161 will assert that the
162 .Fa rw
163 has not been initialized multiple times without intervening calls to
164 .Fn rw_destroy
165 unless this option is specified.
166 .El
167 .It Fn rw_rlock "struct rwlock *rw"
168 Lock
169 .Fa rw
170 as a reader.
171 If any thread holds this lock exclusively, the current thread blocks,
172 and its priority is propagated to the exclusive holder.
173 The
174 .Fn rw_rlock
175 function can be called when the thread has already acquired reader
176 access on
177 .Fa rw .
178 This is called
179 .Dq "recursing on a lock" .
180 .It Fn rw_wlock "struct rwlock *rw"
181 Lock
182 .Fa rw
183 as a writer.
184 If there are any shared owners of the lock, the current thread blocks.
185 The
186 .Fn rw_wlock
187 function can be called recursively only if
188 .Fa rw
189 has been initialized with the
190 .Dv RW_RECURSE
191 option enabled.
192 .It Fn rw_try_rlock "struct rwlock *rw"
193 Try to lock
194 .Fa rw
195 as a reader.
196 This function will return true if the operation succeeds, otherwise 0
197 will be returned.
198 .It Fn rw_try_wlock "struct rwlock *rw"
199 Try to lock
200 .Fa rw
201 as a writer.
202 This function will return true if the operation succeeds, otherwise 0
203 will be returned.
204 .It Fn rw_runlock "struct rwlock *rw"
205 This function releases a shared lock previously acquired by
206 .Fn rw_rlock .
207 .It Fn rw_wunlock "struct rwlock *rw"
208 This function releases an exclusive lock previously acquired by
209 .Fn rw_wlock .
210 .It Fn rw_unlock "struct rwlock *rw"
211 This function releases a shared lock previously acquired by
212 .Fn rw_rlock
213 or an exclusive lock previously acquired by
214 .Fn rw_wlock .
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
218 .Fa rw .
219 This will only succeed if the current thread holds the only shared lock on
220 .Fa rw ,
221 and it only holds a single shared lock.
222 If the attempt succeeds
223 .Fn rw_try_upgrade
224 will return a non-zero value,
225 and the current thread will hold an exclusive lock.
226 If the attempt fails
227 .Fn rw_try_upgrade
228 will return zero,
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
233 .Fa rw .
234 .It Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
235 Atomically release
236 .Fa rw
237 while waiting for an event.
238 For more details on the parameters to this function,
239 see
240 .Xr sleep 9 .
241 .It Fn rw_initialized "const struct rwlock *rw"
242 This function returns non-zero if
243 .Fa rw
244 has been initialized, and zero otherwise.
245 .It Fn rw_destroy "struct rwlock *rw"
246 This functions destroys a lock previously initialized with
247 .Fn rw_init .
248 The
249 .Fa rw
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
253 exclusive lock on
254 .Fa rw .
255 .It Fn rw_assert "const struct rwlock *rw" "int what"
256 This function allows assertions specified in
257 .Fa what
258 to be made about
259 .Fa rw .
260 If the assertions are not true and the kernel is compiled
261 with
262 .Cd "options INVARIANTS"
263 and
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"
268 .It Dv RA_LOCKED
269 Assert that current thread holds either a shared or exclusive lock
270 of
271 .Fa rw .
272 .It Dv RA_RLOCKED
273 Assert that current thread holds a shared lock of
274 .Fa rw .
275 .It Dv RA_WLOCKED
276 Assert that current thread holds an exclusive lock of
277 .Fa rw .
278 .It Dv RA_UNLOCKED
279 Assert that current thread holds neither a shared nor exclusive lock of
280 .Fa rw .
281 .El
282 .Pp
283 In addition, one of the following optional flags may be specified with
284 .Dv RA_LOCKED ,
285 .Dv RA_RLOCKED ,
286 or
287 .Dv RA_WLOCKED :
288 .Bl -tag -width ".Dv RA_NOTRECURSED"
289 .It Dv RA_RECURSED
290 Assert that the current thread holds a recursive lock of
291 .Fa rw .
292 .It Dv RA_NOTRECURSED
293 Assert that the current thread does not hold a recursive lock of
294 .Fa rw .
295 .El
296 .El
297 .Sh SEE ALSO
298 .Xr locking 9 ,
299 .Xr mutex 9 ,
300 .Xr panic 9 ,
301 .Xr sema 9 ,
302 .Xr sx 9
303 .Sh HISTORY
304 These
305 functions appeared in
306 .Fx 7.0 .
307 .Sh AUTHORS
308 .An -nosplit
309 The
310 .Nm
311 facility was written by
312 .An "John Baldwin" .
313 This manual page was written by
314 .An "Gleb Smirnoff" .
315 .Sh BUGS
316 If
317 .Dv WITNESS
318 is not included in the kernel,
319 then it is impossible to assert that the current thread does or does not
320 hold a read lock.
321 In the
322 .Pf non- Dv WITNESS
323 case, the
324 .Dv RA_LOCKED
325 and
326 .Dv RA_RLOCKED
327 assertions merely check that some thread holds a read lock.
328 .Pp
329 Reader/writer is a bit of an awkward name.
330 An
331 .Nm
332 can also be called a
333 .Dq Robert Watson
334 lock if desired.