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/rwlock.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) 2006 John Baldwin <jhb@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the author nor the names of any co-contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  * $FreeBSD: releng/7.3/sys/sys/rwlock.h 197984 2009-10-12 15:56:27Z attilio $
   30  */
   31 
   32 #ifndef _SYS_RWLOCK_H_
   33 #define _SYS_RWLOCK_H_
   34 
   35 #include <sys/_lock.h>
   36 #include <sys/_rwlock.h>
   37 #include <sys/lock_profile.h>
   38 
   39 #ifdef _KERNEL
   40 #include <sys/pcpu.h>
   41 #include <machine/atomic.h>
   42 #endif
   43 
   44 /*
   45  * The rw_lock field consists of several fields.  The low bit indicates
   46  * if the lock is locked with a read (shared) or write (exclusive) lock.
   47  * A value of 0 indicates a write lock, and a value of 1 indicates a read
   48  * lock.  Bit 1 is a boolean indicating if there are any threads waiting
   49  * for a read lock.  Bit 2 is a boolean indicating if there are any threads
   50  * waiting for a write lock.  The rest of the variable's definition is
   51  * dependent on the value of the first bit.  For a write lock, it is a
   52  * pointer to the thread holding the lock, similar to the mtx_lock field of
   53  * mutexes.  For read locks, it is a count of read locks that are held.
   54  *
   55  * When the lock is not locked by any thread, it is encoded as a read lock
   56  * with zero waiters.
   57  */
   58 
   59 #define RW_LOCK_READ            0x01
   60 #define RW_LOCK_READ_WAITERS    0x02
   61 #define RW_LOCK_WRITE_WAITERS   0x04
   62 #define RW_LOCK_RECURSED        0x08
   63 #define RW_LOCK_FLAGMASK                                                \
   64         (RW_LOCK_READ | RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS |  \
   65         RW_LOCK_RECURSED)
   66 
   67 #define RW_OWNER(x)             ((x) & ~RW_LOCK_FLAGMASK)
   68 #define RW_READERS_SHIFT        4
   69 #define RW_READERS(x)           (RW_OWNER((x)) >> RW_READERS_SHIFT)
   70 #define RW_READERS_LOCK(x)      ((x) << RW_READERS_SHIFT | RW_LOCK_READ)
   71 #define RW_ONE_READER           (1 << RW_READERS_SHIFT)
   72 
   73 #define RW_UNLOCKED             RW_READERS_LOCK(0)
   74 #define RW_DESTROYED            (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
   75 
   76 #ifdef _KERNEL
   77 
   78 /* Very simple operations on rw_lock. */
   79 
   80 /* Try to obtain a write lock once. */
   81 #define _rw_write_lock(rw, tid)                                         \
   82         atomic_cmpset_acq_ptr(&(rw)->rw_lock, RW_UNLOCKED, (tid))
   83 
   84 /* Release a write lock quickly if there are no waiters. */
   85 #define _rw_write_unlock(rw, tid)                                       \
   86         atomic_cmpset_rel_ptr(&(rw)->rw_lock, (tid), RW_UNLOCKED)
   87 
   88 /*
   89  * Full lock operations that are suitable to be inlined in non-debug
   90  * kernels.  If the lock cannot be acquired or released trivially then
   91  * the work is deferred to another function.
   92  */
   93 
   94 /* Acquire a write lock. */
   95 #define __rw_wlock(rw, tid, file, line) do {                            \
   96         uintptr_t _tid = (uintptr_t)(tid);                              \
   97                                                                         \
   98         if (!_rw_write_lock((rw), _tid))                                \
   99                 _rw_wlock_hard((rw), _tid, (file), (line));             \
  100         else                                                            \
  101                 lock_profile_obtain_lock_success(&(rw)->lock_object, 0, \
  102                     0, (file), (line));                                 \
  103 } while (0)
  104 
  105 /* Release a write lock. */
  106 #define __rw_wunlock(rw, tid, file, line) do {                          \
  107         uintptr_t _tid = (uintptr_t)(tid);                              \
  108                                                                         \
  109         if (!_rw_write_unlock((rw), _tid))                              \
  110                 _rw_wunlock_hard((rw), _tid, (file), (line));           \
  111 } while (0)
  112 
  113 /*
  114  * Function prototypes.  Routines that start with _ are not part of the
  115  * external API and should not be called directly.  Wrapper macros should
  116  * be used instead.
  117  */
  118 
  119 #define rw_init(rw, name)       rw_init_flags((rw), (name), 0)
  120 void    rw_init_flags(struct rwlock *rw, const char *name, int opts);
  121 void    rw_destroy(struct rwlock *rw);
  122 void    rw_sysinit(void *arg);
  123 int     rw_wowned(struct rwlock *rw);
  124 void    _rw_wlock(struct rwlock *rw, const char *file, int line);
  125 int     _rw_try_wlock(struct rwlock *rw, const char *file, int line);
  126 void    _rw_wunlock(struct rwlock *rw, const char *file, int line);
  127 void    _rw_rlock(struct rwlock *rw, const char *file, int line);
  128 int     _rw_try_rlock(struct rwlock *rw, const char *file, int line);
  129 void    _rw_runlock(struct rwlock *rw, const char *file, int line);
  130 void    _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file,
  131             int line);
  132 void    _rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file,
  133             int line);
  134 int     _rw_try_upgrade(struct rwlock *rw, const char *file, int line);
  135 void    _rw_downgrade(struct rwlock *rw, const char *file, int line);
  136 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
  137 void    _rw_assert(struct rwlock *rw, int what, const char *file, int line);
  138 #endif
  139 
  140 /*
  141  * Public interface for lock operations.
  142  */
  143 
  144 #ifndef LOCK_DEBUG
  145 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rwlock.h>
  146 #endif
  147 #if LOCK_DEBUG > 0 || defined(RWLOCK_NOINLINE)
  148 #define rw_wlock(rw)            _rw_wlock((rw), LOCK_FILE, LOCK_LINE)
  149 #define rw_wunlock(rw)          _rw_wunlock((rw), LOCK_FILE, LOCK_LINE)
  150 #else
  151 #define rw_wlock(rw)                                                    \
  152         __rw_wlock((rw), curthread, LOCK_FILE, LOCK_LINE)
  153 #define rw_wunlock(rw)                                                  \
  154         __rw_wunlock((rw), curthread, LOCK_FILE, LOCK_LINE)
  155 #endif
  156 #define rw_rlock(rw)            _rw_rlock((rw), LOCK_FILE, LOCK_LINE)
  157 #define rw_runlock(rw)          _rw_runlock((rw), LOCK_FILE, LOCK_LINE)
  158 #define rw_try_rlock(rw)        _rw_try_rlock((rw), LOCK_FILE, LOCK_LINE)
  159 #define rw_try_upgrade(rw)      _rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE)
  160 #define rw_try_wlock(rw)        _rw_try_wlock((rw), LOCK_FILE, LOCK_LINE)
  161 #define rw_downgrade(rw)        _rw_downgrade((rw), LOCK_FILE, LOCK_LINE)
  162 #define rw_sleep(chan, rw, pri, wmesg, timo)                            \
  163         _sleep((chan), &(rw)->lock_object, (pri), (wmesg), (timo))
  164 
  165 #define rw_initialized(rw)      lock_initalized(&(rw)->lock_object)
  166 
  167 struct rw_args {
  168         struct rwlock   *ra_rw;
  169         const char      *ra_desc;
  170 };
  171 
  172 #define RW_SYSINIT(name, rw, desc)                                      \
  173         static struct rw_args name##_args = {                           \
  174                 (rw),                                                   \
  175                 (desc),                                                 \
  176         };                                                              \
  177         SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,        \
  178             rw_sysinit, &name##_args);                                  \
  179         SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,    \
  180             rw_destroy, (rw))
  181 
  182 /*
  183  * Options passed to rw_init_flags().
  184  */
  185 #define RW_DUPOK        0x01
  186 #define RW_NOPROFILE    0x02
  187 #define RW_NOWITNESS    0x04
  188 #define RW_QUIET        0x08
  189 #define RW_RECURSE      0x10
  190 
  191 /*
  192  * The INVARIANTS-enabled rw_assert() functionality.
  193  *
  194  * The constants need to be defined for INVARIANT_SUPPORT infrastructure
  195  * support as _rw_assert() itself uses them and the latter implies that
  196  * _rw_assert() must build.
  197  */
  198 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
  199 #define RA_LOCKED               LA_LOCKED
  200 #define RA_RLOCKED              LA_SLOCKED
  201 #define RA_WLOCKED              LA_XLOCKED
  202 #define RA_UNLOCKED             LA_UNLOCKED
  203 #define RA_RECURSED             LA_RECURSED
  204 #define RA_NOTRECURSED          LA_NOTRECURSED
  205 #endif
  206 
  207 #ifdef INVARIANTS
  208 #define rw_assert(rw, what)     _rw_assert((rw), (what), LOCK_FILE, LOCK_LINE)
  209 #else
  210 #define rw_assert(rw, what)
  211 #endif
  212 
  213 #endif /* _KERNEL */
  214 #endif /* !_SYS_RWLOCK_H_ */

Cache object: 7b292923b41d7fbb39b4febbd900d79d


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