]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/rmlock.9
Add rm_wowned(9) function to test whether the current thread owns an
[FreeBSD/FreeBSD.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_destroy ,
36 .Nm rm_rlock ,
37 .Nm rm_wlock ,
38 .Nm rm_runlock ,
39 .Nm rm_wunlock ,
40 .Nm rm_wowned ,
41 .Nm RM_SYSINIT
42 .Nd mostly read lock - a kernel reader/writer lock optimized for mostly read access patterns
43 .Sh SYNOPSIS
44 .In sys/param.h
45 .In sys/lock.h
46 .In sys/rmlock.h
47 .Ft void
48 .Fn rm_init "struct rmlock *rm" "const char *name" "int opts"
49 .Ft void
50 .Fn rm_destroy "struct rmlock *rm"
51 .Ft void
52 .Fn rm_rlock "struct rmlock *rm"  "struct rm_priotracker* tracker"
53 .Ft void
54 .Fn rm_wlock "struct rmlock *rm"
55 .Ft void
56 .Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
57 .Ft void
58 .Fn rm_wunlock "struct rmlock *rm"
59 .Ft int
60 .Fn rm_wowned "struct rmlock *rm"
61 .In sys/kernel.h
62 .Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc" "int opts"
63 .Sh DESCRIPTION
64 Mostly reader locks allow shared access to protected data by multiple threads,
65 or exclusive access by a single thread.
66 The threads with shared access are known as
67 .Em readers
68 since they only read the protected data.
69 A thread with exclusive access is known as a
70 .Em writer
71 since it can modify protected data.
72 .Pp
73 Read mostly locks are designed to be efficient for locks almost exclusively
74 used as reader locks and as such should be used for protecting data that
75 rarely changes.
76 Acquiring an exclusive lock after the lock had been locked for shared access
77 is an expensive operation. 
78 .Pp
79 Although reader/writer locks look very similar to
80 .Xr sx 9
81 locks, their usage pattern is different.
82 Reader/writer locks can be treated as mutexes (see
83 .Xr mutex 9 )
84 with shared/exclusive semantics.
85 Unlike
86 .Xr sx 9 ,
87 an
88 .Nm
89 can be locked while holding a non-spin mutex, and an
90 .Nm
91 cannot be held while sleeping.
92 The
93 .Nm
94 locks have full priority propagation like mutexes. The
95 .Va rm_priotracker
96 structure argument supplied in
97 .Fn rm_rlock
98 and
99 .Fn rm_runlock
100 is used to keep track of the read owner(s).
101 Another important property is that shared holders of
102 .Nm
103 can recurse if the lock has been initialized with the
104 .Dv LO_RECURSABLE
105 option, however exclusive locks are not allowed to recurse.
106 .Ss Macros and Functions
107 .Bl -tag -width indent
108 .It Fn rm_init "struct rmlock *rm" "const char *name" "int opts"
109 Initialize structure located at
110 .Fa rm
111 as mostly reader lock, described by
112 .Fa name .
113 Optionally allowing readers to recurse by setting
114 .Dv LO_RECURSABLE
115 in
116 .Fa opts
117 The name description is used solely for debugging purposes.
118 This function must be called before any other operations
119 on the lock.
120 .It Fn rm_rlock "struct rmlock *rm"  "struct rm_priotracker* tracker"
121 Lock
122 .Fa rm
123 as a reader. Using
124 .Fa tracker
125 to track read owners of a lock for priority propagation.
126 This data structure is only used internally by
127 .Nm
128 and must persist until rm_runlock has been called.
129 This data structure can be allocated on the stack since
130 rmlocks cannot be held while sleeping.
131 If any thread holds this lock exclusively, the current thread blocks,
132 and its priority is propagated to the exclusive holder.
133 If the lock was initialized with the
134 .Dv LO_RECURSABLE
135 option the
136 .Fn rm_rlock
137 function can be called when the thread has already acquired reader
138 access on
139 .Fa rm .
140 This is called
141 .Dq "recursing on a lock" .
142 .It Fn rm_wlock "struct rmlock *rm"
143 Lock
144 .Fa rm
145 as a writer.
146 If there are any shared owners of the lock, the current thread blocks.
147 The
148 .Fn rm_wlock
149 function cannot be called recursively.
150 .It Fn rm_runlock "struct rmlock *rm"  "struct rm_priotracker* tracker"
151 This function releases a shared lock previously acquired by
152 .Fn rm_rlock .
153 The
154 .Fa tracker
155 argument must match the 
156 .Fa tracker 
157 argument used for acquiring the shared lock 
158 .It Fn rm_wunlock "struct rmlock *rm"
159 This function releases an exclusive lock previously acquired by
160 .Fn rm_wlock .
161 .It Fn rm_destroy "struct rmlock *rm"
162 This functions destroys a lock previously initialized with
163 .Fn rm_init .
164 The
165 .Fa rm
166 lock must be unlocked.
167 .It Fn rm_wowned "struct rmlock *rm"
168 This function returns a non-zero value if the current thread owns an
169 exclusive lock on
170 .Fa rm .
171 .El
172 .El
173 .Sh SEE ALSO
174 .Xr locking 9 ,
175 .Xr mutex 9 ,
176 .Xr panic 9 ,
177 .Xr rwlock 9,
178 .Xr sema 9 ,
179 .Xr sx 9
180 .Sh HISTORY
181 These
182 functions appeared in
183 .Fx 7.0 .
184 .Sh AUTHORS
185 .An -nosplit
186 The
187 .Nm
188 facility was written by
189 .An "Stephan Uphoff" .
190 This manual page was written by
191 .An "Gleb Smirnoff"
192 for rwlock and modified to reflect rmlock by
193 .An "Stephan Uphoff" .
194 .Sh BUGS
195 .Pp
196 The
197 .Nm
198 implementation is currently not optimized for single processor systems.
199 .Pp
200 The
201 .Nm
202 implementation uses a single per cpu list shared by all
203 rmlocks in the system.
204 If rmlocks become popular, hashing to multiple per cpu queues may
205 be needed to speed up the writer lock process. 
206 .Pp
207 The
208 .Nm
209 can currently not be used as a lock argument for condition variable
210 wait functions.