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/lock_profile.h>
49 #include <sys/mutex.h>
51 #include <sys/sleepqueue.h>
53 #include <sys/systm.h>
56 #include <machine/cpu.h>
63 #if !defined(SMP) && defined(ADAPTIVE_SX)
64 #error "You must have SMP to enable the ADAPTIVE_SX option"
67 /* Handy macros for sleep queues. */
68 #define SQ_EXCLUSIVE_QUEUE 0
69 #define SQ_SHARED_QUEUE 1
72 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
73 * drop Giant anytime we have to sleep or if we adaptively spin.
75 #define GIANT_DECLARE \
77 WITNESS_SAVE_DECL(Giant) \
79 #define GIANT_SAVE() do { \
80 if (mtx_owned(&Giant)) { \
81 WITNESS_SAVE(&Giant.lock_object, Giant); \
82 while (mtx_owned(&Giant)) { \
89 #define GIANT_RESTORE() do { \
90 if (_giantcnt > 0) { \
91 mtx_assert(&Giant, MA_NOTOWNED); \
94 WITNESS_RESTORE(&Giant.lock_object, Giant); \
99 * Returns true if an exclusive lock is recursed. It curthread
100 * currently has an exclusive lock.
102 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
105 * Return a pointer to the owning thread if the lock is exclusively
108 #define sx_xholder(sx) \
109 ((sx)->sx_lock & SX_LOCK_SHARED ? NULL : \
110 (struct thread *)SX_OWNER((sx)->sx_lock))
113 static void db_show_sx(struct lock_object *lock);
115 static void lock_sx(struct lock_object *lock, int how);
116 static int unlock_sx(struct lock_object *lock);
118 struct lock_class lock_class_sx = {
120 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
122 .lc_ddb_show = db_show_sx,
125 .lc_unlock = unlock_sx,
129 #define _sx_assert(sx, what, file, line)
133 lock_sx(struct lock_object *lock, int how)
137 sx = (struct sx *)lock;
145 unlock_sx(struct lock_object *lock)
149 sx = (struct sx *)lock;
150 sx_assert(sx, SX_LOCKED | SX_NOTRECURSED);
151 if (sx_xlocked(sx)) {
161 sx_sysinit(void *arg)
163 struct sx_args *sargs = arg;
165 sx_init(sargs->sa_sx, sargs->sa_desc);
169 sx_init_flags(struct sx *sx, const char *description, int opts)
173 flags = LO_SLEEPABLE | LO_UPGRADABLE | LO_RECURSABLE;
176 if (opts & SX_NOPROFILE)
177 flags |= LO_NOPROFILE;
178 if (!(opts & SX_NOWITNESS))
183 flags |= opts & SX_ADAPTIVESPIN;
184 sx->sx_lock = SX_LOCK_UNLOCKED;
186 lock_profile_object_init(&sx->lock_object, &lock_class_sx, description);
187 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
191 sx_destroy(struct sx *sx)
194 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
195 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
196 lock_profile_object_destroy(&sx->lock_object);
197 lock_destroy(&sx->lock_object);
201 _sx_slock(struct sx *sx, const char *file, int line)
204 MPASS(curthread != NULL);
205 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line);
206 __sx_slock(sx, file, line);
207 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
208 WITNESS_LOCK(&sx->lock_object, 0, file, line);
209 curthread->td_locks++;
213 _sx_try_slock(struct sx *sx, const char *file, int line)
218 if ((x & SX_LOCK_SHARED) && atomic_cmpset_acq_ptr(&sx->sx_lock, x,
219 x + SX_ONE_SHARER)) {
220 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
221 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
222 curthread->td_locks++;
226 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
231 _sx_xlock(struct sx *sx, const char *file, int line)
234 MPASS(curthread != NULL);
235 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
237 __sx_xlock(sx, curthread, file, line);
238 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, file, line);
239 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
240 curthread->td_locks++;
244 _sx_try_xlock(struct sx *sx, const char *file, int line)
248 MPASS(curthread != NULL);
250 if (sx_xlocked(sx)) {
252 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
255 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
256 (uintptr_t)curthread);
257 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
259 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
261 curthread->td_locks++;
268 _sx_sunlock(struct sx *sx, const char *file, int line)
271 MPASS(curthread != NULL);
272 _sx_assert(sx, SX_SLOCKED, file, line);
273 curthread->td_locks--;
274 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
275 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
276 if (SX_SHARERS(sx->sx_lock) == 0)
277 lock_profile_release_lock(&sx->lock_object);
278 __sx_sunlock(sx, file, line);
282 _sx_xunlock(struct sx *sx, const char *file, int line)
285 MPASS(curthread != NULL);
286 _sx_assert(sx, SX_XLOCKED, file, line);
287 curthread->td_locks--;
288 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
289 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
291 if (!sx_recursed(sx))
292 lock_profile_release_lock(&sx->lock_object);
293 __sx_xunlock(sx, curthread, file, line);
297 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
298 * This will only succeed if this thread holds a single shared lock.
299 * Return 1 if if the upgrade succeed, 0 otherwise.
302 _sx_try_upgrade(struct sx *sx, const char *file, int line)
307 _sx_assert(sx, SX_SLOCKED, file, line);
310 * Try to switch from one shared lock to an exclusive lock. We need
311 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
312 * we will wake up the exclusive waiters when we drop the lock.
314 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
315 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
316 (uintptr_t)curthread | x);
317 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
319 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
325 * Downgrade an unrecursed exclusive lock into a single shared lock.
328 _sx_downgrade(struct sx *sx, const char *file, int line)
332 _sx_assert(sx, SX_XLOCKED | SX_NOTRECURSED, file, line);
335 panic("downgrade of a recursed lock");
338 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
341 * Try to switch from an exclusive lock with no shared waiters
342 * to one sharer with no shared waiters. If there are
343 * exclusive waiters, we don't need to lock the sleep queue so
344 * long as we preserve the flag. We do one quick try and if
345 * that fails we grab the sleepq lock to keep the flags from
346 * changing and do it the slow way.
348 * We have to lock the sleep queue if there are shared waiters
349 * so we can wake them up.
352 if (!(x & SX_LOCK_SHARED_WAITERS) &&
353 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
354 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
355 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
360 * Lock the sleep queue so we can read the waiters bits
361 * without any races and wakeup any shared waiters.
363 sleepq_lock(&sx->lock_object);
366 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
367 * shared lock. If there are any shared waiters, wake them up.
370 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
371 (x & SX_LOCK_EXCLUSIVE_WAITERS));
372 if (x & SX_LOCK_SHARED_WAITERS)
373 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1,
376 sleepq_release(&sx->lock_object);
378 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
382 * This function represents the so-called 'hard case' for sx_xlock
383 * operation. All 'easy case' failures are redirected to this. Note
384 * that ideally this would be a static function, but it needs to be
385 * accessible from at least sx.h.
388 _sx_xlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
392 volatile struct thread *owner;
396 uint64_t waitstart = 0;
398 /* If we already hold an exclusive lock, then recurse. */
399 if (sx_xlocked(sx)) {
401 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
402 if (LOCK_LOG_TEST(&sx->lock_object, 0))
403 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
406 lock_profile_obtain_lock_failed(&(sx)->lock_object,
407 &contested, &waitstart);
409 if (LOCK_LOG_TEST(&sx->lock_object, 0))
410 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
411 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
413 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
416 * If the lock is write locked and the owner is
417 * running on another CPU, spin until the owner stops
418 * running or the state of the lock changes.
421 if (!(x & SX_LOCK_SHARED) &&
422 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
424 owner = (struct thread *)x;
425 if (TD_IS_RUNNING(owner)) {
426 if (LOCK_LOG_TEST(&sx->lock_object, 0))
428 "%s: spinning on %p held by %p",
429 __func__, sx, owner);
431 while (SX_OWNER(sx->sx_lock) == x &&
432 TD_IS_RUNNING(owner))
439 sleepq_lock(&sx->lock_object);
443 * If the lock was released while spinning on the
444 * sleep queue chain lock, try again.
446 if (x == SX_LOCK_UNLOCKED) {
447 sleepq_release(&sx->lock_object);
453 * The current lock owner might have started executing
454 * on another CPU (or the lock could have changed
455 * owners) while we were waiting on the sleep queue
456 * chain lock. If so, drop the sleep queue lock and try
459 if (!(x & SX_LOCK_SHARED) &&
460 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
461 owner = (struct thread *)SX_OWNER(x);
462 if (TD_IS_RUNNING(owner)) {
463 sleepq_release(&sx->lock_object);
470 * If an exclusive lock was released with both shared
471 * and exclusive waiters and a shared waiter hasn't
472 * woken up and acquired the lock yet, sx_lock will be
473 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
474 * If we see that value, try to acquire it once. Note
475 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
476 * as there are other exclusive waiters still. If we
477 * fail, restart the loop.
479 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
480 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
481 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
482 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
483 sleepq_release(&sx->lock_object);
484 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
488 sleepq_release(&sx->lock_object);
493 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
494 * than loop back and retry.
496 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
497 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
498 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
499 sleepq_release(&sx->lock_object);
502 if (LOCK_LOG_TEST(&sx->lock_object, 0))
503 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
508 * Since we have been unable to acquire the exclusive
509 * lock and the exclusive waiters flag is set, we have
512 if (LOCK_LOG_TEST(&sx->lock_object, 0))
513 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
517 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
518 SLEEPQ_SX, SQ_EXCLUSIVE_QUEUE);
519 sleepq_wait(&sx->lock_object);
521 if (LOCK_LOG_TEST(&sx->lock_object, 0))
522 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
527 lock_profile_obtain_lock_success(&(sx)->lock_object, contested,
528 waitstart, file, line);
532 * This function represents the so-called 'hard case' for sx_xunlock
533 * operation. All 'easy case' failures are redirected to this. Note
534 * that ideally this would be a static function, but it needs to be
535 * accessible from at least sx.h.
538 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
543 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
545 /* If the lock is recursed, then unrecurse one level. */
546 if (sx_xlocked(sx) && sx_recursed(sx)) {
547 if ((--sx->sx_recurse) == 0)
548 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
549 if (LOCK_LOG_TEST(&sx->lock_object, 0))
550 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
553 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
554 SX_LOCK_EXCLUSIVE_WAITERS));
555 if (LOCK_LOG_TEST(&sx->lock_object, 0))
556 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
558 sleepq_lock(&sx->lock_object);
559 x = SX_LOCK_UNLOCKED;
562 * The wake up algorithm here is quite simple and probably not
563 * ideal. It gives precedence to shared waiters if they are
564 * present. For this condition, we have to preserve the
565 * state of the exclusive waiters flag.
567 if (sx->sx_lock & SX_LOCK_SHARED_WAITERS) {
568 queue = SQ_SHARED_QUEUE;
569 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
571 queue = SQ_EXCLUSIVE_QUEUE;
573 /* Wake up all the waiters for the specific queue. */
574 if (LOCK_LOG_TEST(&sx->lock_object, 0))
575 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
576 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
578 atomic_store_rel_ptr(&sx->sx_lock, x);
579 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1, queue);
583 * This function represents the so-called 'hard case' for sx_slock
584 * operation. All 'easy case' failures are redirected to this. Note
585 * that ideally this would be a static function, but it needs to be
586 * accessible from at least sx.h.
589 _sx_slock_hard(struct sx *sx, const char *file, int line)
593 volatile struct thread *owner;
596 uint64_t waitstart = 0;
599 * As with rwlocks, we don't make any attempt to try to block
600 * shared locks once there is an exclusive waiter.
607 * If no other thread has an exclusive lock then try to bump up
608 * the count of sharers. Since we have to preserve the state
609 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
610 * shared lock loop back and retry.
612 if (x & SX_LOCK_SHARED) {
613 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
614 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
615 x + SX_ONE_SHARER)) {
616 if (SX_SHARERS(x) == 0)
617 lock_profile_obtain_lock_success(
618 &sx->lock_object, contested,
619 waitstart, file, line);
620 if (LOCK_LOG_TEST(&sx->lock_object, 0))
622 "%s: %p succeed %p -> %p", __func__,
624 (void *)(x + SX_ONE_SHARER));
627 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
635 * If the owner is running on another CPU, spin until
636 * the owner stops running or the state of the lock
639 else if (sx->lock_object.lo_flags & SX_ADAPTIVESPIN) {
641 owner = (struct thread *)x;
642 if (TD_IS_RUNNING(owner)) {
643 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
645 if (LOCK_LOG_TEST(&sx->lock_object, 0))
647 "%s: spinning on %p held by %p",
648 __func__, sx, owner);
650 while (SX_OWNER(sx->sx_lock) == x &&
651 TD_IS_RUNNING(owner))
658 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
662 * Some other thread already has an exclusive lock, so
663 * start the process of blocking.
665 sleepq_lock(&sx->lock_object);
669 * The lock could have been released while we spun.
670 * In this case loop back and retry.
672 if (x & SX_LOCK_SHARED) {
673 sleepq_release(&sx->lock_object);
679 * If the owner is running on another CPU, spin until
680 * the owner stops running or the state of the lock
683 if (!(x & SX_LOCK_SHARED) &&
684 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
685 owner = (struct thread *)SX_OWNER(x);
686 if (TD_IS_RUNNING(owner)) {
687 sleepq_release(&sx->lock_object);
694 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
695 * fail to set it drop the sleep queue lock and loop
698 if (!(x & SX_LOCK_SHARED_WAITERS)) {
699 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
700 x | SX_LOCK_SHARED_WAITERS)) {
701 sleepq_release(&sx->lock_object);
704 if (LOCK_LOG_TEST(&sx->lock_object, 0))
705 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
710 * Since we have been unable to acquire the shared lock,
713 if (LOCK_LOG_TEST(&sx->lock_object, 0))
714 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
718 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
719 SLEEPQ_SX, SQ_SHARED_QUEUE);
720 sleepq_wait(&sx->lock_object);
722 if (LOCK_LOG_TEST(&sx->lock_object, 0))
723 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
731 * This function represents the so-called 'hard case' for sx_sunlock
732 * operation. All 'easy case' failures are redirected to this. Note
733 * that ideally this would be a static function, but it needs to be
734 * accessible from at least sx.h.
737 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
745 * We should never have sharers while at least one thread
746 * holds a shared lock.
748 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
749 ("%s: waiting sharers", __func__));
752 * See if there is more than one shared lock held. If
753 * so, just drop one and return.
755 if (SX_SHARERS(x) > 1) {
756 if (atomic_cmpset_ptr(&sx->sx_lock, x,
757 x - SX_ONE_SHARER)) {
758 if (LOCK_LOG_TEST(&sx->lock_object, 0))
760 "%s: %p succeeded %p -> %p",
761 __func__, sx, (void *)x,
762 (void *)(x - SX_ONE_SHARER));
769 * If there aren't any waiters for an exclusive lock,
770 * then try to drop it quickly.
772 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
773 MPASS(x == SX_SHARERS_LOCK(1));
774 if (atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1),
776 lock_profile_release_lock(&sx->lock_object);
777 if (LOCK_LOG_TEST(&sx->lock_object, 0))
778 CTR2(KTR_LOCK, "%s: %p last succeeded",
786 * At this point, there should just be one sharer with
789 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
791 lock_profile_release_lock(&sx->lock_object);
792 sleepq_lock(&sx->lock_object);
795 * Wake up semantic here is quite simple:
796 * Just wake up all the exclusive waiters.
797 * Note that the state of the lock could have changed,
798 * so if it fails loop back and retry.
800 if (!atomic_cmpset_ptr(&sx->sx_lock,
801 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
803 sleepq_release(&sx->lock_object);
806 if (LOCK_LOG_TEST(&sx->lock_object, 0))
807 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
808 "exclusive queue", __func__, sx);
809 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1,
815 #ifdef INVARIANT_SUPPORT
821 * In the non-WITNESS case, sx_assert() can only detect that at least
822 * *some* thread owns an slock, but it cannot guarantee that *this*
823 * thread owns an slock.
826 _sx_assert(struct sx *sx, int what, const char *file, int line)
832 if (panicstr != NULL)
836 case SX_SLOCKED | SX_NOTRECURSED:
837 case SX_SLOCKED | SX_RECURSED:
843 case SX_LOCKED | SX_NOTRECURSED:
844 case SX_LOCKED | SX_RECURSED:
846 witness_assert(&sx->lock_object, what, file, line);
849 * If some other thread has an exclusive lock or we
850 * have one and are asserting a shared lock, fail.
851 * Also, if no one has a lock at all, fail.
853 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
854 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
855 sx_xholder(sx) != curthread)))
856 panic("Lock %s not %slocked @ %s:%d\n",
857 sx->lock_object.lo_name, slocked ? "share " : "",
860 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
861 if (sx_recursed(sx)) {
862 if (what & SX_NOTRECURSED)
863 panic("Lock %s recursed @ %s:%d\n",
864 sx->lock_object.lo_name, file,
866 } else if (what & SX_RECURSED)
867 panic("Lock %s not recursed @ %s:%d\n",
868 sx->lock_object.lo_name, file, line);
873 case SX_XLOCKED | SX_NOTRECURSED:
874 case SX_XLOCKED | SX_RECURSED:
875 if (sx_xholder(sx) != curthread)
876 panic("Lock %s not exclusively locked @ %s:%d\n",
877 sx->lock_object.lo_name, file, line);
878 if (sx_recursed(sx)) {
879 if (what & SX_NOTRECURSED)
880 panic("Lock %s recursed @ %s:%d\n",
881 sx->lock_object.lo_name, file, line);
882 } else if (what & SX_RECURSED)
883 panic("Lock %s not recursed @ %s:%d\n",
884 sx->lock_object.lo_name, file, line);
888 witness_assert(&sx->lock_object, what, file, line);
891 * If we hold an exclusve lock fail. We can't
892 * reliably check to see if we hold a shared lock or
895 if (sx_xholder(sx) == curthread)
896 panic("Lock %s exclusively locked @ %s:%d\n",
897 sx->lock_object.lo_name, file, line);
901 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
905 #endif /* INVARIANT_SUPPORT */
909 db_show_sx(struct lock_object *lock)
914 sx = (struct sx *)lock;
916 db_printf(" state: ");
917 if (sx->sx_lock == SX_LOCK_UNLOCKED)
918 db_printf("UNLOCKED\n");
919 else if (sx->sx_lock & SX_LOCK_SHARED)
920 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
923 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
924 td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
926 db_printf(" recursed: %d\n", sx->sx_recurse);
929 db_printf(" waiters: ");
931 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
932 case SX_LOCK_SHARED_WAITERS:
933 db_printf("shared\n");
935 case SX_LOCK_EXCLUSIVE_WAITERS:
936 db_printf("exclusive\n");
938 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
939 db_printf("exclusive and shared\n");
947 * Check to see if a thread that is blocked on a sleep queue is actually
948 * blocked on an sx lock. If so, output some details and return true.
949 * If the lock has an exclusive owner, return that in *ownerp.
952 sx_chain(struct thread *td, struct thread **ownerp)
957 * Check to see if this thread is blocked on an sx lock.
958 * First, we check the lock class. If that is ok, then we
959 * compare the lock name against the wait message.
962 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
963 sx->lock_object.lo_name != td->td_wmesg)
966 /* We think we have an sx lock, so output some details. */
967 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
968 *ownerp = sx_xholder(sx);
969 if (sx->sx_lock & SX_LOCK_SHARED)
970 db_printf("SLOCK (count %ju)\n",
971 (uintmax_t)SX_SHARERS(sx->sx_lock));
973 db_printf("XLOCK\n");