xref: /freebsd-11-stable/sys/sys/mutex.h (revision 4ab2e064d7950be84256d671a7ae93f87cc6aa36)
1 /*-
2  * Copyright (c) 1997 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.h,v 2.7.2.35 2000/04/27 03:10:26 cp Exp $
29  * $FreeBSD$
30  */
31 
32 #ifndef _SYS_MUTEX_H_
33 #define _SYS_MUTEX_H_
34 
35 #include <sys/queue.h>
36 #include <sys/_lock.h>
37 #include <sys/_mutex.h>
38 
39 #ifdef _KERNEL
40 #include <sys/pcpu.h>
41 #include <sys/lock_profile.h>
42 #include <sys/lockstat.h>
43 #include <machine/atomic.h>
44 #include <machine/cpufunc.h>
45 
46 /*
47  * Mutex types and options passed to mtx_init().  MTX_QUIET and MTX_DUPOK
48  * can also be passed in.
49  */
50 #define	MTX_DEF		0x00000000	/* DEFAULT (sleep) lock */
51 #define MTX_SPIN	0x00000001	/* Spin lock (disables interrupts) */
52 #define MTX_RECURSE	0x00000004	/* Option: lock allowed to recurse */
53 #define	MTX_NOWITNESS	0x00000008	/* Don't do any witness checking. */
54 #define MTX_NOPROFILE   0x00000020	/* Don't profile this lock */
55 #define	MTX_NEW		0x00000040	/* Don't check for double-init */
56 
57 /*
58  * Option flags passed to certain lock/unlock routines, through the use
59  * of corresponding mtx_{lock,unlock}_flags() interface macros.
60  */
61 #define	MTX_QUIET	LOP_QUIET	/* Don't log a mutex event */
62 #define	MTX_DUPOK	LOP_DUPOK	/* Don't log a duplicate acquire */
63 
64 /*
65  * State bits kept in mutex->mtx_lock, for the DEFAULT lock type. None of this,
66  * with the exception of MTX_UNOWNED, applies to spin locks.
67  */
68 #define	MTX_UNOWNED	0x00000000	/* Cookie for free mutex */
69 #define	MTX_RECURSED	0x00000001	/* lock recursed (for MTX_DEF only) */
70 #define	MTX_CONTESTED	0x00000002	/* lock contested (for MTX_DEF only) */
71 #define	MTX_DESTROYED	0x00000004	/* lock destroyed */
72 #define	MTX_FLAGMASK	(MTX_RECURSED | MTX_CONTESTED | MTX_DESTROYED)
73 
74 /*
75  * Prototypes
76  *
77  * NOTE: Functions prepended with `_' (underscore) are exported to other parts
78  *	 of the kernel via macros, thus allowing us to use the cpp LOCK_FILE
79  *	 and LOCK_LINE or for hiding the lock cookie crunching to the
80  *	 consumers. These functions should not be called directly by any
81  *	 code using the API. Their macros cover their functionality.
82  *	 Functions with a `_' suffix are the entrypoint for the common
83  *	 KPI covering both compat shims and fast path case.  These can be
84  *	 used by consumers willing to pass options, file and line
85  *	 informations, in an option-independent way.
86  *
87  * [See below for descriptions]
88  *
89  */
90 void	_mtx_init(volatile uintptr_t *c, const char *name, const char *type,
91 	    int opts);
92 void	_mtx_destroy(volatile uintptr_t *c);
93 void	mtx_sysinit(void *arg);
94 int	_mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF);
95 int	_mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file,
96 	    int line);
97 void	mutex_init(void);
98 #if LOCK_DEBUG > 0
99 void	__mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
100 	    const char *file, int line);
101 void	__mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
102 	    const char *file, int line);
103 #else
104 void	__mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v);
105 void	__mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v);
106 #endif
107 
108 #ifdef SMP
109 #if LOCK_DEBUG > 0
110 void	_mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
111 	    const char *file, int line);
112 #else
113 void	_mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v);
114 #endif
115 #endif
116 void	__mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file,
117 	    int line);
118 void	__mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file,
119 	    int line);
120 void	__mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
121 	     int line);
122 int	__mtx_trylock_spin_flags(volatile uintptr_t *c, int opts,
123 	     const char *file, int line);
124 void	__mtx_unlock_spin_flags(volatile uintptr_t *c, int opts,
125 	    const char *file, int line);
126 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
127 void	__mtx_assert(const volatile uintptr_t *c, int what, const char *file,
128 	    int line);
129 #endif
130 void	thread_lock_flags_(struct thread *, int, const char *, int);
131 #if LOCK_DEBUG > 0
132 void	_thread_lock(struct thread *td, int opts, const char *file, int line);
133 #else
134 void	_thread_lock(struct thread *);
135 #endif
136 
137 #if defined(LOCK_PROFILING) || defined(KLD_MODULE)
138 #define	thread_lock(tdp)						\
139 	thread_lock_flags_((tdp), 0, __FILE__, __LINE__)
140 #elif LOCK_DEBUG > 0
141 #define	thread_lock(tdp)						\
142 	_thread_lock((tdp), 0, __FILE__, __LINE__)
143 #else
144 #define	thread_lock(tdp)						\
145 	_thread_lock((tdp))
146 #endif
147 
148 #if LOCK_DEBUG > 0
149 #define	thread_lock_flags(tdp, opt)					\
150 	thread_lock_flags_((tdp), (opt), __FILE__, __LINE__)
151 #else
152 #define	thread_lock_flags(tdp, opt)					\
153 	_thread_lock(tdp)
154 #endif
155 
156 #define	thread_unlock(tdp)						\
157        mtx_unlock_spin((tdp)->td_lock)
158 
159 /*
160  * Top-level macros to provide lock cookie once the actual mtx is passed.
161  * They will also prevent passing a malformed object to the mtx KPI by
162  * failing compilation as the mtx_lock reserved member will not be found.
163  */
164 #define	mtx_init(m, n, t, o)						\
165 	_mtx_init(&(m)->mtx_lock, n, t, o)
166 #define	mtx_destroy(m)							\
167 	_mtx_destroy(&(m)->mtx_lock)
168 #define	mtx_trylock_flags_(m, o, f, l)					\
169 	_mtx_trylock_flags_(&(m)->mtx_lock, o, f, l)
170 #if LOCK_DEBUG > 0
171 #define	_mtx_lock_sleep(m, v, o, f, l)					\
172 	__mtx_lock_sleep(&(m)->mtx_lock, v, o, f, l)
173 #define	_mtx_unlock_sleep(m, v, o, f, l)				\
174 	__mtx_unlock_sleep(&(m)->mtx_lock, v, o, f, l)
175 #else
176 #define	_mtx_lock_sleep(m, v, o, f, l)					\
177 	__mtx_lock_sleep(&(m)->mtx_lock, v)
178 #define	_mtx_unlock_sleep(m, v, o, f, l)				\
179 	__mtx_unlock_sleep(&(m)->mtx_lock, v)
180 #endif
181 #ifdef SMP
182 #if LOCK_DEBUG > 0
183 #define	_mtx_lock_spin(m, v, o, f, l)					\
184 	_mtx_lock_spin_cookie(&(m)->mtx_lock, v, o, f, l)
185 #else
186 #define	_mtx_lock_spin(m, v, o, f, l)					\
187 	_mtx_lock_spin_cookie(&(m)->mtx_lock, v)
188 #endif
189 #endif
190 #define	_mtx_lock_flags(m, o, f, l)					\
191 	__mtx_lock_flags(&(m)->mtx_lock, o, f, l)
192 #define	_mtx_unlock_flags(m, o, f, l)					\
193 	__mtx_unlock_flags(&(m)->mtx_lock, o, f, l)
194 #define	_mtx_lock_spin_flags(m, o, f, l)				\
195 	__mtx_lock_spin_flags(&(m)->mtx_lock, o, f, l)
196 #define	_mtx_trylock_spin_flags(m, o, f, l)				\
197 	__mtx_trylock_spin_flags(&(m)->mtx_lock, o, f, l)
198 #define	_mtx_unlock_spin_flags(m, o, f, l)				\
199 	__mtx_unlock_spin_flags(&(m)->mtx_lock, o, f, l)
200 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
201 #define	_mtx_assert(m, w, f, l)						\
202 	__mtx_assert(&(m)->mtx_lock, w, f, l)
203 #endif
204 
205 #define	mtx_recurse	lock_object.lo_data
206 
207 /* Very simple operations on mtx_lock. */
208 
209 /* Try to obtain mtx_lock once. */
210 #define _mtx_obtain_lock(mp, tid)					\
211 	atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
212 
213 #define _mtx_obtain_lock_fetch(mp, vp, tid)				\
214 	atomic_fcmpset_acq_ptr(&(mp)->mtx_lock, vp, (tid))
215 
216 /* Try to release mtx_lock if it is unrecursed and uncontested. */
217 #define _mtx_release_lock(mp, tid)					\
218 	atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), MTX_UNOWNED)
219 
220 /* Release mtx_lock quickly, assuming we own it. */
221 #define _mtx_release_lock_quick(mp)					\
222 	atomic_store_rel_ptr(&(mp)->mtx_lock, MTX_UNOWNED)
223 
224 #define	_mtx_release_lock_fetch(mp, vp)					\
225 	atomic_fcmpset_rel_ptr(&(mp)->mtx_lock, (vp), MTX_UNOWNED)
226 
227 /*
228  * Full lock operations that are suitable to be inlined in non-debug
229  * kernels.  If the lock cannot be acquired or released trivially then
230  * the work is deferred to another function.
231  */
232 
233 /* Lock a normal mutex. */
234 #define __mtx_lock(mp, tid, opts, file, line) do {			\
235 	uintptr_t _tid = (uintptr_t)(tid);				\
236 	uintptr_t _v = MTX_UNOWNED;					\
237 									\
238 	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(adaptive__acquire) ||\
239 	    !_mtx_obtain_lock_fetch((mp), &_v, _tid)))			\
240 		_mtx_lock_sleep((mp), _v, (opts), (file), (line));	\
241 } while (0)
242 
243 /*
244  * Lock a spin mutex.  For spinlocks, we handle recursion inline (it
245  * turns out that function calls can be significantly expensive on
246  * some architectures).  Since spin locks are not _too_ common,
247  * inlining this code is not too big a deal.
248  */
249 #ifdef SMP
250 #define __mtx_lock_spin(mp, tid, opts, file, line) do {			\
251 	uintptr_t _tid = (uintptr_t)(tid);				\
252 	uintptr_t _v = MTX_UNOWNED;					\
253 									\
254 	spinlock_enter();						\
255 	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire) ||	\
256 	    !_mtx_obtain_lock_fetch((mp), &_v, _tid))) 			\
257 		_mtx_lock_spin((mp), _v, (opts), (file), (line)); 	\
258 } while (0)
259 #define __mtx_trylock_spin(mp, tid, opts, file, line) __extension__  ({	\
260 	uintptr_t _tid = (uintptr_t)(tid);				\
261 	int _ret;							\
262 									\
263 	spinlock_enter();						\
264 	if (((mp)->mtx_lock != MTX_UNOWNED || !_mtx_obtain_lock((mp), _tid))) {\
265 		spinlock_exit();					\
266 		_ret = 0;						\
267 	} else {							\
268 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire,	\
269 		    mp, 0, 0, file, line);				\
270 		_ret = 1;						\
271 	}								\
272 	_ret;								\
273 })
274 #else /* SMP */
275 #define __mtx_lock_spin(mp, tid, opts, file, line) do {			\
276 	uintptr_t _tid = (uintptr_t)(tid);				\
277 									\
278 	spinlock_enter();						\
279 	if ((mp)->mtx_lock == _tid)					\
280 		(mp)->mtx_recurse++;					\
281 	else {								\
282 		KASSERT((mp)->mtx_lock == MTX_UNOWNED, ("corrupt spinlock")); \
283 		(mp)->mtx_lock = _tid;					\
284 	}								\
285 } while (0)
286 #define __mtx_trylock_spin(mp, tid, opts, file, line) __extension__  ({	\
287 	uintptr_t _tid = (uintptr_t)(tid);				\
288 	int _ret;							\
289 									\
290 	spinlock_enter();						\
291 	if ((mp)->mtx_lock != MTX_UNOWNED) {				\
292 		spinlock_exit();					\
293 		_ret = 0;						\
294 	} else {							\
295 		(mp)->mtx_lock = _tid;					\
296 		_ret = 1;						\
297 	}								\
298 	_ret;								\
299 })
300 #endif /* SMP */
301 
302 /* Unlock a normal mutex. */
303 #define __mtx_unlock(mp, tid, opts, file, line) do {			\
304 	uintptr_t _v = (uintptr_t)(tid);				\
305 									\
306 	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(adaptive__release) ||\
307 	    !_mtx_release_lock_fetch((mp), &_v)))			\
308 		_mtx_unlock_sleep((mp), _v, (opts), (file), (line));	\
309 } while (0)
310 
311 /*
312  * Unlock a spin mutex.  For spinlocks, we can handle everything
313  * inline, as it's pretty simple and a function call would be too
314  * expensive (at least on some architectures).  Since spin locks are
315  * not _too_ common, inlining this code is not too big a deal.
316  *
317  * Since we always perform a spinlock_enter() when attempting to acquire a
318  * spin lock, we need to always perform a matching spinlock_exit() when
319  * releasing a spin lock.  This includes the recursion cases.
320  */
321 #ifdef SMP
322 #define __mtx_unlock_spin(mp) do {					\
323 	if (mtx_recursed((mp)))						\
324 		(mp)->mtx_recurse--;					\
325 	else {								\
326 		LOCKSTAT_PROFILE_RELEASE_LOCK(spin__release, mp);	\
327 		_mtx_release_lock_quick((mp));				\
328 	}								\
329 	spinlock_exit();						\
330 } while (0)
331 #else /* SMP */
332 #define __mtx_unlock_spin(mp) do {					\
333 	if (mtx_recursed((mp)))						\
334 		(mp)->mtx_recurse--;					\
335 	else {								\
336 		LOCKSTAT_PROFILE_RELEASE_LOCK(spin__release, mp);	\
337 		(mp)->mtx_lock = MTX_UNOWNED;				\
338 	}								\
339 	spinlock_exit();						\
340 } while (0)
341 #endif /* SMP */
342 
343 /*
344  * Exported lock manipulation interface.
345  *
346  * mtx_lock(m) locks MTX_DEF mutex `m'
347  *
348  * mtx_lock_spin(m) locks MTX_SPIN mutex `m'
349  *
350  * mtx_unlock(m) unlocks MTX_DEF mutex `m'
351  *
352  * mtx_unlock_spin(m) unlocks MTX_SPIN mutex `m'
353  *
354  * mtx_lock_spin_flags(m, opts) and mtx_lock_flags(m, opts) locks mutex `m'
355  *     and passes option flags `opts' to the "hard" function, if required.
356  *     With these routines, it is possible to pass flags such as MTX_QUIET
357  *     to the appropriate lock manipulation routines.
358  *
359  * mtx_trylock(m) attempts to acquire MTX_DEF mutex `m' but doesn't sleep if
360  *     it cannot. Rather, it returns 0 on failure and non-zero on success.
361  *     It does NOT handle recursion as we assume that if a caller is properly
362  *     using this part of the interface, he will know that the lock in question
363  *     is _not_ recursed.
364  *
365  * mtx_trylock_flags(m, opts) is used the same way as mtx_trylock() but accepts
366  *     relevant option flags `opts.'
367  *
368  * mtx_trylock_spin(m) attempts to acquire MTX_SPIN mutex `m' but doesn't
369  *     spin if it cannot.  Rather, it returns 0 on failure and non-zero on
370  *     success.  It always returns failure for recursed lock attempts.
371  *
372  * mtx_initialized(m) returns non-zero if the lock `m' has been initialized.
373  *
374  * mtx_owned(m) returns non-zero if the current thread owns the lock `m'
375  *
376  * mtx_recursed(m) returns non-zero if the lock `m' is presently recursed.
377  */
378 #define mtx_lock(m)		mtx_lock_flags((m), 0)
379 #define mtx_lock_spin(m)	mtx_lock_spin_flags((m), 0)
380 #define mtx_trylock(m)		mtx_trylock_flags((m), 0)
381 #define mtx_trylock_spin(m)	mtx_trylock_spin_flags((m), 0)
382 #define mtx_unlock(m)		mtx_unlock_flags((m), 0)
383 #define mtx_unlock_spin(m)	mtx_unlock_spin_flags((m), 0)
384 
385 struct mtx_pool;
386 
387 struct mtx_pool *mtx_pool_create(const char *mtx_name, int pool_size, int opts);
388 void mtx_pool_destroy(struct mtx_pool **poolp);
389 struct mtx *mtx_pool_find(struct mtx_pool *pool, void *ptr);
390 struct mtx *mtx_pool_alloc(struct mtx_pool *pool);
391 #define mtx_pool_lock(pool, ptr)					\
392 	mtx_lock(mtx_pool_find((pool), (ptr)))
393 #define mtx_pool_lock_spin(pool, ptr)					\
394 	mtx_lock_spin(mtx_pool_find((pool), (ptr)))
395 #define mtx_pool_unlock(pool, ptr)					\
396 	mtx_unlock(mtx_pool_find((pool), (ptr)))
397 #define mtx_pool_unlock_spin(pool, ptr)					\
398 	mtx_unlock_spin(mtx_pool_find((pool), (ptr)))
399 
400 /*
401  * mtxpool_sleep is a general purpose pool of sleep mutexes.
402  */
403 extern struct mtx_pool *mtxpool_sleep;
404 
405 #ifndef LOCK_DEBUG
406 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/mutex.h>
407 #endif
408 #if LOCK_DEBUG > 0 || defined(MUTEX_NOINLINE)
409 #define	mtx_lock_flags_(m, opts, file, line)				\
410 	_mtx_lock_flags((m), (opts), (file), (line))
411 #define	mtx_unlock_flags_(m, opts, file, line)				\
412 	_mtx_unlock_flags((m), (opts), (file), (line))
413 #define	mtx_lock_spin_flags_(m, opts, file, line)			\
414 	_mtx_lock_spin_flags((m), (opts), (file), (line))
415 #define	mtx_trylock_spin_flags_(m, opts, file, line)			\
416 	_mtx_trylock_spin_flags((m), (opts), (file), (line))
417 #define	mtx_unlock_spin_flags_(m, opts, file, line)			\
418 	_mtx_unlock_spin_flags((m), (opts), (file), (line))
419 #else	/* LOCK_DEBUG == 0 && !MUTEX_NOINLINE */
420 #define	mtx_lock_flags_(m, opts, file, line)				\
421 	__mtx_lock((m), curthread, (opts), (file), (line))
422 #define	mtx_unlock_flags_(m, opts, file, line)				\
423 	__mtx_unlock((m), curthread, (opts), (file), (line))
424 #define	mtx_lock_spin_flags_(m, opts, file, line)			\
425 	__mtx_lock_spin((m), curthread, (opts), (file), (line))
426 #define	mtx_trylock_spin_flags_(m, opts, file, line)			\
427 	__mtx_trylock_spin((m), curthread, (opts), (file), (line))
428 #define	mtx_unlock_spin_flags_(m, opts, file, line)			\
429 	__mtx_unlock_spin((m))
430 #endif	/* LOCK_DEBUG > 0 || MUTEX_NOINLINE */
431 
432 #ifdef INVARIANTS
433 #define	mtx_assert_(m, what, file, line)				\
434 	_mtx_assert((m), (what), (file), (line))
435 
436 #define GIANT_REQUIRED	mtx_assert_(&Giant, MA_OWNED, __FILE__, __LINE__)
437 
438 #else	/* INVARIANTS */
439 #define mtx_assert_(m, what, file, line)	(void)0
440 #define GIANT_REQUIRED
441 #endif	/* INVARIANTS */
442 
443 #define	mtx_lock_flags(m, opts)						\
444 	mtx_lock_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
445 #define	mtx_unlock_flags(m, opts)					\
446 	mtx_unlock_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
447 #define	mtx_lock_spin_flags(m, opts)					\
448 	mtx_lock_spin_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
449 #define	mtx_unlock_spin_flags(m, opts)					\
450 	mtx_unlock_spin_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
451 #define mtx_trylock_flags(m, opts)					\
452 	mtx_trylock_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
453 #define mtx_trylock_spin_flags(m, opts)					\
454 	mtx_trylock_spin_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
455 #define	mtx_assert(m, what)						\
456 	mtx_assert_((m), (what), __FILE__, __LINE__)
457 
458 #define	mtx_sleep(chan, mtx, pri, wmesg, timo)				\
459 	_sleep((chan), &(mtx)->lock_object, (pri), (wmesg),		\
460 	    tick_sbt * (timo), 0, C_HARDCLOCK)
461 
462 #define	MTX_READ_VALUE(m)	((m)->mtx_lock)
463 
464 #define	mtx_initialized(m)	lock_initialized(&(m)->lock_object)
465 
466 #define lv_mtx_owner(v)	((struct thread *)((v) & ~MTX_FLAGMASK))
467 
468 #define mtx_owner(m)	lv_mtx_owner(MTX_READ_VALUE(m))
469 
470 #define mtx_owned(m)	(mtx_owner(m) == curthread)
471 
472 #define mtx_recursed(m)	((m)->mtx_recurse != 0)
473 
474 #define mtx_name(m)	((m)->lock_object.lo_name)
475 
476 /*
477  * Global locks.
478  */
479 extern struct mtx Giant;
480 extern struct mtx blocked_lock;
481 
482 /*
483  * Giant lock manipulation and clean exit macros.
484  * Used to replace return with an exit Giant and return.
485  *
486  * Note that DROP_GIANT*() needs to be paired with PICKUP_GIANT()
487  * The #ifndef is to allow lint-like tools to redefine DROP_GIANT.
488  */
489 #ifndef DROP_GIANT
490 #define DROP_GIANT()							\
491 do {									\
492 	int _giantcnt = 0;						\
493 	WITNESS_SAVE_DECL(Giant);					\
494 									\
495 	if (mtx_owned(&Giant)) {					\
496 		WITNESS_SAVE(&Giant.lock_object, Giant);		\
497 		for (_giantcnt = 0; mtx_owned(&Giant) &&		\
498 		    !SCHEDULER_STOPPED(); _giantcnt++)			\
499 			mtx_unlock(&Giant);				\
500 	}
501 
502 #define PICKUP_GIANT()							\
503 	PARTIAL_PICKUP_GIANT();						\
504 } while (0)
505 
506 #define PARTIAL_PICKUP_GIANT()						\
507 	mtx_assert(&Giant, MA_NOTOWNED);				\
508 	if (_giantcnt > 0) {						\
509 		while (_giantcnt--)					\
510 			mtx_lock(&Giant);				\
511 		WITNESS_RESTORE(&Giant.lock_object, Giant);		\
512 	}
513 #endif
514 
515 struct mtx_args {
516 	void		*ma_mtx;
517 	const char 	*ma_desc;
518 	int		 ma_opts;
519 };
520 
521 #define	MTX_SYSINIT(name, mtx, desc, opts)				\
522 	static struct mtx_args name##_args = {				\
523 		(mtx),							\
524 		(desc),							\
525 		(opts)							\
526 	};								\
527 	SYSINIT(name##_mtx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
528 	    mtx_sysinit, &name##_args);					\
529 	SYSUNINIT(name##_mtx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
530 	    _mtx_destroy, __DEVOLATILE(void *, &(mtx)->mtx_lock))
531 
532 /*
533  * The INVARIANTS-enabled mtx_assert() functionality.
534  *
535  * The constants need to be defined for INVARIANT_SUPPORT infrastructure
536  * support as _mtx_assert() itself uses them and the latter implies that
537  * _mtx_assert() must build.
538  */
539 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
540 #define MA_OWNED	LA_XLOCKED
541 #define MA_NOTOWNED	LA_UNLOCKED
542 #define MA_RECURSED	LA_RECURSED
543 #define MA_NOTRECURSED	LA_NOTRECURSED
544 #endif
545 
546 /*
547  * Common lock type names.
548  */
549 #define	MTX_NETWORK_LOCK	"network driver"
550 
551 #endif	/* _KERNEL */
552 #endif	/* _SYS_MUTEX_H_ */
553