The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/sys/mutex.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: d9e1e0104e347bfd0673582ae284bd71


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.