2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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
28 * from BSDI $Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp Exp $
36 #include <sys/queue.h>
40 #include <machine/atomic.h>
41 #include <machine/cpufunc.h>
42 #include <machine/globals.h>
46 #include <machine/mutex.h>
51 * Mutex types and options passed to mtx_init(). MTX_QUIET can also be
54 #define MTX_DEF 0x00000000 /* DEFAULT (sleep) lock */
55 #define MTX_SPIN 0x00000001 /* Spin lock (disables interrupts) */
56 #define MTX_RECURSE 0x00000004 /* Option: lock allowed to recurse */
57 #define MTX_NOWITNESS 0x00000008 /* Don't do any witness checking. */
58 #define MTX_SLEEPABLE 0x00000010 /* We can sleep with this lock. */
61 * Option flags passed to certain lock/unlock routines, through the use
62 * of corresponding mtx_{lock,unlock}_flags() interface macros.
64 #define MTX_NOSWITCH LOP_NOSWITCH /* Do not switch on release */
65 #define MTX_QUIET LOP_QUIET /* Don't log a mutex event */
68 * State bits kept in mutex->mtx_lock, for the DEFAULT lock type. None of this,
69 * with the exception of MTX_UNOWNED, applies to spin locks.
71 #define MTX_RECURSED 0x00000001 /* lock recursed (for MTX_DEF only) */
72 #define MTX_CONTESTED 0x00000002 /* lock contested (for MTX_DEF only) */
73 #define MTX_UNOWNED 0x00000004 /* Cookie for free mutex */
74 #define MTX_FLAGMASK ~(MTX_RECURSED | MTX_CONTESTED)
87 struct lock_object mtx_object; /* Common lock properties. */
88 volatile uintptr_t mtx_lock; /* owner (and state for sleep locks) */
89 volatile u_int mtx_recurse; /* number of recursive holds */
90 critical_t mtx_savecrit; /* saved flags (for spin locks) */
91 TAILQ_HEAD(, proc) mtx_blocked; /* threads blocked on this lock */
92 LIST_ENTRY(mtx) mtx_contested; /* list of all contested locks */
96 * XXX: Friendly reminder to fix things in MP code that is presently being
99 #define mp_fixme(string)
106 * NOTE: Functions prepended with `_' (underscore) are exported to other parts
107 * of the kernel via macros, thus allowing us to use the cpp __FILE__
108 * and __LINE__. These functions should not be called directly by any
109 * code using the API. Their macros cover their functionality.
111 * [See below for descriptions]
114 void mtx_init(struct mtx *m, const char *description, int opts);
115 void mtx_destroy(struct mtx *m);
116 void _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line);
117 void _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line);
118 void _mtx_lock_spin(struct mtx *m, int opts, critical_t mtx_crit,
119 const char *file, int line);
120 void _mtx_unlock_spin(struct mtx *m, int opts, const char *file, int line);
121 int _mtx_trylock(struct mtx *m, int opts, const char *file, int line);
122 void _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line);
123 void _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line);
124 void _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file,
126 void _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file,
128 #ifdef INVARIANT_SUPPORT
129 void _mtx_assert(struct mtx *m, int what, const char *file, int line);
132 void _mtx_update_flags(struct mtx *m, int locking);
136 * We define our machine-independent (unoptimized) mutex micro-operations
137 * here, if they are not already defined in the machine-dependent mutex.h
140 /* Actually obtain mtx_lock */
142 #define _obtain_lock(mp, tid) \
143 atomic_cmpset_acq_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED, (tid))
146 /* Actually release mtx_lock */
147 #ifndef _release_lock
148 #define _release_lock(mp, tid) \
149 atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), (void *)MTX_UNOWNED)
152 /* Actually release mtx_lock quickly, assuming we own it. */
153 #ifndef _release_lock_quick
154 #define _release_lock_quick(mp) \
155 atomic_store_rel_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED)
159 * Obtain a sleep lock inline, or call the "hard" function if we can't get it
162 #ifndef _get_sleep_lock
163 #define _get_sleep_lock(mp, tid, opts, file, line) do { \
164 if (!_obtain_lock((mp), (tid))) \
165 _mtx_lock_sleep((mp), (opts), (file), (line)); \
170 * Obtain a spin lock inline, or call the "hard" function if we can't get it
171 * easy. For spinlocks, we handle recursion inline (it turns out that function
172 * calls can be significantly expensive on some architectures).
173 * Since spin locks are not _too_ common, inlining this code is not too big
176 #ifndef _get_spin_lock
177 #define _get_spin_lock(mp, tid, opts, file, line) do { \
178 critical_t _mtx_crit; \
179 _mtx_crit = critical_enter(); \
180 if (!_obtain_lock((mp), (tid))) { \
181 if ((mp)->mtx_lock == (uintptr_t)(tid)) \
182 (mp)->mtx_recurse++; \
184 _mtx_lock_spin((mp), (opts), _mtx_crit, (file), \
187 (mp)->mtx_savecrit = _mtx_crit; \
192 * Release a sleep lock inline, or call the "hard" function if we can't do it
195 #ifndef _rel_sleep_lock
196 #define _rel_sleep_lock(mp, tid, opts, file, line) do { \
197 if (!_release_lock((mp), (tid))) \
198 _mtx_unlock_sleep((mp), (opts), (file), (line)); \
203 * For spinlocks, we can handle everything inline, as it's pretty simple and
204 * a function call would be too expensive (at least on some architectures).
205 * Since spin locks are not _too_ common, inlining this code is not too big
208 #ifndef _rel_spin_lock
209 #define _rel_spin_lock(mp) do { \
210 critical_t _mtx_crit = (mp)->mtx_savecrit; \
211 if (mtx_recursed((mp))) \
212 (mp)->mtx_recurse--; \
214 _release_lock_quick((mp)); \
215 critical_exit(_mtx_crit); \
221 * Update the lock object flags based on the current mutex state.
224 #define mtx_update_flags(m, locking) _mtx_update_flags((m), (locking))
226 #define mtx_update_flags(m, locking)
230 * Exported lock manipulation interface.
232 * mtx_lock(m) locks MTX_DEF mutex `m'
234 * mtx_lock_spin(m) locks MTX_SPIN mutex `m'
236 * mtx_unlock(m) unlocks MTX_DEF mutex `m'
238 * mtx_unlock_spin(m) unlocks MTX_SPIN mutex `m'
240 * mtx_lock_spin_flags(m, opts) and mtx_lock_flags(m, opts) locks mutex `m'
241 * and passes option flags `opts' to the "hard" function, if required.
242 * With these routines, it is possible to pass flags such as MTX_QUIET
243 * and/or MTX_NOSWITCH to the appropriate lock manipulation routines.
245 * mtx_trylock(m) attempts to acquire MTX_DEF mutex `m' but doesn't sleep if
246 * it cannot. Rather, it returns 0 on failure and non-zero on success.
247 * It does NOT handle recursion as we assume that if a caller is properly
248 * using this part of the interface, he will know that the lock in question
251 * mtx_trylock_flags(m, opts) is used the same way as mtx_trylock() but accepts
252 * relevant option flags `opts.'
254 * mtx_initialized(m) returns non-zero if the lock `m' has been initialized.
256 * mtx_owned(m) returns non-zero if the current thread owns the lock `m'
258 * mtx_recursed(m) returns non-zero if the lock `m' is presently recursed.
260 #define mtx_lock(m) mtx_lock_flags((m), 0)
261 #define mtx_lock_spin(m) mtx_lock_spin_flags((m), 0)
262 #define mtx_trylock(m) mtx_trylock_flags((m), 0)
263 #define mtx_unlock(m) mtx_unlock_flags((m), 0)
264 #define mtx_unlock_spin(m) mtx_unlock_spin_flags((m), 0)
267 #define mtx_lock_flags(m, opts) \
268 _mtx_lock_flags((m), (opts), __FILE__, __LINE__)
269 #define mtx_unlock_flags(m, opts) \
270 _mtx_unlock_flags((m), (opts), __FILE__, __LINE__)
271 #define mtx_lock_spin_flags(m, opts) \
272 _mtx_lock_spin_flags((m), (opts), __FILE__, __LINE__)
273 #define mtx_unlock_spin_flags(m, opts) \
274 _mtx_unlock_spin_flags((m), (opts), __FILE__, __LINE__)
276 #define mtx_lock_flags(m, opts) \
277 __mtx_lock_flags((m), (opts), __FILE__, __LINE__)
278 #define mtx_unlock_flags(m, opts) \
279 __mtx_unlock_flags((m), (opts), __FILE__, __LINE__)
280 #define mtx_lock_spin_flags(m, opts) \
281 __mtx_lock_spin_flags((m), (opts), __FILE__, __LINE__)
282 #define mtx_unlock_spin_flags(m, opts) \
283 __mtx_unlock_spin_flags((m), (opts), __FILE__, __LINE__)
286 #define __mtx_lock_flags(m, opts, file, line) do { \
287 MPASS(curproc != NULL); \
288 KASSERT(((opts) & MTX_NOSWITCH) == 0, \
289 ("MTX_NOSWITCH used at %s:%d", (file), (line))); \
290 _get_sleep_lock((m), curproc, (opts), (file), (line)); \
291 LOCK_LOG_LOCK("LOCK", &(m)->mtx_object, opts, m->mtx_recurse, \
293 mtx_update_flags((m), 1); \
294 WITNESS_LOCK(&(m)->mtx_object, (opts), (file), (line)); \
297 #define __mtx_lock_spin_flags(m, opts, file, line) do { \
298 MPASS(curproc != NULL); \
299 _get_spin_lock((m), curproc, (opts), (file), (line)); \
300 LOCK_LOG_LOCK("LOCK", &(m)->mtx_object, opts, m->mtx_recurse, \
302 mtx_update_flags((m), 1); \
303 WITNESS_LOCK(&(m)->mtx_object, (opts), (file), (line)); \
306 #define __mtx_unlock_flags(m, opts, file, line) do { \
307 MPASS(curproc != NULL); \
308 mtx_assert((m), MA_OWNED); \
309 mtx_update_flags((m), 0); \
310 WITNESS_UNLOCK(&(m)->mtx_object, (opts), (file), (line)); \
311 _rel_sleep_lock((m), curproc, (opts), (file), (line)); \
312 LOCK_LOG_LOCK("UNLOCK", &(m)->mtx_object, (opts), \
313 (m)->mtx_recurse, (file), (line)); \
316 #define __mtx_unlock_spin_flags(m, opts, file, line) do { \
317 MPASS(curproc != NULL); \
318 mtx_assert((m), MA_OWNED); \
319 mtx_update_flags((m), 0); \
320 WITNESS_UNLOCK(&(m)->mtx_object, (opts), (file), (line)); \
321 _rel_spin_lock((m)); \
322 LOCK_LOG_LOCK("UNLOCK", &(m)->mtx_object, (opts), \
323 (m)->mtx_recurse, (file), (line)); \
326 #define mtx_trylock_flags(m, opts) \
327 _mtx_trylock((m), (opts), __FILE__, __LINE__)
329 #define mtx_initialized(m) ((m)->mtx_object.lo_flags & LO_INITIALIZED)
331 #define mtx_owned(m) (((m)->mtx_lock & MTX_FLAGMASK) == (uintptr_t)curproc)
333 #define mtx_recursed(m) ((m)->mtx_recurse != 0)
338 extern struct mtx sched_lock;
339 extern struct mtx Giant;
342 * Giant lock manipulation and clean exit macros.
343 * Used to replace return with an exit Giant and return.
345 * Note that DROP_GIANT*() needs to be paired with PICKUP_GIANT()
347 #define DROP_GIANT_NOSWITCH() \
350 WITNESS_SAVE_DECL(Giant); \
352 if (mtx_owned(&Giant)) \
353 WITNESS_SAVE(&Giant.mtx_object, Giant); \
354 for (_giantcnt = 0; mtx_owned(&Giant); _giantcnt++) \
355 mtx_unlock_flags(&Giant, MTX_NOSWITCH)
357 #define DROP_GIANT() \
360 WITNESS_SAVE_DECL(Giant); \
362 if (mtx_owned(&Giant)) \
363 WITNESS_SAVE(&Giant.mtx_object, Giant); \
364 for (_giantcnt = 0; mtx_owned(&Giant); _giantcnt++) \
367 #define PICKUP_GIANT() \
368 mtx_assert(&Giant, MA_NOTOWNED); \
369 while (_giantcnt--) \
371 if (mtx_owned(&Giant)) \
372 WITNESS_RESTORE(&Giant.mtx_object, Giant); \
375 #define PARTIAL_PICKUP_GIANT() \
376 mtx_assert(&Giant, MA_NOTOWNED); \
377 while (_giantcnt--) \
379 if (mtx_owned(&Giant)) \
380 WITNESS_RESTORE(&Giant.mtx_object, Giant)
383 * The INVARIANTS-enabled mtx_assert() functionality.
385 * The constants need to be defined for INVARIANT_SUPPORT infrastructure
386 * support as _mtx_assert() itself uses them and the latter implies that
387 * _mtx_assert() must build.
389 #ifdef INVARIANT_SUPPORT
390 #define MA_OWNED 0x01
391 #define MA_NOTOWNED 0x02
392 #define MA_RECURSED 0x04
393 #define MA_NOTRECURSED 0x08
394 #endif /* INVARIANT_SUPPORT */
397 #define mtx_assert(m, what) \
398 _mtx_assert((m), (what), __FILE__, __LINE__)
400 #else /* INVARIANTS */
401 #define mtx_assert(m, what)
402 #endif /* INVARIANTS */
406 #endif /* _SYS_MUTEX_H_ */