sx.h revision 298981
1139825Simp/*- 2168191Sjhb * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org> 3168191Sjhb * Copyright (c) 2001 Jason Evans <jasone@freebsd.org> 4168191Sjhb * All rights reserved. 573782Sjasone * 673782Sjasone * Redistribution and use in source and binary forms, with or without 773782Sjasone * modification, are permitted provided that the following conditions 873782Sjasone * are met: 973782Sjasone * 1. Redistributions of source code must retain the above copyright 1073782Sjasone * notice(s), this list of conditions and the following disclaimer as 1173782Sjasone * the first lines of this file unmodified other than the possible 1273782Sjasone * addition of one or more copyright notices. 1373782Sjasone * 2. Redistributions in binary form must reproduce the above copyright 1473782Sjasone * notice(s), this list of conditions and the following disclaimer in the 1573782Sjasone * documentation and/or other materials provided with the distribution. 1673782Sjasone * 1773782Sjasone * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 1873782Sjasone * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1973782Sjasone * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 2073782Sjasone * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 2173782Sjasone * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 2273782Sjasone * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 2373782Sjasone * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 2473782Sjasone * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2573782Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2673782Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 2773782Sjasone * DAMAGE. 2873782Sjasone * 2973782Sjasone * $FreeBSD: head/sys/sys/sx.h 298981 2016-05-03 15:14:17Z pfg $ 3073782Sjasone */ 3173782Sjasone 3273782Sjasone#ifndef _SYS_SX_H_ 3373782Sjasone#define _SYS_SX_H_ 3473782Sjasone 3583103Sjhb#include <sys/_lock.h> 36168191Sjhb#include <sys/_sx.h> 3773782Sjasone 38168191Sjhb#ifdef _KERNEL 39181682Sed#include <sys/pcpu.h> 40181682Sed#include <sys/lock_profile.h> 41192853Ssson#include <sys/lockstat.h> 42168191Sjhb#include <machine/atomic.h> 43168191Sjhb#endif 4473782Sjasone 45168191Sjhb/* 46168191Sjhb * In general, the sx locks and rwlocks use very similar algorithms. 47168191Sjhb * The main difference in the implementations is how threads are 48168191Sjhb * blocked when a lock is unavailable. For this, sx locks use sleep 49168191Sjhb * queues which do not support priority propagation, and rwlocks use 50168191Sjhb * turnstiles which do. 51168191Sjhb * 52168191Sjhb * The sx_lock field consists of several fields. The low bit 53168191Sjhb * indicates if the lock is locked with a shared or exclusive lock. A 54168191Sjhb * value of 0 indicates an exclusive lock, and a value of 1 indicates 55168191Sjhb * a shared lock. Bit 1 is a boolean indicating if there are any 56168191Sjhb * threads waiting for a shared lock. Bit 2 is a boolean indicating 57168191Sjhb * if there are any threads waiting for an exclusive lock. Bit 3 is a 58168191Sjhb * boolean indicating if an exclusive lock is recursively held. The 59168191Sjhb * rest of the variable's definition is dependent on the value of the 60168191Sjhb * first bit. For an exclusive lock, it is a pointer to the thread 61168191Sjhb * holding the lock, similar to the mtx_lock field of mutexes. For 62168191Sjhb * shared locks, it is a count of read locks that are held. 63168191Sjhb * 64168191Sjhb * When the lock is not locked by any thread, it is encoded as a 65168191Sjhb * shared lock with zero waiters. 66168191Sjhb */ 67168191Sjhb 68168191Sjhb#define SX_LOCK_SHARED 0x01 69168191Sjhb#define SX_LOCK_SHARED_WAITERS 0x02 70168191Sjhb#define SX_LOCK_EXCLUSIVE_WAITERS 0x04 71168191Sjhb#define SX_LOCK_RECURSED 0x08 72168191Sjhb#define SX_LOCK_FLAGMASK \ 73168191Sjhb (SX_LOCK_SHARED | SX_LOCK_SHARED_WAITERS | \ 74168191Sjhb SX_LOCK_EXCLUSIVE_WAITERS | SX_LOCK_RECURSED) 75168191Sjhb 76168191Sjhb#define SX_OWNER(x) ((x) & ~SX_LOCK_FLAGMASK) 77168191Sjhb#define SX_SHARERS_SHIFT 4 78168191Sjhb#define SX_SHARERS(x) (SX_OWNER(x) >> SX_SHARERS_SHIFT) 79168191Sjhb#define SX_SHARERS_LOCK(x) \ 80168191Sjhb ((x) << SX_SHARERS_SHIFT | SX_LOCK_SHARED) 81168191Sjhb#define SX_ONE_SHARER (1 << SX_SHARERS_SHIFT) 82168191Sjhb 83168191Sjhb#define SX_LOCK_UNLOCKED SX_SHARERS_LOCK(0) 84169394Sjhb#define SX_LOCK_DESTROYED \ 85169394Sjhb (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS) 86168191Sjhb 8773782Sjasone#ifdef _KERNEL 88168191Sjhb 89258541Sattilio#define sx_recurse lock_object.lo_data 90258541Sattilio 91168191Sjhb/* 92168191Sjhb * Function prototipes. Routines that start with an underscore are not part 93168191Sjhb * of the public interface and are wrappered with a macro. 94168191Sjhb */ 9593672Sarrvoid sx_sysinit(void *arg); 96168191Sjhb#define sx_init(sx, desc) sx_init_flags((sx), (desc), 0) 97168191Sjhbvoid sx_init_flags(struct sx *sx, const char *description, int opts); 9873782Sjasonevoid sx_destroy(struct sx *sx); 99227788Sattilioint sx_try_slock_(struct sx *sx, const char *file, int line); 100227788Sattilioint sx_try_xlock_(struct sx *sx, const char *file, int line); 101227788Sattilioint sx_try_upgrade_(struct sx *sx, const char *file, int line); 102227788Sattiliovoid sx_downgrade_(struct sx *sx, const char *file, int line); 103170149Sattilioint _sx_slock(struct sx *sx, int opts, const char *file, int line); 104170149Sattilioint _sx_xlock(struct sx *sx, int opts, const char *file, int line); 10574912Sjhbvoid _sx_sunlock(struct sx *sx, const char *file, int line); 10674912Sjhbvoid _sx_xunlock(struct sx *sx, const char *file, int line); 107170149Sattilioint _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, 108170149Sattilio const char *file, int line); 109170149Sattilioint _sx_slock_hard(struct sx *sx, int opts, const char *file, int line); 110168191Sjhbvoid _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int 111168191Sjhb line); 112168191Sjhbvoid _sx_sunlock_hard(struct sx *sx, const char *file, int line); 113161721Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 114227588Spjdvoid _sx_assert(const struct sx *sx, int what, const char *file, int line); 11593688Sarr#endif 116161337Sjhb#ifdef DDB 117161337Sjhbint sx_chain(struct thread *td, struct thread **ownerp); 118161337Sjhb#endif 11993672Sarr 12093672Sarrstruct sx_args { 12193672Sarr struct sx *sa_sx; 12293672Sarr const char *sa_desc; 123219819Sjeff int sa_flags; 12493672Sarr}; 12593672Sarr 126219819Sjeff#define SX_SYSINIT_FLAGS(name, sxa, desc, flags) \ 12793672Sarr static struct sx_args name##_args = { \ 128149739Sjhb (sxa), \ 129219819Sjeff (desc), \ 130219819Sjeff (flags) \ 13193672Sarr }; \ 13293672Sarr SYSINIT(name##_sx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 133149739Sjhb sx_sysinit, &name##_args); \ 134149739Sjhb SYSUNINIT(name##_sx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 135149739Sjhb sx_destroy, (sxa)) 13673782Sjasone 137219819Sjeff#define SX_SYSINIT(name, sxa, desc) SX_SYSINIT_FLAGS(name, sxa, desc, 0) 138219819Sjeff 139168191Sjhb/* 140170149Sattilio * Full lock operations that are suitable to be inlined in non-debug kernels. 141170149Sattilio * If the lock can't be acquired or released trivially then the work is 142170149Sattilio * deferred to 'tougher' functions. 143170149Sattilio */ 144170149Sattilio 145170149Sattilio/* Acquire an exclusive lock. */ 146170149Sattiliostatic __inline int 147170149Sattilio__sx_xlock(struct sx *sx, struct thread *td, int opts, const char *file, 148170149Sattilio int line) 149170149Sattilio{ 150170149Sattilio uintptr_t tid = (uintptr_t)td; 151170149Sattilio int error = 0; 152170149Sattilio 153170149Sattilio if (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) 154170149Sattilio error = _sx_xlock_hard(sx, tid, opts, file, line); 155192853Ssson else 156285704Smarkj LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 157285704Smarkj 0, 0, file, line, LOCKSTAT_WRITER); 158170149Sattilio 159170149Sattilio return (error); 160170149Sattilio} 161170149Sattilio 162170149Sattilio/* Release an exclusive lock. */ 163170149Sattiliostatic __inline void 164170149Sattilio__sx_xunlock(struct sx *sx, struct thread *td, const char *file, int line) 165170149Sattilio{ 166170149Sattilio uintptr_t tid = (uintptr_t)td; 167170149Sattilio 168258541Sattilio if (sx->sx_recurse == 0) 169285704Smarkj LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, 170285704Smarkj LOCKSTAT_WRITER); 171170149Sattilio if (!atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED)) 172170149Sattilio _sx_xunlock_hard(sx, tid, file, line); 173170149Sattilio} 174170149Sattilio 175170149Sattilio/* Acquire a shared lock. */ 176170149Sattiliostatic __inline int 177170149Sattilio__sx_slock(struct sx *sx, int opts, const char *file, int line) 178170149Sattilio{ 179170149Sattilio uintptr_t x = sx->sx_lock; 180170149Sattilio int error = 0; 181170149Sattilio 182170149Sattilio if (!(x & SX_LOCK_SHARED) || 183170149Sattilio !atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) 184170149Sattilio error = _sx_slock_hard(sx, opts, file, line); 185174629Sjeff else 186285704Smarkj LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 187285704Smarkj 0, 0, file, line, LOCKSTAT_READER); 188170149Sattilio 189170149Sattilio return (error); 190170149Sattilio} 191170149Sattilio 192170149Sattilio/* 193170149Sattilio * Release a shared lock. We can just drop a single shared lock so 194170149Sattilio * long as we aren't trying to drop the last shared lock when other 195170149Sattilio * threads are waiting for an exclusive lock. This takes advantage of 196170149Sattilio * the fact that an unlocked lock is encoded as a shared lock with a 197170149Sattilio * count of 0. 198170149Sattilio */ 199170149Sattiliostatic __inline void 200170149Sattilio__sx_sunlock(struct sx *sx, const char *file, int line) 201170149Sattilio{ 202170149Sattilio uintptr_t x = sx->sx_lock; 203170149Sattilio 204285704Smarkj LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER); 205170149Sattilio if (x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS) || 206197643Sattilio !atomic_cmpset_rel_ptr(&sx->sx_lock, x, x - SX_ONE_SHARER)) 207170149Sattilio _sx_sunlock_hard(sx, file, line); 208170149Sattilio} 209170149Sattilio 210170149Sattilio/* 211168191Sjhb * Public interface for lock operations. 212168191Sjhb */ 213168191Sjhb#ifndef LOCK_DEBUG 214168191Sjhb#error "LOCK_DEBUG not defined, include <sys/lock.h> before <sys/sx.h>" 215168191Sjhb#endif 216168191Sjhb#if (LOCK_DEBUG > 0) || defined(SX_NOINLINE) 217227788Sattilio#define sx_xlock_(sx, file, line) \ 218227788Sattilio (void)_sx_xlock((sx), 0, (file), (line)) 219227788Sattilio#define sx_xlock_sig_(sx, file, line) \ 220227788Sattilio _sx_xlock((sx), SX_INTERRUPTIBLE, (file), (line)) 221227788Sattilio#define sx_xunlock_(sx, file, line) \ 222227788Sattilio _sx_xunlock((sx), (file), (line)) 223227788Sattilio#define sx_slock_(sx, file, line) \ 224227788Sattilio (void)_sx_slock((sx), 0, (file), (line)) 225227788Sattilio#define sx_slock_sig_(sx, file, line) \ 226227788Sattilio _sx_slock((sx), SX_INTERRUPTIBLE, (file) , (line)) 227227788Sattilio#define sx_sunlock_(sx, file, line) \ 228227788Sattilio _sx_sunlock((sx), (file), (line)) 229168191Sjhb#else 230227788Sattilio#define sx_xlock_(sx, file, line) \ 231227788Sattilio (void)__sx_xlock((sx), curthread, 0, (file), (line)) 232227788Sattilio#define sx_xlock_sig_(sx, file, line) \ 233227788Sattilio __sx_xlock((sx), curthread, SX_INTERRUPTIBLE, (file), (line)) 234227788Sattilio#define sx_xunlock_(sx, file, line) \ 235227788Sattilio __sx_xunlock((sx), curthread, (file), (line)) 236227788Sattilio#define sx_slock_(sx, file, line) \ 237227788Sattilio (void)__sx_slock((sx), 0, (file), (line)) 238227788Sattilio#define sx_slock_sig_(sx, file, line) \ 239227788Sattilio __sx_slock((sx), SX_INTERRUPTIBLE, (file), (line)) 240227788Sattilio#define sx_sunlock_(sx, file, line) \ 241227788Sattilio __sx_sunlock((sx), (file), (line)) 242168191Sjhb#endif /* LOCK_DEBUG > 0 || SX_NOINLINE */ 243227788Sattilio#define sx_try_slock(sx) sx_try_slock_((sx), LOCK_FILE, LOCK_LINE) 244227788Sattilio#define sx_try_xlock(sx) sx_try_xlock_((sx), LOCK_FILE, LOCK_LINE) 245227788Sattilio#define sx_try_upgrade(sx) sx_try_upgrade_((sx), LOCK_FILE, LOCK_LINE) 246227788Sattilio#define sx_downgrade(sx) sx_downgrade_((sx), LOCK_FILE, LOCK_LINE) 247227788Sattilio#ifdef INVARIANTS 248227788Sattilio#define sx_assert_(sx, what, file, line) \ 249227788Sattilio _sx_assert((sx), (what), (file), (line)) 250227788Sattilio#else 251227788Sattilio#define sx_assert_(sx, what, file, line) (void)0 252227788Sattilio#endif 253168191Sjhb 254227788Sattilio#define sx_xlock(sx) sx_xlock_((sx), LOCK_FILE, LOCK_LINE) 255227788Sattilio#define sx_xlock_sig(sx) sx_xlock_sig_((sx), LOCK_FILE, LOCK_LINE) 256227788Sattilio#define sx_xunlock(sx) sx_xunlock_((sx), LOCK_FILE, LOCK_LINE) 257227788Sattilio#define sx_slock(sx) sx_slock_((sx), LOCK_FILE, LOCK_LINE) 258227788Sattilio#define sx_slock_sig(sx) sx_slock_sig_((sx), LOCK_FILE, LOCK_LINE) 259227788Sattilio#define sx_sunlock(sx) sx_sunlock_((sx), LOCK_FILE, LOCK_LINE) 260227788Sattilio#define sx_assert(sx, what) sx_assert_((sx), (what), __FILE__, __LINE__) 261227788Sattilio 262169776Sjhb/* 263169776Sjhb * Return a pointer to the owning thread if the lock is exclusively 264169776Sjhb * locked. 265169776Sjhb */ 266169776Sjhb#define sx_xholder(sx) \ 267169776Sjhb ((sx)->sx_lock & SX_LOCK_SHARED ? NULL : \ 268169776Sjhb (struct thread *)SX_OWNER((sx)->sx_lock)) 269169776Sjhb 270168191Sjhb#define sx_xlocked(sx) \ 271168191Sjhb (((sx)->sx_lock & ~(SX_LOCK_FLAGMASK & ~SX_LOCK_SHARED)) == \ 272168191Sjhb (uintptr_t)curthread) 273168191Sjhb 274227788Sattilio#define sx_unlock_(sx, file, line) do { \ 275159844Sjhb if (sx_xlocked(sx)) \ 276227788Sattilio sx_xunlock_(sx, file, line); \ 277157296Sjhb else \ 278227788Sattilio sx_sunlock_(sx, file, line); \ 279157296Sjhb} while (0) 280168191Sjhb 281227788Sattilio#define sx_unlock(sx) sx_unlock_((sx), LOCK_FILE, LOCK_LINE) 282227788Sattilio 283167387Sjhb#define sx_sleep(chan, sx, pri, wmesg, timo) \ 284247787Sdavide _sleep((chan), &(sx)->lock_object, (pri), (wmesg), \ 285247787Sdavide tick_sbt * (timo), 0, C_HARDCLOCK) 28674912Sjhb 287168191Sjhb/* 288168191Sjhb * Options passed to sx_init_flags(). 289168191Sjhb */ 290168191Sjhb#define SX_DUPOK 0x01 291168191Sjhb#define SX_NOPROFILE 0x02 292168191Sjhb#define SX_NOWITNESS 0x04 293168191Sjhb#define SX_QUIET 0x08 294193011Sattilio#define SX_NOADAPTIVE 0x10 295169769Sjhb#define SX_RECURSE 0x20 296275751Sdchagin#define SX_NEW 0x40 297168191Sjhb 298170149Sattilio/* 299170149Sattilio * Options passed to sx_*lock_hard(). 300170149Sattilio */ 301170149Sattilio#define SX_INTERRUPTIBLE 0x40 302170149Sattilio 30385412Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 304169780Sjhb#define SA_LOCKED LA_LOCKED 305169780Sjhb#define SA_SLOCKED LA_SLOCKED 306169780Sjhb#define SA_XLOCKED LA_XLOCKED 307169780Sjhb#define SA_UNLOCKED LA_UNLOCKED 308169780Sjhb#define SA_RECURSED LA_RECURSED 309169780Sjhb#define SA_NOTRECURSED LA_NOTRECURSED 310169780Sjhb 311298981Spfg/* Backwards compatibility. */ 31285388Sjhb#define SX_LOCKED LA_LOCKED 31385388Sjhb#define SX_SLOCKED LA_SLOCKED 31485388Sjhb#define SX_XLOCKED LA_XLOCKED 315125419Spjd#define SX_UNLOCKED LA_UNLOCKED 316168191Sjhb#define SX_RECURSED LA_RECURSED 317168191Sjhb#define SX_NOTRECURSED LA_NOTRECURSED 318125444Sbde#endif 31973863Sbmilekic 320125444Sbde#endif /* _KERNEL */ 321125444Sbde 322125444Sbde#endif /* !_SYS_SX_H_ */ 323