2 * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER 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
31 * Machine independent bits of reader/writer lock implementation.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_no_adaptive_rwlocks.h"
40 #include <sys/param.h>
43 #include <sys/mutex.h>
45 #include <sys/rwlock.h>
46 #include <sys/systm.h>
47 #include <sys/turnstile.h>
49 #include <machine/cpu.h>
51 CTASSERT((RW_RECURSE & LO_CLASSFLAGS) == RW_RECURSE);
53 #if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
54 #define ADAPTIVE_RWLOCKS
60 static void db_show_rwlock(struct lock_object *lock);
62 static void lock_rw(struct lock_object *lock, int how);
63 static int unlock_rw(struct lock_object *lock);
65 struct lock_class lock_class_rw = {
67 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
69 .lc_ddb_show = db_show_rwlock,
72 .lc_unlock = unlock_rw,
76 * Return a pointer to the owning thread if the lock is write-locked or
77 * NULL if the lock is unlocked or read-locked.
79 #define rw_wowner(rw) \
80 ((rw)->rw_lock & RW_LOCK_READ ? NULL : \
81 (struct thread *)RW_OWNER((rw)->rw_lock))
84 * Returns if a write owner is recursed. Write ownership is not assured
85 * here and should be previously checked.
87 #define rw_recursed(rw) ((rw)->rw_recurse != 0)
90 * Return true if curthread helds the lock.
92 #define rw_wlocked(rw) (rw_wowner((rw)) == curthread)
95 * Return a pointer to the owning thread for this lock who should receive
96 * any priority lent by threads that block on this lock. Currently this
97 * is identical to rw_wowner().
99 #define rw_owner(rw) rw_wowner(rw)
102 #define _rw_assert(rw, what, file, line)
106 lock_rw(struct lock_object *lock, int how)
110 rw = (struct rwlock *)lock;
118 unlock_rw(struct lock_object *lock)
122 rw = (struct rwlock *)lock;
123 rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
124 if (rw->rw_lock & RW_LOCK_READ) {
134 rw_init_flags(struct rwlock *rw, const char *name, int opts)
138 MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
141 flags = LO_UPGRADABLE | LO_RECURSABLE;
144 if (opts & RW_NOPROFILE)
145 flags |= LO_NOPROFILE;
146 if (!(opts & RW_NOWITNESS))
150 flags |= opts & RW_RECURSE;
152 rw->rw_lock = RW_UNLOCKED;
154 lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
158 rw_destroy(struct rwlock *rw)
161 KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked"));
162 KASSERT(rw->rw_recurse == 0, ("rw lock still recursed"));
163 rw->rw_lock = RW_DESTROYED;
164 lock_destroy(&rw->lock_object);
168 rw_sysinit(void *arg)
170 struct rw_args *args = arg;
172 rw_init(args->ra_rw, args->ra_desc);
176 rw_wowned(struct rwlock *rw)
179 return (rw_wowner(rw) == curthread);
183 _rw_wlock(struct rwlock *rw, const char *file, int line)
186 MPASS(curthread != NULL);
187 KASSERT(rw->rw_lock != RW_DESTROYED,
188 ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
189 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
191 __rw_wlock(rw, curthread, file, line);
192 LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
193 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
194 curthread->td_locks++;
198 _rw_try_wlock(struct rwlock *rw, const char *file, int line)
202 KASSERT(rw->rw_lock != RW_DESTROYED,
203 ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
205 if (rw_wlocked(rw) && (rw->lock_object.lo_flags & RW_RECURSE) != 0) {
209 rval = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_UNLOCKED,
210 (uintptr_t)curthread);
212 LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
214 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
216 curthread->td_locks++;
222 _rw_wunlock(struct rwlock *rw, const char *file, int line)
225 MPASS(curthread != NULL);
226 KASSERT(rw->rw_lock != RW_DESTROYED,
227 ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
228 _rw_assert(rw, RA_WLOCKED, file, line);
229 curthread->td_locks--;
230 WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
231 LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
233 if (!rw_recursed(rw))
234 lock_profile_release_lock(&rw->lock_object);
235 __rw_wunlock(rw, curthread, file, line);
239 _rw_rlock(struct rwlock *rw, const char *file, int line)
241 struct turnstile *ts;
242 #ifdef ADAPTIVE_RWLOCKS
243 volatile struct thread *owner;
245 #ifdef LOCK_PROFILING_SHARED
246 uint64_t waittime = 0;
251 KASSERT(rw->rw_lock != RW_DESTROYED,
252 ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
253 KASSERT(rw_wowner(rw) != curthread,
254 ("%s (%s): wlock already held @ %s:%d", __func__,
255 rw->lock_object.lo_name, file, line));
256 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line);
259 * Note that we don't make any attempt to try to block read
260 * locks once a writer has blocked on the lock. The reason is
261 * that we currently allow for read locks to recurse and we
262 * don't keep track of all the holders of read locks. Thus, if
263 * we were to block readers once a writer blocked and a reader
264 * tried to recurse on their reader lock after a writer had
265 * blocked we would end up in a deadlock since the reader would
266 * be blocked on the writer, and the writer would be blocked
267 * waiting for the reader to release its original read lock.
271 * Handle the easy case. If no other thread has a write
272 * lock, then try to bump up the count of read locks. Note
273 * that we have to preserve the current state of the
274 * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a
275 * read lock, then rw_lock must have changed, so restart
276 * the loop. Note that this handles the case of a
277 * completely unlocked rwlock since such a lock is encoded
278 * as a read lock with no waiters.
281 if (x & RW_LOCK_READ) {
284 * The RW_LOCK_READ_WAITERS flag should only be set
285 * if another thread currently holds a write lock,
286 * and in that case RW_LOCK_READ should be clear.
288 MPASS((x & RW_LOCK_READ_WAITERS) == 0);
289 if (atomic_cmpset_acq_ptr(&rw->rw_lock, x,
290 x + RW_ONE_READER)) {
291 #ifdef LOCK_PROFILING_SHARED
292 if (RW_READERS(x) == 0)
293 lock_profile_obtain_lock_success(
294 &rw->lock_object, contested,
295 waittime, file, line);
297 if (LOCK_LOG_TEST(&rw->lock_object, 0))
299 "%s: %p succeed %p -> %p", __func__,
301 (void *)(x + RW_ONE_READER));
308 #ifdef ADAPTIVE_RWLOCKS
310 * If the owner is running on another CPU, spin until
311 * the owner stops running or the state of the lock
314 owner = (struct thread *)RW_OWNER(x);
315 if (TD_IS_RUNNING(owner)) {
316 if (LOCK_LOG_TEST(&rw->lock_object, 0))
317 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
318 __func__, rw, owner);
319 #ifdef LOCK_PROFILING_SHARED
320 lock_profile_obtain_lock_failed(&rw->lock_object,
321 &contested, &waittime);
323 while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
324 TD_IS_RUNNING(owner))
331 * Okay, now it's the hard case. Some other thread already
332 * has a write lock, so acquire the turnstile lock so we can
333 * begin the process of blocking.
335 ts = turnstile_trywait(&rw->lock_object);
338 * The lock might have been released while we spun, so
339 * recheck its state and restart the loop if there is no
340 * longer a write lock.
343 if (x & RW_LOCK_READ) {
344 turnstile_cancel(ts);
349 #ifdef ADAPTIVE_RWLOCKS
351 * If the current owner of the lock is executing on another
352 * CPU quit the hard path and try to spin.
354 owner = (struct thread *)RW_OWNER(x);
355 if (TD_IS_RUNNING(owner)) {
356 turnstile_cancel(ts);
363 * Ok, it's still a write lock. If the RW_LOCK_READ_WAITERS
364 * flag is already set, then we can go ahead and block. If
365 * it is not set then try to set it. If we fail to set it
366 * drop the turnstile lock and restart the loop.
368 if (!(x & RW_LOCK_READ_WAITERS)) {
369 if (!atomic_cmpset_ptr(&rw->rw_lock, x,
370 x | RW_LOCK_READ_WAITERS)) {
371 turnstile_cancel(ts);
375 if (LOCK_LOG_TEST(&rw->lock_object, 0))
376 CTR2(KTR_LOCK, "%s: %p set read waiters flag",
381 * We were unable to acquire the lock and the read waiters
382 * flag is set, so we must block on the turnstile.
384 if (LOCK_LOG_TEST(&rw->lock_object, 0))
385 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
387 #ifdef LOCK_PROFILING_SHARED
388 lock_profile_obtain_lock_failed(&rw->lock_object, &contested,
391 turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
392 if (LOCK_LOG_TEST(&rw->lock_object, 0))
393 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
398 * TODO: acquire "owner of record" here. Here be turnstile dragons
399 * however. turnstiles don't like owners changing between calls to
400 * turnstile_wait() currently.
403 LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
404 WITNESS_LOCK(&rw->lock_object, 0, file, line);
405 curthread->td_locks++;
409 _rw_try_rlock(struct rwlock *rw, const char *file, int line)
415 KASSERT(rw->rw_lock != RW_DESTROYED,
416 ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
417 if (!(x & RW_LOCK_READ))
419 if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, x + RW_ONE_READER)) {
420 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
422 WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
423 curthread->td_locks++;
428 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
433 _rw_runlock(struct rwlock *rw, const char *file, int line)
435 struct turnstile *ts;
438 KASSERT(rw->rw_lock != RW_DESTROYED,
439 ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
440 _rw_assert(rw, RA_RLOCKED, file, line);
441 curthread->td_locks--;
442 WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
443 LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
445 /* TODO: drop "owner of record" here. */
449 * See if there is more than one read lock held. If so,
450 * just drop one and return.
453 if (RW_READERS(x) > 1) {
454 if (atomic_cmpset_ptr(&rw->rw_lock, x,
455 x - RW_ONE_READER)) {
456 if (LOCK_LOG_TEST(&rw->lock_object, 0))
458 "%s: %p succeeded %p -> %p",
459 __func__, rw, (void *)x,
460 (void *)(x - RW_ONE_READER));
468 * We should never have read waiters while at least one
469 * thread holds a read lock. (See note above)
471 KASSERT(!(x & RW_LOCK_READ_WAITERS),
472 ("%s: waiting readers", __func__));
473 #ifdef LOCK_PROFILING_SHARED
474 lock_profile_release_lock(&rw->lock_object);
478 * If there aren't any waiters for a write lock, then try
479 * to drop it quickly.
481 if (!(x & RW_LOCK_WRITE_WAITERS)) {
484 * There shouldn't be any flags set and we should
485 * be the only read lock. If we fail to release
486 * the single read lock, then another thread might
487 * have just acquired a read lock, so go back up
488 * to the multiple read locks case.
490 MPASS(x == RW_READERS_LOCK(1));
491 if (atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1),
493 if (LOCK_LOG_TEST(&rw->lock_object, 0))
494 CTR2(KTR_LOCK, "%s: %p last succeeded",
502 * There should just be one reader with one or more
505 MPASS(x == (RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS));
508 * Ok, we know we have a waiting writer and we think we
509 * are the last reader, so grab the turnstile lock.
511 turnstile_chain_lock(&rw->lock_object);
514 * Try to drop our lock leaving the lock in a unlocked
517 * If you wanted to do explicit lock handoff you'd have to
518 * do it here. You'd also want to use turnstile_signal()
519 * and you'd have to handle the race where a higher
520 * priority thread blocks on the write lock before the
521 * thread you wakeup actually runs and have the new thread
522 * "steal" the lock. For now it's a lot simpler to just
523 * wakeup all of the waiters.
525 * As above, if we fail, then another thread might have
526 * acquired a read lock, so drop the turnstile lock and
529 if (!atomic_cmpset_ptr(&rw->rw_lock,
530 RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS, RW_UNLOCKED)) {
531 turnstile_chain_unlock(&rw->lock_object);
534 if (LOCK_LOG_TEST(&rw->lock_object, 0))
535 CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
539 * Ok. The lock is released and all that's left is to
540 * wake up the waiters. Note that the lock might not be
541 * free anymore, but in that case the writers will just
542 * block again if they run before the new lock holder(s)
545 ts = turnstile_lookup(&rw->lock_object);
547 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
548 turnstile_unpend(ts, TS_SHARED_LOCK);
549 turnstile_chain_unlock(&rw->lock_object);
555 * This function is called when we are unable to obtain a write lock on the
556 * first try. This means that at least one other thread holds either a
557 * read or write lock.
560 _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
562 struct turnstile *ts;
563 #ifdef ADAPTIVE_RWLOCKS
564 volatile struct thread *owner;
566 uint64_t waittime = 0;
570 if (rw_wlocked(rw)) {
571 KASSERT(rw->lock_object.lo_flags & RW_RECURSE,
572 ("%s: recursing but non-recursive rw %s @ %s:%d\n",
573 __func__, rw->lock_object.lo_name, file, line));
575 atomic_set_ptr(&rw->rw_lock, RW_LOCK_RECURSED);
576 if (LOCK_LOG_TEST(&rw->lock_object, 0))
577 CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
581 if (LOCK_LOG_TEST(&rw->lock_object, 0))
582 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
583 rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
585 while (!_rw_write_lock(rw, tid)) {
586 #ifdef ADAPTIVE_RWLOCKS
588 * If the lock is write locked and the owner is
589 * running on another CPU, spin until the owner stops
590 * running or the state of the lock changes.
593 owner = (struct thread *)RW_OWNER(v);
594 if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
595 if (LOCK_LOG_TEST(&rw->lock_object, 0))
596 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
597 __func__, rw, owner);
598 lock_profile_obtain_lock_failed(&rw->lock_object,
599 &contested, &waittime);
600 while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
601 TD_IS_RUNNING(owner))
607 ts = turnstile_trywait(&rw->lock_object);
611 * If the lock was released while spinning on the
612 * turnstile chain lock, try again.
614 if (v == RW_UNLOCKED) {
615 turnstile_cancel(ts);
620 #ifdef ADAPTIVE_RWLOCKS
622 * If the current owner of the lock is executing on another
623 * CPU quit the hard path and try to spin.
625 if (!(v & RW_LOCK_READ)) {
626 owner = (struct thread *)RW_OWNER(v);
627 if (TD_IS_RUNNING(owner)) {
628 turnstile_cancel(ts);
636 * If the lock was released by a writer with both readers
637 * and writers waiting and a reader hasn't woken up and
638 * acquired the lock yet, rw_lock will be set to the
639 * value RW_UNLOCKED | RW_LOCK_WRITE_WAITERS. If we see
640 * that value, try to acquire it once. Note that we have
641 * to preserve the RW_LOCK_WRITE_WAITERS flag as there are
642 * other writers waiting still. If we fail, restart the
645 if (v == (RW_UNLOCKED | RW_LOCK_WRITE_WAITERS)) {
646 if (atomic_cmpset_acq_ptr(&rw->rw_lock,
647 RW_UNLOCKED | RW_LOCK_WRITE_WAITERS,
648 tid | RW_LOCK_WRITE_WAITERS)) {
650 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
654 turnstile_cancel(ts);
660 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
661 * set it. If we fail to set it, then loop back and try
664 if (!(v & RW_LOCK_WRITE_WAITERS)) {
665 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
666 v | RW_LOCK_WRITE_WAITERS)) {
667 turnstile_cancel(ts);
671 if (LOCK_LOG_TEST(&rw->lock_object, 0))
672 CTR2(KTR_LOCK, "%s: %p set write waiters flag",
677 * We were unable to acquire the lock and the write waiters
678 * flag is set, so we must block on the turnstile.
680 if (LOCK_LOG_TEST(&rw->lock_object, 0))
681 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
683 lock_profile_obtain_lock_failed(&rw->lock_object, &contested,
685 turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
686 if (LOCK_LOG_TEST(&rw->lock_object, 0))
687 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
690 lock_profile_obtain_lock_success(&rw->lock_object, contested, waittime,
695 * This function is called if the first try at releasing a write lock failed.
696 * This means that one of the 2 waiter bits must be set indicating that at
697 * least one thread is waiting on this lock.
700 _rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
702 struct turnstile *ts;
706 if (rw_wlocked(rw) && rw_recursed(rw)) {
707 if ((--rw->rw_recurse) == 0)
708 atomic_clear_ptr(&rw->rw_lock, RW_LOCK_RECURSED);
709 if (LOCK_LOG_TEST(&rw->lock_object, 0))
710 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
714 KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
715 ("%s: neither of the waiter flags are set", __func__));
717 if (LOCK_LOG_TEST(&rw->lock_object, 0))
718 CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
720 turnstile_chain_lock(&rw->lock_object);
721 ts = turnstile_lookup(&rw->lock_object);
726 * Use the same algo as sx locks for now. Prefer waking up shared
727 * waiters if we have any over writers. This is probably not ideal.
729 * 'v' is the value we are going to write back to rw_lock. If we
730 * have waiters on both queues, we need to preserve the state of
731 * the waiter flag for the queue we don't wake up. For now this is
732 * hardcoded for the algorithm mentioned above.
734 * In the case of both readers and writers waiting we wakeup the
735 * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a
736 * new writer comes in before a reader it will claim the lock up
737 * above. There is probably a potential priority inversion in
738 * there that could be worked around either by waking both queues
739 * of waiters or doing some complicated lock handoff gymnastics.
742 if (rw->rw_lock & RW_LOCK_READ_WAITERS) {
743 queue = TS_SHARED_QUEUE;
744 v |= (rw->rw_lock & RW_LOCK_WRITE_WAITERS);
746 queue = TS_EXCLUSIVE_QUEUE;
748 /* Wake up all waiters for the specific queue. */
749 if (LOCK_LOG_TEST(&rw->lock_object, 0))
750 CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
751 queue == TS_SHARED_QUEUE ? "read" : "write");
752 turnstile_broadcast(ts, queue);
753 atomic_store_rel_ptr(&rw->rw_lock, v);
754 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
755 turnstile_chain_unlock(&rw->lock_object);
759 * Attempt to do a non-blocking upgrade from a read lock to a write
760 * lock. This will only succeed if this thread holds a single read
761 * lock. Returns true if the upgrade succeeded and false otherwise.
764 _rw_try_upgrade(struct rwlock *rw, const char *file, int line)
767 struct turnstile *ts;
770 KASSERT(rw->rw_lock != RW_DESTROYED,
771 ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
772 _rw_assert(rw, RA_RLOCKED, file, line);
775 * Attempt to switch from one reader to a writer. If there
776 * are any write waiters, then we will have to lock the
777 * turnstile first to prevent races with another writer
778 * calling turnstile_wait() before we have claimed this
779 * turnstile. So, do the simple case of no waiters first.
781 tid = (uintptr_t)curthread;
782 if (!(rw->rw_lock & RW_LOCK_WRITE_WAITERS)) {
783 success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1),
789 * Ok, we think we have write waiters, so lock the
792 ts = turnstile_trywait(&rw->lock_object);
795 * Try to switch from one reader to a writer again. This time
796 * we honor the current state of the RW_LOCK_WRITE_WAITERS
797 * flag. If we obtain the lock with the flag set, then claim
798 * ownership of the turnstile.
800 v = rw->rw_lock & RW_LOCK_WRITE_WAITERS;
801 success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
806 turnstile_cancel(ts);
808 LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
810 WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
816 * Downgrade a write lock into a single read lock.
819 _rw_downgrade(struct rwlock *rw, const char *file, int line)
821 struct turnstile *ts;
824 KASSERT(rw->rw_lock != RW_DESTROYED,
825 ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
826 _rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
829 panic("downgrade of a recursed lock");
832 WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
835 * Convert from a writer to a single reader. First we handle
836 * the easy case with no waiters. If there are any waiters, we
837 * lock the turnstile, "disown" the lock, and awaken any read
840 tid = (uintptr_t)curthread;
841 if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
845 * Ok, we think we have waiters, so lock the turnstile so we can
846 * read the waiter flags without any races.
848 turnstile_chain_lock(&rw->lock_object);
850 MPASS(v & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS));
853 * Downgrade from a write lock while preserving
854 * RW_LOCK_WRITE_WAITERS and give up ownership of the
855 * turnstile. If there are any read waiters, wake them up.
857 ts = turnstile_lookup(&rw->lock_object);
859 if (v & RW_LOCK_READ_WAITERS)
860 turnstile_broadcast(ts, TS_SHARED_QUEUE);
861 atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) |
862 (v & RW_LOCK_WRITE_WAITERS));
863 if (v & RW_LOCK_READ_WAITERS)
864 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
866 turnstile_disown(ts);
867 turnstile_chain_unlock(&rw->lock_object);
869 LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
872 #ifdef INVARIANT_SUPPORT
878 * In the non-WITNESS case, rw_assert() can only detect that at least
879 * *some* thread owns an rlock, but it cannot guarantee that *this*
880 * thread owns an rlock.
883 _rw_assert(struct rwlock *rw, int what, const char *file, int line)
886 if (panicstr != NULL)
890 case RA_LOCKED | RA_RECURSED:
891 case RA_LOCKED | RA_NOTRECURSED:
894 witness_assert(&rw->lock_object, what, file, line);
897 * If some other thread has a write lock or we have one
898 * and are asserting a read lock, fail. Also, if no one
899 * has a lock at all, fail.
901 if (rw->rw_lock == RW_UNLOCKED ||
902 (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
903 rw_wowner(rw) != curthread)))
904 panic("Lock %s not %slocked @ %s:%d\n",
905 rw->lock_object.lo_name, (what == RA_RLOCKED) ?
906 "read " : "", file, line);
908 if (!(rw->rw_lock & RW_LOCK_READ)) {
909 if (rw_recursed(rw)) {
910 if (what & RA_NOTRECURSED)
911 panic("Lock %s recursed @ %s:%d\n",
912 rw->lock_object.lo_name, file,
914 } else if (what & RA_RECURSED)
915 panic("Lock %s not recursed @ %s:%d\n",
916 rw->lock_object.lo_name, file, line);
921 case RA_WLOCKED | RA_RECURSED:
922 case RA_WLOCKED | RA_NOTRECURSED:
923 if (rw_wowner(rw) != curthread)
924 panic("Lock %s not exclusively locked @ %s:%d\n",
925 rw->lock_object.lo_name, file, line);
926 if (rw_recursed(rw)) {
927 if (what & RA_NOTRECURSED)
928 panic("Lock %s recursed @ %s:%d\n",
929 rw->lock_object.lo_name, file, line);
930 } else if (what & RA_RECURSED)
931 panic("Lock %s not recursed @ %s:%d\n",
932 rw->lock_object.lo_name, file, line);
936 witness_assert(&rw->lock_object, what, file, line);
939 * If we hold a write lock fail. We can't reliably check
940 * to see if we hold a read lock or not.
942 if (rw_wowner(rw) == curthread)
943 panic("Lock %s exclusively locked @ %s:%d\n",
944 rw->lock_object.lo_name, file, line);
948 panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
952 #endif /* INVARIANT_SUPPORT */
956 db_show_rwlock(struct lock_object *lock)
961 rw = (struct rwlock *)lock;
963 db_printf(" state: ");
964 if (rw->rw_lock == RW_UNLOCKED)
965 db_printf("UNLOCKED\n");
966 else if (rw->rw_lock == RW_DESTROYED) {
967 db_printf("DESTROYED\n");
969 } else if (rw->rw_lock & RW_LOCK_READ)
970 db_printf("RLOCK: %ju locks\n",
971 (uintmax_t)(RW_READERS(rw->rw_lock)));
974 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
975 td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
977 db_printf(" recursed: %u\n", rw->rw_recurse);
979 db_printf(" waiters: ");
980 switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
981 case RW_LOCK_READ_WAITERS:
982 db_printf("readers\n");
984 case RW_LOCK_WRITE_WAITERS:
985 db_printf("writers\n");
987 case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
988 db_printf("readers and writers\n");