1 .\" Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
2 .\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org>
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" Based on rwlock.9 man page
46 .Nm RM_SYSINIT_FLAGS ,
53 .Nd kernel reader/writer lock optimized for read-mostly access patterns
59 .Fn rm_init "struct rmlock *rm" "const char *name"
61 .Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
63 .Fn rm_destroy "struct rmlock *rm"
65 .Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
67 .Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
69 .Fn rm_wlock "struct rmlock *rm"
71 .Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
73 .Fn rm_wunlock "struct rmlock *rm"
75 .Fn rm_wowned "const struct rmlock *rm"
77 .Fn rm_sleep "void *wchan" "struct rmlock *rm" "int priority" "const char *wmesg" "int timo"
79 .Cd "options INVARIANTS"
80 .Cd "options INVARIANT_SUPPORT"
82 .Fn rm_assert "struct rmlock *rm" "int what"
84 .Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc"
85 .Fn RM_SYSINIT_FLAGS "name" "struct rmlock *rm" "const char *desc" "int flags"
87 .Fn rms_init "struct rmslock *rms" "const char *name"
89 .Fn rms_destroy "struct rmslock *rms"
91 .Fn rms_rlock "struct rmslock *rms"
93 .Fn rms_wlock "struct rmslock *rms"
95 .Fn rms_runlock "struct rmslock *rms"
97 .Fn rms_wunlock "struct rmslock *rms"
99 Read-mostly locks allow shared access to protected data by multiple threads,
100 or exclusive access by a single thread.
101 The threads with shared access are known as
103 since they only read the protected data.
104 A thread with exclusive access is known as a
106 since it can modify protected data.
108 Read-mostly locks are designed to be efficient for locks almost exclusively
109 used as reader locks and as such should be used for protecting data that
111 Acquiring an exclusive lock after the lock has been locked for shared access
112 is an expensive operation.
114 Normal read-mostly locks are similar to
116 locks and follow the same lock ordering rules as
119 Read-mostly locks have full priority propagation like mutexes.
122 read-mostly locks propagate priority to both readers and writers.
123 This is implemented via the
125 structure argument supplied to
129 Readers can recurse if the lock is initialized with the
132 however, writers are never allowed to recurse.
134 Sleeping for writers can be allowed by passing
138 It changes lock ordering rules to the same as for
141 They do not propagate priority to writers, but they do propagate priority to readers.
142 Note that readers are not permitted to sleep regardless of the flag.
144 Sleepable read-mostly locks (created with
146 allow sleeping for both readers and writers, but don't do priority propagation
151 .Ss Macros and Functions
152 .Bl -tag -width indent
153 .It Fn rm_init "struct rmlock *rm" "const char *name"
154 Initialize the read-mostly lock
158 description is used solely for debugging purposes.
159 This function must be called before any other operations
161 .It Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
164 initialize the read-mostly lock
166 with a set of optional flags.
169 arguments contains one or more of the following flags:
170 .Bl -tag -width ".Dv RM_NOWITNESS"
176 Allow threads to recursively acquire shared locks for
179 Create a sleepable read-mostly lock.
181 If the kernel has been compiled with
182 .Cd "option INVARIANTS" ,
186 has not been initialized multiple times without intervening calls to
188 unless this option is specified.
191 should not log messages about duplicate locks being acquired.
193 .It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
198 to track read owners of a lock for priority propagation.
199 This data structure is only used internally by
201 and must persist until
204 This data structure can be allocated on the stack since
205 readers cannot sleep.
206 If any thread holds this lock exclusively, the current thread blocks,
207 and its priority is propagated to the exclusive holder.
208 If the lock was initialized with the
212 function can be called when the current thread has already acquired reader
215 .It Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
220 will return 0 if the lock cannot be acquired immediately;
222 the lock will be acquired and a non-zero value will be returned.
225 may fail even while the lock is not currently held by a writer.
226 If the lock was initialized with the
230 will succeed if the current thread has already acquired reader access.
231 .It Fn rm_wlock "struct rmlock *rm"
235 If there are any shared owners of the lock, the current thread blocks.
238 function cannot be called recursively.
239 .It Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
240 This function releases a shared lock previously acquired by
244 argument must match the
246 argument used for acquiring the shared lock
247 .It Fn rm_wunlock "struct rmlock *rm"
248 This function releases an exclusive lock previously acquired by
250 .It Fn rm_destroy "struct rmlock *rm"
251 This functions destroys a lock previously initialized with
255 lock must be unlocked.
256 .It Fn rm_wowned "const struct rmlock *rm"
257 This function returns a non-zero value if the current thread owns an
260 .It Fn rm_sleep "void *wchan" "struct rmlock *rm" "int priority" "const char *wmesg" "int timo"
261 This function atomically releases
263 while waiting for an event.
266 lock must be exclusively locked.
267 For more details on the parameters to this function,
270 .It Fn rm_assert "struct rmlock *rm" "int what"
271 This function asserts that the
273 lock is in the state specified by
275 If the assertions are not true and the kernel is compiled with
276 .Cd "options INVARIANTS"
278 .Cd "options INVARIANT_SUPPORT" ,
279 the kernel will panic.
280 Currently the following base assertions are supported:
281 .Bl -tag -width ".Dv RA_UNLOCKED"
283 Assert that current thread holds either a shared or exclusive lock
287 Assert that current thread holds a shared lock of
290 Assert that current thread holds an exclusive lock of
293 Assert that current thread holds neither a shared nor exclusive lock of
297 In addition, one of the following optional flags may be specified with
302 .Bl -tag -width ".Dv RA_NOTRECURSED"
304 Assert that the current thread holds a recursive lock of
306 .It Dv RA_NOTRECURSED
307 Assert that the current thread does not hold a recursive lock of
311 .Bl -tag -width indent
312 .It Fn rms_init "struct rmslock *rms" "const char *name"
313 Initialize the sleepable read-mostly lock
317 description is used as
322 This function must be called before any other operations on the lock.
323 .It Fn rms_rlock "struct rmlock *rm"
327 If any thread holds this lock exclusively, the current thread blocks.
328 .It Fn rms_wlock "struct rmslock *rms"
332 If the lock is already taken, the current thread blocks.
335 function cannot be called recursively.
336 .It Fn rms_runlock "struct rmslock *rms"
337 This function releases a shared lock previously acquired by
339 .It Fn rms_wunlock "struct rmslock *rms"
340 This function releases an exclusive lock previously acquired by
342 .It Fn rms_destroy "struct rmslock *rms"
343 This functions destroys a lock previously initialized with
347 lock must be unlocked.
357 These functions appeared in
363 facility was written by
364 .An "Stephan Uphoff" .
365 This manual page was written by
367 for rwlock and modified to reflect rmlock by
368 .An "Stephan Uphoff" .
372 implementation is currently not optimized for single processor systems.
375 can fail transiently even when there is no writer, while another reader
376 updates the state on the local CPU.
380 implementation uses a single per CPU list shared by all
381 rmlocks in the system.
382 If rmlocks become popular, hashing to multiple per CPU queues may
383 be needed to speed up the writer lock process.