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

Cache object: 7ddd1132991e4dc6a35c4126dd54a210


[ 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.