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