]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/lock.9
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / share / man / man9 / lock.9
1 .\"
2 .\" Copyright (C) 2002 Chad David <davidc@acns.ab.ca>. All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice(s), this list of conditions and the following disclaimer as
9 .\"    the first lines of this file unmodified other than the possible
10 .\"    addition of one or more copyright notices.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice(s), this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\"
15 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
16 .\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 .\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
19 .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25 .\" DAMAGE.
26 .\"
27 .\" $FreeBSD$
28 .\"
29 .Dd April 12, 2008
30 .Dt LOCK 9
31 .Os
32 .Sh NAME
33 .Nm lockinit ,
34 .Nm lockdestroy ,
35 .Nm lockmgr ,
36 .Nm lockmgr_args ,
37 .Nm lockmgr_args_rw ,
38 .Nm lockmgr_disown ,
39 .Nm lockmgr_printinfo ,
40 .Nm lockmgr_recursed ,
41 .Nm lockmgr_rw ,
42 .Nm lockmgr_waiters ,
43 .Nm lockstatus ,
44 .Nm lockmgr_assert
45 .Nd "lockmgr family of functions"
46 .Sh SYNOPSIS
47 .In sys/types.h
48 .In sys/lock.h
49 .In sys/lockmgr.h
50 .Ft void
51 .Fn lockinit "struct lock *lkp" "int prio" "const char *wmesg" "int timo" "int flags"
52 .Ft void
53 .Fn lockdestroy "struct lock *lkp"
54 .Ft int
55 .Fn lockmgr "struct lock *lkp" "u_int flags" "struct mtx *ilk"
56 .Ft int
57 .Fn lockmgr_args "struct lock *lkp" "u_int flags" "struct mtx *ilk" "const char *wmesg" "int prio" "int timo"
58 .Ft int
59 .Fn lockmgr_args_rw "struct lock *lkp" "u_int flags" "struct rwlock *ilk" "const char *wmesg" "int prio" "int timo"
60 .Ft void
61 .Fn lockmgr_disown "struct lock *lkp"
62 .Ft void
63 .Fn lockmgr_printinfo "struct lock *lkp"
64 .Ft int
65 .Fn lockmgr_recursed "struct lock *lkp"
66 .Ft int
67 .Fn lockmgr_rw "struct lock *lkp" "u_int flags" "struct rwlock *ilk"
68 .Ft int
69 .Fn lockmgr_waiters "struct lock *lkp"
70 .Ft int
71 .Fn lockstatus "struct lock *lkp"
72 .Pp
73 .Cd "options INVARIANTS"
74 .Cd "options INVARIANT_SUPPORT"
75 .Ft void
76 .Fn lockmgr_assert "struct lock *lkp" "int what"
77 .Sh DESCRIPTION
78 The
79 .Fn lockinit
80 function is used to initialize a lock.
81 It must be called before any operation can be performed on a lock.
82 Its arguments are:
83 .Bl -tag -width ".Fa wmesg"
84 .It Fa lkp
85 A pointer to the lock to initialize.
86 .It Fa prio
87 The priority passed to
88 .Xr sleep 9 .
89 .It Fa wmesg
90 The lock message.
91 This is used for both debugging output and
92 .Xr sleep 9 .
93 .It Fa timo
94 The timeout value passed to
95 .Xr sleep 9 .
96 .It Fa flags
97 The flags the lock is to be initialized with:
98 .Bl -tag -width ".Dv LK_CANRECURSE"
99 .It Dv LK_CANRECURSE
100 Allow recursive exclusive locks.
101 .It Dv LK_NOPROFILE
102 Disable lock profiling for this lock.
103 .It Dv LK_NOSHARE
104 Allow exclusive locks only.
105 .It Dv LK_NOWITNESS
106 Instruct
107 .Xr witness 4
108 to ignore this lock.
109 .It Dv LK_NODUP
110 .Xr witness 4
111 should log messages about duplicate locks being acquired.
112 .It Dv LK_QUIET
113 Disable
114 .Xr ktr 4
115 logging for this lock.
116 .It Dv LK_TIMELOCK
117 Use
118 .Fa timo
119 during a sleep; otherwise, 0 is used.
120 .El
121 .El
122 .Pp
123 The
124 .Fn lockdestroy
125 function is used to destroy a lock, and while it is called in a number of
126 places in the kernel, it currently does nothing.
127 .Pp
128 The
129 .Fn lockmgr
130 and
131 .Fn lockmgr_rw
132 functions handle general locking functionality within the kernel, including
133 support for shared and exclusive locks, and recursion.
134 .Fn lockmgr
135 and
136 .Fn lockmgr_rw
137 are also able to upgrade and downgrade locks.
138 .Pp
139 Their arguments are:
140 .Bl -tag -width ".Fa flags"
141 .It Fa lkp
142 A pointer to the lock to manipulate.
143 .It Fa flags
144 Flags indicating what action is to be taken.
145 .Bl -tag -width ".Dv LK_CANRECURSE"
146 .It Dv LK_SHARED
147 Acquire a shared lock.
148 If an exclusive lock is currently held, it will be downgraded.
149 .It Dv LK_EXCLUSIVE
150 Acquire an exclusive lock.
151 If an exclusive lock is already held, and
152 .Dv LK_CANRECURSE
153 is not set, the system will
154 .Xr panic 9 .
155 .It Dv LK_DOWNGRADE
156 Downgrade exclusive lock to a shared lock.
157 Downgrading a shared lock is not permitted.
158 If an exclusive lock has been recursed, all references will be downgraded.
159 .It Dv LK_UPGRADE
160 Upgrade a shared lock to an exclusive lock.
161 If this call fails, the shared lock is lost.
162 During the upgrade, the shared lock could
163 be temporarily dropped.
164 Attempts to upgrade an exclusive lock will cause a
165 .Xr panic 9 .
166 .It Dv LK_RELEASE
167 Release the lock.
168 Releasing a lock that is not held can cause a
169 .Xr panic 9 .
170 .It Dv LK_DRAIN
171 Wait for all activity on the lock to end, then mark it decommissioned.
172 This is used before freeing a lock that is part of a piece of memory that is
173 about to be freed.
174 (As documented in
175 .In sys/lockmgr.h . )
176 .It Dv LK_SLEEPFAIL
177 Fail if operation has slept.
178 .It Dv LK_NOWAIT
179 Do not allow the call to sleep.
180 This can be used to test the lock.
181 .It Dv LK_NOWITNESS
182 Skip the
183 .Xr witness 4
184 checks for this instance.
185 .It Dv LK_CANRECURSE
186 Allow recursion on an exclusive lock.
187 For every lock there must be a release.
188 .It Dv LK_INTERLOCK
189 Unlock the interlock (which should be locked already).
190 .El
191 .It Fa ilk
192 An interlock mutex for controlling group access to the lock.
193 If
194 .Dv LK_INTERLOCK
195 is specified,
196 .Fn lockmgr
197 and
198 .Fn lockmgr_rw
199 assume
200 .Fa ilk
201 is currently owned and not recursed, and will return it unlocked.
202 See
203 .Xr mtx_assert 9 .
204 .El
205 .Pp
206 The
207 .Fn lockmgr_args
208 and
209 .Fn lockmgr_args_rw
210 function work like
211 .Fn lockmgr
212 and
213 .Fn lockmgr_rw
214 but accepting a
215 .Fa wmesg ,
216 .Fa timo
217 and
218 .Fa prio
219 on a per-instance basis.
220 The specified values will override the default
221 ones, but this can still be used passing, respectively,
222 .Dv LK_WMESG_DEFAULT ,
223 .Dv LK_PRIO_DEFAULT
224 and
225 .Dv LK_TIMO_DEFAULT .
226 .Pp
227 The
228 .Fn lockmgr_disown
229 function switches the owner from the current thread to be
230 .Dv LK_KERNPROC,
231 if the lock is already held.
232 .Pp
233 The
234 .Fn lockmgr_printinfo
235 function prints debugging information about the lock.
236 It is used primarily by
237 .Xr VOP_PRINT 9
238 functions.
239 .Pp
240 The
241 .Fn lockmgr_recursed
242 function returns true if the lock is recursed, 0
243 otherwise.
244 .Pp
245 The
246 .Fn lockmgr_waiters
247 function returns true if the lock has waiters, 0 otherwise.
248 .Pp
249 The
250 .Fn lockstatus
251 function returns the status of the lock in relation to the current thread.
252 .Pp
253 When compiled with
254 .Cd "options INVARIANTS"
255 and
256 .Cd "options INVARIANT_SUPPORT" ,
257 the
258 .Fn lockmgr_assert
259 function tests
260 .Fa lkp
261 for the assertions specified in
262 .Fa what ,
263 and panics if they are not met.
264 One of the following assertions must be specified:
265 .Bl -tag -width ".Dv KA_UNLOCKED"
266 .It Dv KA_LOCKED
267 Assert that the current thread has either a shared or an exclusive lock on the
268 .Vt lkp
269 lock pointed to by the first argument.
270 .It Dv KA_SLOCKED
271 Assert that the current thread has a shared lock on the
272 .Vt lkp
273 lock pointed to by the first argument.
274 .It Dv KA_XLOCKED
275 Assert that the current thread has an exclusive lock on the
276 .Vt lkp
277 lock pointed to by the first argument.
278 .It Dv KA_UNLOCKED
279 Assert that the current thread has no lock on the
280 .Vt lkp
281 lock pointed to by the first argument.
282 .It Dv KA_HELD
283 Assert that an unspecified thread has a lock on the
284 .Vt lkp
285 lock pointed to by the first argument.
286 .It Dv KA_UNHELD
287 Assert that no thread has a lock on the
288 .Vt lkp
289 lock pointed to by the first argument.
290 .El
291 .Pp
292 In addition, one of the following optional assertions can be used with
293 either an
294 .Dv KA_LOCKED ,
295 .Dv KA_SLOCKED ,
296 or
297 .Dv KA_XLOCKED
298 assertion:
299 .Bl -tag -width ".Dv KA_NOTRECURSED"
300 .It Dv KA_RECURSED
301 Assert that the current thread has a recursed lock on
302 .Fa lkp .
303 .It Dv KA_NOTRECURSED
304 Assert that the current thread does not have a recursed lock on
305 .Fa lkp .
306 .El
307 .Pp
308 Note that
309 .Dv KA_HELD
310 and
311 .Dv KA_UNHELD
312 usage is highly discouraged.
313 They are intended to cater a bad behaviour
314 introduced by buffer cache lock handling.
315 They will hopefully be
316 made useless by revisiting such locks.
317 .Sh RETURN VALUES
318 The
319 .Fn lockmgr
320 and
321 .Fn lockmgr_rw
322 functions return 0 on success and non-zero on failure.
323 .Pp
324 The
325 .Fn lockstatus
326 function returns:
327 .Bl -tag -width ".Dv LK_EXCLUSIVE"
328 .It Dv LK_EXCLUSIVE
329 An exclusive lock is held by the current thread.
330 .It Dv LK_EXCLOTHER
331 An exclusive lock is held by someone other than the current thread.
332 .It Dv LK_SHARED
333 A shared lock is held.
334 .It Li 0
335 The lock is not held by anyone.
336 .El
337 .Sh ERRORS
338 .Fn lockmgr
339 and
340 .Fn lockmgr_rw
341 fail if:
342 .Bl -tag -width Er
343 .It Bq Er EBUSY
344 .Dv LK_FORCEUPGRADE
345 was requested and another thread had already requested a lock upgrade.
346 .It Bq Er EBUSY
347 .Dv LK_NOWAIT
348 was set, and a sleep would have been required.
349 .It Bq Er ENOLCK
350 .Dv LK_SLEEPFAIL
351 was set and
352 .Fn lockmgr
353 or
354 .Fn lockmgr_rw
355 did sleep.
356 .It Bq Er EINTR
357 .Dv PCATCH
358 was set in the lock priority, and a signal was delivered during a sleep.
359 Note the
360 .Er ERESTART
361 error below.
362 .It Bq Er ERESTART
363 .Dv PCATCH
364 was set in the lock priority, a signal was delivered during a sleep,
365 and the system call is to be restarted.
366 .It Bq Er EWOULDBLOCK
367 a non-zero timeout was given, and the timeout expired.
368 .El
369 .Sh LOCKS
370 If
371 .Dv LK_INTERLOCK
372 is passed in the
373 .Fa flags
374 argument to
375 .Fn lockmgr
376 or
377 .Fn lockmgr_rw ,
378 the
379 .Fa ilk
380 must be held prior to calling
381 .Fn lockmgr 
382 or
383 .Fn lockmgr_rw ,
384 and will be returned unlocked.
385 .Pp
386 Upgrade attempts that fail result in the loss of the lock that
387 is currently held.
388 Also, it is invalid to upgrade an
389 exclusive lock, and a
390 .Xr panic 9
391 will be the result of trying.
392 .Sh SEE ALSO
393 .Xr condvar 9 ,
394 .Xr locking 9 ,
395 .Xr mutex 9 ,
396 .Xr rwlock 9 ,
397 .Xr sleep 9 ,
398 .Xr sx 9 ,
399 .Xr mtx_assert 9 ,
400 .Xr panic 9 ,
401 .Xr VOP_PRINT 9
402 .Sh AUTHORS
403 This manual page was written by
404 .An Chad David Aq davidc@acns.ab.ca .