DESCRIPTION
Mutexes are the most basic and primary method of thread synchronization. The major design considerations for mutexes are: - Acquiring and releasing uncontested mutexes should be as cheap as possible.
- They must have the information and storage space to support priority propagation.
- A thread must be able to recursively acquire a mutex, provided that the mutex is initialized to support recursion.
There are currently two flavors of mutexes, those that context switch when they block and those that do not.
By default, MTX_DEF mutexes will context switch when they are already held. As an optimization, they may spin for some amount of time before context switching. It is important to remember that since a thread may be preempted at any time, the possible context switch introduced by acquiring a mutex is guaranteed to not break anything that is not already broken.
Mutexes which do not context switch are MTX_SPIN mutexes. These should only be used to protect data shared with primary interrupt code. This includes INTR_FAST interrupt handlers and low level scheduling code. In all architectures both acquiring and releasing of a uncontested spin mutex is more expensive than the same operation on a non-spin mutex. In order to protect an interrupt service routine from blocking against itself all interrupts are either blocked or deferred on a processor while holding a spin lock. It is permissible to hold multiple spin mutexes.
Once a spin mutex has been acquired it is not permissible to acquire a blocking mutex.
The storage needed to implement a mutex is provided by a
.Vt struct mtx . In general this should be treated as an opaque object and referenced only with the mutex primitives.
The mtx_init function must be used to initialize a mutex before it can be passed to any of the other mutex functions. The name option is used to identify the lock in debugging output etc. The type option is used by the witness code to classify a mutex when doing checks of lock ordering. If type is NULL, name is used in its place. The pointer passed in as name and type is saved rather than the data it points to. The data pointed to must remain stable until the mutex is destroyed. The opts argument is used to set the type of mutex. It may contain either MTX_DEF or MTX_SPIN but not both. See below for additional initialization options. It is not permissible to pass the same mutex to mtx_init multiple times without intervening calls to mtx_destroy.
The mtx_lock function acquires a MTX_DEF mutual exclusion lock on behalf of the currently running kernel thread. If another kernel thread is holding the mutex, the caller will be disconnected from the CPU until the mutex is available (i.e., it will block).
The mtx_lock_spin function acquires a MTX_SPIN mutual exclusion lock on behalf of the currently running kernel thread. If another kernel thread is holding the mutex, the caller will spin until the mutex becomes available. Interrupts are disabled during the spin and remain disabled following the acquiring of the lock.
It is possible for the same thread to recursively acquire a mutex with no ill effects, provided that the MTX_RECURSE bit was passed to mtx_init during the initialization of the mutex.
The mtx_lock_flags and mtx_lock_spin_flags functions acquire a MTX_DEF or MTX_SPIN lock, respectively, and also accept a flags argument. In both cases, the only flag presently available for lock acquires is MTX_QUIET. If the MTX_QUIET bit is turned on in the flags argument, then if KTR_LOCK tracing is being done, it will be silenced during the lock acquire.
The mtx_trylock attempts to acquire the MTX_DEF mutex pointed to by mutex. If the mutex cannot be immediately acquired mtx_trylock will return 0, otherwise the mutex will be acquired and a non-zero value will be returned.
The mtx_trylock_flags function has the same behavior as mtx_trylock but should be used when the caller desires to pass in a flags value. Presently, the only valid value in the mtx_trylock case is MTX_QUIET, and its effects are identical to those described for mtx_lock above.
The mtx_unlock function releases a MTX_DEF mutual exclusion lock. The current thread may be preempted if a higher priority thread is waiting for the mutex.
The mtx_unlock_spin function releases a MTX_SPIN mutual exclusion lock.
The mtx_unlock_flags and mtx_unlock_spin_flags functions behave in exactly the same way as do the standard mutex unlock routines above, while also allowing a flags argument which may specify MTX_QUIET. The behavior of MTX_QUIET is identical to its behavior in the mutex lock routines.
The mtx_destroy function is used to destroy mutex so the data associated with it may be freed or otherwise overwritten. Any mutex which is destroyed must previously have been initialized with mtx_init. It is permissible to have a single hold count on a mutex when it is destroyed. It is not permissible to hold the mutex recursively, or have another thread blocked on the mutex when it is destroyed.
The mtx_initialized function returns non-zero if mutex has been initialized and zero otherwise.
The mtx_owned function returns non-zero if the current thread holds mutex. If the current thread does not hold mutex zero is returned.
The mtx_recursed function returns non-zero if the mutex is recursed. This check should only be made if the running thread already owns mutex.
The mtx_assert function allows assertions specified in what to be made about mutex. If the assertions are not true and the kernel is compiled with
.Cd "options INVARIANTS" and
.Cd "options INVARIANT_SUPPORT" , the kernel will panic. Currently the following assertions are supported: