]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - share/man/man9/mutex.9
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / share / man / man9 / mutex.9
1 .\"
2 .\" Copyright (c) 1998 Berkeley Software Design, Inc. 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, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Berkeley Software Design Inc's name may not be used to endorse or
13 .\"    promote products derived from this software without specific prior
14 .\"    written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     from BSDI $Id: mutex.4,v 1.1.2.3 1998/04/27 22:53:13 ewv Exp $
29 .\" $FreeBSD$
30 .\"
31 .Dd December 21, 2006
32 .Dt MUTEX 9
33 .Os
34 .Sh NAME
35 .Nm mutex ,
36 .Nm mtx_init ,
37 .Nm mtx_destroy ,
38 .Nm mtx_lock ,
39 .Nm mtx_lock_spin ,
40 .Nm mtx_lock_flags ,
41 .Nm mtx_lock_spin_flags ,
42 .Nm mtx_trylock ,
43 .Nm mtx_trylock_flags ,
44 .Nm mtx_unlock ,
45 .Nm mtx_unlock_spin ,
46 .Nm mtx_unlock_flags ,
47 .Nm mtx_unlock_spin_flags ,
48 .Nm mtx_sleep ,
49 .Nm mtx_initialized ,
50 .Nm mtx_owned ,
51 .Nm mtx_recursed ,
52 .Nm mtx_assert ,
53 .Nm MTX_SYSINIT
54 .Nd kernel synchronization primitives
55 .Sh SYNOPSIS
56 .In sys/param.h
57 .In sys/lock.h
58 .In sys/mutex.h
59 .Ft void
60 .Fn mtx_init "struct mtx *mutex" "const char *name" "const char *type" "int opts"
61 .Ft void
62 .Fn mtx_destroy "struct mtx *mutex"
63 .Ft void
64 .Fn mtx_lock "struct mtx *mutex"
65 .Ft void
66 .Fn mtx_lock_spin "struct mtx *mutex"
67 .Ft void
68 .Fn mtx_lock_flags "struct mtx *mutex" "int flags"
69 .Ft void
70 .Fn mtx_lock_spin_flags "struct mtx *mutex" "int flags"
71 .Ft int
72 .Fn mtx_trylock "struct mtx *mutex"
73 .Ft int
74 .Fn mtx_trylock_flags "struct mtx *mutex" "int flags"
75 .Ft void
76 .Fn mtx_unlock "struct mtx *mutex"
77 .Ft void
78 .Fn mtx_unlock_spin "struct mtx *mutex"
79 .Ft void
80 .Fn mtx_unlock_flags "struct mtx *mutex" "int flags"
81 .Ft void
82 .Fn mtx_unlock_spin_flags "struct mtx *mutex" "int flags"
83 .Ft int
84 .Fn mtx_sleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
85 .Ft int
86 .Fn mtx_initialized "struct mtx *mutex"
87 .Ft int
88 .Fn mtx_owned "struct mtx *mutex"
89 .Ft int
90 .Fn mtx_recursed "struct mtx *mutex"
91 .Pp
92 .Cd "options INVARIANTS"
93 .Cd "options INVARIANT_SUPPORT"
94 .Ft void
95 .Fn mtx_assert "struct mtx *mutex" "int what"
96 .In sys/kernel.h
97 .Fn MTX_SYSINIT "name" "struct mtx *mtx" "const char *description" "int opts"
98 .Sh DESCRIPTION
99 Mutexes are the most basic and primary method of thread synchronization.
100 The major design considerations for mutexes are:
101 .Bl -enum
102 .It
103 Acquiring and releasing uncontested mutexes should be as cheap
104 as possible.
105 .It
106 They must have the information and storage space to support
107 priority propagation.
108 .It
109 A thread must be able to recursively acquire a mutex,
110 provided that the mutex is initialized to support recursion.
111 .El
112 .Pp
113 There are currently two flavors of mutexes, those that context switch
114 when they block and those that do not.
115 .Pp
116 By default,
117 .Dv MTX_DEF
118 mutexes will context switch when they are already held.
119 As an optimization,
120 they may spin for some amount
121 of time before context switching.
122 It is important to remember that since a thread may be preempted at any time,
123 the possible context switch introduced by acquiring a mutex is guaranteed
124 to not break anything that is not already broken.
125 .Pp
126 Mutexes which do not context switch are
127 .Dv MTX_SPIN
128 mutexes.
129 These should only be used to protect data shared with primary interrupt
130 code.
131 This includes interrupt filters and low level scheduling code.
132 In all architectures both acquiring and releasing of a
133 uncontested spin mutex is more expensive than the same operation
134 on a non-spin mutex.
135 In order to protect an interrupt service routine from blocking
136 against itself all interrupts are either blocked or deferred on a processor
137 while holding a spin lock.
138 It is permissible to hold multiple spin mutexes.
139 .Pp
140 Once a spin mutex has been acquired it is not permissible to acquire a
141 blocking mutex.
142 .Pp
143 The storage needed to implement a mutex is provided by a
144 .Vt struct mtx .
145 In general this should be treated as an opaque object and
146 referenced only with the mutex primitives.
147 .Pp
148 The
149 .Fn mtx_init
150 function must be used to initialize a mutex
151 before it can be passed to any of the other mutex functions.
152 The
153 .Fa name
154 option is used to identify the lock in debugging output etc.
155 The
156 .Fa type
157 option is used by the witness code to classify a mutex when doing checks
158 of lock ordering.
159 If
160 .Fa type
161 is
162 .Dv NULL ,
163 .Fa name
164 is used in its place.
165 The pointer passed in as
166 .Fa name
167 and
168 .Fa type
169 is saved rather than the data it points to.
170 The data pointed to must remain stable
171 until the mutex is destroyed.
172 The
173 .Fa opts
174 argument is used to set the type of mutex.
175 It may contain either
176 .Dv MTX_DEF
177 or
178 .Dv MTX_SPIN
179 but not both.
180 See below for additional initialization options.
181 It is not permissible to pass the same
182 .Fa mutex
183 to
184 .Fn mtx_init
185 multiple times without intervening calls to
186 .Fn mtx_destroy .
187 .Pp
188 The
189 .Fn mtx_lock
190 function acquires a
191 .Dv MTX_DEF
192 mutual exclusion lock
193 on behalf of the currently running kernel thread.
194 If another kernel thread is holding the mutex,
195 the caller will be disconnected from the CPU
196 until the mutex is available
197 (i.e., it will block).
198 .Pp
199 The
200 .Fn mtx_lock_spin
201 function acquires a
202 .Dv MTX_SPIN
203 mutual exclusion lock
204 on behalf of the currently running kernel thread.
205 If another kernel thread is holding the mutex,
206 the caller will spin until the mutex becomes available.
207 Interrupts are disabled during the spin and remain disabled
208 following the acquiring of the lock.
209 .Pp
210 It is possible for the same thread to recursively acquire a mutex
211 with no ill effects, provided that the
212 .Dv MTX_RECURSE
213 bit was passed to
214 .Fn mtx_init
215 during the initialization of the mutex.
216 .Pp
217 The
218 .Fn mtx_lock_flags
219 and
220 .Fn mtx_lock_spin_flags
221 functions acquire a
222 .Dv MTX_DEF
223 or
224 .Dv MTX_SPIN
225 lock, respectively, and also accept a
226 .Fa flags
227 argument.
228 In both cases, the only flag presently available for lock acquires is
229 .Dv MTX_QUIET .
230 If the
231 .Dv MTX_QUIET
232 bit is turned on in the
233 .Fa flags
234 argument, then if
235 .Dv KTR_LOCK
236 tracing is being done,
237 it will be silenced during the lock acquire.
238 .Pp
239 The
240 .Fn mtx_trylock
241 attempts to acquire the
242 .Dv MTX_DEF
243 mutex pointed to by
244 .Fa mutex .
245 If the mutex cannot be immediately acquired
246 .Fn mtx_trylock
247 will return 0,
248 otherwise the mutex will be acquired
249 and a non-zero value will be returned.
250 .Pp
251 The
252 .Fn mtx_trylock_flags
253 function has the same behavior as
254 .Fn mtx_trylock
255 but should be used when the caller desires to pass in a
256 .Fa flags
257 value.
258 Presently, the only valid value in the
259 .Fn mtx_trylock
260 case is
261 .Dv MTX_QUIET ,
262 and its effects are identical to those described for
263 .Fn mtx_lock
264 above.
265 .Pp
266 The
267 .Fn mtx_unlock
268 function releases a
269 .Dv MTX_DEF
270 mutual exclusion lock.
271 The current thread may be preempted if a higher priority thread is waiting
272 for the mutex.
273 .Pp
274 The
275 .Fn mtx_unlock_spin
276 function releases a
277 .Dv MTX_SPIN
278 mutual exclusion lock.
279 .Pp
280 The
281 .Fn mtx_unlock_flags
282 and
283 .Fn mtx_unlock_spin_flags
284 functions behave in exactly the same way as do the standard mutex
285 unlock routines above, while also allowing a
286 .Fa flags
287 argument which may specify
288 .Dv MTX_QUIET .
289 The behavior of
290 .Dv MTX_QUIET
291 is identical to its behavior in the mutex lock routines.
292 .Pp
293 The
294 .Fn mtx_destroy
295 function is used to destroy
296 .Fa mutex
297 so the data associated with it may be freed
298 or otherwise overwritten.
299 Any mutex which is destroyed
300 must previously have been initialized with
301 .Fn mtx_init .
302 It is permissible to have a single hold count
303 on a mutex when it is destroyed.
304 It is not permissible to hold the mutex recursively,
305 or have another thread blocked on the mutex
306 when it is destroyed.
307 .Pp
308 The
309 .Fn mtx_sleep
310 function is used to atomically release
311 .Fa mtx
312 while waiting for an event.
313 For more details on the parameters to this function,
314 see
315 .Xr sleep 9 .
316 .Pp
317 The
318 .Fn mtx_initialized
319 function returns non-zero if
320 .Fa mutex
321 has been initialized and zero otherwise.
322 .Pp
323 The
324 .Fn mtx_owned
325 function returns non-zero
326 if the current thread holds
327 .Fa mutex .
328 If the current thread does not hold
329 .Fa mutex
330 zero is returned.
331 .Pp
332 The
333 .Fn mtx_recursed
334 function returns non-zero if the
335 .Fa mutex
336 is recursed.
337 This check should only be made if the running thread already owns
338 .Fa mutex .
339 .Pp
340 The
341 .Fn mtx_assert
342 function allows assertions specified in
343 .Fa what
344 to be made about
345 .Fa mutex .
346 If the assertions are not true and the kernel is compiled with
347 .Cd "options INVARIANTS"
348 and
349 .Cd "options INVARIANT_SUPPORT" ,
350 the kernel will panic.
351 Currently the following assertions are supported:
352 .Bl -tag -width MA_NOTRECURSED
353 .It Dv MA_OWNED
354 Assert that the current thread
355 holds the mutex
356 pointed to by the first argument.
357 .It Dv MA_NOTOWNED
358 Assert that the current thread
359 does not hold the mutex
360 pointed to by the first argument.
361 .It Dv MA_RECURSED
362 Assert that the current thread has recursed on the mutex
363 pointed to by the first argument.
364 This assertion is only valid in conjunction with
365 .Dv MA_OWNED .
366 .It Dv MA_NOTRECURSED
367 Assert that the current thread has not recursed on the mutex
368 pointed to by the first argument.
369 This assertion is only valid in conjunction with
370 .Dv MA_OWNED .
371 .El
372 .Pp
373 The
374 .Fn MTX_SYSINIT
375 macro is used to generate a call to the
376 .Fn mtx_sysinit
377 routine at system startup in order to initialize a given mutex lock.
378 The parameters are the same as
379 .Fn mtx_init
380 but with an additional argument,
381 .Fa name ,
382 that is used in generating unique variable names for the related structures associated with the lock and the sysinit routine.
383 .Ss The Default Mutex Type
384 Most kernel code should use the default lock type,
385 .Dv MTX_DEF .
386 The default lock type will allow the thread
387 to be disconnected from the CPU
388 if the lock is already held by another thread.
389 The implementation
390 may treat the lock as a short term spin lock
391 under some circumstances.
392 However, it is always safe to use these forms of locks
393 in an interrupt thread
394 without fear of deadlock
395 against an interrupted thread on the same CPU.
396 .Ss The Spin Mutex Type
397 A
398 .Dv MTX_SPIN
399 mutex will not relinquish the CPU
400 when it cannot immediately get the requested lock,
401 but will loop, waiting for the mutex to be released by another CPU.
402 This could result in deadlock
403 if another thread interrupted the thread which held a mutex
404 and then tried to acquire the mutex.
405 For this reason spin locks disable all interrupts on the local CPU.
406 .Pp
407 Spin locks are fairly specialized locks
408 that are intended to be held for very short periods of time.
409 Their primary purpose is to protect portions of the code
410 that implement other synchronization primitives such as default mutexes,
411 thread scheduling, and interrupt threads.
412 .Ss Initialization Options
413 The options passed in the
414 .Fa opts
415 argument of
416 .Fn mtx_init
417 specify the mutex type.
418 One of the
419 .Dv MTX_DEF
420 or
421 .Dv MTX_SPIN
422 options is required and only one of those two options may be specified.
423 The possibilities are:
424 .Bl -tag -width MTX_NOWITNESS
425 .It Dv MTX_DEF
426 Default mutexes
427 will always allow the current thread to be suspended
428 to avoid deadlock conditions against interrupt threads.
429 The implementation of this lock type
430 may spin for a while before suspending the current thread.
431 .It Dv MTX_SPIN
432 Spin mutexes
433 will never relinquish the CPU.
434 All interrupts are disabled on the local CPU
435 while any spin lock is held.
436 .It Dv MTX_RECURSE
437 Specifies that the initialized mutex is allowed to recurse.
438 This bit must be present if the mutex is permitted to recurse.
439 .It Dv MTX_QUIET
440 Do not log any mutex operations for this lock.
441 .It Dv MTX_NOWITNESS
442 Instruct
443 .Xr witness 4
444 to ignore this lock.
445 .It Dv MTX_DUPOK
446 Witness should not log messages about duplicate locks being acquired.
447 .It Dv MTX_NOPROFILE
448 Do not profile this lock.
449 .El
450 .Ss Lock and Unlock Flags
451 The flags passed to the
452 .Fn mtx_lock_flags ,
453 .Fn mtx_lock_spin_flags ,
454 .Fn mtx_unlock_flags ,
455 and
456 .Fn mtx_unlock_spin_flags
457 functions provide some basic options to the caller,
458 and are often used only under special circumstances to modify lock or
459 unlock behavior.
460 Standard locking and unlocking should be performed with the
461 .Fn mtx_lock ,
462 .Fn mtx_lock_spin ,
463 .Fn mtx_unlock ,
464 and
465 .Fn mtx_unlock_spin
466 functions.
467 Only if a flag is required should the corresponding
468 flags-accepting routines be used.
469 .Pp
470 Options that modify mutex behavior:
471 .Bl -tag -width MTX_QUIET
472 .It Dv MTX_QUIET
473 This option is used to quiet logging messages during individual mutex
474 operations.
475 This can be used to trim superfluous logging messages for debugging purposes.
476 .El
477 .Ss Giant
478 If
479 .Va Giant
480 must be acquired, it must be acquired prior to acquiring
481 other mutexes.
482 Put another way: it is impossible to acquire
483 .Va Giant
484 non-recursively while
485 holding another mutex.
486 It is possible to acquire other mutexes while holding
487 .Va Giant ,
488 and it is possible to acquire
489 .Va Giant
490 recursively while holding other mutexes.
491 .Ss Sleeping
492 Sleeping while holding a mutex (except for
493 .Va Giant )
494 is never safe
495 and should be avoided.
496 There are numerous assertions which will fail if this is attempted.
497 .Ss Functions Which Access Memory in Userspace
498 No mutexes should be held (except for
499 .Va Giant )
500 across functions which
501 access memory in userspace, such as
502 .Xr copyin 9 ,
503 .Xr copyout 9 ,
504 .Xr uiomove 9 ,
505 .Xr fuword 9 ,
506 etc.
507 No locks are needed when calling these functions.
508 .Sh SEE ALSO
509 .Xr condvar 9 ,
510 .Xr LOCK_PROFILING 9 ,
511 .Xr locking 9 ,
512 .Xr mtx_pool 9 ,
513 .Xr panic 9 ,
514 .Xr rwlock 9 ,
515 .Xr sema 9 ,
516 .Xr sleep 9 ,
517 .Xr sx 9
518 .Sh HISTORY
519 These
520 functions appeared in
521 .Bsx 4.1
522 and
523 .Fx 5.0 .