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
   29  * $FreeBSD$
   30  */
   31 
   32 #ifndef _SYS_LOCK_H_
   33 #define _SYS_LOCK_H_
   34 
   35 #include <sys/queue.h>
   36 #include <sys/_lock.h>
   37 #include <sys/ktr_class.h>
   38 
   39 struct lock_list_entry;
   40 struct thread;
   41 
   42 /*
   43  * Lock classes.  Each lock has a class which describes characteristics
   44  * common to all types of locks of a given class.
   45  *
   46  * Spin locks in general must always protect against preemption, as it is
   47  * an error to perform any type of context switch while holding a spin lock.
   48  * Also, for an individual lock to be recursable, its class must allow
   49  * recursion and the lock itself must explicitly allow recursion.
   50  *
   51  * The 'lc_ddb_show' function pointer is used to dump class-specific
   52  * data for the 'show lock' DDB command.  The 'lc_lock' and
   53  * 'lc_unlock' function pointers are used in sleep(9) and cv_wait(9)
   54  * to lock and unlock locks while blocking on a sleep queue.  The
   55  * return value of 'lc_unlock' will be passed to 'lc_lock' on resume
   56  * to allow communication of state between the two routines.
   57  */
   58 
   59 struct lock_class {
   60         const           char *lc_name;
   61         u_int           lc_flags;
   62         void            (*lc_assert)(const struct lock_object *lock, int what);
   63         void            (*lc_ddb_show)(const struct lock_object *lock);
   64         void            (*lc_lock)(struct lock_object *lock, uintptr_t how);
   65         int             (*lc_owner)(const struct lock_object *lock,
   66                             struct thread **owner);
   67         uintptr_t       (*lc_unlock)(struct lock_object *lock);
   68 };
   69 
   70 #define LC_SLEEPLOCK    0x00000001      /* Sleep lock. */
   71 #define LC_SPINLOCK     0x00000002      /* Spin lock. */
   72 #define LC_SLEEPABLE    0x00000004      /* Sleeping allowed with this lock. */
   73 #define LC_RECURSABLE   0x00000008      /* Locks of this type may recurse. */
   74 #define LC_UPGRADABLE   0x00000010      /* Upgrades and downgrades permitted. */
   75 
   76 #define LO_CLASSFLAGS   0x0000ffff      /* Class specific flags. */
   77 #define LO_INITIALIZED  0x00010000      /* Lock has been initialized. */
   78 #define LO_WITNESS      0x00020000      /* Should witness monitor this lock. */
   79 #define LO_QUIET        0x00040000      /* Don't log locking operations. */
   80 #define LO_RECURSABLE   0x00080000      /* Lock may recurse. */
   81 #define LO_SLEEPABLE    0x00100000      /* Lock may be held while sleeping. */
   82 #define LO_UPGRADABLE   0x00200000      /* Lock may be upgraded/downgraded. */
   83 #define LO_DUPOK        0x00400000      /* Don't check for duplicate acquires */
   84 #define LO_IS_VNODE     0x00800000      /* Tell WITNESS about a VNODE lock */
   85 #define LO_CLASSMASK    0x0f000000      /* Class index bitmask. */
   86 #define LO_NOPROFILE    0x10000000      /* Don't profile this lock */
   87 #define LO_NEW          0x20000000      /* Don't check for double-init */
   88 
   89 /*
   90  * Lock classes are statically assigned an index into the gobal lock_classes
   91  * array.  Debugging code looks up the lock class for a given lock object
   92  * by indexing the array.
   93  */
   94 #define LO_CLASSSHIFT           24
   95 #define LO_CLASSINDEX(lock)     ((((lock)->lo_flags) & LO_CLASSMASK) >> LO_CLASSSHIFT)
   96 #define LOCK_CLASS(lock)        (lock_classes[LO_CLASSINDEX((lock))])
   97 #define LOCK_CLASS_MAX          (LO_CLASSMASK >> LO_CLASSSHIFT)
   98 
   99 /*
  100  * Option flags passed to lock operations that witness also needs to know
  101  * about or that are generic across all locks.
  102  */
  103 #define LOP_NEWORDER    0x00000001      /* Define a new lock order. */
  104 #define LOP_QUIET       0x00000002      /* Don't log locking operations. */
  105 #define LOP_TRYLOCK     0x00000004      /* Don't check lock order. */
  106 #define LOP_EXCLUSIVE   0x00000008      /* Exclusive lock. */
  107 #define LOP_DUPOK       0x00000010      /* Don't check for duplicate acquires */
  108 
  109 /* Flags passed to witness_assert. */
  110 #define LA_MASKASSERT   0x000000ff      /* Mask for witness defined asserts. */
  111 #define LA_UNLOCKED     0x00000000      /* Lock is unlocked. */
  112 #define LA_LOCKED       0x00000001      /* Lock is at least share locked. */
  113 #define LA_SLOCKED      0x00000002      /* Lock is exactly share locked. */
  114 #define LA_XLOCKED      0x00000004      /* Lock is exclusively locked. */
  115 #define LA_RECURSED     0x00000008      /* Lock is recursed. */
  116 #define LA_NOTRECURSED  0x00000010      /* Lock is not recursed. */
  117 
  118 #ifdef _KERNEL
  119 /*
  120  * If any of WITNESS, INVARIANTS, or KTR_LOCK KTR tracing has been enabled,
  121  * then turn on LOCK_DEBUG.  When this option is on, extra debugging
  122  * facilities such as tracking the file and line number of lock operations
  123  * are enabled.  Also, mutex locking operations are not inlined to avoid
  124  * bloat from all the extra debugging code.  We also have to turn on all the
  125  * calling conventions for this debugging code in modules so that modules can
  126  * work with both debug and non-debug kernels.
  127  */
  128 #if defined(KLD_MODULE) || defined(WITNESS) || defined(INVARIANTS) || \
  129     defined(INVARIANT_SUPPORT) || defined(LOCK_PROFILING) || defined(KTR)
  130 #define LOCK_DEBUG      1
  131 #else
  132 #define LOCK_DEBUG      0
  133 #endif
  134 
  135 /*
  136  * In the LOCK_DEBUG case, use the filename and line numbers for debugging
  137  * operations.  Otherwise, use default values to avoid the unneeded bloat.
  138  */
  139 #if LOCK_DEBUG > 0
  140 #define LOCK_FILE_LINE_ARG_DEF  , const char *file, int line
  141 #define LOCK_FILE_LINE_ARG      , file, line
  142 #define LOCK_FILE       __FILE__
  143 #define LOCK_LINE       __LINE__
  144 #else
  145 #define LOCK_FILE_LINE_ARG_DEF
  146 #define LOCK_FILE_LINE_ARG
  147 #define LOCK_FILE       NULL
  148 #define LOCK_LINE       0
  149 #endif
  150 
  151 /*
  152  * Macros for KTR_LOCK tracing.
  153  *
  154  * opname  - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
  155  * lo      - struct lock_object * for this lock
  156  * flags   - flags passed to the lock operation
  157  * recurse - this locks recursion level (or 0 if class is not recursable)
  158  * result  - result of a try lock operation
  159  * file    - file name
  160  * line    - line number
  161  */
  162 #if LOCK_DEBUG > 0
  163 #define LOCK_LOG_TEST(lo, flags)                                        \
  164         (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
  165 #else
  166 #define LOCK_LOG_TEST(lo, flags)        0
  167 #endif
  168 
  169 
  170 #define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do {      \
  171         if (LOCK_LOG_TEST((lo), (flags)))                               \
  172                 CTR6(KTR_LOCK, opname " (%s) %s %p r = %d at %s:%d",    \
  173                     LOCK_CLASS(lo)->lc_name, (lo)->lo_name,             \
  174                     (lo), (u_int)(recurse), (file), (line));            \
  175 } while (0)
  176 
  177 #define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do {        \
  178         if (LOCK_LOG_TEST((lo), (flags)))                               \
  179                 CTR6(KTR_LOCK, "TRY_" opname " (%s) %s %p result=%d at %s:%d",\
  180                     LOCK_CLASS(lo)->lc_name, (lo)->lo_name,             \
  181                     (lo), (u_int)(result), (file), (line));             \
  182 } while (0)
  183 
  184 #define LOCK_LOG_INIT(lo, flags) do {                                   \
  185         if (LOCK_LOG_TEST((lo), (flags)))                               \
  186                 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo),        \
  187                     LOCK_CLASS(lo)->lc_name, (lo)->lo_name);            \
  188 } while (0)
  189 
  190 #define LOCK_LOG_DESTROY(lo, flags)     LOCK_LOG_INIT(lo, flags)
  191 
  192 #define lock_initialized(lo)    ((lo)->lo_flags & LO_INITIALIZED)
  193 
  194 /*
  195  * Helpful macros for quickly coming up with assertions with informative
  196  * panic messages.
  197  */
  198 #define MPASS(ex)               MPASS4(ex, #ex, __FILE__, __LINE__)
  199 #define MPASS2(ex, what)        MPASS4(ex, what, __FILE__, __LINE__)
  200 #define MPASS3(ex, file, line)  MPASS4(ex, #ex, file, line)
  201 #define MPASS4(ex, what, file, line)                                    \
  202         KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
  203 
  204 extern struct lock_class lock_class_mtx_sleep;
  205 extern struct lock_class lock_class_mtx_spin;
  206 extern struct lock_class lock_class_sx;
  207 extern struct lock_class lock_class_rw;
  208 extern struct lock_class lock_class_rm;
  209 extern struct lock_class lock_class_rm_sleepable;
  210 extern struct lock_class lock_class_lockmgr;
  211 
  212 extern struct lock_class *lock_classes[];
  213 
  214 struct lock_delay_config {
  215         u_int base;
  216         u_int max;
  217 };
  218 
  219 struct lock_delay_arg {
  220         struct lock_delay_config *config;
  221         u_int delay;
  222         u_int spin_cnt;
  223 };
  224 
  225 static inline void
  226 lock_delay_arg_init(struct lock_delay_arg *la, struct lock_delay_config *lc)
  227 {
  228         la->config = lc;
  229         la->delay = lc->base;
  230         la->spin_cnt = 0;
  231 }
  232 
  233 #define lock_delay_spin(n)      do {    \
  234         u_int _i;                       \
  235                                         \
  236         for (_i = (n); _i > 0; _i--)    \
  237                 cpu_spinwait();         \
  238 } while (0)
  239 
  240 #define LOCK_DELAY_SYSINIT(func) \
  241         SYSINIT(func##_ld, SI_SUB_LOCK, SI_ORDER_ANY, func, NULL)
  242 
  243 #define LOCK_DELAY_SYSINIT_DEFAULT(lc) \
  244         SYSINIT(lock_delay_##lc##_ld, SI_SUB_LOCK, SI_ORDER_ANY, \
  245             lock_delay_default_init, &lc)
  246 
  247 void    lock_init(struct lock_object *, struct lock_class *,
  248             const char *, const char *, int);
  249 void    lock_destroy(struct lock_object *);
  250 void    lock_delay(struct lock_delay_arg *);
  251 void    lock_delay_default_init(struct lock_delay_config *);
  252 void    spinlock_enter(void);
  253 void    spinlock_exit(void);
  254 void    witness_init(struct lock_object *, const char *);
  255 void    witness_destroy(struct lock_object *);
  256 int     witness_defineorder(struct lock_object *, struct lock_object *);
  257 void    witness_checkorder(struct lock_object *, int, const char *, int,
  258             struct lock_object *);
  259 void    witness_lock(struct lock_object *, int, const char *, int);
  260 void    witness_upgrade(struct lock_object *, int, const char *, int);
  261 void    witness_downgrade(struct lock_object *, int, const char *, int);
  262 void    witness_unlock(struct lock_object *, int, const char *, int);
  263 void    witness_save(struct lock_object *, const char **, int *);
  264 void    witness_restore(struct lock_object *, const char *, int);
  265 int     witness_list_locks(struct lock_list_entry **,
  266             int (*)(const char *, ...));
  267 int     witness_warn(int, struct lock_object *, const char *, ...);
  268 void    witness_assert(const struct lock_object *, int, const char *, int);
  269 void    witness_display_spinlock(struct lock_object *, struct thread *,
  270             int (*)(const char *, ...));
  271 int     witness_line(struct lock_object *);
  272 void    witness_norelease(struct lock_object *);
  273 void    witness_releaseok(struct lock_object *);
  274 const char *witness_file(struct lock_object *);
  275 void    witness_thread_exit(struct thread *);
  276 
  277 #ifdef  WITNESS
  278 
  279 /* Flags for witness_warn(). */
  280 #define WARN_GIANTOK    0x01    /* Giant is exempt from this check. */
  281 #define WARN_PANIC      0x02    /* Panic if check fails. */
  282 #define WARN_SLEEPOK    0x04    /* Sleepable locks are exempt from check. */
  283 
  284 #define WITNESS_INIT(lock, type)                                        \
  285         witness_init((lock), (type))
  286 
  287 #define WITNESS_DESTROY(lock)                                           \
  288         witness_destroy(lock)
  289 
  290 #define WITNESS_CHECKORDER(lock, flags, file, line, interlock)          \
  291         witness_checkorder((lock), (flags), (file), (line), (interlock))
  292 
  293 #define WITNESS_DEFINEORDER(lock1, lock2)                               \
  294         witness_defineorder((struct lock_object *)(lock1),              \
  295             (struct lock_object *)(lock2))
  296 
  297 #define WITNESS_LOCK(lock, flags, file, line)                           \
  298         witness_lock((lock), (flags), (file), (line))
  299 
  300 #define WITNESS_UPGRADE(lock, flags, file, line)                        \
  301         witness_upgrade((lock), (flags), (file), (line))
  302 
  303 #define WITNESS_DOWNGRADE(lock, flags, file, line)                      \
  304         witness_downgrade((lock), (flags), (file), (line))
  305 
  306 #define WITNESS_UNLOCK(lock, flags, file, line)                         \
  307         witness_unlock((lock), (flags), (file), (line))
  308 
  309 #define WITNESS_CHECK(flags, lock, fmt, ...)                            \
  310         witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
  311 
  312 #define WITNESS_WARN(flags, lock, fmt, ...)                             \
  313         witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
  314 
  315 #define WITNESS_SAVE_DECL(n)                                            \
  316         const char * __CONCAT(n, __wf);                                 \
  317         int __CONCAT(n, __wl)
  318 
  319 #define WITNESS_SAVE(lock, n)                                           \
  320         witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
  321 
  322 #define WITNESS_RESTORE(lock, n)                                        \
  323         witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
  324 
  325 #define WITNESS_NORELEASE(lock)                                         \
  326         witness_norelease(&(lock)->lock_object)
  327 
  328 #define WITNESS_RELEASEOK(lock)                                         \
  329         witness_releaseok(&(lock)->lock_object)
  330 
  331 #define WITNESS_FILE(lock)                                              \
  332         witness_file(lock)
  333 
  334 #define WITNESS_LINE(lock)                                              \
  335         witness_line(lock)
  336 
  337 #else   /* WITNESS */
  338 #define WITNESS_INIT(lock, type)                                (void)0
  339 #define WITNESS_DESTROY(lock)                                   (void)0
  340 #define WITNESS_DEFINEORDER(lock1, lock2)       0
  341 #define WITNESS_CHECKORDER(lock, flags, file, line, interlock)  (void)0
  342 #define WITNESS_LOCK(lock, flags, file, line)                   (void)0
  343 #define WITNESS_UPGRADE(lock, flags, file, line)                (void)0
  344 #define WITNESS_DOWNGRADE(lock, flags, file, line)              (void)0
  345 #define WITNESS_UNLOCK(lock, flags, file, line)                 (void)0
  346 #define WITNESS_CHECK(flags, lock, fmt, ...)    0
  347 #define WITNESS_WARN(flags, lock, fmt, ...)                     (void)0
  348 #define WITNESS_SAVE_DECL(n)                                    (void)0
  349 #define WITNESS_SAVE(lock, n)                                   (void)0
  350 #define WITNESS_RESTORE(lock, n)                                (void)0
  351 #define WITNESS_NORELEASE(lock)                                 (void)0
  352 #define WITNESS_RELEASEOK(lock)                                 (void)0
  353 #define WITNESS_FILE(lock) ("?")
  354 #define WITNESS_LINE(lock) (0)
  355 #endif  /* WITNESS */
  356 
  357 #endif  /* _KERNEL */
  358 #endif  /* _SYS_LOCK_H_ */

Cache object: 28d196c8b1203914d19bc897e9a75804


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