2 * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org>
3 * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice(s), this list of conditions and the following disclaimer as
11 * the first lines of this file unmodified other than the possible
12 * addition of one or more copyright notices.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice(s), this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 * Shared/exclusive locks. This implementation attempts to ensure
32 * deterministic lock granting behavior, so that slocks and xlocks are
35 * Priority propagation will not generally raise the priority of lock holders,
36 * so should not be relied upon in combination with sx locks.
39 #include "opt_adaptive_sx.h"
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
48 #include <sys/mutex.h>
50 #include <sys/sleepqueue.h>
52 #include <sys/systm.h>
55 #include <machine/cpu.h>
62 #if !defined(SMP) && defined(ADAPTIVE_SX)
63 #error "You must have SMP to enable the ADAPTIVE_SX option"
66 /* Handy macros for sleep queues. */
67 #define SQ_EXCLUSIVE_QUEUE 0
68 #define SQ_SHARED_QUEUE 1
71 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
72 * drop Giant anytime we have to sleep or if we adaptively spin.
74 #define GIANT_DECLARE \
76 WITNESS_SAVE_DECL(Giant) \
78 #define GIANT_SAVE() do { \
79 if (mtx_owned(&Giant)) { \
80 WITNESS_SAVE(&Giant.lock_object, Giant); \
81 while (mtx_owned(&Giant)) { \
88 #define GIANT_RESTORE() do { \
89 if (_giantcnt > 0) { \
90 mtx_assert(&Giant, MA_NOTOWNED); \
93 WITNESS_RESTORE(&Giant.lock_object, Giant); \
98 * Returns true if an exclusive lock is recursed. It assumes
99 * curthread currently has an exclusive lock.
101 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
104 static void db_show_sx(struct lock_object *lock);
106 static void lock_sx(struct lock_object *lock, int how);
107 static int unlock_sx(struct lock_object *lock);
109 struct lock_class lock_class_sx = {
111 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
113 .lc_ddb_show = db_show_sx,
116 .lc_unlock = unlock_sx,
120 #define _sx_assert(sx, what, file, line)
124 lock_sx(struct lock_object *lock, int how)
128 sx = (struct sx *)lock;
136 unlock_sx(struct lock_object *lock)
140 sx = (struct sx *)lock;
141 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
142 if (sx_xlocked(sx)) {
152 sx_sysinit(void *arg)
154 struct sx_args *sargs = arg;
156 sx_init(sargs->sa_sx, sargs->sa_desc);
160 sx_init_flags(struct sx *sx, const char *description, int opts)
164 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
165 SX_NOPROFILE | SX_ADAPTIVESPIN)) == 0);
167 flags = LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE;
170 if (opts & SX_NOPROFILE)
171 flags |= LO_NOPROFILE;
172 if (!(opts & SX_NOWITNESS))
177 flags |= opts & (SX_ADAPTIVESPIN | SX_RECURSE);
178 sx->sx_lock = SX_LOCK_UNLOCKED;
180 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
184 sx_destroy(struct sx *sx)
187 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
188 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
189 sx->sx_lock = SX_LOCK_DESTROYED;
190 lock_destroy(&sx->lock_object);
194 _sx_slock(struct sx *sx, int opts, const char *file, int line)
198 MPASS(curthread != NULL);
199 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
200 ("sx_slock() of destroyed sx @ %s:%d", file, line));
201 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line);
202 error = __sx_slock(sx, opts, file, line);
204 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
205 WITNESS_LOCK(&sx->lock_object, 0, file, line);
206 curthread->td_locks++;
213 _sx_try_slock(struct sx *sx, const char *file, int line)
218 KASSERT(x != SX_LOCK_DESTROYED,
219 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
220 if ((x & SX_LOCK_SHARED) && atomic_cmpset_acq_ptr(&sx->sx_lock, x,
221 x + SX_ONE_SHARER)) {
222 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
223 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
224 curthread->td_locks++;
228 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
233 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
237 MPASS(curthread != NULL);
238 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
239 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
240 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
242 error = __sx_xlock(sx, curthread, opts, file, line);
244 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
246 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
247 curthread->td_locks++;
254 _sx_try_xlock(struct sx *sx, const char *file, int line)
258 MPASS(curthread != NULL);
259 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
260 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
262 if (sx_xlocked(sx) && (sx->lock_object.lo_flags & SX_RECURSE) != 0) {
264 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
267 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
268 (uintptr_t)curthread);
269 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
271 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
273 curthread->td_locks++;
280 _sx_sunlock(struct sx *sx, const char *file, int line)
283 MPASS(curthread != NULL);
284 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
285 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
286 _sx_assert(sx, SA_SLOCKED, file, line);
287 curthread->td_locks--;
288 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
289 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
290 if (SX_SHARERS(sx->sx_lock) == 1)
291 lock_profile_release_lock(&sx->lock_object);
292 __sx_sunlock(sx, file, line);
296 _sx_xunlock(struct sx *sx, const char *file, int line)
299 MPASS(curthread != NULL);
300 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
301 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
302 _sx_assert(sx, SA_XLOCKED, file, line);
303 curthread->td_locks--;
304 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
305 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
307 if (!sx_recursed(sx))
308 lock_profile_release_lock(&sx->lock_object);
309 __sx_xunlock(sx, curthread, file, line);
313 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
314 * This will only succeed if this thread holds a single shared lock.
315 * Return 1 if if the upgrade succeed, 0 otherwise.
318 _sx_try_upgrade(struct sx *sx, const char *file, int line)
323 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
324 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
325 _sx_assert(sx, SA_SLOCKED, file, line);
328 * Try to switch from one shared lock to an exclusive lock. We need
329 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
330 * we will wake up the exclusive waiters when we drop the lock.
332 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
333 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
334 (uintptr_t)curthread | x);
335 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
337 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
343 * Downgrade an unrecursed exclusive lock into a single shared lock.
346 _sx_downgrade(struct sx *sx, const char *file, int line)
350 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
351 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
352 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
355 panic("downgrade of a recursed lock");
358 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
361 * Try to switch from an exclusive lock with no shared waiters
362 * to one sharer with no shared waiters. If there are
363 * exclusive waiters, we don't need to lock the sleep queue so
364 * long as we preserve the flag. We do one quick try and if
365 * that fails we grab the sleepq lock to keep the flags from
366 * changing and do it the slow way.
368 * We have to lock the sleep queue if there are shared waiters
369 * so we can wake them up.
372 if (!(x & SX_LOCK_SHARED_WAITERS) &&
373 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
374 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
375 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
380 * Lock the sleep queue so we can read the waiters bits
381 * without any races and wakeup any shared waiters.
383 sleepq_lock(&sx->lock_object);
386 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
387 * shared lock. If there are any shared waiters, wake them up.
390 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
391 (x & SX_LOCK_EXCLUSIVE_WAITERS));
392 if (x & SX_LOCK_SHARED_WAITERS)
393 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1,
396 sleepq_release(&sx->lock_object);
398 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
402 * This function represents the so-called 'hard case' for sx_xlock
403 * operation. All 'easy case' failures are redirected to this. Note
404 * that ideally this would be a static function, but it needs to be
405 * accessible from at least sx.h.
408 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
413 volatile struct thread *owner;
416 int contested = 0, error = 0;
417 uint64_t waitstart = 0;
419 /* If we already hold an exclusive lock, then recurse. */
420 if (sx_xlocked(sx)) {
421 KASSERT((sx->lock_object.lo_flags & SX_RECURSE) != 0,
422 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
423 sx->lock_object.lo_name, file, line));
425 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
426 if (LOCK_LOG_TEST(&sx->lock_object, 0))
427 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
430 lock_profile_obtain_lock_failed(&(sx)->lock_object,
431 &contested, &waitstart);
433 if (LOCK_LOG_TEST(&sx->lock_object, 0))
434 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
435 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
437 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
440 * If the lock is write locked and the owner is
441 * running on another CPU, spin until the owner stops
442 * running or the state of the lock changes.
445 if (!(x & SX_LOCK_SHARED) &&
446 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
448 owner = (struct thread *)x;
449 if (TD_IS_RUNNING(owner)) {
450 if (LOCK_LOG_TEST(&sx->lock_object, 0))
452 "%s: spinning on %p held by %p",
453 __func__, sx, owner);
455 while (SX_OWNER(sx->sx_lock) == x &&
456 TD_IS_RUNNING(owner))
463 sleepq_lock(&sx->lock_object);
467 * If the lock was released while spinning on the
468 * sleep queue chain lock, try again.
470 if (x == SX_LOCK_UNLOCKED) {
471 sleepq_release(&sx->lock_object);
477 * The current lock owner might have started executing
478 * on another CPU (or the lock could have changed
479 * owners) while we were waiting on the sleep queue
480 * chain lock. If so, drop the sleep queue lock and try
483 if (!(x & SX_LOCK_SHARED) &&
484 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
485 owner = (struct thread *)SX_OWNER(x);
486 if (TD_IS_RUNNING(owner)) {
487 sleepq_release(&sx->lock_object);
494 * If an exclusive lock was released with both shared
495 * and exclusive waiters and a shared waiter hasn't
496 * woken up and acquired the lock yet, sx_lock will be
497 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
498 * If we see that value, try to acquire it once. Note
499 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
500 * as there are other exclusive waiters still. If we
501 * fail, restart the loop.
503 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
504 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
505 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
506 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
507 sleepq_release(&sx->lock_object);
508 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
512 sleepq_release(&sx->lock_object);
517 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
518 * than loop back and retry.
520 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
521 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
522 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
523 sleepq_release(&sx->lock_object);
526 if (LOCK_LOG_TEST(&sx->lock_object, 0))
527 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
532 * Since we have been unable to acquire the exclusive
533 * lock and the exclusive waiters flag is set, we have
536 if (LOCK_LOG_TEST(&sx->lock_object, 0))
537 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
541 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
542 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
543 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
544 if (!(opts & SX_INTERRUPTIBLE))
545 sleepq_wait(&sx->lock_object);
547 error = sleepq_wait_sig(&sx->lock_object);
550 if (LOCK_LOG_TEST(&sx->lock_object, 0))
552 "%s: interruptible sleep by %p suspended by signal",
556 if (LOCK_LOG_TEST(&sx->lock_object, 0))
557 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
563 lock_profile_obtain_lock_success(&(sx)->lock_object, contested,
564 waitstart, file, line);
569 * This function represents the so-called 'hard case' for sx_xunlock
570 * operation. All 'easy case' failures are redirected to this. Note
571 * that ideally this would be a static function, but it needs to be
572 * accessible from at least sx.h.
575 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
580 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
582 /* If the lock is recursed, then unrecurse one level. */
583 if (sx_xlocked(sx) && sx_recursed(sx)) {
584 if ((--sx->sx_recurse) == 0)
585 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
586 if (LOCK_LOG_TEST(&sx->lock_object, 0))
587 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
590 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
591 SX_LOCK_EXCLUSIVE_WAITERS));
592 if (LOCK_LOG_TEST(&sx->lock_object, 0))
593 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
595 sleepq_lock(&sx->lock_object);
596 x = SX_LOCK_UNLOCKED;
599 * The wake up algorithm here is quite simple and probably not
600 * ideal. It gives precedence to shared waiters if they are
601 * present. For this condition, we have to preserve the
602 * state of the exclusive waiters flag.
604 if (sx->sx_lock & SX_LOCK_SHARED_WAITERS) {
605 queue = SQ_SHARED_QUEUE;
606 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
608 queue = SQ_EXCLUSIVE_QUEUE;
610 /* Wake up all the waiters for the specific queue. */
611 if (LOCK_LOG_TEST(&sx->lock_object, 0))
612 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
613 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
615 atomic_store_rel_ptr(&sx->sx_lock, x);
616 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1, queue);
620 * This function represents the so-called 'hard case' for sx_slock
621 * operation. All 'easy case' failures are redirected to this. Note
622 * that ideally this would be a static function, but it needs to be
623 * accessible from at least sx.h.
626 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
630 volatile struct thread *owner;
633 uint64_t waitstart = 0;
634 int contested = 0, error = 0;
636 * As with rwlocks, we don't make any attempt to try to block
637 * shared locks once there is an exclusive waiter.
644 * If no other thread has an exclusive lock then try to bump up
645 * the count of sharers. Since we have to preserve the state
646 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
647 * shared lock loop back and retry.
649 if (x & SX_LOCK_SHARED) {
650 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
651 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
652 x + SX_ONE_SHARER)) {
653 if (SX_SHARERS(x) == 0)
654 lock_profile_obtain_lock_success(
655 &sx->lock_object, contested,
656 waitstart, file, line);
657 if (LOCK_LOG_TEST(&sx->lock_object, 0))
659 "%s: %p succeed %p -> %p", __func__,
661 (void *)(x + SX_ONE_SHARER));
664 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
672 * If the owner is running on another CPU, spin until
673 * the owner stops running or the state of the lock
676 else if (sx->lock_object.lo_flags & SX_ADAPTIVESPIN) {
678 owner = (struct thread *)x;
679 if (TD_IS_RUNNING(owner)) {
680 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
682 if (LOCK_LOG_TEST(&sx->lock_object, 0))
684 "%s: spinning on %p held by %p",
685 __func__, sx, owner);
687 while (SX_OWNER(sx->sx_lock) == x &&
688 TD_IS_RUNNING(owner))
695 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
699 * Some other thread already has an exclusive lock, so
700 * start the process of blocking.
702 sleepq_lock(&sx->lock_object);
706 * The lock could have been released while we spun.
707 * In this case loop back and retry.
709 if (x & SX_LOCK_SHARED) {
710 sleepq_release(&sx->lock_object);
716 * If the owner is running on another CPU, spin until
717 * the owner stops running or the state of the lock
720 if (!(x & SX_LOCK_SHARED) &&
721 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
722 owner = (struct thread *)SX_OWNER(x);
723 if (TD_IS_RUNNING(owner)) {
724 sleepq_release(&sx->lock_object);
731 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
732 * fail to set it drop the sleep queue lock and loop
735 if (!(x & SX_LOCK_SHARED_WAITERS)) {
736 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
737 x | SX_LOCK_SHARED_WAITERS)) {
738 sleepq_release(&sx->lock_object);
741 if (LOCK_LOG_TEST(&sx->lock_object, 0))
742 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
747 * Since we have been unable to acquire the shared lock,
750 if (LOCK_LOG_TEST(&sx->lock_object, 0))
751 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
755 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
756 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
757 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
758 if (!(opts & SX_INTERRUPTIBLE))
759 sleepq_wait(&sx->lock_object);
761 error = sleepq_wait_sig(&sx->lock_object);
764 if (LOCK_LOG_TEST(&sx->lock_object, 0))
766 "%s: interruptible sleep by %p suspended by signal",
770 if (LOCK_LOG_TEST(&sx->lock_object, 0))
771 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
780 * This function represents the so-called 'hard case' for sx_sunlock
781 * operation. All 'easy case' failures are redirected to this. Note
782 * that ideally this would be a static function, but it needs to be
783 * accessible from at least sx.h.
786 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
794 * We should never have sharers while at least one thread
795 * holds a shared lock.
797 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
798 ("%s: waiting sharers", __func__));
801 * See if there is more than one shared lock held. If
802 * so, just drop one and return.
804 if (SX_SHARERS(x) > 1) {
805 if (atomic_cmpset_ptr(&sx->sx_lock, x,
806 x - SX_ONE_SHARER)) {
807 if (LOCK_LOG_TEST(&sx->lock_object, 0))
809 "%s: %p succeeded %p -> %p",
810 __func__, sx, (void *)x,
811 (void *)(x - SX_ONE_SHARER));
818 * If there aren't any waiters for an exclusive lock,
819 * then try to drop it quickly.
821 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
822 MPASS(x == SX_SHARERS_LOCK(1));
823 if (atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1),
825 lock_profile_release_lock(&sx->lock_object);
826 if (LOCK_LOG_TEST(&sx->lock_object, 0))
827 CTR2(KTR_LOCK, "%s: %p last succeeded",
835 * At this point, there should just be one sharer with
838 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
840 lock_profile_release_lock(&sx->lock_object);
841 sleepq_lock(&sx->lock_object);
844 * Wake up semantic here is quite simple:
845 * Just wake up all the exclusive waiters.
846 * Note that the state of the lock could have changed,
847 * so if it fails loop back and retry.
849 if (!atomic_cmpset_ptr(&sx->sx_lock,
850 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
852 sleepq_release(&sx->lock_object);
855 if (LOCK_LOG_TEST(&sx->lock_object, 0))
856 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
857 "exclusive queue", __func__, sx);
858 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1,
864 #ifdef INVARIANT_SUPPORT
870 * In the non-WITNESS case, sx_assert() can only detect that at least
871 * *some* thread owns an slock, but it cannot guarantee that *this*
872 * thread owns an slock.
875 _sx_assert(struct sx *sx, int what, const char *file, int line)
881 if (panicstr != NULL)
885 case SA_SLOCKED | SA_NOTRECURSED:
886 case SA_SLOCKED | SA_RECURSED:
892 case SA_LOCKED | SA_NOTRECURSED:
893 case SA_LOCKED | SA_RECURSED:
895 witness_assert(&sx->lock_object, what, file, line);
898 * If some other thread has an exclusive lock or we
899 * have one and are asserting a shared lock, fail.
900 * Also, if no one has a lock at all, fail.
902 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
903 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
904 sx_xholder(sx) != curthread)))
905 panic("Lock %s not %slocked @ %s:%d\n",
906 sx->lock_object.lo_name, slocked ? "share " : "",
909 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
910 if (sx_recursed(sx)) {
911 if (what & SA_NOTRECURSED)
912 panic("Lock %s recursed @ %s:%d\n",
913 sx->lock_object.lo_name, file,
915 } else if (what & SA_RECURSED)
916 panic("Lock %s not recursed @ %s:%d\n",
917 sx->lock_object.lo_name, file, line);
922 case SA_XLOCKED | SA_NOTRECURSED:
923 case SA_XLOCKED | SA_RECURSED:
924 if (sx_xholder(sx) != curthread)
925 panic("Lock %s not exclusively locked @ %s:%d\n",
926 sx->lock_object.lo_name, file, line);
927 if (sx_recursed(sx)) {
928 if (what & SA_NOTRECURSED)
929 panic("Lock %s recursed @ %s:%d\n",
930 sx->lock_object.lo_name, file, line);
931 } else if (what & SA_RECURSED)
932 panic("Lock %s not recursed @ %s:%d\n",
933 sx->lock_object.lo_name, file, line);
937 witness_assert(&sx->lock_object, what, file, line);
940 * If we hold an exclusve lock fail. We can't
941 * reliably check to see if we hold a shared lock or
944 if (sx_xholder(sx) == curthread)
945 panic("Lock %s exclusively locked @ %s:%d\n",
946 sx->lock_object.lo_name, file, line);
950 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
954 #endif /* INVARIANT_SUPPORT */
958 db_show_sx(struct lock_object *lock)
963 sx = (struct sx *)lock;
965 db_printf(" state: ");
966 if (sx->sx_lock == SX_LOCK_UNLOCKED)
967 db_printf("UNLOCKED\n");
968 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
969 db_printf("DESTROYED\n");
971 } else if (sx->sx_lock & SX_LOCK_SHARED)
972 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
975 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
976 td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
978 db_printf(" recursed: %d\n", sx->sx_recurse);
981 db_printf(" waiters: ");
983 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
984 case SX_LOCK_SHARED_WAITERS:
985 db_printf("shared\n");
987 case SX_LOCK_EXCLUSIVE_WAITERS:
988 db_printf("exclusive\n");
990 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
991 db_printf("exclusive and shared\n");
999 * Check to see if a thread that is blocked on a sleep queue is actually
1000 * blocked on an sx lock. If so, output some details and return true.
1001 * If the lock has an exclusive owner, return that in *ownerp.
1004 sx_chain(struct thread *td, struct thread **ownerp)
1009 * Check to see if this thread is blocked on an sx lock.
1010 * First, we check the lock class. If that is ok, then we
1011 * compare the lock name against the wait message.
1014 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1015 sx->lock_object.lo_name != td->td_wmesg)
1018 /* We think we have an sx lock, so output some details. */
1019 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1020 *ownerp = sx_xholder(sx);
1021 if (sx->sx_lock & SX_LOCK_SHARED)
1022 db_printf("SLOCK (count %ju)\n",
1023 (uintmax_t)SX_SHARERS(sx->sx_lock));
1025 db_printf("XLOCK\n");