1.\" $NetBSD: mutex.9,v 1.22 2010/02/16 19:21:30 rmind Exp $ 2.\" 3.\" Copyright (c) 2007, 2009 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Andrew Doran. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd February 16, 2010 31.Dt MUTEX 9 32.Os 33.Sh NAME 34.Nm mutex , 35.Nm mutex_init , 36.Nm mutex_destroy , 37.Nm mutex_enter , 38.Nm mutex_exit , 39.Nm mutex_owned , 40.Nm mutex_spin_enter , 41.Nm mutex_spin_exit , 42.Nm mutex_tryenter 43.Nd mutual exclusion primitives 44.Sh SYNOPSIS 45.In sys/mutex.h 46.Ft void 47.Fn mutex_init "kmutex_t *mtx" "kmutex_type_t type" "int ipl" 48.Ft void 49.Fn mutex_destroy "kmutex_t *mtx" 50.Ft void 51.Fn mutex_enter "kmutex_t *mtx" 52.Ft void 53.Fn mutex_exit "kmutex_t *mtx" 54.Ft int 55.Fn mutex_owned "kmutex_t *mtx" 56.Ft void 57.Fn mutex_spin_enter "kmutex_t *mtx" 58.Ft void 59.Fn mutex_spin_exit "kmutex_t *mtx" 60.Ft int 61.Fn mutex_tryenter "kmutex_t *mtx" 62.Pp 63.Cd "options DIAGNOSTIC" 64.Cd "options LOCKDEBUG" 65.Sh DESCRIPTION 66Mutexes are used in the kernel to implement mutual exclusion among LWPs 67(lightweight processes) and interrupt handlers. 68.Pp 69The 70.Vt kmutex_t 71type provides storage for the mutex object. 72This should be treated as an opaque object and not examined directly by 73consumers. 74.Pp 75Mutexes replace the 76.Xr spl 9 77system traditionally used to provide synchronization between interrupt 78handlers and LWPs. 79.Sh OPTIONS 80.Bl -tag -width abcd 81.It Cd "options DIAGNOSTIC" 82.Pp 83Kernels compiled with the 84.Dv DIAGNOSTIC 85option perform basic sanity checks on mutex operations. 86.It Cd "options LOCKDEBUG" 87.Pp 88Kernels compiled with the 89.Dv LOCKDEBUG 90option perform potentially CPU intensive sanity checks 91on mutex operations. 92.El 93.Sh FUNCTIONS 94.Bl -tag -width abcd 95.It Fn mutex_init "mtx" "type" "ipl" 96.Pp 97Dynamically initialize a mutex for use. 98.Pp 99No other operations can be performed on a mutex until it has been initialized. 100Once initialized, all types of mutex are manipulated using the same interface. 101Note that 102.Fn mutex_init 103may block in order to allocate memory. 104.Pp 105The 106.Fa type 107argument must be given as 108.Dv MUTEX_DEFAULT . 109Other constants are defined but are for low-level system use and are not 110an endorsed, stable part of the interface. 111.Pp 112The type of mutex returned depends on the 113.Fa ipl 114argument: 115.Bl -tag -width abcd 116.It IPL_NONE, or one of the IPL_SOFT* constants 117.Pp 118An adaptive mutex will be returned. 119Adaptive mutexes provide mutual exclusion between LWPs, 120and between LWPs and soft interrupt handlers. 121.Pp 122Adaptive mutexes cannot be acquired from a hardware interrupt handler. 123An LWP may either sleep or busy-wait when attempting to acquire 124an adaptive mutex that is already held. 125.It IPL_VM, IPL_SCHED, IPL_HIGH 126.Pp 127A spin mutex will be returned. 128Spin mutexes provide mutual exclusion between LWPs, and between LWPs 129and interrupt handlers. 130.Pp 131The 132.Fa ipl 133argument is used to pass a system interrupt priority level (IPL) 134that will block all interrupt handlers that may try to acquire the mutex. 135.Pp 136LWPs that own spin mutexes may not sleep, and therefore must not 137try to acquire adaptive mutexes or other sleep locks. 138.Pp 139A processor will always busy-wait when attempting to acquire 140a spin mutex that is already held. 141.El 142.Pp 143See 144.Xr spl 9 145for further information on interrupt priority levels (IPLs). 146.Pp 147.It Fn mutex_destroy "mtx" 148.Pp 149Release resources used by a mutex. 150The mutex may not be used after it has been destroyed. 151.Fn mutex_destroy 152may block in order to free memory. 153.It Fn mutex_enter "mtx" 154.Pp 155Acquire a mutex. 156If the mutex is already held, the caller will block and not return until the 157mutex is acquired. 158.Pp 159Mutexes and other types of locks must always be acquired in a 160consistent order with respect to each other. 161Otherwise, the potential for system deadlock exists. 162.Pp 163Adaptive mutexes and other types of lock that can sleep may 164not be acquired while a spin mutex is held by the caller. 165.It Fn mutex_exit "mtx" 166.Pp 167Release a mutex. 168The mutex must have been previously acquired by the caller. 169Mutexes may be released out of order as needed. 170.It Fn mutex_owned "mtx" 171.Pp 172For adaptive mutexes, return non-zero if the current LWP holds the mutex. 173For spin mutexes, return non-zero if the mutex is held, potentially by the 174current processor. 175Otherwise, return zero. 176.Pp 177.Fn mutex_owned 178is provided for making diagnostic checks to verify that a lock is held. 179For example: 180.Bd -literal 181 KASSERT(mutex_owned(\*[Am]driver_lock)); 182.Ed 183.Pp 184It should not be used to make locking decisions at run time, or to 185verify that a lock is not held. 186.It Fn mutex_spin_enter "mtx" 187.Pp 188Equivalent to 189.Fn mutex_enter , 190but may only be used when it is known that 191.Ar mtx 192is a spin mutex. 193On some architectures, this can substantially reduce the cost of acquring 194a spin mutex. 195.It Fn mutex_spin_exit "mtx" 196.Pp 197Equivalent to 198.Fn mutex_exit , 199but may only be used when it is known that 200.Ar mtx 201is a spin mutex. 202On some architectures, this can substantially reduce the cost of releasing 203a spin mutex. 204.It Fn mutex_tryenter "mtx" 205.Pp 206Try to acquire a mutex, but do not block if the mutex is already held. 207Returns non-zero if the mutex was acquired, or zero if the mutex was 208already held. 209.Pp 210.Fn mutex_tryenter 211can be used as an optimization when acquiring locks in the wrong order. 212For example, in a setting where the convention is that 213.Dv first_lock 214must be acquired before 215.Dv second_lock , 216the following can be used to optimistically lock in reverse order: 217.Bd -literal 218 /* We hold second_lock, but not first_lock. */ 219 KASSERT(mutex_owned(\*[Am]second_lock)); 220 221 if (!mutex_tryenter(\*[Am]first_lock)) { 222 /* Failed to get it - lock in the correct order. */ 223 mutex_exit(\*[Am]second_lock); 224 mutex_enter(\*[Am]first_lock); 225 mutex_enter(\*[Am]second_lock); 226 227 /* 228 * We may need to recheck any conditions the code 229 * path depends on, as we released second_lock 230 * briefly. 231 */ 232 } 233.Ed 234.El 235.Sh CODE REFERENCES 236This section describes places within the 237.Nx 238source tree where code implementing mutexes can be found. 239All pathnames are relative to 240.Pa /usr/src . 241.Pp 242The core of the mutex implementation is in 243.Pa sys/kern/kern_mutex.c . 244.Pp 245The header file 246.Pa sys/sys/mutex.h 247describes the public interface, and interfaces that machine-dependent 248code must provide to support mutexes. 249.Sh SEE ALSO 250.Xr atomic_ops 3 , 251.Xr membar_ops 3 , 252.Xr lockstat 8 , 253.Xr condvar 9 , 254.Xr kpreempt 9 , 255.Xr rwlock 9 , 256.Xr spl 9 257.Pp 258.Rs 259.%A Jim Mauro 260.%A Richard McDougall 261.%T Solaris Internals: Core Kernel Architecture 262.%I Prentice Hall 263.%D 2001 264.%O ISBN 0-13-022496-0 265.Re 266.Sh HISTORY 267The mutex primitives first appeared in 268.Nx 5.0 . 269