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
47 .Nd kernel reader/writer lock optimized for read-mostly access patterns
53 .Fn rm_init "struct rmlock *rm" "const char *name"
55 .Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
57 .Fn rm_destroy "struct rmlock *rm"
59 .Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
61 .Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
63 .Fn rm_wlock "struct rmlock *rm"
65 .Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
67 .Fn rm_wunlock "struct rmlock *rm"
69 .Fn rm_wowned "const struct rmlock *rm"
71 .Fn rm_sleep "void *wchan" "struct rmlock *rm" "int priority" "const char *wmesg" "int timo"
73 .Cd "options INVARIANTS"
74 .Cd "options INVARIANT_SUPPORT"
76 .Fn rm_assert "struct rmlock *rm" "int what"
78 .Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc"
79 .Fn RM_SYSINIT_FLAGS "name" "struct rmlock *rm" "const char *desc" "int flags"
81 Read-mostly locks allow shared access to protected data by multiple threads,
82 or exclusive access by a single thread.
83 The threads with shared access are known as
85 since they only read the protected data.
86 A thread with exclusive access is known as a
88 since it can modify protected data.
90 Read-mostly locks are designed to be efficient for locks almost exclusively
91 used as reader locks and as such should be used for protecting data that
93 Acquiring an exclusive lock after the lock has been locked for shared access
94 is an expensive operation.
96 Normal read-mostly locks are similar to
98 locks and follow the same lock ordering rules as
101 Read-mostly locks have full priority propagation like mutexes.
104 read-mostly locks propagate priority to both readers and writers.
105 This is implemented via the
107 structure argument supplied to
111 Readers can recurse if the lock is initialized with the
114 however, writers are never allowed to recurse.
116 Sleepable read-mostly locks are created by passing
120 Unlike normal read-mostly locks,
121 sleepable read-mostly locks follow the same lock ordering rules as
124 Sleepable read-mostly locks do not propagate priority to writers,
125 but they do propagate priority to readers.
126 Writers are permitted to sleep while holding a read-mostly lock,
128 Unlike other sleepable locks such as
131 readers must use try operations on other sleepable locks to avoid sleeping.
132 .Ss Macros and Functions
133 .Bl -tag -width indent
134 .It Fn rm_init "struct rmlock *rm" "const char *name"
135 Initialize the read-mostly lock
139 description is used solely for debugging purposes.
140 This function must be called before any other operations
142 .It Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
145 initialize the read-mostly lock
147 with a set of optional flags.
150 arguments contains one or more of the following flags:
151 .Bl -tag -width ".Dv RM_NOWITNESS"
157 Allow threads to recursively acquire shared locks for
160 Create a sleepable read-mostly lock.
162 If the kernel has been compiled with
163 .Cd "option INVARIANTS" ,
167 has not been initialized multiple times without intervening calls to
169 unless this option is specified.
171 .It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
176 to track read owners of a lock for priority propagation.
177 This data structure is only used internally by
179 and must persist until
182 This data structure can be allocated on the stack since
183 readers cannot sleep.
184 If any thread holds this lock exclusively, the current thread blocks,
185 and its priority is propagated to the exclusive holder.
186 If the lock was initialized with the
190 function can be called when the current thread has already acquired reader
193 .It Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
198 will return 0 if the lock cannot be acquired immediately;
200 the lock will be acquired and a non-zero value will be returned.
203 may fail even while the lock is not currently held by a writer.
204 If the lock was initialized with the
208 will succeed if the current thread has already acquired reader access.
209 .It Fn rm_wlock "struct rmlock *rm"
213 If there are any shared owners of the lock, the current thread blocks.
216 function cannot be called recursively.
217 .It Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
218 This function releases a shared lock previously acquired by
222 argument must match the
224 argument used for acquiring the shared lock
225 .It Fn rm_wunlock "struct rmlock *rm"
226 This function releases an exclusive lock previously acquired by
228 .It Fn rm_destroy "struct rmlock *rm"
229 This functions destroys a lock previously initialized with
233 lock must be unlocked.
234 .It Fn rm_wowned "const struct rmlock *rm"
235 This function returns a non-zero value if the current thread owns an
238 .It Fn rm_sleep "void *wchan" "struct rmlock *rm" "int priority" "const char *wmesg" "int timo"
239 This function atomically releases
241 while waiting for an event.
244 lock must be exclusively locked.
245 For more details on the parameters to this function,
248 .It Fn rm_assert "struct rmlock *rm" "int what"
249 This function asserts that the
251 lock is in the state specified by
253 If the assertions are not true and the kernel is compiled with
254 .Cd "options INVARIANTS"
256 .Cd "options INVARIANT_SUPPORT" ,
257 the kernel will panic.
258 Currently the following base assertions are supported:
259 .Bl -tag -width ".Dv RA_UNLOCKED"
261 Assert that current thread holds either a shared or exclusive lock
265 Assert that current thread holds a shared lock of
268 Assert that current thread holds an exclusive lock of
271 Assert that current thread holds neither a shared nor exclusive lock of
275 In addition, one of the following optional flags may be specified with
280 .Bl -tag -width ".Dv RA_NOTRECURSED"
282 Assert that the current thread holds a recursive lock of
284 .It Dv RA_NOTRECURSED
285 Assert that the current thread does not hold a recursive lock of
299 functions appeared in
305 facility was written by
306 .An "Stephan Uphoff" .
307 This manual page was written by
309 for rwlock and modified to reflect rmlock by
310 .An "Stephan Uphoff" .
314 implementation is currently not optimized for single processor systems.
317 can fail transiently even when there is no writer, while another reader
318 updates the state on the local CPU.
322 implementation uses a single per CPU list shared by all
323 rmlocks in the system.
324 If rmlocks become popular, hashing to multiple per CPU queues may
325 be needed to speed up the writer lock process.