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