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