1 .\" $NetBSD: mutex.9,v 1.19 2009/05/18 14:02:51 wiz Exp $
3 .\" Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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.
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.
40 .Nm mutex_spin_enter ,
43 .Nd mutual exclusion primitives
47 .Fn mutex_init "kmutex_t *mtx" "kmutex_type_t type" "int ipl"
49 .Fn mutex_destroy "kmutex_t *mtx"
51 .Fn mutex_enter "kmutex_t *mtx"
53 .Fn mutex_exit "kmutex_t *mtx"
55 .Fn mutex_owned "kmutex_t *mtx"
57 .Fn mutex_spin_enter "kmutex_t *mtx"
59 .Fn mutex_spin_exit "kmutex_t *mtx"
61 .Fn mutex_tryenter "kmutex_t *mtx"
63 .Cd "options DIAGNOSTIC"
64 .Cd "options LOCKDEBUG"
66 Mutexes are used in the kernel to implement mutual exclusion among LWPs
67 (lightweight processes) and interrupt handlers.
71 type provides storage for the mutex object.
72 This should be treated as an opaque object and not examined directly by
77 system traditionally used to provide synchronization between interrupt
81 .It Cd "options DIAGNOSTIC"
83 Kernels compiled with the
85 option perform basic sanity checks on mutex operations.
86 .It Cd "options LOCKDEBUG"
88 Kernels compiled with the
90 option perform potentially CPU intensive sanity checks
95 .It Fn mutex_init "mtx" "type" "ipl"
97 Dynamically initialize a mutex for use.
99 No other operations can be performed on a mutex until it has been initialized.
100 Once initialized, all types of mutex are manipulated using the same interface.
103 may block in order to allocate memory.
107 argument must be given as
109 Other constants are defined but are for low-level system use and are not
110 an endorsed, stable part of the interface.
112 The type of mutex returned depends on the
116 .It IPL_NONE, or one of the IPL_SOFT* constants
118 An adaptive mutex will be returned.
119 Adaptive mutexes provide mutual exclusion between LWPs,
120 and between LWPs and soft interrupt handlers.
122 Adaptive mutexes cannot be acquired from a hardware interrupt handler.
123 An LWP may either sleep or busy-wait when attempting to acquire
124 an adaptive mutex that is already held.
125 .It IPL_VM, IPL_SCHED, IPL_HIGH
127 A spin mutex will be returned.
128 Spin mutexes provide mutual exclusion between LWPs, and between LWPs
129 and interrupt handlers.
133 argument is used to pass a system interrupt priority level (IPL)
134 that will block all interrupt handlers that may try to acquire the mutex.
136 LWPs that own spin mutexes may not sleep, and therefore must not
137 try to acquire adaptive mutexes or other sleep locks.
139 A processor will always busy-wait when attempting to acquire
140 a spin mutex that is already held.
145 for further information on interrupt priority levels (IPLs).
147 .It Fn mutex_destroy "mtx"
149 Release resources used by a mutex.
150 The mutex may not be used after it has been destroyed.
152 may block in order to free memory.
153 .It Fn mutex_enter "mtx"
156 If the mutex is already held, the caller will block and not return until the
159 Mutexes and other types of locks must always be acquired in a
160 consistent order with respect to each other.
161 Otherwise, the potential for system deadlock exists.
163 Adaptive mutexes and other types of lock that can sleep may
164 not be acquired while a spin mutex is held by the caller.
165 .It Fn mutex_exit "mtx"
168 The mutex must have been previously acquired by the caller.
169 Mutexes may be released out of order as needed.
170 .It Fn mutex_owned "mtx"
172 For adaptive mutexes, return non-zero if the current LWP holds the mutex.
173 For spin mutexes, return non-zero if the mutex is held, potentially by the
175 Otherwise, return zero.
178 is provided for making diagnostic checks to verify that a lock is held.
181 KASSERT(mutex_owned(\*[Am]driver_lock));
184 It should not be used to make locking decisions at run time, or to
185 verify that a lock is not held.
186 .It Fn mutex_spin_enter "mtx"
190 but may only be used when it is known that
193 On some architectures, this can substantially reduce the cost of acquring
195 .It Fn mutex_spin_exit "mtx"
199 but may only be used when it is known that
202 On some architectures, this can substantially reduce the cost of releasing
204 .It Fn mutex_tryenter "mtx"
206 Try to acquire a mutex, but do not block if the mutex is already held.
207 Returns non-zero if the mutex was acquired, or zero if the mutex was
211 can be used as an optimization when acquiring locks in the wrong order.
212 For example, in a setting where the convention is that
214 must be acquired before
216 the following can be used to optimistically lock in reverse order:
218 /* We hold second_lock, but not first_lock. */
219 KASSERT(mutex_owned(\*[Am]second_lock));
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);
228 * We may need to recheck any conditions the code
229 * path depends on, as we released second_lock
236 This section describes places within the
238 source tree where code implementing mutexes can be found.
239 All pathnames are relative to
242 The core of the mutex implementation is in
243 .Pa sys/kern/kern_mutex.c .
247 describes the public interface, and interfaces that machine-dependent
248 code must provide to support mutexes.
259 .%A Richard McDougall
260 .%T Solaris Internals: Core Kernel Architecture ,
263 .%O ISBN 0-13-022496-0
266 The mutex primitives first appeared in