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
44 .Nd kernel reader/writer lock optimized for mostly read access patterns
50 .Fn rm_init "struct rmlock *rm" "const char *name"
52 .Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
54 .Fn rm_destroy "struct rmlock *rm"
56 .Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
58 .Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
60 .Fn rm_wlock "struct rmlock *rm"
62 .Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
64 .Fn rm_wunlock "struct rmlock *rm"
66 .Fn rm_wowned "struct rmlock *rm"
68 .Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc" "int opts"
70 Mostly reader locks allow shared access to protected data by multiple threads,
71 or exclusive access by a single thread.
72 The threads with shared access are known as
74 since they only read the protected data.
75 A thread with exclusive access is known as a
77 since it can modify protected data.
79 Read mostly locks are designed to be efficient for locks almost exclusively
80 used as reader locks and as such should be used for protecting data that
82 Acquiring an exclusive lock after the lock had been locked for shared access
83 is an expensive operation.
85 Although reader/writer locks look very similar to
87 locks, their usage pattern is different.
88 Reader/writer locks can be treated as mutexes (see
90 with shared/exclusive semantics unless initialized with
96 can be locked while holding a non-spin mutex, and an
98 cannot be held while sleeping, again unless initialized with
102 locks have full priority propagation like mutexes.
105 structure argument supplied in
109 is used to keep track of the read owner(s).
110 Another important property is that shared holders of
112 can recurse if the lock has been initialized with the
114 option, however exclusive locks are not allowed to recurse.
115 .Ss Macros and Functions
116 .Bl -tag -width indent
117 .It Fn rm_init "struct rmlock *rm" "const char *name"
118 Initialize structure located at
120 as mostly reader lock, described by
122 The name description is used solely for debugging purposes.
123 This function must be called before any other operations
125 .It Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
126 Initialize the rm lock just like the
128 function, but specifying a set of optional flags to alter the
134 It contains one or more of the following flags:
135 .Bl -tag -width ".Dv RM_NOWITNESS"
141 Allow threads to recursively acquire exclusive locks for
144 Allow writers to sleep while holding the lock.
145 Readers must not sleep while holding the lock and can avoid to sleep on
146 taking the lock by using
151 .It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
157 to track read owners of a lock for priority propagation.
158 This data structure is only used internally by
160 and must persist until
163 This data structure can be allocated on the stack since
164 rmlocks cannot be held while sleeping.
165 If any thread holds this lock exclusively, the current thread blocks,
166 and its priority is propagated to the exclusive holder.
167 If the lock was initialized with the
171 function can be called when the thread has already acquired reader
175 .Dq "recursing on a lock" .
176 .It Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
181 will return 0 if the lock cannot be acquired immediately;
182 otherwise the lock will be acquired and a non-zero value will be returned.
185 may fail even while the lock is not currently held by a writer.
186 .It Fn rm_wlock "struct rmlock *rm"
190 If there are any shared owners of the lock, the current thread blocks.
193 function cannot be called recursively.
194 .It Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
195 This function releases a shared lock previously acquired by
199 argument must match the
201 argument used for acquiring the shared lock
202 .It Fn rm_wunlock "struct rmlock *rm"
203 This function releases an exclusive lock previously acquired by
205 .It Fn rm_destroy "struct rmlock *rm"
206 This functions destroys a lock previously initialized with
210 lock must be unlocked.
211 .It Fn rm_wowned "struct rmlock *rm"
212 This function returns a non-zero value if the current thread owns an
225 functions appeared in
231 facility was written by
232 .An "Stephan Uphoff" .
233 This manual page was written by
235 for rwlock and modified to reflect rmlock by
236 .An "Stephan Uphoff" .
240 implementation is currently not optimized for single processor systems.
243 can fail transiently even when there is no writer, while another reader
244 updates the state on the local CPU.
248 implementation uses a single per CPU list shared by all
249 rmlocks in the system.
250 If rmlocks become popular, hashing to multiple per CPU queues may
251 be needed to speed up the writer lock process.
255 can currently not be used as a lock argument for condition variable