kern_rwlock.c revision 176076
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 176076 2008-02-07 06:16:54Z jeff $"); 36154941Sjhb 37154941Sjhb#include "opt_ddb.h" 38167801Sjhb#include "opt_no_adaptive_rwlocks.h" 39154941Sjhb 40154941Sjhb#include <sys/param.h> 41154941Sjhb#include <sys/ktr.h> 42154941Sjhb#include <sys/lock.h> 43154941Sjhb#include <sys/mutex.h> 44154941Sjhb#include <sys/proc.h> 45154941Sjhb#include <sys/rwlock.h> 46154941Sjhb#include <sys/systm.h> 47154941Sjhb#include <sys/turnstile.h> 48171516Sattilio 49154941Sjhb#include <machine/cpu.h> 50154941Sjhb 51171052SattilioCTASSERT((RW_RECURSE & LO_CLASSFLAGS) == RW_RECURSE); 52171052Sattilio 53167801Sjhb#if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS) 54167801Sjhb#define ADAPTIVE_RWLOCKS 55167801Sjhb#endif 56167801Sjhb 57154941Sjhb#ifdef DDB 58154941Sjhb#include <ddb/ddb.h> 59154941Sjhb 60154941Sjhbstatic void db_show_rwlock(struct lock_object *lock); 61154941Sjhb#endif 62173733Sattiliostatic void assert_rw(struct lock_object *lock, int what); 63167368Sjhbstatic void lock_rw(struct lock_object *lock, int how); 64167368Sjhbstatic int unlock_rw(struct lock_object *lock); 65154941Sjhb 66154941Sjhbstruct lock_class lock_class_rw = { 67167365Sjhb .lc_name = "rw", 68167365Sjhb .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE, 69173733Sattilio .lc_assert = assert_rw, 70154941Sjhb#ifdef DDB 71167365Sjhb .lc_ddb_show = db_show_rwlock, 72154941Sjhb#endif 73167368Sjhb .lc_lock = lock_rw, 74167368Sjhb .lc_unlock = unlock_rw, 75154941Sjhb}; 76154941Sjhb 77157826Sjhb/* 78157826Sjhb * Return a pointer to the owning thread if the lock is write-locked or 79157826Sjhb * NULL if the lock is unlocked or read-locked. 80157826Sjhb */ 81157826Sjhb#define rw_wowner(rw) \ 82154941Sjhb ((rw)->rw_lock & RW_LOCK_READ ? NULL : \ 83154941Sjhb (struct thread *)RW_OWNER((rw)->rw_lock)) 84154941Sjhb 85157826Sjhb/* 86171052Sattilio * Returns if a write owner is recursed. Write ownership is not assured 87171052Sattilio * here and should be previously checked. 88171052Sattilio */ 89171052Sattilio#define rw_recursed(rw) ((rw)->rw_recurse != 0) 90171052Sattilio 91171052Sattilio/* 92171052Sattilio * Return true if curthread helds the lock. 93171052Sattilio */ 94171052Sattilio#define rw_wlocked(rw) (rw_wowner((rw)) == curthread) 95171052Sattilio 96171052Sattilio/* 97157826Sjhb * Return a pointer to the owning thread for this lock who should receive 98157826Sjhb * any priority lent by threads that block on this lock. Currently this 99157826Sjhb * is identical to rw_wowner(). 100157826Sjhb */ 101157826Sjhb#define rw_owner(rw) rw_wowner(rw) 102157826Sjhb 103154941Sjhb#ifndef INVARIANTS 104154941Sjhb#define _rw_assert(rw, what, file, line) 105154941Sjhb#endif 106154941Sjhb 107154941Sjhbvoid 108173733Sattilioassert_rw(struct lock_object *lock, int what) 109173733Sattilio{ 110173733Sattilio 111173733Sattilio rw_assert((struct rwlock *)lock, what); 112173733Sattilio} 113173733Sattilio 114173733Sattiliovoid 115167368Sjhblock_rw(struct lock_object *lock, int how) 116167368Sjhb{ 117167368Sjhb struct rwlock *rw; 118167368Sjhb 119167368Sjhb rw = (struct rwlock *)lock; 120167368Sjhb if (how) 121167368Sjhb rw_wlock(rw); 122167368Sjhb else 123167368Sjhb rw_rlock(rw); 124167368Sjhb} 125167368Sjhb 126167368Sjhbint 127167368Sjhbunlock_rw(struct lock_object *lock) 128167368Sjhb{ 129167368Sjhb struct rwlock *rw; 130167368Sjhb 131167368Sjhb rw = (struct rwlock *)lock; 132167368Sjhb rw_assert(rw, RA_LOCKED | LA_NOTRECURSED); 133167368Sjhb if (rw->rw_lock & RW_LOCK_READ) { 134167368Sjhb rw_runlock(rw); 135167368Sjhb return (0); 136167368Sjhb } else { 137167368Sjhb rw_wunlock(rw); 138167368Sjhb return (1); 139167368Sjhb } 140167368Sjhb} 141167368Sjhb 142167368Sjhbvoid 143171052Sattiliorw_init_flags(struct rwlock *rw, const char *name, int opts) 144154941Sjhb{ 145171052Sattilio int flags; 146154941Sjhb 147171052Sattilio MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET | 148171052Sattilio RW_RECURSE)) == 0); 149171052Sattilio 150171052Sattilio flags = LO_UPGRADABLE | LO_RECURSABLE; 151171052Sattilio if (opts & RW_DUPOK) 152171052Sattilio flags |= LO_DUPOK; 153171052Sattilio if (opts & RW_NOPROFILE) 154171052Sattilio flags |= LO_NOPROFILE; 155171052Sattilio if (!(opts & RW_NOWITNESS)) 156171052Sattilio flags |= LO_WITNESS; 157171052Sattilio if (opts & RW_QUIET) 158171052Sattilio flags |= LO_QUIET; 159171052Sattilio flags |= opts & RW_RECURSE; 160171052Sattilio 161154941Sjhb rw->rw_lock = RW_UNLOCKED; 162171052Sattilio rw->rw_recurse = 0; 163171052Sattilio lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags); 164154941Sjhb} 165154941Sjhb 166154941Sjhbvoid 167154941Sjhbrw_destroy(struct rwlock *rw) 168154941Sjhb{ 169154941Sjhb 170154941Sjhb KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked")); 171171052Sattilio KASSERT(rw->rw_recurse == 0, ("rw lock still recursed")); 172169394Sjhb rw->rw_lock = RW_DESTROYED; 173167787Sjhb lock_destroy(&rw->lock_object); 174154941Sjhb} 175154941Sjhb 176154941Sjhbvoid 177154941Sjhbrw_sysinit(void *arg) 178154941Sjhb{ 179154941Sjhb struct rw_args *args = arg; 180154941Sjhb 181154941Sjhb rw_init(args->ra_rw, args->ra_desc); 182154941Sjhb} 183154941Sjhb 184167024Srwatsonint 185167024Srwatsonrw_wowned(struct rwlock *rw) 186167024Srwatson{ 187167024Srwatson 188167024Srwatson return (rw_wowner(rw) == curthread); 189167024Srwatson} 190167024Srwatson 191154941Sjhbvoid 192154941Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line) 193154941Sjhb{ 194154941Sjhb 195154941Sjhb MPASS(curthread != NULL); 196169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 197169394Sjhb ("rw_wlock() of destroyed rwlock @ %s:%d", file, line)); 198167787Sjhb WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 199154941Sjhb line); 200154941Sjhb __rw_wlock(rw, curthread, file, line); 201171052Sattilio LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line); 202167787Sjhb WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line); 203160771Sjhb curthread->td_locks++; 204154941Sjhb} 205154941Sjhb 206154941Sjhbvoid 207154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line) 208154941Sjhb{ 209154941Sjhb 210154941Sjhb MPASS(curthread != NULL); 211169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 212169394Sjhb ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line)); 213154941Sjhb _rw_assert(rw, RA_WLOCKED, file, line); 214160771Sjhb curthread->td_locks--; 215167787Sjhb WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line); 216171052Sattilio LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file, 217171052Sattilio line); 218171052Sattilio if (!rw_recursed(rw)) 219171052Sattilio lock_profile_release_lock(&rw->lock_object); 220154941Sjhb __rw_wunlock(rw, curthread, file, line); 221154941Sjhb} 222176017Sjeff/* 223176017Sjeff * Determines whether a new reader can acquire a lock. Succeeds if the 224176017Sjeff * reader already owns a read lock and the lock is locked for read to 225176017Sjeff * prevent deadlock from reader recursion. Also succeeds if the lock 226176017Sjeff * is unlocked and has no writer waiters or spinners. Failing otherwise 227176017Sjeff * prioritizes writers before readers. 228176017Sjeff */ 229176017Sjeff#define RW_CAN_READ(_rw) \ 230176017Sjeff ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) & \ 231176017Sjeff (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) == \ 232176017Sjeff RW_LOCK_READ) 233154941Sjhb 234154941Sjhbvoid 235154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line) 236154941Sjhb{ 237170295Sjeff struct turnstile *ts; 238167801Sjhb#ifdef ADAPTIVE_RWLOCKS 239157846Sjhb volatile struct thread *owner; 240157851Swkoszek#endif 241167307Sjhb uint64_t waittime = 0; 242167054Skmacy int contested = 0; 243176017Sjeff uintptr_t v; 244154941Sjhb 245169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 246169394Sjhb ("rw_rlock() of destroyed rwlock @ %s:%d", file, line)); 247157826Sjhb KASSERT(rw_wowner(rw) != curthread, 248154941Sjhb ("%s (%s): wlock already held @ %s:%d", __func__, 249167787Sjhb rw->lock_object.lo_name, file, line)); 250167787Sjhb WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line); 251154941Sjhb 252154941Sjhb for (;;) { 253154941Sjhb /* 254154941Sjhb * Handle the easy case. If no other thread has a write 255154941Sjhb * lock, then try to bump up the count of read locks. Note 256154941Sjhb * that we have to preserve the current state of the 257154941Sjhb * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a 258154941Sjhb * read lock, then rw_lock must have changed, so restart 259154941Sjhb * the loop. Note that this handles the case of a 260154941Sjhb * completely unlocked rwlock since such a lock is encoded 261154941Sjhb * as a read lock with no waiters. 262154941Sjhb */ 263176017Sjeff v = rw->rw_lock; 264176017Sjeff if (RW_CAN_READ(v)) { 265154941Sjhb /* 266154941Sjhb * The RW_LOCK_READ_WAITERS flag should only be set 267176017Sjeff * if the lock has been unlocked and write waiters 268176017Sjeff * were present. 269154941Sjhb */ 270176017Sjeff if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, 271176017Sjeff v + RW_ONE_READER)) { 272167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 273154941Sjhb CTR4(KTR_LOCK, 274154941Sjhb "%s: %p succeed %p -> %p", __func__, 275176017Sjeff rw, (void *)v, 276176017Sjeff (void *)(v + RW_ONE_READER)); 277154941Sjhb break; 278154941Sjhb } 279157846Sjhb cpu_spinwait(); 280154941Sjhb continue; 281154941Sjhb } 282174629Sjeff lock_profile_obtain_lock_failed(&rw->lock_object, 283174629Sjeff &contested, &waittime); 284154941Sjhb 285173960Sattilio#ifdef ADAPTIVE_RWLOCKS 286154941Sjhb /* 287173960Sattilio * If the owner is running on another CPU, spin until 288173960Sattilio * the owner stops running or the state of the lock 289173960Sattilio * changes. 290173960Sattilio */ 291176017Sjeff if ((v & RW_LOCK_READ) == 0) { 292176017Sjeff owner = (struct thread *)RW_OWNER(v); 293176017Sjeff if (TD_IS_RUNNING(owner)) { 294176017Sjeff if (LOCK_LOG_TEST(&rw->lock_object, 0)) 295176017Sjeff CTR3(KTR_LOCK, 296176017Sjeff "%s: spinning on %p held by %p", 297176017Sjeff __func__, rw, owner); 298176017Sjeff while ((struct thread*)RW_OWNER(rw->rw_lock) == 299176017Sjeff owner && TD_IS_RUNNING(owner)) 300176017Sjeff cpu_spinwait(); 301176017Sjeff continue; 302176017Sjeff } 303173960Sattilio } 304173960Sattilio#endif 305173960Sattilio 306173960Sattilio /* 307154941Sjhb * Okay, now it's the hard case. Some other thread already 308176017Sjeff * has a write lock or there are write waiters present, 309176017Sjeff * acquire the turnstile lock so we can begin the process 310176017Sjeff * of blocking. 311154941Sjhb */ 312170295Sjeff ts = turnstile_trywait(&rw->lock_object); 313154941Sjhb 314154941Sjhb /* 315154941Sjhb * The lock might have been released while we spun, so 316176017Sjeff * recheck its state and restart the loop if needed. 317154941Sjhb */ 318176017Sjeff v = rw->rw_lock; 319176017Sjeff if (RW_CAN_READ(v)) { 320170295Sjeff turnstile_cancel(ts); 321157846Sjhb cpu_spinwait(); 322154941Sjhb continue; 323154941Sjhb } 324154941Sjhb 325173960Sattilio#ifdef ADAPTIVE_RWLOCKS 326154941Sjhb /* 327173960Sattilio * If the current owner of the lock is executing on another 328173960Sattilio * CPU quit the hard path and try to spin. 329173960Sattilio */ 330176017Sjeff if ((v & RW_LOCK_READ) == 0) { 331176017Sjeff owner = (struct thread *)RW_OWNER(v); 332176017Sjeff if (TD_IS_RUNNING(owner)) { 333176017Sjeff turnstile_cancel(ts); 334176017Sjeff cpu_spinwait(); 335176017Sjeff continue; 336176017Sjeff } 337173960Sattilio } 338173960Sattilio#endif 339173960Sattilio 340173960Sattilio /* 341176017Sjeff * The lock is held in write mode or it already has waiters. 342154941Sjhb */ 343176017Sjeff MPASS(!RW_CAN_READ(v)); 344176017Sjeff 345176017Sjeff /* 346176017Sjeff * If the RW_LOCK_READ_WAITERS flag is already set, then 347176017Sjeff * we can go ahead and block. If it is not set then try 348176017Sjeff * to set it. If we fail to set it drop the turnstile 349176017Sjeff * lock and restart the loop. 350176017Sjeff */ 351176017Sjeff if (!(v & RW_LOCK_READ_WAITERS)) { 352176017Sjeff if (!atomic_cmpset_ptr(&rw->rw_lock, v, 353176017Sjeff v | RW_LOCK_READ_WAITERS)) { 354170295Sjeff turnstile_cancel(ts); 355157826Sjhb cpu_spinwait(); 356157826Sjhb continue; 357157826Sjhb } 358167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 359157826Sjhb CTR2(KTR_LOCK, "%s: %p set read waiters flag", 360157826Sjhb __func__, rw); 361154941Sjhb } 362154941Sjhb 363154941Sjhb /* 364154941Sjhb * We were unable to acquire the lock and the read waiters 365154941Sjhb * flag is set, so we must block on the turnstile. 366154941Sjhb */ 367167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 368154941Sjhb CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 369154941Sjhb rw); 370170295Sjeff turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE); 371167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 372154941Sjhb CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 373154941Sjhb __func__, rw); 374154941Sjhb } 375154941Sjhb 376154941Sjhb /* 377154941Sjhb * TODO: acquire "owner of record" here. Here be turnstile dragons 378154941Sjhb * however. turnstiles don't like owners changing between calls to 379154941Sjhb * turnstile_wait() currently. 380154941Sjhb */ 381174629Sjeff lock_profile_obtain_lock_success( &rw->lock_object, contested, 382174629Sjeff waittime, file, line); 383167787Sjhb LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line); 384167787Sjhb WITNESS_LOCK(&rw->lock_object, 0, file, line); 385160771Sjhb curthread->td_locks++; 386176017Sjeff curthread->td_rw_rlocks++; 387154941Sjhb} 388154941Sjhb 389154941Sjhbvoid 390154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line) 391154941Sjhb{ 392154941Sjhb struct turnstile *ts; 393176017Sjeff uintptr_t x, v, queue; 394154941Sjhb 395169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 396169394Sjhb ("rw_runlock() of destroyed rwlock @ %s:%d", file, line)); 397154941Sjhb _rw_assert(rw, RA_RLOCKED, file, line); 398160771Sjhb curthread->td_locks--; 399176017Sjeff curthread->td_rw_rlocks--; 400167787Sjhb WITNESS_UNLOCK(&rw->lock_object, 0, file, line); 401167787Sjhb LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line); 402154941Sjhb 403154941Sjhb /* TODO: drop "owner of record" here. */ 404154941Sjhb 405154941Sjhb for (;;) { 406154941Sjhb /* 407154941Sjhb * See if there is more than one read lock held. If so, 408154941Sjhb * just drop one and return. 409154941Sjhb */ 410154941Sjhb x = rw->rw_lock; 411154941Sjhb if (RW_READERS(x) > 1) { 412154941Sjhb if (atomic_cmpset_ptr(&rw->rw_lock, x, 413154941Sjhb x - RW_ONE_READER)) { 414167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 415154941Sjhb CTR4(KTR_LOCK, 416154941Sjhb "%s: %p succeeded %p -> %p", 417154941Sjhb __func__, rw, (void *)x, 418154941Sjhb (void *)(x - RW_ONE_READER)); 419154941Sjhb break; 420154941Sjhb } 421154941Sjhb continue; 422167307Sjhb } 423154941Sjhb /* 424154941Sjhb * If there aren't any waiters for a write lock, then try 425154941Sjhb * to drop it quickly. 426154941Sjhb */ 427176017Sjeff if (!(x & RW_LOCK_WAITERS)) { 428176017Sjeff MPASS((x & ~RW_LOCK_WRITE_SPINNER) == 429176017Sjeff RW_READERS_LOCK(1)); 430176017Sjeff if (atomic_cmpset_ptr(&rw->rw_lock, x, RW_UNLOCKED)) { 431167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 432154941Sjhb CTR2(KTR_LOCK, "%s: %p last succeeded", 433154941Sjhb __func__, rw); 434154941Sjhb break; 435154941Sjhb } 436154941Sjhb continue; 437154941Sjhb } 438154941Sjhb /* 439176017Sjeff * Ok, we know we have waiters and we think we are the 440176017Sjeff * last reader, so grab the turnstile lock. 441154941Sjhb */ 442170295Sjeff turnstile_chain_lock(&rw->lock_object); 443176017Sjeff v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER); 444176017Sjeff MPASS(v & RW_LOCK_WAITERS); 445154941Sjhb 446154941Sjhb /* 447154941Sjhb * Try to drop our lock leaving the lock in a unlocked 448154941Sjhb * state. 449154941Sjhb * 450154941Sjhb * If you wanted to do explicit lock handoff you'd have to 451154941Sjhb * do it here. You'd also want to use turnstile_signal() 452154941Sjhb * and you'd have to handle the race where a higher 453154941Sjhb * priority thread blocks on the write lock before the 454154941Sjhb * thread you wakeup actually runs and have the new thread 455154941Sjhb * "steal" the lock. For now it's a lot simpler to just 456154941Sjhb * wakeup all of the waiters. 457154941Sjhb * 458154941Sjhb * As above, if we fail, then another thread might have 459154941Sjhb * acquired a read lock, so drop the turnstile lock and 460154941Sjhb * restart. 461154941Sjhb */ 462176017Sjeff x = RW_UNLOCKED; 463176017Sjeff if (v & RW_LOCK_WRITE_WAITERS) { 464176017Sjeff queue = TS_EXCLUSIVE_QUEUE; 465176017Sjeff x |= (v & RW_LOCK_READ_WAITERS); 466176017Sjeff } else 467176017Sjeff queue = TS_SHARED_QUEUE; 468176017Sjeff if (!atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v, 469176017Sjeff x)) { 470170295Sjeff turnstile_chain_unlock(&rw->lock_object); 471154941Sjhb continue; 472154941Sjhb } 473167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 474154941Sjhb CTR2(KTR_LOCK, "%s: %p last succeeded with waiters", 475154941Sjhb __func__, rw); 476154941Sjhb 477154941Sjhb /* 478154941Sjhb * Ok. The lock is released and all that's left is to 479154941Sjhb * wake up the waiters. Note that the lock might not be 480154941Sjhb * free anymore, but in that case the writers will just 481154941Sjhb * block again if they run before the new lock holder(s) 482154941Sjhb * release the lock. 483154941Sjhb */ 484167787Sjhb ts = turnstile_lookup(&rw->lock_object); 485157846Sjhb MPASS(ts != NULL); 486176017Sjeff turnstile_broadcast(ts, queue); 487154941Sjhb turnstile_unpend(ts, TS_SHARED_LOCK); 488170295Sjeff turnstile_chain_unlock(&rw->lock_object); 489154941Sjhb break; 490154941Sjhb } 491174629Sjeff lock_profile_release_lock(&rw->lock_object); 492154941Sjhb} 493154941Sjhb 494154941Sjhb/* 495154941Sjhb * This function is called when we are unable to obtain a write lock on the 496154941Sjhb * first try. This means that at least one other thread holds either a 497154941Sjhb * read or write lock. 498154941Sjhb */ 499154941Sjhbvoid 500154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line) 501154941Sjhb{ 502170295Sjeff struct turnstile *ts; 503167801Sjhb#ifdef ADAPTIVE_RWLOCKS 504157846Sjhb volatile struct thread *owner; 505176017Sjeff int spintries = 0; 506176017Sjeff int i; 507157851Swkoszek#endif 508171516Sattilio uint64_t waittime = 0; 509176017Sjeff uintptr_t v, x; 510171516Sattilio int contested = 0; 511154941Sjhb 512171052Sattilio if (rw_wlocked(rw)) { 513171052Sattilio KASSERT(rw->lock_object.lo_flags & RW_RECURSE, 514171052Sattilio ("%s: recursing but non-recursive rw %s @ %s:%d\n", 515171052Sattilio __func__, rw->lock_object.lo_name, file, line)); 516171052Sattilio rw->rw_recurse++; 517171052Sattilio if (LOCK_LOG_TEST(&rw->lock_object, 0)) 518171052Sattilio CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw); 519171052Sattilio return; 520171052Sattilio } 521171052Sattilio 522167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 523154941Sjhb CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 524167787Sjhb rw->lock_object.lo_name, (void *)rw->rw_lock, file, line); 525154941Sjhb 526154941Sjhb while (!_rw_write_lock(rw, tid)) { 527174629Sjeff lock_profile_obtain_lock_failed(&rw->lock_object, 528174629Sjeff &contested, &waittime); 529173960Sattilio#ifdef ADAPTIVE_RWLOCKS 530173960Sattilio /* 531173960Sattilio * If the lock is write locked and the owner is 532173960Sattilio * running on another CPU, spin until the owner stops 533173960Sattilio * running or the state of the lock changes. 534173960Sattilio */ 535173960Sattilio v = rw->rw_lock; 536173960Sattilio owner = (struct thread *)RW_OWNER(v); 537173960Sattilio if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) { 538173960Sattilio if (LOCK_LOG_TEST(&rw->lock_object, 0)) 539173960Sattilio CTR3(KTR_LOCK, "%s: spinning on %p held by %p", 540173960Sattilio __func__, rw, owner); 541173960Sattilio while ((struct thread*)RW_OWNER(rw->rw_lock) == owner && 542173960Sattilio TD_IS_RUNNING(owner)) 543173960Sattilio cpu_spinwait(); 544173960Sattilio continue; 545173960Sattilio } 546176017Sjeff if ((v & RW_LOCK_READ) && RW_READERS(v) && spintries < 100) { 547176017Sjeff if (!(v & RW_LOCK_WRITE_SPINNER)) { 548176017Sjeff if (!atomic_cmpset_ptr(&rw->rw_lock, v, 549176017Sjeff v | RW_LOCK_WRITE_SPINNER)) { 550176017Sjeff cpu_spinwait(); 551176017Sjeff continue; 552176017Sjeff } 553176017Sjeff } 554176017Sjeff spintries++; 555176017Sjeff for (i = 100000; i > 0; i--) { 556176017Sjeff if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0) 557176017Sjeff break; 558176017Sjeff cpu_spinwait(); 559176017Sjeff } 560176017Sjeff if (i) 561176017Sjeff continue; 562176017Sjeff } 563173960Sattilio#endif 564170295Sjeff ts = turnstile_trywait(&rw->lock_object); 565154941Sjhb v = rw->rw_lock; 566154941Sjhb 567173960Sattilio#ifdef ADAPTIVE_RWLOCKS 568154941Sjhb /* 569173960Sattilio * If the current owner of the lock is executing on another 570173960Sattilio * CPU quit the hard path and try to spin. 571173960Sattilio */ 572173960Sattilio if (!(v & RW_LOCK_READ)) { 573173960Sattilio owner = (struct thread *)RW_OWNER(v); 574173960Sattilio if (TD_IS_RUNNING(owner)) { 575173960Sattilio turnstile_cancel(ts); 576173960Sattilio cpu_spinwait(); 577173960Sattilio continue; 578173960Sattilio } 579173960Sattilio } 580173960Sattilio#endif 581173960Sattilio /* 582176017Sjeff * If the lock was released while waiting for the turnstile 583176017Sjeff * chain lock retry. 584154941Sjhb */ 585176017Sjeff x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER); 586176017Sjeff if ((v & ~x) == RW_UNLOCKED) { 587176017Sjeff x &= ~RW_LOCK_WRITE_SPINNER; 588176017Sjeff if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) { 589176017Sjeff if (x) 590176017Sjeff turnstile_claim(ts); 591176017Sjeff else 592176017Sjeff turnstile_cancel(ts); 593154941Sjhb break; 594154941Sjhb } 595170295Sjeff turnstile_cancel(ts); 596154941Sjhb cpu_spinwait(); 597154941Sjhb continue; 598154941Sjhb } 599154941Sjhb /* 600154941Sjhb * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to 601154941Sjhb * set it. If we fail to set it, then loop back and try 602154941Sjhb * again. 603154941Sjhb */ 604157826Sjhb if (!(v & RW_LOCK_WRITE_WAITERS)) { 605157826Sjhb if (!atomic_cmpset_ptr(&rw->rw_lock, v, 606157826Sjhb v | RW_LOCK_WRITE_WAITERS)) { 607170295Sjeff turnstile_cancel(ts); 608157826Sjhb cpu_spinwait(); 609157826Sjhb continue; 610157826Sjhb } 611167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 612157826Sjhb CTR2(KTR_LOCK, "%s: %p set write waiters flag", 613157826Sjhb __func__, rw); 614154941Sjhb } 615157846Sjhb /* 616154941Sjhb * We were unable to acquire the lock and the write waiters 617154941Sjhb * flag is set, so we must block on the turnstile. 618154941Sjhb */ 619167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 620154941Sjhb CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 621154941Sjhb rw); 622170295Sjeff turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE); 623167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 624154941Sjhb CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 625154941Sjhb __func__, rw); 626176017Sjeff#ifdef ADAPTIVE_RWLOCKS 627176017Sjeff spintries = 0; 628176017Sjeff#endif 629154941Sjhb } 630171516Sattilio lock_profile_obtain_lock_success(&rw->lock_object, contested, waittime, 631171516Sattilio file, line); 632154941Sjhb} 633154941Sjhb 634154941Sjhb/* 635154941Sjhb * This function is called if the first try at releasing a write lock failed. 636154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at 637154941Sjhb * least one thread is waiting on this lock. 638154941Sjhb */ 639154941Sjhbvoid 640154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line) 641154941Sjhb{ 642154941Sjhb struct turnstile *ts; 643154941Sjhb uintptr_t v; 644154941Sjhb int queue; 645154941Sjhb 646171052Sattilio if (rw_wlocked(rw) && rw_recursed(rw)) { 647176017Sjeff rw->rw_recurse--; 648171052Sattilio if (LOCK_LOG_TEST(&rw->lock_object, 0)) 649171052Sattilio CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw); 650171052Sattilio return; 651171052Sattilio } 652176017Sjeff v = rw->rw_lock; 653171052Sattilio 654154941Sjhb KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS), 655154941Sjhb ("%s: neither of the waiter flags are set", __func__)); 656154941Sjhb 657167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 658154941Sjhb CTR2(KTR_LOCK, "%s: %p contested", __func__, rw); 659154941Sjhb 660170295Sjeff turnstile_chain_lock(&rw->lock_object); 661167787Sjhb ts = turnstile_lookup(&rw->lock_object); 662154941Sjhb 663154941Sjhb MPASS(ts != NULL); 664154941Sjhb 665154941Sjhb /* 666154941Sjhb * Use the same algo as sx locks for now. Prefer waking up shared 667154941Sjhb * waiters if we have any over writers. This is probably not ideal. 668154941Sjhb * 669154941Sjhb * 'v' is the value we are going to write back to rw_lock. If we 670154941Sjhb * have waiters on both queues, we need to preserve the state of 671154941Sjhb * the waiter flag for the queue we don't wake up. For now this is 672154941Sjhb * hardcoded for the algorithm mentioned above. 673154941Sjhb * 674154941Sjhb * In the case of both readers and writers waiting we wakeup the 675154941Sjhb * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a 676154941Sjhb * new writer comes in before a reader it will claim the lock up 677154941Sjhb * above. There is probably a potential priority inversion in 678154941Sjhb * there that could be worked around either by waking both queues 679154941Sjhb * of waiters or doing some complicated lock handoff gymnastics. 680154941Sjhb */ 681157846Sjhb v = RW_UNLOCKED; 682176076Sjeff if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) { 683176076Sjeff queue = TS_EXCLUSIVE_QUEUE; 684176076Sjeff v |= (rw->rw_lock & RW_LOCK_READ_WAITERS); 685176076Sjeff } else 686154941Sjhb queue = TS_SHARED_QUEUE; 687157846Sjhb 688157846Sjhb /* Wake up all waiters for the specific queue. */ 689167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 690154941Sjhb CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw, 691154941Sjhb queue == TS_SHARED_QUEUE ? "read" : "write"); 692154941Sjhb turnstile_broadcast(ts, queue); 693154941Sjhb atomic_store_rel_ptr(&rw->rw_lock, v); 694154941Sjhb turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 695170295Sjeff turnstile_chain_unlock(&rw->lock_object); 696154941Sjhb} 697154941Sjhb 698157882Sjhb/* 699157882Sjhb * Attempt to do a non-blocking upgrade from a read lock to a write 700157882Sjhb * lock. This will only succeed if this thread holds a single read 701157882Sjhb * lock. Returns true if the upgrade succeeded and false otherwise. 702157882Sjhb */ 703157882Sjhbint 704157882Sjhb_rw_try_upgrade(struct rwlock *rw, const char *file, int line) 705157882Sjhb{ 706176017Sjeff uintptr_t v, x, tid; 707170295Sjeff struct turnstile *ts; 708157882Sjhb int success; 709157882Sjhb 710169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 711169394Sjhb ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line)); 712157882Sjhb _rw_assert(rw, RA_RLOCKED, file, line); 713157882Sjhb 714157882Sjhb /* 715157882Sjhb * Attempt to switch from one reader to a writer. If there 716157882Sjhb * are any write waiters, then we will have to lock the 717157882Sjhb * turnstile first to prevent races with another writer 718157882Sjhb * calling turnstile_wait() before we have claimed this 719157882Sjhb * turnstile. So, do the simple case of no waiters first. 720157882Sjhb */ 721157882Sjhb tid = (uintptr_t)curthread; 722176017Sjeff success = 0; 723176017Sjeff for (;;) { 724176017Sjeff v = rw->rw_lock; 725176017Sjeff if (RW_READERS(v) > 1) 726176017Sjeff break; 727176017Sjeff if (!(v & RW_LOCK_WAITERS)) { 728176017Sjeff success = atomic_cmpset_ptr(&rw->rw_lock, v, tid); 729176017Sjeff if (!success) 730176017Sjeff continue; 731176017Sjeff break; 732176017Sjeff } 733157882Sjhb 734176017Sjeff /* 735176017Sjeff * Ok, we think we have waiters, so lock the turnstile. 736176017Sjeff */ 737176017Sjeff ts = turnstile_trywait(&rw->lock_object); 738176017Sjeff v = rw->rw_lock; 739176017Sjeff if (RW_READERS(v) > 1) { 740176017Sjeff turnstile_cancel(ts); 741176017Sjeff break; 742176017Sjeff } 743176017Sjeff /* 744176017Sjeff * Try to switch from one reader to a writer again. This time 745176017Sjeff * we honor the current state of the waiters flags. 746176017Sjeff * If we obtain the lock with the flags set, then claim 747176017Sjeff * ownership of the turnstile. 748176017Sjeff */ 749176017Sjeff x = rw->rw_lock & RW_LOCK_WAITERS; 750176017Sjeff success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x); 751176017Sjeff if (success) { 752176017Sjeff if (x) 753176017Sjeff turnstile_claim(ts); 754176017Sjeff else 755176017Sjeff turnstile_cancel(ts); 756176017Sjeff break; 757176017Sjeff } 758170295Sjeff turnstile_cancel(ts); 759176017Sjeff } 760167787Sjhb LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line); 761176017Sjeff if (success) { 762176017Sjeff curthread->td_rw_rlocks--; 763167787Sjhb WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 764157882Sjhb file, line); 765176017Sjeff } 766157882Sjhb return (success); 767157882Sjhb} 768157882Sjhb 769157882Sjhb/* 770157882Sjhb * Downgrade a write lock into a single read lock. 771157882Sjhb */ 772157882Sjhbvoid 773157882Sjhb_rw_downgrade(struct rwlock *rw, const char *file, int line) 774157882Sjhb{ 775157882Sjhb struct turnstile *ts; 776157882Sjhb uintptr_t tid, v; 777176017Sjeff int rwait, wwait; 778157882Sjhb 779169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 780169394Sjhb ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line)); 781171052Sattilio _rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line); 782171052Sattilio#ifndef INVARIANTS 783171052Sattilio if (rw_recursed(rw)) 784171052Sattilio panic("downgrade of a recursed lock"); 785171052Sattilio#endif 786157882Sjhb 787167787Sjhb WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line); 788157882Sjhb 789157882Sjhb /* 790157882Sjhb * Convert from a writer to a single reader. First we handle 791157882Sjhb * the easy case with no waiters. If there are any waiters, we 792176017Sjeff * lock the turnstile and "disown" the lock. 793157882Sjhb */ 794157882Sjhb tid = (uintptr_t)curthread; 795157882Sjhb if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1))) 796157882Sjhb goto out; 797157882Sjhb 798157882Sjhb /* 799157882Sjhb * Ok, we think we have waiters, so lock the turnstile so we can 800157882Sjhb * read the waiter flags without any races. 801157882Sjhb */ 802170295Sjeff turnstile_chain_lock(&rw->lock_object); 803176017Sjeff v = rw->rw_lock & RW_LOCK_WAITERS; 804176017Sjeff rwait = v & RW_LOCK_READ_WAITERS; 805176017Sjeff wwait = v & RW_LOCK_WRITE_WAITERS; 806176017Sjeff MPASS(rwait | wwait); 807157882Sjhb 808157882Sjhb /* 809176017Sjeff * Downgrade from a write lock while preserving waiters flag 810176017Sjeff * and give up ownership of the turnstile. 811157882Sjhb */ 812167787Sjhb ts = turnstile_lookup(&rw->lock_object); 813157882Sjhb MPASS(ts != NULL); 814176017Sjeff if (!wwait) 815176017Sjeff v &= ~RW_LOCK_READ_WAITERS; 816176017Sjeff atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v); 817176017Sjeff /* 818176017Sjeff * Wake other readers if there are no writers pending. Otherwise they 819176017Sjeff * won't be able to acquire the lock anyway. 820176017Sjeff */ 821176017Sjeff if (rwait && !wwait) { 822157882Sjhb turnstile_broadcast(ts, TS_SHARED_QUEUE); 823157882Sjhb turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 824176017Sjeff } else 825157882Sjhb turnstile_disown(ts); 826170295Sjeff turnstile_chain_unlock(&rw->lock_object); 827157882Sjhbout: 828176017Sjeff curthread->td_rw_rlocks++; 829167787Sjhb LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line); 830157882Sjhb} 831157882Sjhb 832154941Sjhb#ifdef INVARIANT_SUPPORT 833155162Sscottl#ifndef INVARIANTS 834154941Sjhb#undef _rw_assert 835154941Sjhb#endif 836154941Sjhb 837154941Sjhb/* 838154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least 839154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this* 840154941Sjhb * thread owns an rlock. 841154941Sjhb */ 842154941Sjhbvoid 843154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line) 844154941Sjhb{ 845154941Sjhb 846154941Sjhb if (panicstr != NULL) 847154941Sjhb return; 848154941Sjhb switch (what) { 849154941Sjhb case RA_LOCKED: 850171052Sattilio case RA_LOCKED | RA_RECURSED: 851171052Sattilio case RA_LOCKED | RA_NOTRECURSED: 852154941Sjhb case RA_RLOCKED: 853154941Sjhb#ifdef WITNESS 854167787Sjhb witness_assert(&rw->lock_object, what, file, line); 855154941Sjhb#else 856154941Sjhb /* 857154941Sjhb * If some other thread has a write lock or we have one 858154941Sjhb * and are asserting a read lock, fail. Also, if no one 859154941Sjhb * has a lock at all, fail. 860154941Sjhb */ 861155061Sscottl if (rw->rw_lock == RW_UNLOCKED || 862155061Sscottl (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED || 863157826Sjhb rw_wowner(rw) != curthread))) 864154941Sjhb panic("Lock %s not %slocked @ %s:%d\n", 865167787Sjhb rw->lock_object.lo_name, (what == RA_RLOCKED) ? 866154941Sjhb "read " : "", file, line); 867171052Sattilio 868171052Sattilio if (!(rw->rw_lock & RW_LOCK_READ)) { 869171052Sattilio if (rw_recursed(rw)) { 870171052Sattilio if (what & RA_NOTRECURSED) 871171052Sattilio panic("Lock %s recursed @ %s:%d\n", 872171052Sattilio rw->lock_object.lo_name, file, 873171052Sattilio line); 874171052Sattilio } else if (what & RA_RECURSED) 875171052Sattilio panic("Lock %s not recursed @ %s:%d\n", 876171052Sattilio rw->lock_object.lo_name, file, line); 877171052Sattilio } 878154941Sjhb#endif 879154941Sjhb break; 880154941Sjhb case RA_WLOCKED: 881171052Sattilio case RA_WLOCKED | RA_RECURSED: 882171052Sattilio case RA_WLOCKED | RA_NOTRECURSED: 883157826Sjhb if (rw_wowner(rw) != curthread) 884154941Sjhb panic("Lock %s not exclusively locked @ %s:%d\n", 885167787Sjhb rw->lock_object.lo_name, file, line); 886171052Sattilio if (rw_recursed(rw)) { 887171052Sattilio if (what & RA_NOTRECURSED) 888171052Sattilio panic("Lock %s recursed @ %s:%d\n", 889171052Sattilio rw->lock_object.lo_name, file, line); 890171052Sattilio } else if (what & RA_RECURSED) 891171052Sattilio panic("Lock %s not recursed @ %s:%d\n", 892171052Sattilio rw->lock_object.lo_name, file, line); 893154941Sjhb break; 894154941Sjhb case RA_UNLOCKED: 895154941Sjhb#ifdef WITNESS 896167787Sjhb witness_assert(&rw->lock_object, what, file, line); 897154941Sjhb#else 898154941Sjhb /* 899154941Sjhb * If we hold a write lock fail. We can't reliably check 900154941Sjhb * to see if we hold a read lock or not. 901154941Sjhb */ 902157826Sjhb if (rw_wowner(rw) == curthread) 903154941Sjhb panic("Lock %s exclusively locked @ %s:%d\n", 904167787Sjhb rw->lock_object.lo_name, file, line); 905154941Sjhb#endif 906154941Sjhb break; 907154941Sjhb default: 908154941Sjhb panic("Unknown rw lock assertion: %d @ %s:%d", what, file, 909154941Sjhb line); 910154941Sjhb } 911154941Sjhb} 912154941Sjhb#endif /* INVARIANT_SUPPORT */ 913154941Sjhb 914154941Sjhb#ifdef DDB 915154941Sjhbvoid 916154941Sjhbdb_show_rwlock(struct lock_object *lock) 917154941Sjhb{ 918154941Sjhb struct rwlock *rw; 919154941Sjhb struct thread *td; 920154941Sjhb 921154941Sjhb rw = (struct rwlock *)lock; 922154941Sjhb 923154941Sjhb db_printf(" state: "); 924154941Sjhb if (rw->rw_lock == RW_UNLOCKED) 925154941Sjhb db_printf("UNLOCKED\n"); 926169394Sjhb else if (rw->rw_lock == RW_DESTROYED) { 927169394Sjhb db_printf("DESTROYED\n"); 928169394Sjhb return; 929169394Sjhb } else if (rw->rw_lock & RW_LOCK_READ) 930167504Sjhb db_printf("RLOCK: %ju locks\n", 931167504Sjhb (uintmax_t)(RW_READERS(rw->rw_lock))); 932154941Sjhb else { 933157826Sjhb td = rw_wowner(rw); 934154941Sjhb db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 935173600Sjulian td->td_tid, td->td_proc->p_pid, td->td_name); 936171052Sattilio if (rw_recursed(rw)) 937171052Sattilio db_printf(" recursed: %u\n", rw->rw_recurse); 938154941Sjhb } 939154941Sjhb db_printf(" waiters: "); 940154941Sjhb switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) { 941154941Sjhb case RW_LOCK_READ_WAITERS: 942154941Sjhb db_printf("readers\n"); 943154941Sjhb break; 944154941Sjhb case RW_LOCK_WRITE_WAITERS: 945154941Sjhb db_printf("writers\n"); 946154941Sjhb break; 947154941Sjhb case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS: 948167492Sjhb db_printf("readers and writers\n"); 949154941Sjhb break; 950154941Sjhb default: 951154941Sjhb db_printf("none\n"); 952154941Sjhb break; 953154941Sjhb } 954154941Sjhb} 955154941Sjhb 956154941Sjhb#endif 957