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>
37 #include <sys/_lock.h>
38 #include <sys/_mutex.h>
42 #include <sys/lock_profile.h>
43 #include <sys/lockstat.h>
44 #include <machine/atomic.h>
45 #include <machine/cpufunc.h>
49 #include <machine/mutex.h>
54 * Mutex types and options passed to mtx_init(). MTX_QUIET and MTX_DUPOK
55 * can also be passed in.
57 #define MTX_DEF 0x00000000 /* DEFAULT (sleep) lock */
58 #define MTX_SPIN 0x00000001 /* Spin lock (disables interrupts) */
59 #define MTX_RECURSE 0x00000004 /* Option: lock allowed to recurse */
60 #define MTX_NOWITNESS 0x00000008 /* Don't do any witness checking. */
61 #define MTX_NOPROFILE 0x00000020 /* Don't profile this lock */
64 * Option flags passed to certain lock/unlock routines, through the use
65 * of corresponding mtx_{lock,unlock}_flags() interface macros.
67 #define MTX_QUIET LOP_QUIET /* Don't log a mutex event */
68 #define MTX_DUPOK LOP_DUPOK /* Don't log a duplicate acquire */
71 * State bits kept in mutex->mtx_lock, for the DEFAULT lock type. None of this,
72 * with the exception of MTX_UNOWNED, applies to spin locks.
74 #define MTX_RECURSED 0x00000001 /* lock recursed (for MTX_DEF only) */
75 #define MTX_CONTESTED 0x00000002 /* lock contested (for MTX_DEF only) */
76 #define MTX_UNOWNED 0x00000004 /* Cookie for free mutex */
77 #define MTX_FLAGMASK (MTX_RECURSED | MTX_CONTESTED | MTX_UNOWNED)
80 * Value stored in mutex->mtx_lock to denote a destroyed mutex.
82 #define MTX_DESTROYED (MTX_CONTESTED | MTX_UNOWNED)
89 * XXX: Friendly reminder to fix things in MP code that is presently being
92 #define mp_fixme(string)
99 * NOTE: Functions prepended with `_' (underscore) are exported to other parts
100 * of the kernel via macros, thus allowing us to use the cpp LOCK_FILE
101 * and LOCK_LINE. These functions should not be called directly by any
102 * code using the API. Their macros cover their functionality.
104 * [See below for descriptions]
107 void mtx_init(struct mtx *m, const char *name, const char *type, int opts);
108 void mtx_destroy(struct mtx *m);
109 void mtx_sysinit(void *arg);
110 void mutex_init(void);
111 void _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts,
112 const char *file, int line);
113 void _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line);
115 void _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts,
116 const char *file, int line);
118 void _mtx_unlock_spin(struct mtx *m, int opts, const char *file, int line);
119 int _mtx_trylock(struct mtx *m, int opts, const char *file, int line);
120 void _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line);
121 void _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line);
122 void _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file,
124 void _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file,
126 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
127 void _mtx_assert(struct mtx *m, int what, const char *file, int line);
129 void _thread_lock_flags(struct thread *, int, const char *, int);
131 #define thread_lock(tdp) \
132 _thread_lock_flags((tdp), 0, __FILE__, __LINE__)
133 #define thread_lock_flags(tdp, opt) \
134 _thread_lock_flags((tdp), (opt), __FILE__, __LINE__)
135 #define thread_unlock(tdp) \
136 mtx_unlock_spin((tdp)->td_lock)
138 #define mtx_recurse lock_object.lo_data
141 * We define our machine-independent (unoptimized) mutex micro-operations
142 * here, if they are not already defined in the machine-dependent mutex.h
145 /* Try to obtain mtx_lock once. */
147 #define _obtain_lock(mp, tid) \
148 atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
151 /* Try to release mtx_lock if it is unrecursed and uncontested. */
152 #ifndef _release_lock
153 #define _release_lock(mp, tid) \
154 atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), MTX_UNOWNED)
157 /* Release mtx_lock quickly, assuming we own it. */
158 #ifndef _release_lock_quick
159 #define _release_lock_quick(mp) \
160 atomic_store_rel_ptr(&(mp)->mtx_lock, MTX_UNOWNED)
164 * Obtain a sleep lock inline, or call the "hard" function if we can't get it
167 #ifndef _get_sleep_lock
168 #define _get_sleep_lock(mp, tid, opts, file, line) do { \
169 uintptr_t _tid = (uintptr_t)(tid); \
170 if (!_obtain_lock((mp), _tid)) \
171 _mtx_lock_sleep((mp), _tid, (opts), (file), (line)); \
173 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_LOCK_ACQUIRE, \
174 mp, 0, 0, (file), (line)); \
179 * Obtain a spin lock inline, or call the "hard" function if we can't get it
180 * easy. For spinlocks, we handle recursion inline (it turns out that function
181 * calls can be significantly expensive on some architectures).
182 * Since spin locks are not _too_ common, inlining this code is not too big
185 #ifndef _get_spin_lock
187 #define _get_spin_lock(mp, tid, opts, file, line) do { \
188 uintptr_t _tid = (uintptr_t)(tid); \
190 if (!_obtain_lock((mp), _tid)) { \
191 if ((mp)->mtx_lock == _tid) \
192 (mp)->mtx_recurse++; \
194 _mtx_lock_spin((mp), _tid, (opts), (file), (line)); \
197 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_SPIN_LOCK_ACQUIRE, \
198 mp, 0, 0, (file), (line)); \
201 #define _get_spin_lock(mp, tid, opts, file, line) do { \
202 uintptr_t _tid = (uintptr_t)(tid); \
205 if ((mp)->mtx_lock == _tid) \
206 (mp)->mtx_recurse++; \
208 KASSERT((mp)->mtx_lock == MTX_UNOWNED, ("corrupt spinlock")); \
209 (mp)->mtx_lock = _tid; \
216 * Release a sleep lock inline, or call the "hard" function if we can't do it
219 #ifndef _rel_sleep_lock
220 #define _rel_sleep_lock(mp, tid, opts, file, line) do { \
221 uintptr_t _tid = (uintptr_t)(tid); \
223 if (!_release_lock((mp), _tid)) \
224 _mtx_unlock_sleep((mp), (opts), (file), (line)); \
229 * For spinlocks, we can handle everything inline, as it's pretty simple and
230 * a function call would be too expensive (at least on some architectures).
231 * Since spin locks are not _too_ common, inlining this code is not too big
234 * Since we always perform a spinlock_enter() when attempting to acquire a
235 * spin lock, we need to always perform a matching spinlock_exit() when
236 * releasing a spin lock. This includes the recursion cases.
238 #ifndef _rel_spin_lock
240 #define _rel_spin_lock(mp) do { \
241 if (mtx_recursed((mp))) \
242 (mp)->mtx_recurse--; \
244 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_MTX_SPIN_UNLOCK_RELEASE, \
246 _release_lock_quick((mp)); \
251 #define _rel_spin_lock(mp) do { \
252 if (mtx_recursed((mp))) \
253 (mp)->mtx_recurse--; \
255 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_MTX_SPIN_UNLOCK_RELEASE, \
257 (mp)->mtx_lock = MTX_UNOWNED; \
265 * Exported lock manipulation interface.
267 * mtx_lock(m) locks MTX_DEF mutex `m'
269 * mtx_lock_spin(m) locks MTX_SPIN mutex `m'
271 * mtx_unlock(m) unlocks MTX_DEF mutex `m'
273 * mtx_unlock_spin(m) unlocks MTX_SPIN mutex `m'
275 * mtx_lock_spin_flags(m, opts) and mtx_lock_flags(m, opts) locks mutex `m'
276 * and passes option flags `opts' to the "hard" function, if required.
277 * With these routines, it is possible to pass flags such as MTX_QUIET
278 * to the appropriate lock manipulation routines.
280 * mtx_trylock(m) attempts to acquire MTX_DEF mutex `m' but doesn't sleep if
281 * it cannot. Rather, it returns 0 on failure and non-zero on success.
282 * It does NOT handle recursion as we assume that if a caller is properly
283 * using this part of the interface, he will know that the lock in question
286 * mtx_trylock_flags(m, opts) is used the same way as mtx_trylock() but accepts
287 * relevant option flags `opts.'
289 * mtx_initialized(m) returns non-zero if the lock `m' has been initialized.
291 * mtx_owned(m) returns non-zero if the current thread owns the lock `m'
293 * mtx_recursed(m) returns non-zero if the lock `m' is presently recursed.
295 #define mtx_lock(m) mtx_lock_flags((m), 0)
296 #define mtx_lock_spin(m) mtx_lock_spin_flags((m), 0)
297 #define mtx_trylock(m) mtx_trylock_flags((m), 0)
298 #define mtx_unlock(m) mtx_unlock_flags((m), 0)
299 #define mtx_unlock_spin(m) mtx_unlock_spin_flags((m), 0)
303 struct mtx_pool *mtx_pool_create(const char *mtx_name, int pool_size, int opts);
304 void mtx_pool_destroy(struct mtx_pool **poolp);
305 struct mtx *mtx_pool_find(struct mtx_pool *pool, void *ptr);
306 struct mtx *mtx_pool_alloc(struct mtx_pool *pool);
307 #define mtx_pool_lock(pool, ptr) \
308 mtx_lock(mtx_pool_find((pool), (ptr)))
309 #define mtx_pool_lock_spin(pool, ptr) \
310 mtx_lock_spin(mtx_pool_find((pool), (ptr)))
311 #define mtx_pool_unlock(pool, ptr) \
312 mtx_unlock(mtx_pool_find((pool), (ptr)))
313 #define mtx_pool_unlock_spin(pool, ptr) \
314 mtx_unlock_spin(mtx_pool_find((pool), (ptr)))
317 * mtxpool_lockbuilder is a pool of sleep locks that is not witness
318 * checked and should only be used for building higher level locks.
320 * mtxpool_sleep is a general purpose pool of sleep mutexes.
322 extern struct mtx_pool *mtxpool_lockbuilder;
323 extern struct mtx_pool *mtxpool_sleep;
326 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/mutex.h>
328 #if LOCK_DEBUG > 0 || defined(MUTEX_NOINLINE)
329 #define mtx_lock_flags(m, opts) \
330 _mtx_lock_flags((m), (opts), LOCK_FILE, LOCK_LINE)
331 #define mtx_unlock_flags(m, opts) \
332 _mtx_unlock_flags((m), (opts), LOCK_FILE, LOCK_LINE)
333 #define mtx_lock_spin_flags(m, opts) \
334 _mtx_lock_spin_flags((m), (opts), LOCK_FILE, LOCK_LINE)
335 #define mtx_unlock_spin_flags(m, opts) \
336 _mtx_unlock_spin_flags((m), (opts), LOCK_FILE, LOCK_LINE)
337 #else /* LOCK_DEBUG == 0 && !MUTEX_NOINLINE */
338 #define mtx_lock_flags(m, opts) \
339 _get_sleep_lock((m), curthread, (opts), LOCK_FILE, LOCK_LINE)
340 #define mtx_unlock_flags(m, opts) \
341 _rel_sleep_lock((m), curthread, (opts), LOCK_FILE, LOCK_LINE)
342 #define mtx_lock_spin_flags(m, opts) \
343 _get_spin_lock((m), curthread, (opts), LOCK_FILE, LOCK_LINE)
344 #define mtx_unlock_spin_flags(m, opts) \
346 #endif /* LOCK_DEBUG > 0 || MUTEX_NOINLINE */
348 #define mtx_trylock_flags(m, opts) \
349 _mtx_trylock((m), (opts), LOCK_FILE, LOCK_LINE)
351 #define mtx_sleep(chan, mtx, pri, wmesg, timo) \
352 _sleep((chan), &(mtx)->lock_object, (pri), (wmesg), (timo))
354 #define mtx_initialized(m) lock_initalized(&(m)->lock_object)
356 #define mtx_owned(m) (((m)->mtx_lock & ~MTX_FLAGMASK) == (uintptr_t)curthread)
358 #define mtx_recursed(m) ((m)->mtx_recurse != 0)
360 #define mtx_name(m) ((m)->lock_object.lo_name)
365 extern struct mtx Giant;
366 extern struct mtx blocked_lock;
369 * Giant lock manipulation and clean exit macros.
370 * Used to replace return with an exit Giant and return.
372 * Note that DROP_GIANT*() needs to be paired with PICKUP_GIANT()
373 * The #ifndef is to allow lint-like tools to redefine DROP_GIANT.
376 #define DROP_GIANT() \
379 WITNESS_SAVE_DECL(Giant); \
381 if (mtx_owned(&Giant)) { \
382 WITNESS_SAVE(&Giant.lock_object, Giant); \
383 for (_giantcnt = 0; mtx_owned(&Giant) && \
384 !SCHEDULER_STOPPED(); _giantcnt++) \
385 mtx_unlock(&Giant); \
388 #define PICKUP_GIANT() \
389 PARTIAL_PICKUP_GIANT(); \
392 #define PARTIAL_PICKUP_GIANT() \
393 mtx_assert(&Giant, MA_NOTOWNED); \
394 if (_giantcnt > 0) { \
395 while (_giantcnt--) \
397 WITNESS_RESTORE(&Giant.lock_object, Giant); \
401 #define UGAR(rval) do { \
403 mtx_unlock(&Giant); \
413 #define MTX_SYSINIT(name, mtx, desc, opts) \
414 static struct mtx_args name##_args = { \
419 SYSINIT(name##_mtx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \
420 mtx_sysinit, &name##_args); \
421 SYSUNINIT(name##_mtx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \
425 * The INVARIANTS-enabled mtx_assert() functionality.
427 * The constants need to be defined for INVARIANT_SUPPORT infrastructure
428 * support as _mtx_assert() itself uses them and the latter implies that
429 * _mtx_assert() must build.
431 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
432 #define MA_OWNED LA_XLOCKED
433 #define MA_NOTOWNED LA_UNLOCKED
434 #define MA_RECURSED LA_RECURSED
435 #define MA_NOTRECURSED LA_NOTRECURSED
439 #define mtx_assert(m, what) \
440 _mtx_assert((m), (what), __FILE__, __LINE__)
442 #define GIANT_REQUIRED mtx_assert(&Giant, MA_OWNED)
444 #else /* INVARIANTS */
445 #define mtx_assert(m, what) (void)0
446 #define GIANT_REQUIRED
447 #endif /* INVARIANTS */
450 * Common lock type names.
452 #define MTX_NETWORK_LOCK "network driver"
456 #endif /* _SYS_MUTEX_H_ */