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