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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * $FreeBSD: releng/11.1/sys/sys/rwlock.h 315379 2017-03-16 06:51:00Z mjg $
   27  */
   28 
   29 #ifndef _SYS_RWLOCK_H_
   30 #define _SYS_RWLOCK_H_
   31 
   32 #include <sys/_lock.h>
   33 #include <sys/_rwlock.h>
   34 #include <sys/lock_profile.h>
   35 #include <sys/lockstat.h>
   36 
   37 #ifdef _KERNEL
   38 #include <sys/pcpu.h>
   39 #include <machine/atomic.h>
   40 #endif
   41 
   42 /*
   43  * The rw_lock field consists of several fields.  The low bit indicates
   44  * if the lock is locked with a read (shared) or write (exclusive) lock.
   45  * A value of 0 indicates a write lock, and a value of 1 indicates a read
   46  * lock.  Bit 1 is a boolean indicating if there are any threads waiting
   47  * for a read lock.  Bit 2 is a boolean indicating if there are any threads
   48  * waiting for a write lock.  The rest of the variable's definition is
   49  * dependent on the value of the first bit.  For a write lock, it is a
   50  * pointer to the thread holding the lock, similar to the mtx_lock field of
   51  * mutexes.  For read locks, it is a count of read locks that are held.
   52  *
   53  * When the lock is not locked by any thread, it is encoded as a read lock
   54  * with zero waiters.
   55  */
   56 
   57 #define RW_LOCK_READ            0x01
   58 #define RW_LOCK_READ_WAITERS    0x02
   59 #define RW_LOCK_WRITE_WAITERS   0x04
   60 #define RW_LOCK_WRITE_SPINNER   0x08
   61 #define RW_LOCK_WRITER_RECURSED 0x10
   62 #define RW_LOCK_FLAGMASK                                                \
   63         (RW_LOCK_READ | RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS |  \
   64         RW_LOCK_WRITE_SPINNER | RW_LOCK_WRITER_RECURSED)
   65 #define RW_LOCK_WAITERS         (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
   66 
   67 #define RW_OWNER(x)             ((x) & ~RW_LOCK_FLAGMASK)
   68 #define RW_READERS_SHIFT        5
   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 #define rw_recurse      lock_object.lo_data
   79 
   80 #define RW_READ_VALUE(x)        ((x)->rw_lock)
   81 
   82 /* Very simple operations on rw_lock. */
   83 
   84 /* Try to obtain a write lock once. */
   85 #define _rw_write_lock(rw, tid)                                         \
   86         atomic_cmpset_acq_ptr(&(rw)->rw_lock, RW_UNLOCKED, (tid))
   87 
   88 #define _rw_write_lock_fetch(rw, vp, tid)                               \
   89         atomic_fcmpset_acq_ptr(&(rw)->rw_lock, vp, (tid))
   90 
   91 /* Release a write lock quickly if there are no waiters. */
   92 #define _rw_write_unlock(rw, tid)                                       \
   93         atomic_cmpset_rel_ptr(&(rw)->rw_lock, (tid), RW_UNLOCKED)
   94 
   95 /*
   96  * Full lock operations that are suitable to be inlined in non-debug
   97  * kernels.  If the lock cannot be acquired or released trivially then
   98  * the work is deferred to another function.
   99  */
  100 
  101 /* Acquire a write lock. */
  102 #define __rw_wlock(rw, tid, file, line) do {                            \
  103         uintptr_t _tid = (uintptr_t)(tid);                              \
  104         uintptr_t _v = RW_UNLOCKED;                                     \
  105                                                                         \
  106         if (__predict_false(LOCKSTAT_PROFILE_ENABLED(rw__acquire) ||    \
  107             !_rw_write_lock_fetch((rw), &_v, _tid)))                    \
  108                 _rw_wlock_hard((rw), _v, _tid, (file), (line));         \
  109 } while (0)
  110 
  111 /* Release a write lock. */
  112 #define __rw_wunlock(rw, tid, file, line) do {                          \
  113         uintptr_t _tid = (uintptr_t)(tid);                              \
  114                                                                         \
  115         if (__predict_false(LOCKSTAT_PROFILE_ENABLED(rw__release) ||    \
  116             !_rw_write_unlock((rw), _tid)))                             \
  117                 _rw_wunlock_hard((rw), _tid, (file), (line));           \
  118 } while (0)
  119 
  120 /*
  121  * Function prototypes.  Routines that start with _ are not part of the
  122  * external API and should not be called directly.  Wrapper macros should
  123  * be used instead.
  124  */
  125 void    _rw_init_flags(volatile uintptr_t *c, const char *name, int opts);
  126 void    _rw_destroy(volatile uintptr_t *c);
  127 void    rw_sysinit(void *arg);
  128 void    rw_sysinit_flags(void *arg);
  129 int     _rw_wowned(const volatile uintptr_t *c);
  130 void    _rw_wlock_cookie(volatile uintptr_t *c, const char *file, int line);
  131 int     __rw_try_wlock(volatile uintptr_t *c, const char *file, int line);
  132 void    _rw_wunlock_cookie(volatile uintptr_t *c, const char *file, int line);
  133 void    __rw_rlock(volatile uintptr_t *c, const char *file, int line);
  134 int     __rw_try_rlock(volatile uintptr_t *c, const char *file, int line);
  135 void    _rw_runlock_cookie(volatile uintptr_t *c, const char *file, int line);
  136 void    __rw_wlock_hard(volatile uintptr_t *c, uintptr_t v, uintptr_t tid,
  137             const char *file, int line);
  138 void    __rw_wunlock_hard(volatile uintptr_t *c, uintptr_t tid,
  139             const char *file, int line);
  140 int     __rw_try_upgrade(volatile uintptr_t *c, const char *file, int line);
  141 void    __rw_downgrade(volatile uintptr_t *c, const char *file, int line);
  142 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
  143 void    __rw_assert(const volatile uintptr_t *c, int what, const char *file,
  144             int line);
  145 #endif
  146 
  147 /*
  148  * Top-level macros to provide lock cookie once the actual rwlock is passed.
  149  * They will also prevent passing a malformed object to the rwlock KPI by
  150  * failing compilation as the rw_lock reserved member will not be found.
  151  */
  152 #define rw_init(rw, n)                                                  \
  153         _rw_init_flags(&(rw)->rw_lock, n, 0)
  154 #define rw_init_flags(rw, n, o)                                         \
  155         _rw_init_flags(&(rw)->rw_lock, n, o)
  156 #define rw_destroy(rw)                                                  \
  157         _rw_destroy(&(rw)->rw_lock)
  158 #define rw_wowned(rw)                                                   \
  159         _rw_wowned(&(rw)->rw_lock)
  160 #define _rw_wlock(rw, f, l)                                             \
  161         _rw_wlock_cookie(&(rw)->rw_lock, f, l)
  162 #define _rw_try_wlock(rw, f, l)                                         \
  163         __rw_try_wlock(&(rw)->rw_lock, f, l)
  164 #define _rw_wunlock(rw, f, l)                                           \
  165         _rw_wunlock_cookie(&(rw)->rw_lock, f, l)
  166 #define _rw_rlock(rw, f, l)                                             \
  167         __rw_rlock(&(rw)->rw_lock, f, l)
  168 #define _rw_try_rlock(rw, f, l)                                         \
  169         __rw_try_rlock(&(rw)->rw_lock, f, l)
  170 #define _rw_runlock(rw, f, l)                                           \
  171         _rw_runlock_cookie(&(rw)->rw_lock, f, l)
  172 #define _rw_wlock_hard(rw, v, t, f, l)                                  \
  173         __rw_wlock_hard(&(rw)->rw_lock, v, t, f, l)
  174 #define _rw_wunlock_hard(rw, t, f, l)                                   \
  175         __rw_wunlock_hard(&(rw)->rw_lock, t, f, l)
  176 #define _rw_try_upgrade(rw, f, l)                                       \
  177         __rw_try_upgrade(&(rw)->rw_lock, f, l)
  178 #define _rw_downgrade(rw, f, l)                                         \
  179         __rw_downgrade(&(rw)->rw_lock, f, l)
  180 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
  181 #define _rw_assert(rw, w, f, l)                                         \
  182         __rw_assert(&(rw)->rw_lock, w, f, l)
  183 #endif
  184 
  185 
  186 /*
  187  * Public interface for lock operations.
  188  */
  189 
  190 #ifndef LOCK_DEBUG
  191 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rwlock.h>
  192 #endif
  193 #if LOCK_DEBUG > 0 || defined(RWLOCK_NOINLINE)
  194 #define rw_wlock(rw)            _rw_wlock((rw), LOCK_FILE, LOCK_LINE)
  195 #define rw_wunlock(rw)          _rw_wunlock((rw), LOCK_FILE, LOCK_LINE)
  196 #else
  197 #define rw_wlock(rw)                                                    \
  198         __rw_wlock((rw), curthread, LOCK_FILE, LOCK_LINE)
  199 #define rw_wunlock(rw)                                                  \
  200         __rw_wunlock((rw), curthread, LOCK_FILE, LOCK_LINE)
  201 #endif
  202 #define rw_rlock(rw)            _rw_rlock((rw), LOCK_FILE, LOCK_LINE)
  203 #define rw_runlock(rw)          _rw_runlock((rw), LOCK_FILE, LOCK_LINE)
  204 #define rw_try_rlock(rw)        _rw_try_rlock((rw), LOCK_FILE, LOCK_LINE)
  205 #define rw_try_upgrade(rw)      _rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE)
  206 #define rw_try_wlock(rw)        _rw_try_wlock((rw), LOCK_FILE, LOCK_LINE)
  207 #define rw_downgrade(rw)        _rw_downgrade((rw), LOCK_FILE, LOCK_LINE)
  208 #define rw_unlock(rw)   do {                                            \
  209         if (rw_wowned(rw))                                              \
  210                 rw_wunlock(rw);                                         \
  211         else                                                            \
  212                 rw_runlock(rw);                                         \
  213 } while (0)
  214 #define rw_sleep(chan, rw, pri, wmesg, timo)                            \
  215         _sleep((chan), &(rw)->lock_object, (pri), (wmesg),              \
  216             tick_sbt * (timo), 0, C_HARDCLOCK)
  217 
  218 #define rw_initialized(rw)      lock_initialized(&(rw)->lock_object)
  219 
  220 struct rw_args {
  221         void            *ra_rw;
  222         const char      *ra_desc;
  223 };
  224 
  225 struct rw_args_flags {
  226         void            *ra_rw;
  227         const char      *ra_desc;
  228         int             ra_flags;
  229 };
  230 
  231 #define RW_SYSINIT(name, rw, desc)                                      \
  232         static struct rw_args name##_args = {                           \
  233                 (rw),                                                   \
  234                 (desc),                                                 \
  235         };                                                              \
  236         SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,        \
  237             rw_sysinit, &name##_args);                                  \
  238         SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,    \
  239             _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
  240 
  241 
  242 #define RW_SYSINIT_FLAGS(name, rw, desc, flags)                         \
  243         static struct rw_args_flags name##_args = {                     \
  244                 (rw),                                                   \
  245                 (desc),                                                 \
  246                 (flags),                                                \
  247         };                                                              \
  248         SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,        \
  249             rw_sysinit_flags, &name##_args);                            \
  250         SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,    \
  251             _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
  252 
  253 /*
  254  * Options passed to rw_init_flags().
  255  */
  256 #define RW_DUPOK        0x01
  257 #define RW_NOPROFILE    0x02
  258 #define RW_NOWITNESS    0x04
  259 #define RW_QUIET        0x08
  260 #define RW_RECURSE      0x10
  261 #define RW_NEW          0x20
  262 
  263 /*
  264  * The INVARIANTS-enabled rw_assert() functionality.
  265  *
  266  * The constants need to be defined for INVARIANT_SUPPORT infrastructure
  267  * support as _rw_assert() itself uses them and the latter implies that
  268  * _rw_assert() must build.
  269  */
  270 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
  271 #define RA_LOCKED               LA_LOCKED
  272 #define RA_RLOCKED              LA_SLOCKED
  273 #define RA_WLOCKED              LA_XLOCKED
  274 #define RA_UNLOCKED             LA_UNLOCKED
  275 #define RA_RECURSED             LA_RECURSED
  276 #define RA_NOTRECURSED          LA_NOTRECURSED
  277 #endif
  278 
  279 #ifdef INVARIANTS
  280 #define rw_assert(rw, what)     _rw_assert((rw), (what), LOCK_FILE, LOCK_LINE)
  281 #else
  282 #define rw_assert(rw, what)
  283 #endif
  284 
  285 #endif /* _KERNEL */
  286 #endif /* !_SYS_RWLOCK_H_ */

Cache object: 98da98594c00bcd3465cca7138255acd


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