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>
42 #include <sys/kernel.h>
44 #include <sys/mutex.h>
46 #include <sys/rwlock.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/turnstile.h>
51 #include <machine/cpu.h>
53 CTASSERT((RW_RECURSE
& LO_CLASSFLAGS
) == RW_RECURSE
);
55 #if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
56 #define ADAPTIVE_RWLOCKS
59 #ifdef ADAPTIVE_RWLOCKS
60 static int rowner_retries
= 10;
61 static int rowner_loops
= 10000;
62 SYSCTL_NODE(_debug
, OID_AUTO
, rwlock
, CTLFLAG_RD
, NULL
, "rwlock debugging");
63 SYSCTL_INT(_debug_rwlock
, OID_AUTO
, retry
, CTLFLAG_RW
, &rowner_retries
, 0, "");
64 SYSCTL_INT(_debug_rwlock
, OID_AUTO
, loops
, CTLFLAG_RW
, &rowner_loops
, 0, "");
70 static void db_show_rwlock(struct lock_object
*lock
);
72 static void assert_rw(struct lock_object
*lock
, int what
);
73 static void lock_rw(struct lock_object
*lock
, int how
);
74 static int unlock_rw(struct lock_object
*lock
);
76 struct lock_class lock_class_rw
= {
78 .lc_flags
= LC_SLEEPLOCK
| LC_RECURSABLE
| LC_UPGRADABLE
,
79 .lc_assert
= assert_rw
,
81 .lc_ddb_show
= db_show_rwlock
,
84 .lc_unlock
= unlock_rw
,
88 * Return a pointer to the owning thread if the lock is write-locked or
89 * NULL if the lock is unlocked or read-locked.
91 #define rw_wowner(rw) \
92 ((rw)->rw_lock & RW_LOCK_READ ? NULL : \
93 (struct thread *)RW_OWNER((rw)->rw_lock))
96 * Returns if a write owner is recursed. Write ownership is not assured
97 * here and should be previously checked.
99 #define rw_recursed(rw) ((rw)->rw_recurse != 0)
102 * Return true if curthread helds the lock.
104 #define rw_wlocked(rw) (rw_wowner((rw)) == curthread)
107 * Return a pointer to the owning thread for this lock who should receive
108 * any priority lent by threads that block on this lock. Currently this
109 * is identical to rw_wowner().
111 #define rw_owner(rw) rw_wowner(rw)
114 #define _rw_assert(rw, what, file, line)
118 assert_rw(struct lock_object
*lock
, int what
)
121 rw_assert((struct rwlock
*)lock
, what
);
125 lock_rw(struct lock_object
*lock
, int how
)
129 rw
= (struct rwlock
*)lock
;
137 unlock_rw(struct lock_object
*lock
)
141 rw
= (struct rwlock
*)lock
;
142 rw_assert(rw
, RA_LOCKED
| LA_NOTRECURSED
);
143 if (rw
->rw_lock
& RW_LOCK_READ
) {
153 rw_init_flags(struct rwlock
*rw
, const char *name
, int opts
)
157 MPASS((opts
& ~(RW_DUPOK
| RW_NOPROFILE
| RW_NOWITNESS
| RW_QUIET
|
160 flags
= LO_UPGRADABLE
| LO_RECURSABLE
;
163 if (opts
& RW_NOPROFILE
)
164 flags
|= LO_NOPROFILE
;
165 if (!(opts
& RW_NOWITNESS
))
169 flags
|= opts
& RW_RECURSE
;
171 rw
->rw_lock
= RW_UNLOCKED
;
173 lock_init(&rw
->lock_object
, &lock_class_rw
, name
, NULL
, flags
);
177 rw_destroy(struct rwlock
*rw
)
180 KASSERT(rw
->rw_lock
== RW_UNLOCKED
, ("rw lock not unlocked"));
181 KASSERT(rw
->rw_recurse
== 0, ("rw lock still recursed"));
182 rw
->rw_lock
= RW_DESTROYED
;
183 lock_destroy(&rw
->lock_object
);
187 rw_sysinit(void *arg
)
189 struct rw_args
*args
= arg
;
191 rw_init(args
->ra_rw
, args
->ra_desc
);
195 rw_wowned(struct rwlock
*rw
)
198 return (rw_wowner(rw
) == curthread
);
202 _rw_wlock(struct rwlock
*rw
, const char *file
, int line
)
205 MPASS(curthread
!= NULL
);
206 KASSERT(rw
->rw_lock
!= RW_DESTROYED
,
207 ("rw_wlock() of destroyed rwlock @ %s:%d", file
, line
));
208 WITNESS_CHECKORDER(&rw
->lock_object
, LOP_NEWORDER
| LOP_EXCLUSIVE
, file
,
210 __rw_wlock(rw
, curthread
, file
, line
);
211 LOCK_LOG_LOCK("WLOCK", &rw
->lock_object
, 0, rw
->rw_recurse
, file
, line
);
212 WITNESS_LOCK(&rw
->lock_object
, LOP_EXCLUSIVE
, file
, line
);
213 curthread
->td_locks
++;
217 _rw_try_wlock(struct rwlock
*rw
, const char *file
, int line
)
221 KASSERT(rw
->rw_lock
!= RW_DESTROYED
,
222 ("rw_try_wlock() of destroyed rwlock @ %s:%d", file
, line
));
224 if (rw_wlocked(rw
) && (rw
->lock_object
.lo_flags
& RW_RECURSE
) != 0) {
228 rval
= atomic_cmpset_acq_ptr(&rw
->rw_lock
, RW_UNLOCKED
,
229 (uintptr_t)curthread
);
231 LOCK_LOG_TRY("WLOCK", &rw
->lock_object
, 0, rval
, file
, line
);
233 WITNESS_LOCK(&rw
->lock_object
, LOP_EXCLUSIVE
| LOP_TRYLOCK
,
235 curthread
->td_locks
++;
241 _rw_wunlock(struct rwlock
*rw
, const char *file
, int line
)
244 MPASS(curthread
!= NULL
);
245 KASSERT(rw
->rw_lock
!= RW_DESTROYED
,
246 ("rw_wunlock() of destroyed rwlock @ %s:%d", file
, line
));
247 _rw_assert(rw
, RA_WLOCKED
, file
, line
);
248 curthread
->td_locks
--;
249 WITNESS_UNLOCK(&rw
->lock_object
, LOP_EXCLUSIVE
, file
, line
);
250 LOCK_LOG_LOCK("WUNLOCK", &rw
->lock_object
, 0, rw
->rw_recurse
, file
,
252 if (!rw_recursed(rw
))
253 lock_profile_release_lock(&rw
->lock_object
);
254 __rw_wunlock(rw
, curthread
, file
, line
);
257 * Determines whether a new reader can acquire a lock. Succeeds if the
258 * reader already owns a read lock and the lock is locked for read to
259 * prevent deadlock from reader recursion. Also succeeds if the lock
260 * is unlocked and has no writer waiters or spinners. Failing otherwise
261 * prioritizes writers before readers.
263 #define RW_CAN_READ(_rw) \
264 ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) & \
265 (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) == \
269 _rw_rlock(struct rwlock
*rw
, const char *file
, int line
)
271 struct turnstile
*ts
;
272 #ifdef ADAPTIVE_RWLOCKS
273 volatile struct thread
*owner
;
277 uint64_t waittime
= 0;
281 KASSERT(rw
->rw_lock
!= RW_DESTROYED
,
282 ("rw_rlock() of destroyed rwlock @ %s:%d", file
, line
));
283 KASSERT(rw_wowner(rw
) != curthread
,
284 ("%s (%s): wlock already held @ %s:%d", __func__
,
285 rw
->lock_object
.lo_name
, file
, line
));
286 WITNESS_CHECKORDER(&rw
->lock_object
, LOP_NEWORDER
, file
, line
);
290 * Handle the easy case. If no other thread has a write
291 * lock, then try to bump up the count of read locks. Note
292 * that we have to preserve the current state of the
293 * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a
294 * read lock, then rw_lock must have changed, so restart
295 * the loop. Note that this handles the case of a
296 * completely unlocked rwlock since such a lock is encoded
297 * as a read lock with no waiters.
300 if (RW_CAN_READ(v
)) {
302 * The RW_LOCK_READ_WAITERS flag should only be set
303 * if the lock has been unlocked and write waiters
306 if (atomic_cmpset_acq_ptr(&rw
->rw_lock
, v
,
307 v
+ RW_ONE_READER
)) {
308 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
310 "%s: %p succeed %p -> %p", __func__
,
312 (void *)(v
+ RW_ONE_READER
));
318 lock_profile_obtain_lock_failed(&rw
->lock_object
,
319 &contested
, &waittime
);
321 #ifdef ADAPTIVE_RWLOCKS
323 * If the owner is running on another CPU, spin until
324 * the owner stops running or the state of the lock
327 if ((v
& RW_LOCK_READ
) == 0) {
328 owner
= (struct thread
*)RW_OWNER(v
);
329 if (TD_IS_RUNNING(owner
)) {
330 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
332 "%s: spinning on %p held by %p",
333 __func__
, rw
, owner
);
334 while ((struct thread
*)RW_OWNER(rw
->rw_lock
) ==
335 owner
&& TD_IS_RUNNING(owner
))
339 } else if (spintries
< rowner_retries
) {
341 for (i
= 0; i
< rowner_loops
; i
++) {
343 if ((v
& RW_LOCK_READ
) == 0 || RW_CAN_READ(v
))
347 if (i
!= rowner_loops
)
353 * Okay, now it's the hard case. Some other thread already
354 * has a write lock or there are write waiters present,
355 * acquire the turnstile lock so we can begin the process
358 ts
= turnstile_trywait(&rw
->lock_object
);
361 * The lock might have been released while we spun, so
362 * recheck its state and restart the loop if needed.
365 if (RW_CAN_READ(v
)) {
366 turnstile_cancel(ts
);
371 #ifdef ADAPTIVE_RWLOCKS
373 * If the current owner of the lock is executing on another
374 * CPU quit the hard path and try to spin.
376 if ((v
& RW_LOCK_READ
) == 0) {
377 owner
= (struct thread
*)RW_OWNER(v
);
378 if (TD_IS_RUNNING(owner
)) {
379 turnstile_cancel(ts
);
387 * The lock is held in write mode or it already has waiters.
389 MPASS(!RW_CAN_READ(v
));
392 * If the RW_LOCK_READ_WAITERS flag is already set, then
393 * we can go ahead and block. If it is not set then try
394 * to set it. If we fail to set it drop the turnstile
395 * lock and restart the loop.
397 if (!(v
& RW_LOCK_READ_WAITERS
)) {
398 if (!atomic_cmpset_ptr(&rw
->rw_lock
, v
,
399 v
| RW_LOCK_READ_WAITERS
)) {
400 turnstile_cancel(ts
);
404 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
405 CTR2(KTR_LOCK
, "%s: %p set read waiters flag",
410 * We were unable to acquire the lock and the read waiters
411 * flag is set, so we must block on the turnstile.
413 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
414 CTR2(KTR_LOCK
, "%s: %p blocking on turnstile", __func__
,
416 turnstile_wait(ts
, rw_owner(rw
), TS_SHARED_QUEUE
);
417 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
418 CTR2(KTR_LOCK
, "%s: %p resuming from turnstile",
423 * TODO: acquire "owner of record" here. Here be turnstile dragons
424 * however. turnstiles don't like owners changing between calls to
425 * turnstile_wait() currently.
427 lock_profile_obtain_lock_success( &rw
->lock_object
, contested
,
428 waittime
, file
, line
);
429 LOCK_LOG_LOCK("RLOCK", &rw
->lock_object
, 0, 0, file
, line
);
430 WITNESS_LOCK(&rw
->lock_object
, 0, file
, line
);
431 curthread
->td_locks
++;
432 curthread
->td_rw_rlocks
++;
436 _rw_try_rlock(struct rwlock
*rw
, const char *file
, int line
)
442 KASSERT(rw
->rw_lock
!= RW_DESTROYED
,
443 ("rw_try_rlock() of destroyed rwlock @ %s:%d", file
, line
));
444 if (!(x
& RW_LOCK_READ
))
446 if (atomic_cmpset_acq_ptr(&rw
->rw_lock
, x
, x
+ RW_ONE_READER
)) {
447 LOCK_LOG_TRY("RLOCK", &rw
->lock_object
, 0, 1, file
,
449 WITNESS_LOCK(&rw
->lock_object
, LOP_TRYLOCK
, file
, line
);
450 curthread
->td_locks
++;
451 curthread
->td_rw_rlocks
++;
456 LOCK_LOG_TRY("RLOCK", &rw
->lock_object
, 0, 0, file
, line
);
461 _rw_runlock(struct rwlock
*rw
, const char *file
, int line
)
463 struct turnstile
*ts
;
464 uintptr_t x
, v
, queue
;
466 KASSERT(rw
->rw_lock
!= RW_DESTROYED
,
467 ("rw_runlock() of destroyed rwlock @ %s:%d", file
, line
));
468 _rw_assert(rw
, RA_RLOCKED
, file
, line
);
469 curthread
->td_locks
--;
470 curthread
->td_rw_rlocks
--;
471 WITNESS_UNLOCK(&rw
->lock_object
, 0, file
, line
);
472 LOCK_LOG_LOCK("RUNLOCK", &rw
->lock_object
, 0, 0, file
, line
);
474 /* TODO: drop "owner of record" here. */
478 * See if there is more than one read lock held. If so,
479 * just drop one and return.
482 if (RW_READERS(x
) > 1) {
483 if (atomic_cmpset_ptr(&rw
->rw_lock
, x
,
484 x
- RW_ONE_READER
)) {
485 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
487 "%s: %p succeeded %p -> %p",
488 __func__
, rw
, (void *)x
,
489 (void *)(x
- RW_ONE_READER
));
495 * If there aren't any waiters for a write lock, then try
496 * to drop it quickly.
498 if (!(x
& RW_LOCK_WAITERS
)) {
499 MPASS((x
& ~RW_LOCK_WRITE_SPINNER
) ==
501 if (atomic_cmpset_ptr(&rw
->rw_lock
, x
, RW_UNLOCKED
)) {
502 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
503 CTR2(KTR_LOCK
, "%s: %p last succeeded",
510 * Ok, we know we have waiters and we think we are the
511 * last reader, so grab the turnstile lock.
513 turnstile_chain_lock(&rw
->lock_object
);
514 v
= rw
->rw_lock
& (RW_LOCK_WAITERS
| RW_LOCK_WRITE_SPINNER
);
515 MPASS(v
& RW_LOCK_WAITERS
);
518 * Try to drop our lock leaving the lock in a unlocked
521 * If you wanted to do explicit lock handoff you'd have to
522 * do it here. You'd also want to use turnstile_signal()
523 * and you'd have to handle the race where a higher
524 * priority thread blocks on the write lock before the
525 * thread you wakeup actually runs and have the new thread
526 * "steal" the lock. For now it's a lot simpler to just
527 * wakeup all of the waiters.
529 * As above, if we fail, then another thread might have
530 * acquired a read lock, so drop the turnstile lock and
534 if (v
& RW_LOCK_WRITE_WAITERS
) {
535 queue
= TS_EXCLUSIVE_QUEUE
;
536 x
|= (v
& RW_LOCK_READ_WAITERS
);
538 queue
= TS_SHARED_QUEUE
;
539 if (!atomic_cmpset_ptr(&rw
->rw_lock
, RW_READERS_LOCK(1) | v
,
541 turnstile_chain_unlock(&rw
->lock_object
);
544 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
545 CTR2(KTR_LOCK
, "%s: %p last succeeded with waiters",
549 * Ok. The lock is released and all that's left is to
550 * wake up the waiters. Note that the lock might not be
551 * free anymore, but in that case the writers will just
552 * block again if they run before the new lock holder(s)
555 ts
= turnstile_lookup(&rw
->lock_object
);
557 turnstile_broadcast(ts
, queue
);
558 turnstile_unpend(ts
, TS_SHARED_LOCK
);
559 turnstile_chain_unlock(&rw
->lock_object
);
562 lock_profile_release_lock(&rw
->lock_object
);
566 * This function is called when we are unable to obtain a write lock on the
567 * first try. This means that at least one other thread holds either a
568 * read or write lock.
571 _rw_wlock_hard(struct rwlock
*rw
, uintptr_t tid
, const char *file
, int line
)
573 struct turnstile
*ts
;
574 #ifdef ADAPTIVE_RWLOCKS
575 volatile struct thread
*owner
;
579 uint64_t waittime
= 0;
583 if (rw_wlocked(rw
)) {
584 KASSERT(rw
->lock_object
.lo_flags
& RW_RECURSE
,
585 ("%s: recursing but non-recursive rw %s @ %s:%d\n",
586 __func__
, rw
->lock_object
.lo_name
, file
, line
));
588 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
589 CTR2(KTR_LOCK
, "%s: %p recursing", __func__
, rw
);
593 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
594 CTR5(KTR_LOCK
, "%s: %s contested (lock=%p) at %s:%d", __func__
,
595 rw
->lock_object
.lo_name
, (void *)rw
->rw_lock
, file
, line
);
597 while (!_rw_write_lock(rw
, tid
)) {
598 lock_profile_obtain_lock_failed(&rw
->lock_object
,
599 &contested
, &waittime
);
600 #ifdef ADAPTIVE_RWLOCKS
602 * If the lock is write locked and the owner is
603 * running on another CPU, spin until the owner stops
604 * running or the state of the lock changes.
607 owner
= (struct thread
*)RW_OWNER(v
);
608 if (!(v
& RW_LOCK_READ
) && TD_IS_RUNNING(owner
)) {
609 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
610 CTR3(KTR_LOCK
, "%s: spinning on %p held by %p",
611 __func__
, rw
, owner
);
612 while ((struct thread
*)RW_OWNER(rw
->rw_lock
) == owner
&&
613 TD_IS_RUNNING(owner
))
617 if ((v
& RW_LOCK_READ
) && RW_READERS(v
) &&
618 spintries
< rowner_retries
) {
619 if (!(v
& RW_LOCK_WRITE_SPINNER
)) {
620 if (!atomic_cmpset_ptr(&rw
->rw_lock
, v
,
621 v
| RW_LOCK_WRITE_SPINNER
)) {
627 for (i
= 0; i
< rowner_loops
; i
++) {
628 if ((rw
->rw_lock
& RW_LOCK_WRITE_SPINNER
) == 0)
632 if (i
!= rowner_loops
)
636 ts
= turnstile_trywait(&rw
->lock_object
);
639 #ifdef ADAPTIVE_RWLOCKS
641 * If the current owner of the lock is executing on another
642 * CPU quit the hard path and try to spin.
644 if (!(v
& RW_LOCK_READ
)) {
645 owner
= (struct thread
*)RW_OWNER(v
);
646 if (TD_IS_RUNNING(owner
)) {
647 turnstile_cancel(ts
);
654 * Check for the waiters flags about this rwlock.
655 * If the lock was released, without maintain any pending
656 * waiters queue, simply try to acquire it.
657 * If a pending waiters queue is present, claim the lock
658 * ownership and maintain the pending queue.
660 x
= v
& (RW_LOCK_WAITERS
| RW_LOCK_WRITE_SPINNER
);
661 if ((v
& ~x
) == RW_UNLOCKED
) {
662 x
&= ~RW_LOCK_WRITE_SPINNER
;
663 if (atomic_cmpset_acq_ptr(&rw
->rw_lock
, v
, tid
| x
)) {
667 turnstile_cancel(ts
);
670 turnstile_cancel(ts
);
675 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
676 * set it. If we fail to set it, then loop back and try
679 if (!(v
& RW_LOCK_WRITE_WAITERS
)) {
680 if (!atomic_cmpset_ptr(&rw
->rw_lock
, v
,
681 v
| RW_LOCK_WRITE_WAITERS
)) {
682 turnstile_cancel(ts
);
686 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
687 CTR2(KTR_LOCK
, "%s: %p set write waiters flag",
691 * We were unable to acquire the lock and the write waiters
692 * flag is set, so we must block on the turnstile.
694 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
695 CTR2(KTR_LOCK
, "%s: %p blocking on turnstile", __func__
,
697 turnstile_wait(ts
, rw_owner(rw
), TS_EXCLUSIVE_QUEUE
);
698 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
699 CTR2(KTR_LOCK
, "%s: %p resuming from turnstile",
701 #ifdef ADAPTIVE_RWLOCKS
705 lock_profile_obtain_lock_success(&rw
->lock_object
, contested
, waittime
,
710 * This function is called if the first try at releasing a write lock failed.
711 * This means that one of the 2 waiter bits must be set indicating that at
712 * least one thread is waiting on this lock.
715 _rw_wunlock_hard(struct rwlock
*rw
, uintptr_t tid
, const char *file
, int line
)
717 struct turnstile
*ts
;
721 if (rw_wlocked(rw
) && rw_recursed(rw
)) {
723 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
724 CTR2(KTR_LOCK
, "%s: %p unrecursing", __func__
, rw
);
729 KASSERT(rw
->rw_lock
& (RW_LOCK_READ_WAITERS
| RW_LOCK_WRITE_WAITERS
),
730 ("%s: neither of the waiter flags are set", __func__
));
732 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
733 CTR2(KTR_LOCK
, "%s: %p contested", __func__
, rw
);
735 turnstile_chain_lock(&rw
->lock_object
);
736 ts
= turnstile_lookup(&rw
->lock_object
);
741 * Use the same algo as sx locks for now. Prefer waking up shared
742 * waiters if we have any over writers. This is probably not ideal.
744 * 'v' is the value we are going to write back to rw_lock. If we
745 * have waiters on both queues, we need to preserve the state of
746 * the waiter flag for the queue we don't wake up. For now this is
747 * hardcoded for the algorithm mentioned above.
749 * In the case of both readers and writers waiting we wakeup the
750 * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a
751 * new writer comes in before a reader it will claim the lock up
752 * above. There is probably a potential priority inversion in
753 * there that could be worked around either by waking both queues
754 * of waiters or doing some complicated lock handoff gymnastics.
757 if (rw
->rw_lock
& RW_LOCK_WRITE_WAITERS
) {
758 queue
= TS_EXCLUSIVE_QUEUE
;
759 v
|= (rw
->rw_lock
& RW_LOCK_READ_WAITERS
);
761 queue
= TS_SHARED_QUEUE
;
763 /* Wake up all waiters for the specific queue. */
764 if (LOCK_LOG_TEST(&rw
->lock_object
, 0))
765 CTR3(KTR_LOCK
, "%s: %p waking up %s waiters", __func__
, rw
,
766 queue
== TS_SHARED_QUEUE
? "read" : "write");
767 turnstile_broadcast(ts
, queue
);
768 atomic_store_rel_ptr(&rw
->rw_lock
, v
);
769 turnstile_unpend(ts
, TS_EXCLUSIVE_LOCK
);
770 turnstile_chain_unlock(&rw
->lock_object
);
774 * Attempt to do a non-blocking upgrade from a read lock to a write
775 * lock. This will only succeed if this thread holds a single read
776 * lock. Returns true if the upgrade succeeded and false otherwise.
779 _rw_try_upgrade(struct rwlock
*rw
, const char *file
, int line
)
782 struct turnstile
*ts
;
785 KASSERT(rw
->rw_lock
!= RW_DESTROYED
,
786 ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file
, line
));
787 _rw_assert(rw
, RA_RLOCKED
, file
, line
);
790 * Attempt to switch from one reader to a writer. If there
791 * are any write waiters, then we will have to lock the
792 * turnstile first to prevent races with another writer
793 * calling turnstile_wait() before we have claimed this
794 * turnstile. So, do the simple case of no waiters first.
796 tid
= (uintptr_t)curthread
;
800 if (RW_READERS(v
) > 1)
802 if (!(v
& RW_LOCK_WAITERS
)) {
803 success
= atomic_cmpset_ptr(&rw
->rw_lock
, v
, tid
);
810 * Ok, we think we have waiters, so lock the turnstile.
812 ts
= turnstile_trywait(&rw
->lock_object
);
814 if (RW_READERS(v
) > 1) {
815 turnstile_cancel(ts
);
819 * Try to switch from one reader to a writer again. This time
820 * we honor the current state of the waiters flags.
821 * If we obtain the lock with the flags set, then claim
822 * ownership of the turnstile.
824 x
= rw
->rw_lock
& RW_LOCK_WAITERS
;
825 success
= atomic_cmpset_ptr(&rw
->rw_lock
, v
, tid
| x
);
830 turnstile_cancel(ts
);
833 turnstile_cancel(ts
);
835 LOCK_LOG_TRY("WUPGRADE", &rw
->lock_object
, 0, success
, file
, line
);
837 curthread
->td_rw_rlocks
--;
838 WITNESS_UPGRADE(&rw
->lock_object
, LOP_EXCLUSIVE
| LOP_TRYLOCK
,
845 * Downgrade a write lock into a single read lock.
848 _rw_downgrade(struct rwlock
*rw
, const char *file
, int line
)
850 struct turnstile
*ts
;
854 KASSERT(rw
->rw_lock
!= RW_DESTROYED
,
855 ("rw_downgrade() of destroyed rwlock @ %s:%d", file
, line
));
856 _rw_assert(rw
, RA_WLOCKED
| RA_NOTRECURSED
, file
, line
);
859 panic("downgrade of a recursed lock");
862 WITNESS_DOWNGRADE(&rw
->lock_object
, 0, file
, line
);
865 * Convert from a writer to a single reader. First we handle
866 * the easy case with no waiters. If there are any waiters, we
867 * lock the turnstile and "disown" the lock.
869 tid
= (uintptr_t)curthread
;
870 if (atomic_cmpset_rel_ptr(&rw
->rw_lock
, tid
, RW_READERS_LOCK(1)))
874 * Ok, we think we have waiters, so lock the turnstile so we can
875 * read the waiter flags without any races.
877 turnstile_chain_lock(&rw
->lock_object
);
878 v
= rw
->rw_lock
& RW_LOCK_WAITERS
;
879 rwait
= v
& RW_LOCK_READ_WAITERS
;
880 wwait
= v
& RW_LOCK_WRITE_WAITERS
;
881 MPASS(rwait
| wwait
);
884 * Downgrade from a write lock while preserving waiters flag
885 * and give up ownership of the turnstile.
887 ts
= turnstile_lookup(&rw
->lock_object
);
890 v
&= ~RW_LOCK_READ_WAITERS
;
891 atomic_store_rel_ptr(&rw
->rw_lock
, RW_READERS_LOCK(1) | v
);
893 * Wake other readers if there are no writers pending. Otherwise they
894 * won't be able to acquire the lock anyway.
896 if (rwait
&& !wwait
) {
897 turnstile_broadcast(ts
, TS_SHARED_QUEUE
);
898 turnstile_unpend(ts
, TS_EXCLUSIVE_LOCK
);
900 turnstile_disown(ts
);
901 turnstile_chain_unlock(&rw
->lock_object
);
903 curthread
->td_rw_rlocks
++;
904 LOCK_LOG_LOCK("WDOWNGRADE", &rw
->lock_object
, 0, 0, file
, line
);
907 #ifdef INVARIANT_SUPPORT
913 * In the non-WITNESS case, rw_assert() can only detect that at least
914 * *some* thread owns an rlock, but it cannot guarantee that *this*
915 * thread owns an rlock.
918 _rw_assert(struct rwlock
*rw
, int what
, const char *file
, int line
)
921 if (panicstr
!= NULL
)
925 case RA_LOCKED
| RA_RECURSED
:
926 case RA_LOCKED
| RA_NOTRECURSED
:
929 witness_assert(&rw
->lock_object
, what
, file
, line
);
932 * If some other thread has a write lock or we have one
933 * and are asserting a read lock, fail. Also, if no one
934 * has a lock at all, fail.
936 if (rw
->rw_lock
== RW_UNLOCKED
||
937 (!(rw
->rw_lock
& RW_LOCK_READ
) && (what
== RA_RLOCKED
||
938 rw_wowner(rw
) != curthread
)))
939 panic("Lock %s not %slocked @ %s:%d\n",
940 rw
->lock_object
.lo_name
, (what
== RA_RLOCKED
) ?
941 "read " : "", file
, line
);
943 if (!(rw
->rw_lock
& RW_LOCK_READ
)) {
944 if (rw_recursed(rw
)) {
945 if (what
& RA_NOTRECURSED
)
946 panic("Lock %s recursed @ %s:%d\n",
947 rw
->lock_object
.lo_name
, file
,
949 } else if (what
& RA_RECURSED
)
950 panic("Lock %s not recursed @ %s:%d\n",
951 rw
->lock_object
.lo_name
, file
, line
);
956 case RA_WLOCKED
| RA_RECURSED
:
957 case RA_WLOCKED
| RA_NOTRECURSED
:
958 if (rw_wowner(rw
) != curthread
)
959 panic("Lock %s not exclusively locked @ %s:%d\n",
960 rw
->lock_object
.lo_name
, file
, line
);
961 if (rw_recursed(rw
)) {
962 if (what
& RA_NOTRECURSED
)
963 panic("Lock %s recursed @ %s:%d\n",
964 rw
->lock_object
.lo_name
, file
, line
);
965 } else if (what
& RA_RECURSED
)
966 panic("Lock %s not recursed @ %s:%d\n",
967 rw
->lock_object
.lo_name
, file
, line
);
971 witness_assert(&rw
->lock_object
, what
, file
, line
);
974 * If we hold a write lock fail. We can't reliably check
975 * to see if we hold a read lock or not.
977 if (rw_wowner(rw
) == curthread
)
978 panic("Lock %s exclusively locked @ %s:%d\n",
979 rw
->lock_object
.lo_name
, file
, line
);
983 panic("Unknown rw lock assertion: %d @ %s:%d", what
, file
,
987 #endif /* INVARIANT_SUPPORT */
991 db_show_rwlock(struct lock_object
*lock
)
996 rw
= (struct rwlock
*)lock
;
998 db_printf(" state: ");
999 if (rw
->rw_lock
== RW_UNLOCKED
)
1000 db_printf("UNLOCKED\n");
1001 else if (rw
->rw_lock
== RW_DESTROYED
) {
1002 db_printf("DESTROYED\n");
1004 } else if (rw
->rw_lock
& RW_LOCK_READ
)
1005 db_printf("RLOCK: %ju locks\n",
1006 (uintmax_t)(RW_READERS(rw
->rw_lock
)));
1009 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td
,
1010 td
->td_tid
, td
->td_proc
->p_pid
, td
->td_name
);
1011 if (rw_recursed(rw
))
1012 db_printf(" recursed: %u\n", rw
->rw_recurse
);
1014 db_printf(" waiters: ");
1015 switch (rw
->rw_lock
& (RW_LOCK_READ_WAITERS
| RW_LOCK_WRITE_WAITERS
)) {
1016 case RW_LOCK_READ_WAITERS
:
1017 db_printf("readers\n");
1019 case RW_LOCK_WRITE_WAITERS
:
1020 db_printf("writers\n");
1022 case RW_LOCK_READ_WAITERS
| RW_LOCK_WRITE_WAITERS
:
1023 db_printf("readers and writers\n");
1026 db_printf("none\n");