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