Index: share/man/man9/mutex.9 =================================================================== RCS file: /home/ncvs/src/share/man/man9/mutex.9,v retrieving revision 1.9 diff -u -r1.9 mutex.9 --- share/man/man9/mutex.9 2001/02/02 00:49:17 1.9 +++ share/man/man9/mutex.9 2001/02/12 03:13:46 @@ -34,27 +34,51 @@ .Sh NAME .Nm mutex , .Nm mtx_init , -.Nm mtx_enter , -.Nm mtx_try_enter , -.Nm mtx_exit , +.Nm mtx_lock , +.Nm mtx_lock_spin , +.Nm mtx_lock_flags , +.Nm mtx_lock_spin_flags , +.Nm mtx_trylock , +.Nm mtx_trylock_flags , +.Nm mtx_unlock , +.Nm mtx_unlock_spin , +.Nm mtx_unlock_flags , +.Nm mtx_unlock_spin_flags , .Nm mtx_destroy , .Nm mtx_owned , +.Nm mtx_recursed , .Nm mtx_assert .Nd kernel synchronization primitives .Sh SYNOPSIS .Fd #include .Ft int -.Fn mtx_init "struct mtx *mutex" "char *name" "flags" +.Fn mtx_init "struct mtx *mutex" "char *name" "int opts" .Ft void -.Fn mtx_enter "struct mtx *mutex" "int flags" +.Fn mtx_lock "struct mtx *mutex" +.Ft void +.Fn mtx_lock_spin "struct mtx *mutex" +.Ft void +.Fn mtx_lock_flags "struct mtx *mutex" "int flags" +.Ft void +.Fn mtx_lock_spin_flags "struct mtx *mutex" "int flags" +.Ft int +.Fn mtx_trylock "struct mtx *mutex" .Ft int -.Fn mtx_try_enter "struct mtx *mutex" "int flags" +.Fn mtx_trylock_flags "struct mtx *mutex" "int flags" +.Ft void +.Fn mtx_unlock "struct mtx *mutex" +.Ft void +.Fn mtx_unlock_spin "struct mtx *mutex" +.Ft void +.Fn mtx_unlock_flags "struct mtx *mutex" "int flags" .Ft void -.Fn mtx_exit "struct mtx *mutex" "int flags" +.Fn mtx_unlock_spin_flags "struct mtx *mutex" "int flags" .Ft void .Fn mtx_destroy "struct mtx *mutex" .Ft int .Fn mtx_owned "struct mtx *mutex" +.Ft int +.Fn mtx_recursed "struct mtx *mutex" .Ft void .Fn mtx_assert "struct mtx *mutex" "int what" .Sh DESCRIPTION @@ -68,21 +92,26 @@ They must have the information and storage space to support priority propagation. .It -A process must be able to recursively acquire a mutex. +A process must be able to recursively acquire a mutex, +provided that the mutex is initialized to support recursion. .El .Pp There are currently two flavors of mutexes, those that context switch when they block and those that do not. .Pp -By default mutexes will context switch when they are already held. +By default, +.Dv MTX_DEF +mutexes will context switch when they are already held. As a machine dependent optimization they may spin for some amount of time before context switching. -It is important to remember that since a process may be preempted -at any time, the possible context switch introduced by acquiring a -mutex is guaranteed to not break anything that isn't already broken. -.Pp -Mutexes which do not context switch are spin mutexes. -These should only be used to protect data shared with device that +It is important to remember that since a process may be preempted at any time, +the possible context switch introduced by acquiring a mutex is guaranteed +to not break anything that isn't already broken. +.Pp +Mutexes which do not context switch are +.Dv MTX_SPIN +mutexes. +These should only be used to protect data shared with any devices that require non-preemptive interrupts, and low level scheduling code. In most/all architectures both acquiring and releasing of a uncontested spin mutex is more expensive than the same operation @@ -106,7 +135,7 @@ .Fn mtx_init function must be used to initialize a mutex before it can be passed to -.Fn mtx_enter . +.Fn mtx_lock . The .Ar name argument is used by the witness code @@ -116,13 +145,15 @@ is saved rather than the data it points to. The data pointed to must remain stable until the mutex is destroyed. -Currently the -.Ar flag -argument is unused. -In the future it will likely be at least -used to identify spin mutexes -so that debug code can verify -consistent use of a mutex. +The +.Ar opts +argument is used to set the type of mutex. It may contain either +.Dv MTX_DEF +or +.Dv MTX_SPIN +but not both. Optionally, it may also contain the +.Dv MTX_RECURSE +bit, which signifies that the mutex will be allowed to recurse. It is not permissible to pass the same .Ar mutex to @@ -131,33 +162,60 @@ .Fn mtx_destroy . .Pp The -.Fn mtx_enter -function acquires a mutual exclusion lock +.Fn mtx_lock +function acquires a +.Dv 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 sleep), -spin wait for the mutex, -or possibly a combination of both. +(i.e. it will sleep). .Pp +The +.Fn mtx_lock_spin +function acquires a +.Dv 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. +.Pp It is possible for the same thread to recursively acquire a mutex -with no ill effects; -if recursion on a given mutex can be avoided, -faster and smaller code will usually be generated. +with no ill effects, provided that the +.Dv MTX_RECURSE +bit was passed to +.Fn mtx_init +during the initialization of the mutex. .Pp The -.Fn mtx_try_enter +.Fn mtx_lock_flags +and +.Fn mtx_lock_spin_flags +functions acquire a +.Dv MTX_DEF +or +.Dv MTX_SPIN +lock, respectively, and also accept a +.Ar flags +argument. +In both cases, the only flag presently available for lock acquires is +.Dv MTX_QUIET . +If the +.Dv MTX_QUIET +bit is turned on in the +.Ar flags +argument, then if KTR_LOCK tracing is being done, +it will be silenced during the lock acquire. +.Pp +The +.Fn mtx_trylock function is used to acquire exclusive access to those objects protected by the mutex pointed to by .Ar mutex . -The -.Ar flag -argument is used to specify various options, -typically -.Dv MTX_DEF -is supplied. If the mutex can not be immediately acquired .Fn mtx_try_enter will return 0, @@ -165,13 +223,67 @@ and a non-zero value will be returned. .Pp The -.Fn mtx_exit -function releases a mutual exclusion lock; +.Fn mtx_trylock_flags +function has the same behavior as +.Fn mtx_trylock +but should be used when the caller desires to pass in a +.Ar flags +value. Presently, the only valid value in the +.Fn mtx_trylock +case is +.Dv MTX_QUIET , +and its effects are identical to those described for +.Fn mtx_lock +and +.Fn mtx_lock_spin +above. +.Pp +The +.Fn mtx_unlock +function releases a +.Dv MTX_DEF +mutual exclusion lock; if a higher priority thread is waiting for the mutex, -the releasing thread may be disconnected +the releasing thread will be disconnected to allow the higher priority thread to acquire the mutex and run. .Pp The +.Fn mtx_unlock_spin +function releases a +.Dv MTX_SPIN +mutual exclusion lock; +interrupt state prior to the acquiring of the lock is restored. +.Pp +The +.Fn mtx_unlock_flags +and +.Fn mtx_unlock_spin_flags +functions behave in exactly the same way as do the standard mutex +unlock routines above, while also allowing a +.Ar flags +argument which may only be +.Dv MTX_QUIET +in the +.Fn mtx_unlock_spin_flags +case, and may be one or more of +.Dv MTX_QUIET +and +.Dv MTX_NOSWITCH +in the +.Fn mtx_unlock_flags +case. The behavior of +.Dv MTX_QUIET +is identical to its behavior in the mutex lock routines. +The +.Dv MTX_NOSWITCH +flag bit signifies, +for a +.Dv MTX_DEF +mutex only, +that the releasing thread is not to be disconnected from the CPU following +the release of the mutex. +.Pp +The .Fn mtx_destroy function is used to destroy .Ar mutex @@ -196,6 +308,14 @@ zero is returned. .Pp The +.Fn mtx_recursed +function returns non-zero if the +.Ar mutex +is recursed. +This check should only be made if the running thread already owns +.Ar mutex . +.Pp +The .Fn mtx_assert function allows assertions to be made about .Ar mutex . @@ -223,30 +343,9 @@ This assertion is only valid in conjuction with .Dv MA_OWNED . .El -.Pp -The type of a mutex is not an attribute of the mutex, -but instead a function of the -.Fa flags -argument passed to -.Fn mtx_enter -and -.Fn mtx_exit ; -this allows code to be generated for the specific mutex type -at compile time -and avoids wasting run time on the determination of lock features. -This does place on the programmer, -the burden of using matching forms of the -.Fn mtx_enter -and -.Fn mtx_exit -functions for a given mutex. -It is an error to acquire a mutex in one mode (e.g. spin) -and release it in another (e.g. default). -It is also an error to get the lock in one mode -and allow another thread to attempt to get the lock in another mode. -A good general rule is to always use a given mutex in one mode only. -.Ss The default Mutex Type -Most kernel code should use the default lock type; +.Ss The Default Mutex Type +Most kernel code should use the default lock type, +.Dv MTX_DEF ; the default lock type will allow the thread to be disconnected from the CPU if it cannot get the lock. @@ -257,117 +356,87 @@ in an interrupt thread without fear of deadlock against an interrupted thread on the same CPU. -.Ss The spin Mutex Type -A spin mutex will not relinquish the CPU +.Ss The Spin Mutex Type +A +.Dv MTX_SPIN +mutex will not relinquish the CPU when it cannot immediately get the requested lock, but will loop, waiting for the mutex to be released by another CPU. This could result in deadlock if a thread interrupted the thread which held a mutex and then tried to acquire the mutex; for this reason spin locks will disable all interrupts -(on the local CPU only) -by default. +(on the local CPU only). .Pp Spin locks are fairly specialized locks that are intended to be held for very short periods of time; their primary purpose is to protect portions of the code that implement default (i.e. sleep) locks. -.Ss Flags -The flags passed to the -.Fn mtx_enter -and -.Fn mtx_exit -functions determine what type of mutex is being used -and also provide various options -used to generate more efficient code under certain circumstances. -.Pp -Both lock types (default and spin) -can be acquired recursively by the same thread. -This behavior can be changed with flags. -.Pp -The type of the mutex must always be specified: -.Bl -tag -width MTX_NORECURSE +.Ss Initialization Options +The options passed in the +.Ar opts +argument of +.Fn mtx_init +specify the mutex type. +The possibilities are: +.Bl -tag -width MTX_RECURSE .It Dv MTX_DEF Default lock type; will always allow the current thread to be suspended to avoid deadlock conditions against interrupt threads. The machine dependent implementation of this lock type may spin for a while before suspending the current thread. -Most locks should be of this type. +If this flag is specified, clearly +.Dv MTX_SPIN +must NOT be specified. .It Dv MTX_SPIN -Spin lock; +Spin lock type; will never relinquish the CPU. -By default all interrupts are disabled on the local CPU +All interrupts are disabled on the local CPU while any spin lock is held. +.It Dv MTX_RECURSE +Recursion option bit; +specifies that the initialized mutex is allowed to recurse. +This bit must be present if the mutex is going to be permitted to recurse. +It must always be accompanied by either +.Dv MTX_DEF +or +.Dv MTX_SPIN +but not both. .El +.Ss Lock and Unlock Flags +The flags passed to the +.Fn mtx_lock_flags , +.Fn mtx_lock_spin_flags , +.Fn mtx_unlock_flags , +and +.Fn mtx_unlock_spin_flags +functions provide some basic options to the caller, +and are often used only under special circumstances to modify lock or +unlock behavior. Standard locking and unlocking should be performed with the +.Fn mtx_lock , +.Fn mtx_lock_spin , +.Fn mtx_unlock , +and +.Fn mtx_unlock_spin +functions. If one of these flags is required then, and only then, +should the corresponding flags-accepting routines be used. .Pp Options that modify mutex behavior: -.Bl -tag -width MTX_NORECURSE -.It Dv MTX_NORECURSE -If it is known, absolutely, -that the mutex will not be recursively acquired at this invocation -then this flag should be specified. -.Pp -If the lock is already held by the current thread, -then a kernel with -.Dv MUTEX_DEBUG -defined will panic; -without debugging enabled, -the thread may deadlock against itself -or leave the mutex in a corrupted state. -.Pp -This flag prevents generation of additional inline code -to deal with recursive lock acquisitions -and should be specified whenever possible -in the interests of efficiency. -Not specifying this flag will only cause the generated code -to be a little larger than necessary; -it will still operate correctly. -.It Dv MTX_RLIKELY -This provides a hint that it is likely that this mutex -will be held recursively at this invocation. -The actual optimization used is machine dependent; -generally, this will inline code to handle recursion -where a function call would otherwise be needed. -.Pp -This is a hint only; -leaving it out or specifying it inappropriately -will not cause any great harm other than -possibly generating less efficient code. -.It Dv MTX_TOPHALF -This option applies to spin locks only. -It indicates that the mutex is never acquired -from an interrupt thread, -so it is safe to leave interrupts enabled while holding the lock. -Since an interrupt may occur while holding the lock, -this may be detrimental to other processors -spin waiting for the lock. -Do not forget to include this option when the lock is released. -.Pp -This option should not be used in new code; -it is documented here for completeness only. -.It Dv MTX_FIRST -This option applies to spin locks only. -It indicates this is the first spin lock acquired by the thread. -No other spin locks may be held, -and the requested lock also may not be currently held. -Do not forget to include this option when the lock is released. +.Bl -tag -width MTX_NOSWITCH .It Dv MTX_NOSWITCH -When releasing a mutex, +When releasing a +.Dv MTX_DEF +mutex, this flag prevents a thread switch that might occur if another higher priority thread was waiting for the mutex. This may cause priority inversion and should be used carefully. +This flag can only be passed to +.Fn mtx_unlock_flags . .Pp This flag is used internally by the lock code. It should not be used in general kernel code and is documented here for completeness only. -.It Dv MTX_NOSPIN -For default locks, -this hint will prevent spinning before relinquishing the CPU. -This should be specified when it is known -that the lock will usually remain unavailable for some time -when it is not immediately available -(i.e.: coarse grained locks protecting large subsystems). .It Dv MTX_QUIET This option is used to quiet logging messages during mutex operations. This can be used to trim superfluous logging messages for debugging purposes. @@ -377,4 +446,4 @@ functions appeared in .Tn BSD/OS 4.1 and -.Fx 5.0 . +.Fx 5.0 .