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/lock.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/6.2/sys/sys/lock.h 164286 2006-11-14 20:42:41Z cvs2svn $
   30  */
   31 
   32 #ifndef _SYS_LOCK_H_
   33 #define _SYS_LOCK_H_
   34 
   35 #include <sys/queue.h>
   36 #include <sys/_lock.h>
   37 
   38 struct thread;
   39 
   40 /*
   41  * Lock classes.  Each lock has a class which describes characteristics
   42  * common to all types of locks of a given class.
   43  *
   44  * Spin locks in general must always protect against preemption, as it is
   45  * an error to perform any type of context switch while holding a spin lock.
   46  * Also, for an individual lock to be recursable, its class must allow
   47  * recursion and the lock itself must explicitly allow recursion.
   48  */
   49 
   50 struct lock_class {
   51         const   char *lc_name;
   52         u_int   lc_flags;
   53         void    (*lc_ddb_show)(struct lock_object *lock);
   54 };
   55 
   56 #define LC_SLEEPLOCK    0x00000001      /* Sleep lock. */
   57 #define LC_SPINLOCK     0x00000002      /* Spin lock. */
   58 #define LC_SLEEPABLE    0x00000004      /* Sleeping allowed with this lock. */
   59 #define LC_RECURSABLE   0x00000008      /* Locks of this type may recurse. */
   60 #define LC_UPGRADABLE   0x00000010      /* Upgrades and downgrades permitted. */
   61 
   62 #define LO_CLASSFLAGS   0x0000ffff      /* Class specific flags. */
   63 #define LO_INITIALIZED  0x00010000      /* Lock has been initialized. */
   64 #define LO_WITNESS      0x00020000      /* Should witness monitor this lock. */
   65 #define LO_QUIET        0x00040000      /* Don't log locking operations. */
   66 #define LO_RECURSABLE   0x00080000      /* Lock may recurse. */
   67 #define LO_SLEEPABLE    0x00100000      /* Lock may be held while sleeping. */
   68 #define LO_UPGRADABLE   0x00200000      /* Lock may be upgraded/downgraded. */
   69 #define LO_DUPOK        0x00400000      /* Don't check for duplicate acquires */
   70 
   71 #define LOCK_CLASS(lock)        ((lock)->lo_class)
   72 
   73 #define LI_RECURSEMASK  0x0000ffff      /* Recursion depth of lock instance. */
   74 #define LI_EXCLUSIVE    0x00010000      /* Exclusive lock instance. */
   75 
   76 /*
   77  * Option flags passed to lock operations that witness also needs to know
   78  * about or that are generic across all locks.
   79  */
   80 #define LOP_NEWORDER    0x00000001      /* Define a new lock order. */
   81 #define LOP_QUIET       0x00000002      /* Don't log locking operations. */
   82 #define LOP_TRYLOCK     0x00000004      /* Don't check lock order. */
   83 #define LOP_EXCLUSIVE   0x00000008      /* Exclusive lock. */
   84 #define LOP_DUPOK       0x00000010      /* Don't check for duplicate acquires */
   85 
   86 /* Flags passed to witness_assert. */
   87 #define LA_UNLOCKED     0x00000000      /* Lock is unlocked. */
   88 #define LA_LOCKED       0x00000001      /* Lock is at least share locked. */
   89 #define LA_SLOCKED      0x00000002      /* Lock is exactly share locked. */
   90 #define LA_XLOCKED      0x00000004      /* Lock is exclusively locked. */
   91 #define LA_RECURSED     0x00000008      /* Lock is recursed. */
   92 #define LA_NOTRECURSED  0x00000010      /* Lock is not recursed. */
   93 
   94 #ifdef _KERNEL
   95 /*
   96  * Lock instances.  A lock instance is the data associated with a lock while
   97  * it is held by witness.  For example, a lock instance will hold the
   98  * recursion count of a lock.  Lock instances are held in lists.  Spin locks
   99  * are held in a per-cpu list while sleep locks are held in per-process list.
  100  */
  101 struct lock_instance {
  102         struct  lock_object *li_lock;
  103         const   char *li_file;          /* File and line of last acquire. */
  104         int     li_line;
  105         u_int   li_flags;               /* Recursion count and LI_* flags. */
  106 };
  107 
  108 /*
  109  * A simple list type used to build the list of locks held by a process
  110  * or CPU.  We can't simply embed the list in struct lock_object since a
  111  * lock may be held by more than one process if it is a shared lock.  Locks
  112  * are added to the head of the list, so we fill up each list entry from
  113  * "the back" logically.  To ease some of the arithmetic, we actually fill
  114  * in each list entry the normal way (childer[0] then children[1], etc.) but
  115  * when we traverse the list we read children[count-1] as the first entry
  116  * down to children[0] as the final entry.
  117  */
  118 #define LOCK_NCHILDREN  3
  119 
  120 struct lock_list_entry {
  121         struct  lock_list_entry *ll_next;
  122         struct  lock_instance ll_children[LOCK_NCHILDREN];
  123         u_int   ll_count;
  124 };
  125 
  126 /*
  127  * If any of WITNESS, INVARIANTS, or KTR_LOCK KTR tracing has been enabled,
  128  * then turn on LOCK_DEBUG.  When this option is on, extra debugging
  129  * facilities such as tracking the file and line number of lock operations
  130  * are enabled.  Also, mutex locking operations are not inlined to avoid
  131  * bloat from all the extra debugging code.  We also have to turn on all the
  132  * calling conventions for this debugging code in modules so that modules can
  133  * work with both debug and non-debug kernels.
  134  */
  135 #if defined(KLD_MODULE) || defined(WITNESS) || defined(INVARIANTS) || defined(INVARIANT_SUPPORT) || defined(KTR) || defined(MUTEX_PROFILING)
  136 #define LOCK_DEBUG      1
  137 #else
  138 #define LOCK_DEBUG      0
  139 #endif
  140 
  141 /*
  142  * In the LOCK_DEBUG case, use the filename and line numbers for debugging
  143  * operations.  Otherwise, use default values to avoid the unneeded bloat.
  144  */
  145 #if LOCK_DEBUG > 0
  146 #define LOCK_FILE       __FILE__
  147 #define LOCK_LINE       __LINE__
  148 #else
  149 #define LOCK_FILE       NULL
  150 #define LOCK_LINE       0
  151 #endif
  152 
  153 /*
  154  * Macros for KTR_LOCK tracing.
  155  *
  156  * opname  - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
  157  * lo      - struct lock_object * for this lock
  158  * flags   - flags passed to the lock operation
  159  * recurse - this locks recursion level (or 0 if class is not recursable)
  160  * result  - result of a try lock operation
  161  * file    - file name
  162  * line    - line number
  163  */
  164 #define LOCK_LOG_TEST(lo, flags)                                        \
  165         (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
  166 
  167 #define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do {      \
  168         if (LOCK_LOG_TEST((lo), (flags)))                               \
  169                 CTR5(KTR_LOCK, opname " (%s) %s r = %d at %s:%d",       \
  170                     LOCK_CLASS(lo)->lc_name, (lo)->lo_name,             \
  171                     (u_int)(recurse), (file), (line));                  \
  172 } while (0)
  173 
  174 #define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do {        \
  175         if (LOCK_LOG_TEST((lo), (flags)))                               \
  176                 CTR5(KTR_LOCK, "TRY_" opname " (%s) %s result=%d at %s:%d",\
  177                     LOCK_CLASS(lo)->lc_name, (lo)->lo_name,             \
  178                     (u_int)(result), (file), (line));                   \
  179 } while (0)
  180 
  181 #define LOCK_LOG_INIT(lo, flags) do {                                   \
  182         if (LOCK_LOG_TEST((lo), (flags)))                               \
  183                 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo),        \
  184                     LOCK_CLASS(lo)->lc_name, (lo)->lo_name);            \
  185 } while (0)
  186 
  187 #define LOCK_LOG_DESTROY(lo, flags)     LOCK_LOG_INIT(lo, flags)
  188 
  189 #define lock_initalized(lo)     ((lo)->lo_flags & LO_INITIALIZED)
  190 
  191 /*
  192  * Helpful macros for quickly coming up with assertions with informative
  193  * panic messages.
  194  */
  195 #define MPASS(ex)               MPASS4(ex, #ex, __FILE__, __LINE__)
  196 #define MPASS2(ex, what)        MPASS4(ex, what, __FILE__, __LINE__)
  197 #define MPASS3(ex, file, line)  MPASS4(ex, #ex, file, line)
  198 #define MPASS4(ex, what, file, line)                                    \
  199         KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
  200 
  201 extern struct lock_class lock_class_mtx_sleep;
  202 extern struct lock_class lock_class_mtx_spin;
  203 extern struct lock_class lock_class_sx;
  204 
  205 void    lock_init(struct lock_object *lock, struct lock_class *class,
  206     const char *name, const char *type, int flags);
  207 void    lock_destroy(struct lock_object *lock);
  208 void    spinlock_enter(void);
  209 void    spinlock_exit(void);
  210 void    witness_init(struct lock_object *);
  211 void    witness_destroy(struct lock_object *);
  212 int     witness_defineorder(struct lock_object *, struct lock_object *);
  213 void    witness_checkorder(struct lock_object *, int, const char *, int);
  214 void    witness_lock(struct lock_object *, int, const char *, int);
  215 void    witness_upgrade(struct lock_object *, int, const char *, int);
  216 void    witness_downgrade(struct lock_object *, int, const char *, int);
  217 void    witness_unlock(struct lock_object *, int, const char *, int);
  218 void    witness_save(struct lock_object *, const char **, int *);
  219 void    witness_restore(struct lock_object *, const char *, int);
  220 int     witness_list_locks(struct lock_list_entry **);
  221 int     witness_warn(int, struct lock_object *, const char *, ...);
  222 void    witness_assert(struct lock_object *, int, const char *, int);
  223 void    witness_display_spinlock(struct lock_object *, struct thread *);
  224 int     witness_line(struct lock_object *);
  225 const char *witness_file(struct lock_object *);
  226 
  227 #ifdef  WITNESS
  228 
  229 /* Flags for witness_warn(). */
  230 #define WARN_GIANTOK    0x01    /* Giant is exempt from this check. */
  231 #define WARN_PANIC      0x02    /* Panic if check fails. */
  232 #define WARN_SLEEPOK    0x04    /* Sleepable locks are exempt from check. */
  233 
  234 #define WITNESS_INIT(lock)                                              \
  235         witness_init((lock))
  236 
  237 #define WITNESS_DESTROY(lock)                                           \
  238         witness_destroy(lock)
  239 
  240 #define WITNESS_CHECKORDER(lock, flags, file, line)                     \
  241         witness_checkorder((lock), (flags), (file), (line))
  242 
  243 #define WITNESS_DEFINEORDER(lock1, lock2)                               \
  244         witness_defineorder((struct lock_object *)(lock1),              \
  245             (struct lock_object *)(lock2))
  246 
  247 #define WITNESS_LOCK(lock, flags, file, line)                           \
  248         witness_lock((lock), (flags), (file), (line))
  249 
  250 #define WITNESS_UPGRADE(lock, flags, file, line)                        \
  251         witness_upgrade((lock), (flags), (file), (line))
  252 
  253 #define WITNESS_DOWNGRADE(lock, flags, file, line)                      \
  254         witness_downgrade((lock), (flags), (file), (line))
  255 
  256 #define WITNESS_UNLOCK(lock, flags, file, line)                         \
  257         witness_unlock((lock), (flags), (file), (line))
  258 
  259 #define WITNESS_WARN(flags, lock, fmt, ...)                             \
  260         witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
  261 
  262 #define WITNESS_SAVE_DECL(n)                                            \
  263         const char * __CONCAT(n, __wf);                                 \
  264         int __CONCAT(n, __wl)
  265 
  266 #define WITNESS_SAVE(lock, n)                                           \
  267         witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
  268 
  269 #define WITNESS_RESTORE(lock, n)                                        \
  270         witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
  271 
  272 #define WITNESS_FILE(lock)                                              \
  273         witness_file(lock)
  274 
  275 #define WITNESS_LINE(lock)                                              \
  276         witness_line(lock)
  277 
  278 #else   /* WITNESS */
  279 #define WITNESS_INIT(lock)
  280 #define WITNESS_DESTROY(lock)
  281 #define WITNESS_DEFINEORDER(lock1, lock2)       0
  282 #define WITNESS_CHECKORDER(lock, flags, file, line)
  283 #define WITNESS_LOCK(lock, flags, file, line)
  284 #define WITNESS_UPGRADE(lock, flags, file, line)
  285 #define WITNESS_DOWNGRADE(lock, flags, file, line)
  286 #define WITNESS_UNLOCK(lock, flags, file, line)
  287 #define WITNESS_WARN(flags, lock, fmt, ...)
  288 #define WITNESS_SAVE_DECL(n)
  289 #define WITNESS_SAVE(lock, n)
  290 #define WITNESS_RESTORE(lock, n)
  291 #define WITNESS_FILE(lock) ("?")
  292 #define WITNESS_LINE(lock) (0)
  293 #endif  /* WITNESS */
  294 
  295 /*
  296  * Helper macros to allow developers to add explicit lock order checks
  297  * wherever they please without having to actually grab a lock to do so.
  298  */
  299 #define witness_check_mutex(m)                                          \
  300         WITNESS_CHECKORDER(&(m)->mtx_object, LOP_EXCLUSIVE, LOCK_FILE,  \
  301             LOCK_LINE)
  302 
  303 #define witness_check_shared_sx(sx)                                     \
  304         WITNESS_CHECKORDER(&(sx)->sx_object, 0, LOCK_FILE, LOCK_LINE)
  305         
  306 #define witness_check_exclusive_sx(sx)                                  \
  307         WITNESS_CHECKORDER(&(sx)->sx_object, LOP_EXCLUSIVE, LOCK_FILE,  \
  308             LOCK_LINE)
  309 
  310 #endif  /* _KERNEL */
  311 #endif  /* _SYS_LOCK_H_ */

Cache object: 3e2e107cdefaf92dae79e08d8b94a4cd


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