kern_rwlock.c revision 154941
1154941Sjhb/*- 2154941Sjhb * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org> 3154941Sjhb * All rights reserved. 4154941Sjhb * 5154941Sjhb * Redistribution and use in source and binary forms, with or without 6154941Sjhb * modification, are permitted provided that the following conditions 7154941Sjhb * are met: 8154941Sjhb * 1. Redistributions of source code must retain the above copyright 9154941Sjhb * notice, this list of conditions and the following disclaimer. 10154941Sjhb * 2. Redistributions in binary form must reproduce the above copyright 11154941Sjhb * notice, this list of conditions and the following disclaimer in the 12154941Sjhb * documentation and/or other materials provided with the distribution. 13154941Sjhb * 3. Neither the name of the author nor the names of any co-contributors 14154941Sjhb * may be used to endorse or promote products derived from this software 15154941Sjhb * without specific prior written permission. 16154941Sjhb * 17154941Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18154941Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19154941Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20154941Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21154941Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22154941Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23154941Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24154941Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25154941Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26154941Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27154941Sjhb * SUCH DAMAGE. 28154941Sjhb */ 29154941Sjhb 30154941Sjhb/* 31154941Sjhb * Machine independent bits of reader/writer lock implementation. 32154941Sjhb */ 33154941Sjhb 34154941Sjhb#include <sys/cdefs.h> 35154941Sjhb__FBSDID("$FreeBSD: head/sys/kern/kern_rwlock.c 154941 2006-01-27 23:13:26Z jhb $"); 36154941Sjhb 37154941Sjhb#include "opt_ddb.h" 38154941Sjhb 39154941Sjhb#include <sys/param.h> 40154941Sjhb#include <sys/ktr.h> 41154941Sjhb#include <sys/lock.h> 42154941Sjhb#include <sys/mutex.h> 43154941Sjhb#include <sys/proc.h> 44154941Sjhb#include <sys/rwlock.h> 45154941Sjhb#include <sys/systm.h> 46154941Sjhb#include <sys/turnstile.h> 47154941Sjhb 48154941Sjhb#include <machine/cpu.h> 49154941Sjhb 50154941Sjhb#ifdef DDB 51154941Sjhb#include <ddb/ddb.h> 52154941Sjhb 53154941Sjhbstatic void db_show_rwlock(struct lock_object *lock); 54154941Sjhb#endif 55154941Sjhb 56154941Sjhbstruct lock_class lock_class_rw = { 57154941Sjhb "rw", 58154941Sjhb LC_SLEEPLOCK | LC_RECURSABLE /* | LC_UPGRADABLE */, 59154941Sjhb#ifdef DDB 60154941Sjhb db_show_rwlock 61154941Sjhb#endif 62154941Sjhb}; 63154941Sjhb 64154941Sjhb#define rw_owner(rw) \ 65154941Sjhb ((rw)->rw_lock & RW_LOCK_READ ? NULL : \ 66154941Sjhb (struct thread *)RW_OWNER((rw)->rw_lock)) 67154941Sjhb 68154941Sjhb#ifndef INVARIANTS 69154941Sjhb#define _rw_assert(rw, what, file, line) 70154941Sjhb#endif 71154941Sjhb 72154941Sjhbvoid 73154941Sjhbrw_init(struct rwlock *rw, const char *name) 74154941Sjhb{ 75154941Sjhb 76154941Sjhb rw->rw_lock = RW_UNLOCKED; 77154941Sjhb 78154941Sjhb lock_init(&rw->rw_object, &lock_class_rw, name, NULL, LO_WITNESS | 79154941Sjhb LO_RECURSABLE /* | LO_UPGRADABLE */); 80154941Sjhb} 81154941Sjhb 82154941Sjhbvoid 83154941Sjhbrw_destroy(struct rwlock *rw) 84154941Sjhb{ 85154941Sjhb 86154941Sjhb KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked")); 87154941Sjhb lock_destroy(&rw->rw_object); 88154941Sjhb} 89154941Sjhb 90154941Sjhbvoid 91154941Sjhbrw_sysinit(void *arg) 92154941Sjhb{ 93154941Sjhb struct rw_args *args = arg; 94154941Sjhb 95154941Sjhb rw_init(args->ra_rw, args->ra_desc); 96154941Sjhb} 97154941Sjhb 98154941Sjhbvoid 99154941Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line) 100154941Sjhb{ 101154941Sjhb 102154941Sjhb MPASS(curthread != NULL); 103154941Sjhb KASSERT(rw_owner(rw) != curthread, 104154941Sjhb ("%s (%s): wlock already held @ %s:%d", __func__, 105154941Sjhb rw->rw_object.lo_name, file, line)); 106154941Sjhb WITNESS_CHECKORDER(&rw->rw_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 107154941Sjhb line); 108154941Sjhb __rw_wlock(rw, curthread, file, line); 109154941Sjhb LOCK_LOG_LOCK("WLOCK", &rw->rw_object, 0, 0, file, line); 110154941Sjhb WITNESS_LOCK(&rw->rw_object, LOP_EXCLUSIVE, file, line); 111154941Sjhb} 112154941Sjhb 113154941Sjhbvoid 114154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line) 115154941Sjhb{ 116154941Sjhb 117154941Sjhb MPASS(curthread != NULL); 118154941Sjhb _rw_assert(rw, RA_WLOCKED, file, line); 119154941Sjhb WITNESS_UNLOCK(&rw->rw_object, LOP_EXCLUSIVE, file, line); 120154941Sjhb LOCK_LOG_LOCK("WUNLOCK", &rw->rw_object, 0, 0, file, line); 121154941Sjhb __rw_wunlock(rw, curthread, file, line); 122154941Sjhb} 123154941Sjhb 124154941Sjhbvoid 125154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line) 126154941Sjhb{ 127154941Sjhb uintptr_t x; 128154941Sjhb 129154941Sjhb KASSERT(rw_owner(rw) != curthread, 130154941Sjhb ("%s (%s): wlock already held @ %s:%d", __func__, 131154941Sjhb rw->rw_object.lo_name, file, line)); 132154941Sjhb WITNESS_CHECKORDER(&rw->rw_object, LOP_NEWORDER, file, line); 133154941Sjhb 134154941Sjhb /* 135154941Sjhb * Note that we don't make any attempt to try to block read 136154941Sjhb * locks once a writer has blocked on the lock. The reason is 137154941Sjhb * that we currently allow for read locks to recurse and we 138154941Sjhb * don't keep track of all the holders of read locks. Thus, if 139154941Sjhb * we were to block readers once a writer blocked and a reader 140154941Sjhb * tried to recurse on their reader lock after a writer had 141154941Sjhb * blocked we would end up in a deadlock since the reader would 142154941Sjhb * be blocked on the writer, and the writer would be blocked 143154941Sjhb * waiting for the reader to release its original read lock. 144154941Sjhb */ 145154941Sjhb for (;;) { 146154941Sjhb /* 147154941Sjhb * Handle the easy case. If no other thread has a write 148154941Sjhb * lock, then try to bump up the count of read locks. Note 149154941Sjhb * that we have to preserve the current state of the 150154941Sjhb * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a 151154941Sjhb * read lock, then rw_lock must have changed, so restart 152154941Sjhb * the loop. Note that this handles the case of a 153154941Sjhb * completely unlocked rwlock since such a lock is encoded 154154941Sjhb * as a read lock with no waiters. 155154941Sjhb */ 156154941Sjhb x = rw->rw_lock; 157154941Sjhb if (x & RW_LOCK_READ) { 158154941Sjhb 159154941Sjhb /* 160154941Sjhb * The RW_LOCK_READ_WAITERS flag should only be set 161154941Sjhb * if another thread currently holds a write lock, 162154941Sjhb * and in that case RW_LOCK_READ should be clear. 163154941Sjhb */ 164154941Sjhb MPASS((x & RW_LOCK_READ_WAITERS) == 0); 165154941Sjhb if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, 166154941Sjhb x + RW_ONE_READER)) { 167154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 168154941Sjhb CTR4(KTR_LOCK, 169154941Sjhb "%s: %p succeed %p -> %p", __func__, 170154941Sjhb rw, (void *)x, 171154941Sjhb (void *)(x + RW_ONE_READER)); 172154941Sjhb break; 173154941Sjhb } 174154941Sjhb continue; 175154941Sjhb } 176154941Sjhb 177154941Sjhb /* 178154941Sjhb * Okay, now it's the hard case. Some other thread already 179154941Sjhb * has a write lock, so acquire the turnstile lock so we can 180154941Sjhb * begin the process of blocking. 181154941Sjhb */ 182154941Sjhb turnstile_lock(&rw->rw_object); 183154941Sjhb 184154941Sjhb /* 185154941Sjhb * The lock might have been released while we spun, so 186154941Sjhb * recheck its state and restart the loop if there is no 187154941Sjhb * longer a write lock. 188154941Sjhb */ 189154941Sjhb x = rw->rw_lock; 190154941Sjhb if (x & RW_LOCK_READ) { 191154941Sjhb turnstile_release(&rw->rw_object); 192154941Sjhb continue; 193154941Sjhb } 194154941Sjhb 195154941Sjhb /* 196154941Sjhb * Ok, it's still a write lock. If the RW_LOCK_READ_WAITERS 197154941Sjhb * flag is already set, then we can go ahead and block. If 198154941Sjhb * it is not set then try to set it. If we fail to set it 199154941Sjhb * drop the turnstile lock and restart the loop. 200154941Sjhb */ 201154941Sjhb if (!(x & RW_LOCK_READ_WAITERS) && 202154941Sjhb !atomic_cmpset_ptr(&rw->rw_lock, x, 203154941Sjhb x | RW_LOCK_READ_WAITERS)) { 204154941Sjhb turnstile_release(&rw->rw_object); 205154941Sjhb continue; 206154941Sjhb } 207154941Sjhb if (!(x & RW_LOCK_READ_WAITERS) && 208154941Sjhb LOCK_LOG_TEST(&rw->rw_object, 0)) 209154941Sjhb CTR2(KTR_LOCK, "%s: %p set read waiters flag", __func__, 210154941Sjhb rw); 211154941Sjhb 212154941Sjhb /* 213154941Sjhb * We were unable to acquire the lock and the read waiters 214154941Sjhb * flag is set, so we must block on the turnstile. 215154941Sjhb */ 216154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 217154941Sjhb CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 218154941Sjhb rw); 219154941Sjhb turnstile_wait(&rw->rw_object, rw_owner(rw), TS_SHARED_QUEUE); 220154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 221154941Sjhb CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 222154941Sjhb __func__, rw); 223154941Sjhb } 224154941Sjhb 225154941Sjhb /* 226154941Sjhb * TODO: acquire "owner of record" here. Here be turnstile dragons 227154941Sjhb * however. turnstiles don't like owners changing between calls to 228154941Sjhb * turnstile_wait() currently. 229154941Sjhb */ 230154941Sjhb 231154941Sjhb LOCK_LOG_LOCK("RLOCK", &rw->rw_object, 0, 0, file, line); 232154941Sjhb WITNESS_LOCK(&rw->rw_object, 0, file, line); 233154941Sjhb} 234154941Sjhb 235154941Sjhbvoid 236154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line) 237154941Sjhb{ 238154941Sjhb struct turnstile *ts; 239154941Sjhb uintptr_t x; 240154941Sjhb 241154941Sjhb _rw_assert(rw, RA_RLOCKED, file, line); 242154941Sjhb WITNESS_UNLOCK(&rw->rw_object, 0, file, line); 243154941Sjhb LOCK_LOG_LOCK("RUNLOCK", &rw->rw_object, 0, 0, file, line); 244154941Sjhb 245154941Sjhb /* TODO: drop "owner of record" here. */ 246154941Sjhb 247154941Sjhb for (;;) { 248154941Sjhb /* 249154941Sjhb * See if there is more than one read lock held. If so, 250154941Sjhb * just drop one and return. 251154941Sjhb */ 252154941Sjhb x = rw->rw_lock; 253154941Sjhb if (RW_READERS(x) > 1) { 254154941Sjhb if (atomic_cmpset_ptr(&rw->rw_lock, x, 255154941Sjhb x - RW_ONE_READER)) { 256154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 257154941Sjhb CTR4(KTR_LOCK, 258154941Sjhb "%s: %p succeeded %p -> %p", 259154941Sjhb __func__, rw, (void *)x, 260154941Sjhb (void *)(x - RW_ONE_READER)); 261154941Sjhb break; 262154941Sjhb } 263154941Sjhb continue; 264154941Sjhb } 265154941Sjhb 266154941Sjhb /* 267154941Sjhb * We should never have read waiters while at least one 268154941Sjhb * thread holds a read lock. (See note above) 269154941Sjhb */ 270154941Sjhb KASSERT(!(x & RW_LOCK_READ_WAITERS), 271154941Sjhb ("%s: waiting readers", __func__)); 272154941Sjhb 273154941Sjhb /* 274154941Sjhb * If there aren't any waiters for a write lock, then try 275154941Sjhb * to drop it quickly. 276154941Sjhb */ 277154941Sjhb if (!(x & RW_LOCK_WRITE_WAITERS)) { 278154941Sjhb 279154941Sjhb /* 280154941Sjhb * There shouldn't be any flags set and we should 281154941Sjhb * be the only read lock. If we fail to release 282154941Sjhb * the single read lock, then another thread might 283154941Sjhb * have just acquired a read lock, so go back up 284154941Sjhb * to the multiple read locks case. 285154941Sjhb */ 286154941Sjhb MPASS(x == RW_READERS_LOCK(1)); 287154941Sjhb if (atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1), 288154941Sjhb RW_UNLOCKED)) { 289154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 290154941Sjhb CTR2(KTR_LOCK, "%s: %p last succeeded", 291154941Sjhb __func__, rw); 292154941Sjhb break; 293154941Sjhb } 294154941Sjhb continue; 295154941Sjhb } 296154941Sjhb 297154941Sjhb /* 298154941Sjhb * There should just be one reader with one or more 299154941Sjhb * writers waiting. 300154941Sjhb */ 301154941Sjhb MPASS(x == (RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS)); 302154941Sjhb 303154941Sjhb /* 304154941Sjhb * Ok, we know we have a waiting writer and we think we 305154941Sjhb * are the last reader, so grab the turnstile lock. 306154941Sjhb */ 307154941Sjhb turnstile_lock(&rw->rw_object); 308154941Sjhb 309154941Sjhb /* 310154941Sjhb * Try to drop our lock leaving the lock in a unlocked 311154941Sjhb * state. 312154941Sjhb * 313154941Sjhb * If you wanted to do explicit lock handoff you'd have to 314154941Sjhb * do it here. You'd also want to use turnstile_signal() 315154941Sjhb * and you'd have to handle the race where a higher 316154941Sjhb * priority thread blocks on the write lock before the 317154941Sjhb * thread you wakeup actually runs and have the new thread 318154941Sjhb * "steal" the lock. For now it's a lot simpler to just 319154941Sjhb * wakeup all of the waiters. 320154941Sjhb * 321154941Sjhb * As above, if we fail, then another thread might have 322154941Sjhb * acquired a read lock, so drop the turnstile lock and 323154941Sjhb * restart. 324154941Sjhb */ 325154941Sjhb if (!atomic_cmpset_ptr(&rw->rw_lock, 326154941Sjhb RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS, RW_UNLOCKED)) { 327154941Sjhb turnstile_release(&rw->rw_object); 328154941Sjhb continue; 329154941Sjhb } 330154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 331154941Sjhb CTR2(KTR_LOCK, "%s: %p last succeeded with waiters", 332154941Sjhb __func__, rw); 333154941Sjhb 334154941Sjhb /* 335154941Sjhb * Ok. The lock is released and all that's left is to 336154941Sjhb * wake up the waiters. Note that the lock might not be 337154941Sjhb * free anymore, but in that case the writers will just 338154941Sjhb * block again if they run before the new lock holder(s) 339154941Sjhb * release the lock. 340154941Sjhb */ 341154941Sjhb ts = turnstile_lookup(&rw->rw_object); 342154941Sjhb turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 343154941Sjhb turnstile_unpend(ts, TS_SHARED_LOCK); 344154941Sjhb break; 345154941Sjhb } 346154941Sjhb} 347154941Sjhb 348154941Sjhb/* 349154941Sjhb * This function is called when we are unable to obtain a write lock on the 350154941Sjhb * first try. This means that at least one other thread holds either a 351154941Sjhb * read or write lock. 352154941Sjhb */ 353154941Sjhbvoid 354154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line) 355154941Sjhb{ 356154941Sjhb uintptr_t v; 357154941Sjhb 358154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 359154941Sjhb CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 360154941Sjhb rw->rw_object.lo_name, (void *)rw->rw_lock, file, line); 361154941Sjhb 362154941Sjhb while (!_rw_write_lock(rw, tid)) { 363154941Sjhb turnstile_lock(&rw->rw_object); 364154941Sjhb v = rw->rw_lock; 365154941Sjhb 366154941Sjhb /* 367154941Sjhb * If the lock was released while spinning on the 368154941Sjhb * turnstile chain lock, try again. 369154941Sjhb */ 370154941Sjhb if (v == RW_UNLOCKED) { 371154941Sjhb turnstile_release(&rw->rw_object); 372154941Sjhb cpu_spinwait(); 373154941Sjhb continue; 374154941Sjhb } 375154941Sjhb 376154941Sjhb /* 377154941Sjhb * If the lock was released by a writer with both readers 378154941Sjhb * and writers waiting and a reader hasn't woken up and 379154941Sjhb * acquired the lock yet, rw_lock will be set to the 380154941Sjhb * value RW_UNLOCKED | RW_LOCK_WRITE_WAITERS. If we see 381154941Sjhb * that value, try to acquire it once. Note that we have 382154941Sjhb * to preserve the RW_LOCK_WRITE_WAITERS flag as there are 383154941Sjhb * other writers waiting still. If we fail, restart the 384154941Sjhb * loop. 385154941Sjhb */ 386154941Sjhb if (v == (RW_UNLOCKED | RW_LOCK_WRITE_WAITERS)) { 387154941Sjhb if (atomic_cmpset_acq_ptr(&rw->rw_lock, 388154941Sjhb RW_UNLOCKED | RW_LOCK_WRITE_WAITERS, 389154941Sjhb tid | RW_LOCK_WRITE_WAITERS)) { 390154941Sjhb turnstile_claim(&rw->rw_object); 391154941Sjhb CTR2(KTR_LOCK, "%s: %p claimed by new writer", 392154941Sjhb __func__, rw); 393154941Sjhb break; 394154941Sjhb } 395154941Sjhb turnstile_release(&rw->rw_object); 396154941Sjhb cpu_spinwait(); 397154941Sjhb continue; 398154941Sjhb } 399154941Sjhb 400154941Sjhb /* 401154941Sjhb * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to 402154941Sjhb * set it. If we fail to set it, then loop back and try 403154941Sjhb * again. 404154941Sjhb */ 405154941Sjhb if (!(v & RW_LOCK_WRITE_WAITERS) && 406154941Sjhb !atomic_cmpset_ptr(&rw->rw_lock, v, 407154941Sjhb v | RW_LOCK_WRITE_WAITERS)) { 408154941Sjhb turnstile_release(&rw->rw_object); 409154941Sjhb cpu_spinwait(); 410154941Sjhb continue; 411154941Sjhb } 412154941Sjhb if (!(v & RW_LOCK_WRITE_WAITERS) && 413154941Sjhb LOCK_LOG_TEST(&rw->rw_object, 0)) 414154941Sjhb CTR2(KTR_LOCK, "%s: %p set write waiters flag", 415154941Sjhb __func__, rw); 416154941Sjhb 417154941Sjhb /* XXX: Adaptively spin if current wlock owner on another CPU? */ 418154941Sjhb 419154941Sjhb /* 420154941Sjhb * We were unable to acquire the lock and the write waiters 421154941Sjhb * flag is set, so we must block on the turnstile. 422154941Sjhb */ 423154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 424154941Sjhb CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 425154941Sjhb rw); 426154941Sjhb turnstile_wait(&rw->rw_object, rw_owner(rw), 427154941Sjhb TS_EXCLUSIVE_QUEUE); 428154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 429154941Sjhb CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 430154941Sjhb __func__, rw); 431154941Sjhb } 432154941Sjhb} 433154941Sjhb 434154941Sjhb/* 435154941Sjhb * This function is called if the first try at releasing a write lock failed. 436154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at 437154941Sjhb * least one thread is waiting on this lock. 438154941Sjhb */ 439154941Sjhbvoid 440154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line) 441154941Sjhb{ 442154941Sjhb struct turnstile *ts; 443154941Sjhb uintptr_t v; 444154941Sjhb int queue; 445154941Sjhb 446154941Sjhb KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS), 447154941Sjhb ("%s: neither of the waiter flags are set", __func__)); 448154941Sjhb 449154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 450154941Sjhb CTR2(KTR_LOCK, "%s: %p contested", __func__, rw); 451154941Sjhb 452154941Sjhb turnstile_lock(&rw->rw_object); 453154941Sjhb ts = turnstile_lookup(&rw->rw_object); 454154941Sjhb 455154941Sjhb /* XXX: Adaptive fixup would be required here. */ 456154941Sjhb MPASS(ts != NULL); 457154941Sjhb 458154941Sjhb /* 459154941Sjhb * Use the same algo as sx locks for now. Prefer waking up shared 460154941Sjhb * waiters if we have any over writers. This is probably not ideal. 461154941Sjhb * 462154941Sjhb * 'v' is the value we are going to write back to rw_lock. If we 463154941Sjhb * have waiters on both queues, we need to preserve the state of 464154941Sjhb * the waiter flag for the queue we don't wake up. For now this is 465154941Sjhb * hardcoded for the algorithm mentioned above. 466154941Sjhb * 467154941Sjhb * In the case of both readers and writers waiting we wakeup the 468154941Sjhb * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a 469154941Sjhb * new writer comes in before a reader it will claim the lock up 470154941Sjhb * above. There is probably a potential priority inversion in 471154941Sjhb * there that could be worked around either by waking both queues 472154941Sjhb * of waiters or doing some complicated lock handoff gymnastics. 473154941Sjhb */ 474154941Sjhb if (rw->rw_lock & RW_LOCK_READ_WAITERS) { 475154941Sjhb queue = TS_SHARED_QUEUE; 476154941Sjhb v = RW_UNLOCKED | (rw->rw_lock & RW_LOCK_WRITE_WAITERS); 477154941Sjhb } else { 478154941Sjhb queue = TS_EXCLUSIVE_QUEUE; 479154941Sjhb v = RW_UNLOCKED; 480154941Sjhb } 481154941Sjhb if (LOCK_LOG_TEST(&rw->rw_object, 0)) 482154941Sjhb CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw, 483154941Sjhb queue == TS_SHARED_QUEUE ? "read" : "write"); 484154941Sjhb 485154941Sjhb /* Wake up all waiters for the specific queue. */ 486154941Sjhb turnstile_broadcast(ts, queue); 487154941Sjhb atomic_store_rel_ptr(&rw->rw_lock, v); 488154941Sjhb turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 489154941Sjhb} 490154941Sjhb 491154941Sjhb#ifdef INVARIANT_SUPPORT 492154941Sjhb#ifndef INVARIANT_SUPPORT 493154941Sjhb#undef _rw_assert 494154941Sjhb#endif 495154941Sjhb 496154941Sjhb/* 497154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least 498154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this* 499154941Sjhb * thread owns an rlock. 500154941Sjhb */ 501154941Sjhbvoid 502154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line) 503154941Sjhb{ 504154941Sjhb 505154941Sjhb if (panicstr != NULL) 506154941Sjhb return; 507154941Sjhb switch (what) { 508154941Sjhb case RA_LOCKED: 509154941Sjhb case RA_RLOCKED: 510154941Sjhb#ifdef WITNESS 511154941Sjhb witness_assert(&rw->rw_object, what, file, line); 512154941Sjhb#else 513154941Sjhb /* 514154941Sjhb * If some other thread has a write lock or we have one 515154941Sjhb * and are asserting a read lock, fail. Also, if no one 516154941Sjhb * has a lock at all, fail. 517154941Sjhb */ 518154941Sjhb if (rw->rw_lock == RW_UNLOCKED || 519154941Sjhb !(rw->rw_lock & RW_LOCK_READ) && (what == RW_RLOCKED || 520154941Sjhb RW_OWNER(rw) != (uintptr_t)curthread)) 521154941Sjhb panic("Lock %s not %slocked @ %s:%d\n", 522154941Sjhb rw->rw_object.lo_name, (what == RW_RLOCKED) ? 523154941Sjhb "read " : "", file, line); 524154941Sjhb#endif 525154941Sjhb break; 526154941Sjhb case RA_WLOCKED: 527154941Sjhb if (rw_owner(rw) != curthread) 528154941Sjhb panic("Lock %s not exclusively locked @ %s:%d\n", 529154941Sjhb rw->rw_object.lo_name, file, line); 530154941Sjhb break; 531154941Sjhb case RA_UNLOCKED: 532154941Sjhb#ifdef WITNESS 533154941Sjhb witness_assert(&rw->rw_object, what, file, line); 534154941Sjhb#else 535154941Sjhb /* 536154941Sjhb * If we hold a write lock fail. We can't reliably check 537154941Sjhb * to see if we hold a read lock or not. 538154941Sjhb */ 539154941Sjhb if (rw_owner(rw) == curthread) 540154941Sjhb panic("Lock %s exclusively locked @ %s:%d\n", 541154941Sjhb rw->rw_object.lo_name, file, line); 542154941Sjhb#endif 543154941Sjhb break; 544154941Sjhb default: 545154941Sjhb panic("Unknown rw lock assertion: %d @ %s:%d", what, file, 546154941Sjhb line); 547154941Sjhb } 548154941Sjhb} 549154941Sjhb#endif /* INVARIANT_SUPPORT */ 550154941Sjhb 551154941Sjhb#ifdef DDB 552154941Sjhbvoid 553154941Sjhbdb_show_rwlock(struct lock_object *lock) 554154941Sjhb{ 555154941Sjhb struct rwlock *rw; 556154941Sjhb struct thread *td; 557154941Sjhb 558154941Sjhb rw = (struct rwlock *)lock; 559154941Sjhb 560154941Sjhb db_printf(" state: "); 561154941Sjhb if (rw->rw_lock == RW_UNLOCKED) 562154941Sjhb db_printf("UNLOCKED\n"); 563154941Sjhb else if (rw->rw_lock & RW_LOCK_READ) 564154941Sjhb db_printf("RLOCK: %d locks\n", RW_READERS(rw->rw_lock)); 565154941Sjhb else { 566154941Sjhb td = rw_owner(rw); 567154941Sjhb db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 568154941Sjhb td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm); 569154941Sjhb } 570154941Sjhb db_printf(" waiters: "); 571154941Sjhb switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) { 572154941Sjhb case RW_LOCK_READ_WAITERS: 573154941Sjhb db_printf("readers\n"); 574154941Sjhb break; 575154941Sjhb case RW_LOCK_WRITE_WAITERS: 576154941Sjhb db_printf("writers\n"); 577154941Sjhb break; 578154941Sjhb case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS: 579154941Sjhb db_printf("readers and waiters\n"); 580154941Sjhb break; 581154941Sjhb default: 582154941Sjhb db_printf("none\n"); 583154941Sjhb break; 584154941Sjhb } 585154941Sjhb} 586154941Sjhb 587154941Sjhb#endif 588