]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - share/man/man9/rmlock.9
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / share / man / man9 / rmlock.9
1 .\" Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
2 .\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org>
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
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.
13 .\"
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
24 .\" SUCH DAMAGE.
25 .\"
26 .\" $FreeBSD$
27 .\"
28 .\" Based on rwlock.9 man page
29 .Dd November 10, 2007
30 .Dt RMLOCK 9
31 .Os
32 .Sh NAME
33 .Nm rmlock ,
34 .Nm rm_init ,
35 .Nm rm_init_flags ,
36 .Nm rm_destroy ,
37 .Nm rm_rlock ,
38 .Nm rm_try_rlock ,
39 .Nm rm_wlock ,
40 .Nm rm_runlock ,
41 .Nm rm_wunlock ,
42 .Nm rm_wowned ,
43 .Nm RM_SYSINIT
44 .Nd kernel reader/writer lock optimized for mostly read access patterns
45 .Sh SYNOPSIS
46 .In sys/param.h
47 .In sys/lock.h
48 .In sys/rmlock.h
49 .Ft void
50 .Fn rm_init "struct rmlock *rm" "const char *name"
51 .Ft void
52 .Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
53 .Ft void
54 .Fn rm_destroy "struct rmlock *rm"
55 .Ft void
56 .Fn rm_rlock "struct rmlock *rm"  "struct rm_priotracker* tracker"
57 .Ft int
58 .Fn rm_try_rlock "struct rmlock *rm"  "struct rm_priotracker* tracker"
59 .Ft void
60 .Fn rm_wlock "struct rmlock *rm"
61 .Ft void
62 .Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
63 .Ft void
64 .Fn rm_wunlock "struct rmlock *rm"
65 .Ft int
66 .Fn rm_wowned "struct rmlock *rm"
67 .In sys/kernel.h
68 .Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc" "int opts"
69 .Sh DESCRIPTION
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
73 .Em readers
74 since they only read the protected data.
75 A thread with exclusive access is known as a
76 .Em writer
77 since it can modify protected data.
78 .Pp
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
81 rarely changes.
82 Acquiring an exclusive lock after the lock had been locked for shared access
83 is an expensive operation.
84 .Pp
85 Although reader/writer locks look very similar to
86 .Xr sx 9
87 locks, their usage pattern is different.
88 Reader/writer locks can be treated as mutexes (see
89 .Xr mutex 9 )
90 with shared/exclusive semantics unless initialized with
91 .Dv RM_SLEEPABLE .
92 Unlike
93 .Xr sx 9 ,
94 an
95 .Nm
96 can be locked while holding a non-spin mutex, and an
97 .Nm
98 cannot be held while sleeping, again unless initialized with
99 .Dv RM_SLEEPABLE .
100 The
101 .Nm
102 locks have full priority propagation like mutexes.
103 The
104 .Va rm_priotracker
105 structure argument supplied in
106 .Fn rm_rlock
107 and
108 .Fn rm_runlock
109 is used to keep track of the read owner(s).
110 Another important property is that shared holders of
111 .Nm
112 can recurse if the lock has been initialized with the
113 .Dv LO_RECURSABLE
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
119 .Fa rm
120 as mostly reader lock, described by
121 .Fa name .
122 The name description is used solely for debugging purposes.
123 This function must be called before any other operations
124 on the lock.
125 .It Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts"
126 Initialize the rm lock just like the
127 .Fn rm_init
128 function, but specifying a set of optional flags to alter the
129 behaviour of
130 .Fa rm ,
131 through the
132 .Fa opts
133 argument.
134 It contains one or more of the following flags:
135 .Bl -tag -width ".Dv RM_NOWITNESS"
136 .It Dv RM_NOWITNESS
137 Instruct
138 .Xr witness 4
139 to ignore this lock.
140 .It Dv RM_RECURSE
141 Allow threads to recursively acquire exclusive locks for
142 .Fa rm .
143 .It Dv RM_SLEEPABLE
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
147 .Fn rm_try_rlock
148 instead of
149 .Fn rm_rlock .
150 .El
151 .It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
152 Lock
153 .Fa rm
154 as a reader.
155 Using
156 .Fa tracker
157 to track read owners of a lock for priority propagation.
158 This data structure is only used internally by
159 .Nm
160 and must persist until
161 .Fn rm_runlock
162 has been called.
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
168 .Dv LO_RECURSABLE
169 option the
170 .Fn rm_rlock
171 function can be called when the thread has already acquired reader
172 access on
173 .Fa rm .
174 This is called
175 .Dq "recursing on a lock" .
176 .It Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
177 Try to lock
178 .Fa rm
179 as a reader.
180 .Fn rm_try_rlock
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.
183 Note that
184 .Fn rm_try_rlock
185 may fail even while the lock is not currently held by a writer.
186 .It Fn rm_wlock "struct rmlock *rm"
187 Lock
188 .Fa rm
189 as a writer.
190 If there are any shared owners of the lock, the current thread blocks.
191 The
192 .Fn rm_wlock
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
196 .Fn rm_rlock .
197 The
198 .Fa tracker
199 argument must match the
200 .Fa tracker
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
204 .Fn rm_wlock .
205 .It Fn rm_destroy "struct rmlock *rm"
206 This functions destroys a lock previously initialized with
207 .Fn rm_init .
208 The
209 .Fa rm
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
213 exclusive lock on
214 .Fa rm .
215 .El
216 .Sh SEE ALSO
217 .Xr locking 9 ,
218 .Xr mutex 9 ,
219 .Xr panic 9 ,
220 .Xr rwlock 9 ,
221 .Xr sema 9 ,
222 .Xr sx 9
223 .Sh HISTORY
224 These
225 functions appeared in
226 .Fx 7.0 .
227 .Sh AUTHORS
228 .An -nosplit
229 The
230 .Nm
231 facility was written by
232 .An "Stephan Uphoff" .
233 This manual page was written by
234 .An "Gleb Smirnoff"
235 for rwlock and modified to reflect rmlock by
236 .An "Stephan Uphoff" .
237 .Sh BUGS
238 The
239 .Nm
240 implementation is currently not optimized for single processor systems.
241 .Pp
242 .Fn rm_try_rlock
243 can fail transiently even when there is no writer, while another reader
244 updates the state on the local CPU.
245 .Pp
246 The
247 .Nm
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.
252 .Pp
253 The
254 .Nm
255 can currently not be used as a lock argument for condition variable
256 wait functions.