sx.h revision 170149
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: head/sys/sys/sx.h 170149 2007-05-31 09:14:48Z 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#include <sys/lock_profile.h> 38 39#ifdef _KERNEL 40#include <machine/atomic.h> 41#endif 42 43/* 44 * In general, the sx locks and rwlocks use very similar algorithms. 45 * The main difference in the implementations is how threads are 46 * blocked when a lock is unavailable. For this, sx locks use sleep 47 * queues which do not support priority propagation, and rwlocks use 48 * turnstiles which do. 49 * 50 * The sx_lock field consists of several fields. The low bit 51 * indicates if the lock is locked with a shared or exclusive lock. A 52 * value of 0 indicates an exclusive lock, and a value of 1 indicates 53 * a shared lock. Bit 1 is a boolean indicating if there are any 54 * threads waiting for a shared lock. Bit 2 is a boolean indicating 55 * if there are any threads waiting for an exclusive lock. Bit 3 is a 56 * boolean indicating if an exclusive lock is recursively held. The 57 * rest of the variable's definition is dependent on the value of the 58 * first bit. For an exclusive lock, it is a pointer to the thread 59 * holding the lock, similar to the mtx_lock field of mutexes. For 60 * shared locks, it is a count of read locks that are held. 61 * 62 * When the lock is not locked by any thread, it is encoded as a 63 * shared lock with zero waiters. 64 * 65 * A note about memory barriers. Exclusive locks need to use the same 66 * memory barriers as mutexes: _acq when acquiring an exclusive lock 67 * and _rel when releasing an exclusive lock. On the other side, 68 * shared lock needs to use an _acq barrier when acquiring the lock 69 * but, since they don't update any locked data, no memory barrier is 70 * needed when releasing a shared lock. 71 */ 72 73#define SX_LOCK_SHARED 0x01 74#define SX_LOCK_SHARED_WAITERS 0x02 75#define SX_LOCK_EXCLUSIVE_WAITERS 0x04 76#define SX_LOCK_RECURSED 0x08 77#define SX_LOCK_FLAGMASK \ 78 (SX_LOCK_SHARED | SX_LOCK_SHARED_WAITERS | \ 79 SX_LOCK_EXCLUSIVE_WAITERS | SX_LOCK_RECURSED) 80 81#define SX_OWNER(x) ((x) & ~SX_LOCK_FLAGMASK) 82#define SX_SHARERS_SHIFT 4 83#define SX_SHARERS(x) (SX_OWNER(x) >> SX_SHARERS_SHIFT) 84#define SX_SHARERS_LOCK(x) \ 85 ((x) << SX_SHARERS_SHIFT | SX_LOCK_SHARED) 86#define SX_ONE_SHARER (1 << SX_SHARERS_SHIFT) 87 88#define SX_LOCK_UNLOCKED SX_SHARERS_LOCK(0) 89#define SX_LOCK_DESTROYED \ 90 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS) 91 92#ifdef _KERNEL 93 94/* 95 * Function prototipes. Routines that start with an underscore are not part 96 * of the public interface and are wrappered with a macro. 97 */ 98void sx_sysinit(void *arg); 99#define sx_init(sx, desc) sx_init_flags((sx), (desc), 0) 100void sx_init_flags(struct sx *sx, const char *description, int opts); 101void sx_destroy(struct sx *sx); 102int _sx_slock(struct sx *sx, int opts, const char *file, int line); 103int _sx_xlock(struct sx *sx, int opts, const char *file, int line); 104int _sx_try_slock(struct sx *sx, const char *file, int line); 105int _sx_try_xlock(struct sx *sx, const char *file, int line); 106void _sx_sunlock(struct sx *sx, const char *file, int line); 107void _sx_xunlock(struct sx *sx, const char *file, int line); 108int _sx_try_upgrade(struct sx *sx, const char *file, int line); 109void _sx_downgrade(struct sx *sx, const char *file, int line); 110int _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, 111 const char *file, int line); 112int _sx_slock_hard(struct sx *sx, int opts, const char *file, int line); 113void _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int 114 line); 115void _sx_sunlock_hard(struct sx *sx, const char *file, int line); 116#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 117void _sx_assert(struct sx *sx, int what, const char *file, int line); 118#endif 119#ifdef DDB 120int sx_chain(struct thread *td, struct thread **ownerp); 121#endif 122 123struct sx_args { 124 struct sx *sa_sx; 125 const char *sa_desc; 126}; 127 128#define SX_SYSINIT(name, sxa, desc) \ 129 static struct sx_args name##_args = { \ 130 (sxa), \ 131 (desc) \ 132 }; \ 133 SYSINIT(name##_sx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 134 sx_sysinit, &name##_args); \ 135 SYSUNINIT(name##_sx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 136 sx_destroy, (sxa)) 137 138/* 139 * Full lock operations that are suitable to be inlined in non-debug kernels. 140 * If the lock can't be acquired or released trivially then the work is 141 * deferred to 'tougher' functions. 142 */ 143 144/* Acquire an exclusive lock. */ 145static __inline int 146__sx_xlock(struct sx *sx, struct thread *td, int opts, const char *file, 147 int line) 148{ 149 uintptr_t tid = (uintptr_t)td; 150 int error = 0; 151 152 if (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) 153 error = _sx_xlock_hard(sx, tid, opts, file, line); 154 else 155 lock_profile_obtain_lock_success(&sx->lock_object, 0, 0, file, 156 line); 157 158 return (error); 159} 160 161/* Release an exclusive lock. */ 162static __inline void 163__sx_xunlock(struct sx *sx, struct thread *td, const char *file, int line) 164{ 165 uintptr_t tid = (uintptr_t)td; 166 167 if (!atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED)) 168 _sx_xunlock_hard(sx, tid, file, line); 169} 170 171/* Acquire a shared lock. */ 172static __inline int 173__sx_slock(struct sx *sx, int opts, const char *file, int line) 174{ 175 uintptr_t x = sx->sx_lock; 176 int error = 0; 177 178 if (!(x & SX_LOCK_SHARED) || 179 !atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) 180 error = _sx_slock_hard(sx, opts, file, line); 181 else 182 lock_profile_obtain_lock_success(&sx->lock_object, 0, 0, file, 183 line); 184 185 return (error); 186} 187 188/* 189 * Release a shared lock. We can just drop a single shared lock so 190 * long as we aren't trying to drop the last shared lock when other 191 * threads are waiting for an exclusive lock. This takes advantage of 192 * the fact that an unlocked lock is encoded as a shared lock with a 193 * count of 0. 194 */ 195static __inline void 196__sx_sunlock(struct sx *sx, const char *file, int line) 197{ 198 uintptr_t x = sx->sx_lock; 199 200 if (x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS) || 201 !atomic_cmpset_ptr(&sx->sx_lock, x, x - SX_ONE_SHARER)) 202 _sx_sunlock_hard(sx, file, line); 203} 204 205/* 206 * Public interface for lock operations. 207 */ 208#ifndef LOCK_DEBUG 209#error "LOCK_DEBUG not defined, include <sys/lock.h> before <sys/sx.h>" 210#endif 211#if (LOCK_DEBUG > 0) || defined(SX_NOINLINE) 212#define sx_xlock(sx) (void)_sx_xlock((sx), 0, LOCK_FILE, LOCK_LINE) 213#define sx_xlock_sig(sx) \ 214 _sx_xlock((sx), SX_INTERRUPTIBLE, LOCK_FILE, LOCK_LINE) 215#define sx_xunlock(sx) _sx_xunlock((sx), LOCK_FILE, LOCK_LINE) 216#define sx_slock(sx) (void)_sx_slock((sx), 0, LOCK_FILE, LOCK_LINE) 217#define sx_slock_sig(sx) \ 218 _sx_slock((sx), SX_INTERRUPTIBLE, LOCK_FILE, LOCK_LINE) 219#define sx_sunlock(sx) _sx_sunlock((sx), LOCK_FILE, LOCK_LINE) 220#else 221#define sx_xlock(sx) \ 222 (void)__sx_xlock((sx), curthread, 0, LOCK_FILE, LOCK_LINE) 223#define sx_xlock_sig(sx) \ 224 __sx_xlock((sx), curthread, SX_INTERRUPTIBLE, LOCK_FILE, LOCK_LINE) 225#define sx_xunlock(sx) \ 226 __sx_xunlock((sx), curthread, LOCK_FILE, LOCK_LINE) 227#define sx_slock(sx) (void)__sx_slock((sx), 0, LOCK_FILE, LOCK_LINE) 228#define sx_slock_sig(sx) \ 229 __sx_slock((sx), SX_INTERRUPTIBLE, LOCK_FILE, LOCK_LINE) 230#define sx_sunlock(sx) __sx_sunlock((sx), LOCK_FILE, LOCK_LINE) 231#endif /* LOCK_DEBUG > 0 || SX_NOINLINE */ 232#define sx_try_slock(sx) _sx_try_slock((sx), LOCK_FILE, LOCK_LINE) 233#define sx_try_xlock(sx) _sx_try_xlock((sx), LOCK_FILE, LOCK_LINE) 234#define sx_try_upgrade(sx) _sx_try_upgrade((sx), LOCK_FILE, LOCK_LINE) 235#define sx_downgrade(sx) _sx_downgrade((sx), LOCK_FILE, LOCK_LINE) 236 237/* 238 * Return a pointer to the owning thread if the lock is exclusively 239 * locked. 240 */ 241#define sx_xholder(sx) \ 242 ((sx)->sx_lock & SX_LOCK_SHARED ? NULL : \ 243 (struct thread *)SX_OWNER((sx)->sx_lock)) 244 245#define sx_xlocked(sx) \ 246 (((sx)->sx_lock & ~(SX_LOCK_FLAGMASK & ~SX_LOCK_SHARED)) == \ 247 (uintptr_t)curthread) 248 249#define sx_unlock(sx) do { \ 250 if (sx_xlocked(sx)) \ 251 sx_xunlock(sx); \ 252 else \ 253 sx_sunlock(sx); \ 254} while (0) 255 256#define sx_sleep(chan, sx, pri, wmesg, timo) \ 257 _sleep((chan), &(sx)->lock_object, (pri), (wmesg), (timo)) 258 259/* 260 * Options passed to sx_init_flags(). 261 */ 262#define SX_DUPOK 0x01 263#define SX_NOPROFILE 0x02 264#define SX_NOWITNESS 0x04 265#define SX_QUIET 0x08 266#define SX_ADAPTIVESPIN 0x10 267#define SX_RECURSE 0x20 268 269/* 270 * Options passed to sx_*lock_hard(). 271 */ 272#define SX_INTERRUPTIBLE 0x40 273 274#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 275#define SA_LOCKED LA_LOCKED 276#define SA_SLOCKED LA_SLOCKED 277#define SA_XLOCKED LA_XLOCKED 278#define SA_UNLOCKED LA_UNLOCKED 279#define SA_RECURSED LA_RECURSED 280#define SA_NOTRECURSED LA_NOTRECURSED 281 282/* Backwards compatability. */ 283#define SX_LOCKED LA_LOCKED 284#define SX_SLOCKED LA_SLOCKED 285#define SX_XLOCKED LA_XLOCKED 286#define SX_UNLOCKED LA_UNLOCKED 287#define SX_RECURSED LA_RECURSED 288#define SX_NOTRECURSED LA_NOTRECURSED 289#endif 290 291#ifdef INVARIANTS 292#define sx_assert(sx, what) _sx_assert((sx), (what), LOCK_FILE, LOCK_LINE) 293#else 294#define sx_assert(sx, what) 295#endif 296 297#endif /* _KERNEL */ 298 299#endif /* !_SYS_SX_H_ */ 300