2 .\" Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
28 .\" from BSDI $Id: mutex.4,v 1.1.2.3 1998/04/27 22:53:13 ewv Exp $
41 .Nm mtx_lock_spin_flags ,
43 .Nm mtx_trylock_flags ,
44 .Nm mtx_trylock_spin ,
45 .Nm mtx_trylock_spin_flags ,
48 .Nm mtx_unlock_flags ,
49 .Nm mtx_unlock_spin_flags ,
56 .Nd kernel synchronization primitives
62 .Fn mtx_init "struct mtx *mutex" "const char *name" "const char *type" "int opts"
64 .Fn mtx_destroy "struct mtx *mutex"
66 .Fn mtx_lock "struct mtx *mutex"
68 .Fn mtx_lock_spin "struct mtx *mutex"
70 .Fn mtx_lock_flags "struct mtx *mutex" "int flags"
72 .Fn mtx_lock_spin_flags "struct mtx *mutex" "int flags"
74 .Fn mtx_trylock "struct mtx *mutex"
76 .Fn mtx_trylock_flags "struct mtx *mutex" "int flags"
78 .Fn mtx_trylock_spin "struct mtx *mutex"
80 .Fn mtx_trylock_spin_flags "struct mtx *mutex" "int flags"
82 .Fn mtx_unlock "struct mtx *mutex"
84 .Fn mtx_unlock_spin "struct mtx *mutex"
86 .Fn mtx_unlock_flags "struct mtx *mutex" "int flags"
88 .Fn mtx_unlock_spin_flags "struct mtx *mutex" "int flags"
90 .Fn mtx_sleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
92 .Fn mtx_initialized "const struct mtx *mutex"
94 .Fn mtx_owned "const struct mtx *mutex"
96 .Fn mtx_recursed "const struct mtx *mutex"
98 .Cd "options INVARIANTS"
99 .Cd "options INVARIANT_SUPPORT"
101 .Fn mtx_assert "const struct mtx *mutex" "int what"
103 .Fn MTX_SYSINIT "name" "struct mtx *mtx" "const char *description" "int opts"
105 Mutexes are the most basic and primary method of thread synchronization.
106 The major design considerations for mutexes are:
109 Acquiring and releasing uncontested mutexes should be as cheap
112 They must have the information and storage space to support
113 priority propagation.
115 A thread must be able to recursively acquire a mutex,
116 provided that the mutex is initialized to support recursion.
119 There are currently two flavors of mutexes, those that context switch
120 when they block and those that do not.
124 mutexes will context switch when they are already held.
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.
132 Mutexes which do not context switch are
135 These should only be used to protect data shared with primary interrupt
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
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.
146 Once a spin mutex has been acquired it is not permissible to acquire a
149 The storage needed to implement a mutex is provided by a
151 In general this should be treated as an opaque object and
152 referenced only with the mutex primitives.
156 function must be used to initialize a mutex
157 before it can be passed to any of the other mutex functions.
160 option is used to identify the lock in debugging output etc.
163 option is used by the witness code to classify a mutex when doing checks
170 is used in its place.
171 The pointer passed in as
175 is saved rather than the data it points to.
176 The data pointed to must remain stable
177 until the mutex is destroyed.
180 argument is used to set the type of mutex.
181 It may contain either
186 If the kernel has been compiled with
187 .Cd "option INVARIANTS" ,
191 has not been initialized multiple times without intervening calls to
196 See below for additional initialization options.
202 mutual exclusion lock
203 on behalf of the currently running kernel thread.
204 If another kernel thread is holding the mutex,
205 the caller will be disconnected from the CPU
206 until the mutex is available
207 (i.e., it will block).
213 mutual exclusion lock
214 on behalf of the currently running kernel thread.
215 If another kernel thread is holding the mutex,
216 the caller will spin until the mutex becomes available.
217 Interrupts are disabled during the spin and remain disabled
218 following the acquiring of the lock.
220 It is possible for the same thread to recursively acquire a mutex
221 with no ill effects, provided that the
225 during the initialization of the mutex.
230 .Fn mtx_lock_spin_flags
235 lock, respectively, and also accept a
238 In both cases, the only flags presently available for lock acquires are
244 bit is turned on in the
248 tracing is being done,
249 it will be silenced during the lock acquire.
252 bit is turned on in the
254 argument, then the mutex can be acquired recursively.
260 functions attempt to acquire a
264 mutex, respectively, pointed to by
266 If the mutex cannot be immediately acquired, the functions will return 0,
267 otherwise the mutex will be acquired and a non-zero value will be returned.
270 .Fn mtx_trylock_flags
272 .Fn mtx_trylock_spin_flags
273 functions have the same behavior as
277 respectively, but should be used when the caller desires to pass in a
280 Presently, the only valid value in the
286 and its effects are identical to those described for
294 mutual exclusion lock.
295 The current thread may be preempted if a higher priority thread is waiting
302 mutual exclusion lock.
307 .Fn mtx_unlock_spin_flags
308 functions behave in exactly the same way as do the standard mutex
309 unlock routines above, while also allowing a
311 argument which may specify
315 is identical to its behavior in the mutex lock routines.
319 function is used to destroy
321 so the data associated with it may be freed
322 or otherwise overwritten.
323 Any mutex which is destroyed
324 must previously have been initialized with
326 It is permissible to have a single hold count
327 on a mutex when it is destroyed.
328 It is not permissible to hold the mutex recursively,
329 or have another thread blocked on the mutex
330 when it is destroyed.
334 function is used to atomically release
336 while waiting for an event.
337 For more details on the parameters to this function,
343 function returns non-zero if
345 has been initialized and zero otherwise.
349 function returns non-zero
350 if the current thread holds
352 If the current thread does not hold
358 function returns non-zero if the
361 This check should only be made if the running thread already owns
366 function allows assertions specified in
370 If the assertions are not true and the kernel is compiled with
371 .Cd "options INVARIANTS"
373 .Cd "options INVARIANT_SUPPORT" ,
374 the kernel will panic.
375 Currently the following assertions are supported:
376 .Bl -tag -width MA_NOTRECURSED
378 Assert that the current thread
380 pointed to by the first argument.
382 Assert that the current thread
383 does not hold the mutex
384 pointed to by the first argument.
386 Assert that the current thread has recursed on the mutex
387 pointed to by the first argument.
388 This assertion is only valid in conjunction with
390 .It Dv MA_NOTRECURSED
391 Assert that the current thread has not recursed on the mutex
392 pointed to by the first argument.
393 This assertion is only valid in conjunction with
399 macro is used to generate a call to the
401 routine at system startup in order to initialize a given mutex lock.
402 The parameters are the same as
404 but with an additional argument,
406 that is used in generating unique variable names for the related structures associated with the lock and the sysinit routine.
407 .Ss The Default Mutex Type
408 Most kernel code should use the default lock type,
410 The default lock type will allow the thread
411 to be disconnected from the CPU
412 if the lock is already held by another thread.
414 may treat the lock as a short term spin lock
415 under some circumstances.
416 However, it is always safe to use these forms of locks
417 in an interrupt thread
418 without fear of deadlock
419 against an interrupted thread on the same CPU.
420 .Ss The Spin Mutex Type
423 mutex will not relinquish the CPU
424 when it cannot immediately get the requested lock,
425 but will loop, waiting for the mutex to be released by another CPU.
426 This could result in deadlock
427 if another thread interrupted the thread which held a mutex
428 and then tried to acquire the mutex.
429 For this reason spin locks disable all interrupts on the local CPU.
431 Spin locks are fairly specialized locks
432 that are intended to be held for very short periods of time.
433 Their primary purpose is to protect portions of the code
434 that implement other synchronization primitives such as default mutexes,
435 thread scheduling, and interrupt threads.
436 .Ss Initialization Options
437 The options passed in the
441 specify the mutex type.
446 options is required and only one of those two options may be specified.
447 The possibilities are:
448 .Bl -tag -width MTX_NOWITNESS
451 will always allow the current thread to be suspended
452 to avoid deadlock conditions against interrupt threads.
453 The implementation of this lock type
454 may spin for a while before suspending the current thread.
457 will never relinquish the CPU.
458 All interrupts are disabled on the local CPU
459 while any spin lock is held.
461 Specifies that the initialized mutex is allowed to recurse.
462 This bit must be present if the mutex is permitted to recurse.
469 that is, attempting to acquire an already-owned mutex fails.
471 Do not log any mutex operations for this lock.
477 Witness should not log messages about duplicate locks being acquired.
479 Do not profile this lock.
481 Do not check for double-init.
483 .Ss Lock and Unlock Flags
484 The flags passed to the
486 .Fn mtx_lock_spin_flags ,
487 .Fn mtx_unlock_flags ,
489 .Fn mtx_unlock_spin_flags
490 functions provide some basic options to the caller,
491 and are often used only under special circumstances to modify lock or
493 Standard locking and unlocking should be performed with the
500 Only if a flag is required should the corresponding
501 flags-accepting routines be used.
503 Options that modify mutex behavior:
504 .Bl -tag -width MTX_QUIET
506 This option is used to quiet logging messages during individual mutex
508 This can be used to trim superfluous logging messages for debugging purposes.
513 must be acquired, it must be acquired prior to acquiring
515 Put another way: it is impossible to acquire
517 non-recursively while
518 holding another mutex.
519 It is possible to acquire other mutexes while holding
521 and it is possible to acquire
523 recursively while holding other mutexes.
525 Sleeping while holding a mutex (except for
528 and should be avoided.
529 There are numerous assertions which will fail if this is attempted.
530 .Ss Functions Which Access Memory in Userspace
531 No mutexes should be held (except for
533 across functions which
534 access memory in userspace, such as
540 No locks are needed when calling these functions.
543 .Xr LOCK_PROFILING 9 ,
553 functions appeared in
559 function was added in