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/sx.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) 2007 Attilio Rao <attilio@freebsd.org>
    3  * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice(s), this list of conditions and the following disclaimer as
   11  *    the first lines of this file unmodified other than the possible 
   12  *    addition of one or more copyright notices.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice(s), this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
   18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   20  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
   21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   24  * 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 SUCH
   27  * DAMAGE.
   28  *
   29  * $FreeBSD: releng/7.3/sys/sys/sx.h 197984 2009-10-12 15:56:27Z attilio $
   30  */
   31 
   32 #ifndef _SYS_SX_H_
   33 #define _SYS_SX_H_
   34 
   35 #include <sys/_lock.h>
   36 #include <sys/_sx.h>
   37 
   38 #ifdef  _KERNEL
   39 #include <sys/pcpu.h>
   40 #include <sys/lock_profile.h>
   41 #include <machine/atomic.h>
   42 #endif
   43 
   44 /*
   45  * In general, the sx locks and rwlocks use very similar algorithms.
   46  * The main difference in the implementations is how threads are
   47  * blocked when a lock is unavailable.  For this, sx locks use sleep
   48  * queues which do not support priority propagation, and rwlocks use
   49  * turnstiles which do.
   50  *
   51  * The sx_lock field consists of several fields.  The low bit
   52  * indicates if the lock is locked with a shared or exclusive lock.  A
   53  * value of 0 indicates an exclusive lock, and a value of 1 indicates
   54  * a shared lock.  Bit 1 is a boolean indicating if there are any
   55  * threads waiting for a shared lock.  Bit 2 is a boolean indicating
   56  * if there are any threads waiting for an exclusive lock.  Bit 3 is a
   57  * boolean indicating if an exclusive lock is recursively held.  The
   58  * rest of the variable's definition is dependent on the value of the
   59  * first bit.  For an exclusive lock, it is a pointer to the thread
   60  * holding the lock, similar to the mtx_lock field of mutexes.  For
   61  * shared locks, it is a count of read locks that are held.
   62  *
   63  * When the lock is not locked by any thread, it is encoded as a
   64  * shared lock with zero waiters.
   65  */
   66 
   67 #define SX_LOCK_SHARED                  0x01
   68 #define SX_LOCK_SHARED_WAITERS          0x02
   69 #define SX_LOCK_EXCLUSIVE_WAITERS       0x04
   70 #define SX_LOCK_RECURSED                0x08
   71 #define SX_LOCK_FLAGMASK                                                \
   72         (SX_LOCK_SHARED | SX_LOCK_SHARED_WAITERS |                      \
   73         SX_LOCK_EXCLUSIVE_WAITERS | SX_LOCK_RECURSED)
   74 
   75 #define SX_OWNER(x)                     ((x) & ~SX_LOCK_FLAGMASK)
   76 #define SX_SHARERS_SHIFT                4
   77 #define SX_SHARERS(x)                   (SX_OWNER(x) >> SX_SHARERS_SHIFT)
   78 #define SX_SHARERS_LOCK(x)                                              \
   79         ((x) << SX_SHARERS_SHIFT | SX_LOCK_SHARED)
   80 #define SX_ONE_SHARER                   (1 << SX_SHARERS_SHIFT)
   81 
   82 #define SX_LOCK_UNLOCKED                SX_SHARERS_LOCK(0)
   83 #define SX_LOCK_DESTROYED                                               \
   84         (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)
   85 
   86 #ifdef _KERNEL
   87 
   88 /*
   89  * Function prototipes.  Routines that start with an underscore are not part
   90  * of the public interface and are wrappered with a macro.
   91  */
   92 void    sx_sysinit(void *arg);
   93 #define sx_init(sx, desc)       sx_init_flags((sx), (desc), 0)
   94 void    sx_init_flags(struct sx *sx, const char *description, int opts);
   95 void    sx_destroy(struct sx *sx);
   96 int     _sx_slock(struct sx *sx, int opts, const char *file, int line);
   97 int     _sx_xlock(struct sx *sx, int opts, const char *file, int line);
   98 int     _sx_try_slock(struct sx *sx, const char *file, int line);
   99 int     _sx_try_xlock(struct sx *sx, const char *file, int line);
  100 void    _sx_sunlock(struct sx *sx, const char *file, int line);
  101 void    _sx_xunlock(struct sx *sx, const char *file, int line);
  102 int     _sx_try_upgrade(struct sx *sx, const char *file, int line);
  103 void    _sx_downgrade(struct sx *sx, const char *file, int line);
  104 int     _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts,
  105             const char *file, int line);
  106 int     _sx_slock_hard(struct sx *sx, int opts, const char *file, int line);
  107 void    _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int
  108             line);
  109 void    _sx_sunlock_hard(struct sx *sx, const char *file, int line);
  110 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
  111 void    _sx_assert(struct sx *sx, int what, const char *file, int line);
  112 #endif
  113 #ifdef DDB
  114 int     sx_chain(struct thread *td, struct thread **ownerp);
  115 #endif
  116 
  117 struct sx_args {
  118         struct sx       *sa_sx;
  119         const char      *sa_desc;
  120 };
  121 
  122 #define SX_SYSINIT(name, sxa, desc)                                     \
  123         static struct sx_args name##_args = {                           \
  124                 (sxa),                                                  \
  125                 (desc)                                                  \
  126         };                                                              \
  127         SYSINIT(name##_sx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,        \
  128             sx_sysinit, &name##_args);                                  \
  129         SYSUNINIT(name##_sx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,    \
  130             sx_destroy, (sxa))
  131 
  132 /*
  133  * Full lock operations that are suitable to be inlined in non-debug kernels.
  134  * If the lock can't be acquired or released trivially then the work is
  135  * deferred to 'tougher' functions.
  136  */
  137 
  138 /* Acquire an exclusive lock. */
  139 static __inline int
  140 __sx_xlock(struct sx *sx, struct thread *td, int opts, const char *file,
  141     int line)
  142 {
  143         uintptr_t tid = (uintptr_t)td;
  144         int error = 0;
  145 
  146         if (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid))
  147                 error = _sx_xlock_hard(sx, tid, opts, file, line);
  148         else
  149                 lock_profile_obtain_lock_success(&sx->lock_object, 0, 0, file,
  150                     line);
  151 
  152         return (error);
  153 }
  154 
  155 /* Release an exclusive lock. */
  156 static __inline void
  157 __sx_xunlock(struct sx *sx, struct thread *td, const char *file, int line)
  158 {
  159         uintptr_t tid = (uintptr_t)td;
  160 
  161         if (!atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED))
  162                 _sx_xunlock_hard(sx, tid, file, line);
  163 }
  164 
  165 /* Acquire a shared lock. */
  166 static __inline int
  167 __sx_slock(struct sx *sx, int opts, const char *file, int line)
  168 {
  169         uintptr_t x = sx->sx_lock;
  170         int error = 0;
  171 
  172         if (!(x & SX_LOCK_SHARED) ||
  173             !atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER))
  174                 error = _sx_slock_hard(sx, opts, file, line);
  175 #ifdef LOCK_PROFILING_SHARED
  176         else if (SX_SHARERS(x) == 0)
  177                 lock_profile_obtain_lock_success(&sx->lock_object, 0, 0, file,
  178                     line);
  179 #endif
  180 
  181         return (error);
  182 }
  183 
  184 /*
  185  * Release a shared lock.  We can just drop a single shared lock so
  186  * long as we aren't trying to drop the last shared lock when other
  187  * threads are waiting for an exclusive lock.  This takes advantage of
  188  * the fact that an unlocked lock is encoded as a shared lock with a
  189  * count of 0.
  190  */
  191 static __inline void
  192 __sx_sunlock(struct sx *sx, const char *file, int line)
  193 {
  194         uintptr_t x = sx->sx_lock;
  195 
  196         if (x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS) ||
  197             !atomic_cmpset_rel_ptr(&sx->sx_lock, x, x - SX_ONE_SHARER))
  198                 _sx_sunlock_hard(sx, file, line);
  199 }
  200 
  201 /*
  202  * Public interface for lock operations.
  203  */
  204 #ifndef LOCK_DEBUG
  205 #error  "LOCK_DEBUG not defined, include <sys/lock.h> before <sys/sx.h>"
  206 #endif
  207 #if     (LOCK_DEBUG > 0) || defined(SX_NOINLINE)
  208 #define sx_xlock(sx)            (void)_sx_xlock((sx), 0, LOCK_FILE, LOCK_LINE)
  209 #define sx_xlock_sig(sx)                                                \
  210         _sx_xlock((sx), SX_INTERRUPTIBLE, LOCK_FILE, LOCK_LINE)
  211 #define sx_xunlock(sx)          _sx_xunlock((sx), LOCK_FILE, LOCK_LINE)
  212 #define sx_slock(sx)            (void)_sx_slock((sx), 0, LOCK_FILE, LOCK_LINE)
  213 #define sx_slock_sig(sx)                                                \
  214         _sx_slock((sx), SX_INTERRUPTIBLE, LOCK_FILE, LOCK_LINE)
  215 #define sx_sunlock(sx)          _sx_sunlock((sx), LOCK_FILE, LOCK_LINE)
  216 #else
  217 #define sx_xlock(sx)                                                    \
  218         (void)__sx_xlock((sx), curthread, 0, LOCK_FILE, LOCK_LINE)
  219 #define sx_xlock_sig(sx)                                                \
  220         __sx_xlock((sx), curthread, SX_INTERRUPTIBLE, LOCK_FILE, LOCK_LINE)
  221 #define sx_xunlock(sx)                                                  \
  222         __sx_xunlock((sx), curthread, LOCK_FILE, LOCK_LINE)
  223 #define sx_slock(sx)            (void)__sx_slock((sx), 0, LOCK_FILE, LOCK_LINE)
  224 #define sx_slock_sig(sx)                                                \
  225         __sx_slock((sx), SX_INTERRUPTIBLE, LOCK_FILE, LOCK_LINE)
  226 #define sx_sunlock(sx)          __sx_sunlock((sx), LOCK_FILE, LOCK_LINE)
  227 #endif  /* LOCK_DEBUG > 0 || SX_NOINLINE */
  228 #define sx_try_slock(sx)        _sx_try_slock((sx), LOCK_FILE, LOCK_LINE)
  229 #define sx_try_xlock(sx)        _sx_try_xlock((sx), LOCK_FILE, LOCK_LINE)
  230 #define sx_try_upgrade(sx)      _sx_try_upgrade((sx), LOCK_FILE, LOCK_LINE)
  231 #define sx_downgrade(sx)        _sx_downgrade((sx), LOCK_FILE, LOCK_LINE)
  232 
  233 /*
  234  * Return a pointer to the owning thread if the lock is exclusively
  235  * locked.
  236  */
  237 #define sx_xholder(sx)                                                  \
  238         ((sx)->sx_lock & SX_LOCK_SHARED ? NULL :                        \
  239         (struct thread *)SX_OWNER((sx)->sx_lock))
  240 
  241 #define sx_xlocked(sx)                                                  \
  242         (((sx)->sx_lock & ~(SX_LOCK_FLAGMASK & ~SX_LOCK_SHARED)) ==     \
  243             (uintptr_t)curthread)
  244 
  245 #define sx_unlock(sx) do {                                              \
  246         if (sx_xlocked(sx))                                             \
  247                 sx_xunlock(sx);                                         \
  248         else                                                            \
  249                 sx_sunlock(sx);                                         \
  250 } while (0)
  251 
  252 #define sx_sleep(chan, sx, pri, wmesg, timo)                            \
  253         _sleep((chan), &(sx)->lock_object, (pri), (wmesg), (timo))
  254 
  255 /*
  256  * Options passed to sx_init_flags().
  257  */
  258 #define SX_DUPOK                0x01
  259 #define SX_NOPROFILE            0x02
  260 #define SX_NOWITNESS            0x04
  261 #define SX_QUIET                0x08
  262 #define SX_ADAPTIVESPIN         0x10
  263 #define SX_RECURSE              0x20
  264 
  265 /*
  266  * Options passed to sx_*lock_hard().
  267  */
  268 #define SX_INTERRUPTIBLE        0x40
  269 
  270 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
  271 #define SA_LOCKED               LA_LOCKED
  272 #define SA_SLOCKED              LA_SLOCKED
  273 #define SA_XLOCKED              LA_XLOCKED
  274 #define SA_UNLOCKED             LA_UNLOCKED
  275 #define SA_RECURSED             LA_RECURSED
  276 #define SA_NOTRECURSED          LA_NOTRECURSED
  277 
  278 /* Backwards compatability. */
  279 #define SX_LOCKED               LA_LOCKED
  280 #define SX_SLOCKED              LA_SLOCKED
  281 #define SX_XLOCKED              LA_XLOCKED
  282 #define SX_UNLOCKED             LA_UNLOCKED
  283 #define SX_RECURSED             LA_RECURSED
  284 #define SX_NOTRECURSED          LA_NOTRECURSED
  285 #endif
  286 
  287 #ifdef INVARIANTS
  288 #define sx_assert(sx, what)     _sx_assert((sx), (what), LOCK_FILE, LOCK_LINE)
  289 #else
  290 #define sx_assert(sx, what)
  291 #endif
  292 
  293 #endif /* _KERNEL */
  294 
  295 #endif /* !_SYS_SX_H_ */

Cache object: 0802ccff28736d8d971f55dd5fb5ef79


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