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/5.3/sys/sys/mutex.h 136588 2004-10-16 08:43:07Z cvs2svn $
   30  */
   31 
   32 #ifndef _SYS_MUTEX_H_
   33 #define _SYS_MUTEX_H_
   34 
   35 #ifndef LOCORE
   36 #include <sys/queue.h>
   37 #include <sys/_lock.h>
   38 #include <sys/_mutex.h>
   39 
   40 #ifdef _KERNEL
   41 #include <sys/pcpu.h>
   42 #include <machine/atomic.h>
   43 #include <machine/cpufunc.h>
   44 #endif  /* _KERNEL_ */
   45 #endif  /* !LOCORE */
   46 
   47 #include <machine/mutex.h>
   48 
   49 #ifdef _KERNEL
   50 
   51 /*
   52  * Mutex types and options passed to mtx_init().  MTX_QUIET can also be
   53  * passed in.
   54  */
   55 #define MTX_DEF         0x00000000      /* DEFAULT (sleep) lock */ 
   56 #define MTX_SPIN        0x00000001      /* Spin lock (disables interrupts) */
   57 #define MTX_RECURSE     0x00000004      /* Option: lock allowed to recurse */
   58 #define MTX_NOWITNESS   0x00000008      /* Don't do any witness checking. */
   59 #define MTX_DUPOK       0x00000020      /* Don't log a duplicate acquire */
   60 
   61 /*
   62  * Option flags passed to certain lock/unlock routines, through the use
   63  * of corresponding mtx_{lock,unlock}_flags() interface macros.
   64  */
   65 #define MTX_QUIET       LOP_QUIET       /* Don't log a mutex event */
   66 
   67 /*
   68  * State bits kept in mutex->mtx_lock, for the DEFAULT lock type. None of this,
   69  * with the exception of MTX_UNOWNED, applies to spin locks.
   70  */
   71 #define MTX_RECURSED    0x00000001      /* lock recursed (for MTX_DEF only) */
   72 #define MTX_CONTESTED   0x00000002      /* lock contested (for MTX_DEF only) */
   73 #define MTX_UNOWNED     0x00000004      /* Cookie for free mutex */
   74 #define MTX_FLAGMASK    ~(MTX_RECURSED | MTX_CONTESTED)
   75 
   76 #endif  /* _KERNEL */
   77 
   78 #ifndef LOCORE
   79 
   80 /*
   81  * XXX: Friendly reminder to fix things in MP code that is presently being
   82  * XXX: worked on.
   83  */
   84 #define mp_fixme(string)
   85 
   86 #ifdef _KERNEL
   87 
   88 /*
   89  * Prototypes
   90  *
   91  * NOTE: Functions prepended with `_' (underscore) are exported to other parts
   92  *       of the kernel via macros, thus allowing us to use the cpp LOCK_FILE
   93  *       and LOCK_LINE. These functions should not be called directly by any
   94  *       code using the API. Their macros cover their functionality.
   95  *
   96  * [See below for descriptions]
   97  *
   98  */
   99 void    mtx_init(struct mtx *m, const char *name, const char *type, int opts);
  100 void    mtx_destroy(struct mtx *m);
  101 void    mtx_sysinit(void *arg);
  102 void    mutex_init(void);
  103 void    _mtx_lock_sleep(struct mtx *m, struct thread *td, int opts,
  104             const char *file, int line);
  105 void    _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line);
  106 void    _mtx_lock_spin(struct mtx *m, struct thread *td, int opts,
  107             const char *file, int line);
  108 void    _mtx_unlock_spin(struct mtx *m, int opts, const char *file, int line);
  109 int     _mtx_trylock(struct mtx *m, int opts, const char *file, int line);
  110 void    _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line);
  111 void    _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line);
  112 void    _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file,
  113              int line);
  114 void    _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file,
  115              int line);
  116 #ifdef INVARIANT_SUPPORT
  117 void    _mtx_assert(struct mtx *m, int what, const char *file, int line);
  118 #endif
  119 
  120 /*
  121  * We define our machine-independent (unoptimized) mutex micro-operations
  122  * here, if they are not already defined in the machine-dependent mutex.h 
  123  */
  124 
  125 /* Actually obtain mtx_lock */
  126 #ifndef _obtain_lock
  127 #define _obtain_lock(mp, tid)                                           \
  128         atomic_cmpset_acq_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED, (tid))
  129 #endif
  130 
  131 /* Actually release mtx_lock */
  132 #ifndef _release_lock
  133 #define _release_lock(mp, tid)                                          \
  134         atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), (void *)MTX_UNOWNED)
  135 #endif
  136 
  137 /* Actually release mtx_lock quickly, assuming we own it. */
  138 #ifndef _release_lock_quick
  139 #define _release_lock_quick(mp)                                         \
  140         atomic_store_rel_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED)
  141 #endif
  142 
  143 /*
  144  * Obtain a sleep lock inline, or call the "hard" function if we can't get it
  145  * easy.
  146  */
  147 #ifndef _get_sleep_lock
  148 #define _get_sleep_lock(mp, tid, opts, file, line) do {                 \
  149         struct thread *_tid = (tid);                                    \
  150                                                                         \
  151         if (!_obtain_lock((mp), _tid))                                  \
  152                 _mtx_lock_sleep((mp), _tid, (opts), (file), (line));    \
  153 } while (0)
  154 #endif
  155 
  156 /*
  157  * Obtain a spin lock inline, or call the "hard" function if we can't get it
  158  * easy. For spinlocks, we handle recursion inline (it turns out that function
  159  * calls can be significantly expensive on some architectures).
  160  * Since spin locks are not _too_ common, inlining this code is not too big 
  161  * a deal.
  162  */
  163 #ifndef _get_spin_lock
  164 #define _get_spin_lock(mp, tid, opts, file, line) do {                  \
  165         struct thread *_tid = (tid);                                    \
  166                                                                         \
  167         critical_enter();                                               \
  168         if (!_obtain_lock((mp), _tid)) {                                \
  169                 if ((mp)->mtx_lock == (uintptr_t)_tid)                  \
  170                         (mp)->mtx_recurse++;                            \
  171                 else                                                    \
  172                         _mtx_lock_spin((mp), _tid, (opts), (file), (line)); \
  173         }                                                               \
  174 } while (0)
  175 #endif
  176 
  177 /*
  178  * Release a sleep lock inline, or call the "hard" function if we can't do it
  179  * easy.
  180  */
  181 #ifndef _rel_sleep_lock
  182 #define _rel_sleep_lock(mp, tid, opts, file, line) do {                 \
  183         if (!_release_lock((mp), (tid)))                                \
  184                 _mtx_unlock_sleep((mp), (opts), (file), (line));        \
  185 } while (0)
  186 #endif
  187 
  188 /*
  189  * For spinlocks, we can handle everything inline, as it's pretty simple and
  190  * a function call would be too expensive (at least on some architectures).
  191  * Since spin locks are not _too_ common, inlining this code is not too big 
  192  * a deal.
  193  *
  194  * Since we always perform a critical_enter() when attempting to acquire a
  195  * spin lock, we need to always perform a matching critical_exit() when
  196  * releasing a spin lock.  This includes the recursion cases.
  197  */
  198 #ifndef _rel_spin_lock
  199 #define _rel_spin_lock(mp) do {                                         \
  200         if (mtx_recursed((mp)))                                         \
  201                 (mp)->mtx_recurse--;                                    \
  202         else                                                            \
  203                 _release_lock_quick((mp));                              \
  204         critical_exit();                                                \
  205 } while (0)
  206 #endif
  207 
  208 /*
  209  * Exported lock manipulation interface.
  210  *
  211  * mtx_lock(m) locks MTX_DEF mutex `m'
  212  *
  213  * mtx_lock_spin(m) locks MTX_SPIN mutex `m'
  214  *
  215  * mtx_unlock(m) unlocks MTX_DEF mutex `m'
  216  *
  217  * mtx_unlock_spin(m) unlocks MTX_SPIN mutex `m'
  218  *
  219  * mtx_lock_spin_flags(m, opts) and mtx_lock_flags(m, opts) locks mutex `m'
  220  *     and passes option flags `opts' to the "hard" function, if required.
  221  *     With these routines, it is possible to pass flags such as MTX_QUIET
  222  *     to the appropriate lock manipulation routines.
  223  *
  224  * mtx_trylock(m) attempts to acquire MTX_DEF mutex `m' but doesn't sleep if
  225  *     it cannot. Rather, it returns 0 on failure and non-zero on success.
  226  *     It does NOT handle recursion as we assume that if a caller is properly
  227  *     using this part of the interface, he will know that the lock in question
  228  *     is _not_ recursed.
  229  *
  230  * mtx_trylock_flags(m, opts) is used the same way as mtx_trylock() but accepts
  231  *     relevant option flags `opts.'
  232  *
  233  * mtx_initialized(m) returns non-zero if the lock `m' has been initialized.
  234  *
  235  * mtx_owned(m) returns non-zero if the current thread owns the lock `m'
  236  *
  237  * mtx_recursed(m) returns non-zero if the lock `m' is presently recursed.
  238  */ 
  239 #define mtx_lock(m)             mtx_lock_flags((m), 0)
  240 #define mtx_lock_spin(m)        mtx_lock_spin_flags((m), 0)
  241 #define mtx_trylock(m)          mtx_trylock_flags((m), 0)
  242 #define mtx_unlock(m)           mtx_unlock_flags((m), 0)
  243 #define mtx_unlock_spin(m)      mtx_unlock_spin_flags((m), 0)
  244 
  245 struct mtx_pool;
  246 
  247 struct mtx_pool *mtx_pool_create(const char *mtx_name, int pool_size, int opts);
  248 void mtx_pool_destroy(struct mtx_pool **poolp);
  249 struct mtx *mtx_pool_find(struct mtx_pool *pool, void *ptr);
  250 struct mtx *mtx_pool_alloc(struct mtx_pool *pool);
  251 #define mtx_pool_lock(pool, ptr)                                        \
  252         mtx_lock(mtx_pool_find((pool), (ptr)))
  253 #define mtx_pool_lock_spin(pool, ptr)                                   \
  254         mtx_lock_spin(mtx_pool_find((pool), (ptr)))
  255 #define mtx_pool_unlock(pool, ptr)                                      \
  256         mtx_unlock(mtx_pool_find((pool), (ptr)))
  257 #define mtx_pool_unlock_spin(pool, ptr)                                 \
  258         mtx_unlock_spin(mtx_pool_find((pool), (ptr)))
  259 
  260 /*
  261  * mtxpool_lockbuilder is a pool of sleep locks that is not witness
  262  * checked and should only be used for building higher level locks.
  263  *
  264  * mtxpool_sleep is a general purpose pool of sleep mutexes.
  265  */
  266 extern struct mtx_pool *mtxpool_lockbuilder;
  267 extern struct mtx_pool *mtxpool_sleep;
  268 
  269 #ifndef LOCK_DEBUG
  270 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/mutex.h>
  271 #endif
  272 #if LOCK_DEBUG > 0 || defined(MUTEX_NOINLINE)
  273 #define mtx_lock_flags(m, opts)                                         \
  274         _mtx_lock_flags((m), (opts), LOCK_FILE, LOCK_LINE)
  275 #define mtx_unlock_flags(m, opts)                                       \
  276         _mtx_unlock_flags((m), (opts), LOCK_FILE, LOCK_LINE)
  277 #define mtx_lock_spin_flags(m, opts)                                    \
  278         _mtx_lock_spin_flags((m), (opts), LOCK_FILE, LOCK_LINE)
  279 #define mtx_unlock_spin_flags(m, opts)                                  \
  280         _mtx_unlock_spin_flags((m), (opts), LOCK_FILE, LOCK_LINE)
  281 #else   /* LOCK_DEBUG == 0 && !MUTEX_NOINLINE */
  282 #define mtx_lock_flags(m, opts)                                         \
  283         _get_sleep_lock((m), curthread, (opts), LOCK_FILE, LOCK_LINE)
  284 #define mtx_unlock_flags(m, opts)                                       \
  285         _rel_sleep_lock((m), curthread, (opts), LOCK_FILE, LOCK_LINE)
  286 #ifndef SMPnotyet
  287 #define mtx_lock_spin_flags(m, opts)                                    \
  288         _get_spin_lock((m), curthread, (opts), LOCK_FILE, LOCK_LINE)
  289 #define mtx_unlock_spin_flags(m, opts)                                  \
  290         _rel_spin_lock((m))
  291 #else   /* SMP */
  292 #define mtx_lock_spin_flags(m, opts)    critical_enter()
  293 #define mtx_unlock_spin_flags(m, opts)  critical_exit()
  294 #endif  /* SMP */
  295 #endif  /* LOCK_DEBUG > 0 || MUTEX_NOINLINE */
  296 
  297 #define mtx_trylock_flags(m, opts)                                      \
  298         _mtx_trylock((m), (opts), LOCK_FILE, LOCK_LINE)
  299 
  300 #define mtx_initialized(m)      ((m)->mtx_object.lo_flags & LO_INITIALIZED)
  301 
  302 #define mtx_owned(m)    (((m)->mtx_lock & MTX_FLAGMASK) == (uintptr_t)curthread)
  303 
  304 #define mtx_recursed(m) ((m)->mtx_recurse != 0)
  305 
  306 #define mtx_name(m)     ((m)->mtx_object.lo_name)
  307 
  308 /*
  309  * Global locks.
  310  */
  311 extern struct mtx sched_lock;
  312 extern struct mtx Giant;
  313 
  314 /*
  315  * Giant lock manipulation and clean exit macros.
  316  * Used to replace return with an exit Giant and return.
  317  *
  318  * Note that DROP_GIANT*() needs to be paired with PICKUP_GIANT() 
  319  * The #ifndef is to allow lint-like tools to redefine DROP_GIANT.
  320  */
  321 #ifndef DROP_GIANT
  322 #define DROP_GIANT()                                                    \
  323 do {                                                                    \
  324         int _giantcnt;                                                  \
  325         WITNESS_SAVE_DECL(Giant);                                       \
  326                                                                         \
  327         if (mtx_owned(&Giant))                                          \
  328                 WITNESS_SAVE(&Giant.mtx_object, Giant);                 \
  329         for (_giantcnt = 0; mtx_owned(&Giant); _giantcnt++)             \
  330                 mtx_unlock(&Giant)
  331 
  332 #define PICKUP_GIANT()                                                  \
  333         mtx_assert(&Giant, MA_NOTOWNED);                                \
  334         while (_giantcnt--)                                             \
  335                 mtx_lock(&Giant);                                       \
  336         if (mtx_owned(&Giant))                                          \
  337                 WITNESS_RESTORE(&Giant.mtx_object, Giant);              \
  338 } while (0)
  339 
  340 #define PARTIAL_PICKUP_GIANT()                                          \
  341         mtx_assert(&Giant, MA_NOTOWNED);                                \
  342         while (_giantcnt--)                                             \
  343                 mtx_lock(&Giant);                                       \
  344         if (mtx_owned(&Giant))                                          \
  345                 WITNESS_RESTORE(&Giant.mtx_object, Giant)
  346 #endif
  347 
  348 /*
  349  * Network MPSAFE temporary workarounds.  When debug_mpsafenet
  350  * is 1 the network is assumed to operate without Giant on the
  351  * input path and protocols that require Giant must collect it
  352  * on entry.  When 0 Giant is grabbed in the network interface
  353  * ISR's and in the netisr path and there is no need to grab
  354  * the Giant lock.  Note that, unlike GIANT_PICKUP() and
  355  * GIANT_DROP(), these macros directly wrap mutex operations
  356  * without special recursion handling.
  357  *
  358  * This mechanism is intended as temporary until everything of
  359  * importance is properly locked.  Note: the semantics for
  360  * NET_{LOCK,UNLOCK}_GIANT() are not the same as DROP_GIANT()
  361  * and PICKUP_GIANT(), as they are plain mutex operations
  362  * without a recursion counter.
  363  */
  364 extern  int debug_mpsafenet;            /* defined in net/netisr.c */
  365 #define NET_LOCK_GIANT() do {                                           \
  366         if (!debug_mpsafenet)                                           \
  367                 mtx_lock(&Giant);                                       \
  368 } while (0)
  369 #define NET_UNLOCK_GIANT() do {                                         \
  370         if (!debug_mpsafenet)                                           \
  371                 mtx_unlock(&Giant);                                     \
  372 } while (0)
  373 #define NET_ASSERT_GIANT() do {                                         \
  374         if (!debug_mpsafenet)                                           \
  375                 mtx_assert(&Giant, MA_OWNED);                           \
  376 } while (0)
  377 
  378 #define UGAR(rval) do {                                                 \
  379         int _val = (rval);                                              \
  380         mtx_unlock(&Giant);                                             \
  381         return (_val);                                                  \
  382 } while (0)
  383 
  384 struct mtx_args {
  385         struct mtx      *ma_mtx;
  386         const char      *ma_desc;
  387         int              ma_opts;
  388 };
  389 
  390 #define MTX_SYSINIT(name, mtx, desc, opts)                              \
  391         static struct mtx_args name##_args = {                          \
  392                 (mtx),                                                  \
  393                 (desc),                                                 \
  394                 (opts)                                                  \
  395         };                                                              \
  396         SYSINIT(name##_mtx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,       \
  397             mtx_sysinit, &name##_args)
  398 
  399 /*
  400  * The INVARIANTS-enabled mtx_assert() functionality.
  401  *
  402  * The constants need to be defined for INVARIANT_SUPPORT infrastructure
  403  * support as _mtx_assert() itself uses them and the latter implies that
  404  * _mtx_assert() must build.
  405  */
  406 #ifdef INVARIANT_SUPPORT
  407 #define MA_OWNED        0x01
  408 #define MA_NOTOWNED     0x02
  409 #define MA_RECURSED     0x04
  410 #define MA_NOTRECURSED  0x08
  411 #endif /* INVARIANT_SUPPORT */
  412 
  413 #ifdef INVARIANTS
  414 #define mtx_assert(m, what)                                             \
  415         _mtx_assert((m), (what), __FILE__, __LINE__)
  416 
  417 #define GIANT_REQUIRED  mtx_assert(&Giant, MA_OWNED)
  418 
  419 #else   /* INVARIANTS */
  420 #define mtx_assert(m, what)
  421 #define GIANT_REQUIRED
  422 #endif  /* INVARIANTS */
  423 
  424 /*
  425  * Common lock type names.
  426  */
  427 #define MTX_NETWORK_LOCK        "network driver"
  428 
  429 #endif  /* _KERNEL */
  430 #endif  /* !LOCORE */
  431 #endif  /* _SYS_MUTEX_H_ */

Cache object: caaa8d54b9debab561c09d9fbd7f7b23


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