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
142 readers. 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
147 for either. They follow
150 .Ss Macros and Functions
151 .Bl -tag -width indent
152 .It Fn rm_init "struct rmlock *rm" "const char *name"
153 Initialize the read-mostly lock
157 description is used solely for debugging purposes.
158 This function must be called before any other operations
160 .It Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
163 initialize the read-mostly lock
165 with a set of optional flags.
168 arguments contains one or more of the following flags:
169 .Bl -tag -width ".Dv RM_NOWITNESS"
175 Allow threads to recursively acquire shared locks for
178 Create a sleepable read-mostly lock.
180 If the kernel has been compiled with
181 .Cd "option INVARIANTS" ,
185 has not been initialized multiple times without intervening calls to
187 unless this option is specified.
189 .It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
194 to track read owners of a lock for priority propagation.
195 This data structure is only used internally by
197 and must persist until
200 This data structure can be allocated on the stack since
201 readers cannot sleep.
202 If any thread holds this lock exclusively, the current thread blocks,
203 and its priority is propagated to the exclusive holder.
204 If the lock was initialized with the
208 function can be called when the current thread has already acquired reader
211 .It Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
216 will return 0 if the lock cannot be acquired immediately;
218 the lock will be acquired and a non-zero value will be returned.
221 may fail even while the lock is not currently held by a writer.
222 If the lock was initialized with the
226 will succeed if the current thread has already acquired reader access.
227 .It Fn rm_wlock "struct rmlock *rm"
231 If there are any shared owners of the lock, the current thread blocks.
234 function cannot be called recursively.
235 .It Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
236 This function releases a shared lock previously acquired by
240 argument must match the
242 argument used for acquiring the shared lock
243 .It Fn rm_wunlock "struct rmlock *rm"
244 This function releases an exclusive lock previously acquired by
246 .It Fn rm_destroy "struct rmlock *rm"
247 This functions destroys a lock previously initialized with
251 lock must be unlocked.
252 .It Fn rm_wowned "const struct rmlock *rm"
253 This function returns a non-zero value if the current thread owns an
256 .It Fn rm_sleep "void *wchan" "struct rmlock *rm" "int priority" "const char *wmesg" "int timo"
257 This function atomically releases
259 while waiting for an event.
262 lock must be exclusively locked.
263 For more details on the parameters to this function,
266 .It Fn rm_assert "struct rmlock *rm" "int what"
267 This function asserts that the
269 lock is in the state specified by
271 If the assertions are not true and the kernel is compiled with
272 .Cd "options INVARIANTS"
274 .Cd "options INVARIANT_SUPPORT" ,
275 the kernel will panic.
276 Currently the following base assertions are supported:
277 .Bl -tag -width ".Dv RA_UNLOCKED"
279 Assert that current thread holds either a shared or exclusive lock
283 Assert that current thread holds a shared lock of
286 Assert that current thread holds an exclusive lock of
289 Assert that current thread holds neither a shared nor exclusive lock of
293 In addition, one of the following optional flags may be specified with
298 .Bl -tag -width ".Dv RA_NOTRECURSED"
300 Assert that the current thread holds a recursive lock of
302 .It Dv RA_NOTRECURSED
303 Assert that the current thread does not hold a recursive lock of
307 .Bl -tag -width indent
308 .It Fn rms_init "struct rmslock *rms" "const char *name"
309 Initialize the sleepable read-mostly lock
313 description is used as
318 This function must be called before any other operations on the lock.
319 .It Fn rms_rlock "struct rmlock *rm"
323 If any thread holds this lock exclusively, the current thread blocks.
324 .It Fn rms_wlock "struct rmslock *rms"
328 If the lock is already taken, the current thread blocks.
331 function cannot be called recursively.
332 .It Fn rms_runlock "struct rmslock *rms"
333 This function releases a shared lock previously acquired by
335 .It Fn rms_wunlock "struct rmslock *rms"
336 This function releases an exclusive lock previously acquired by
338 .It Fn rms_destroy "struct rmslock *rms"
339 This functions destroys a lock previously initialized with
343 lock must be unlocked.
354 functions appeared in
360 facility was written by
361 .An "Stephan Uphoff" .
362 This manual page was written by
364 for rwlock and modified to reflect rmlock by
365 .An "Stephan Uphoff" .
369 implementation is currently not optimized for single processor systems.
372 can fail transiently even when there is no writer, while another reader
373 updates the state on the local CPU.
377 implementation uses a single per CPU list shared by all
378 rmlocks in the system.
379 If rmlocks become popular, hashing to multiple per CPU queues may
380 be needed to speed up the writer lock process.