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 assumes
100 * curthread 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 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
174 SX_NOPROFILE | SX_ADAPTIVESPIN)) == 0);
176 flags = LO_SLEEPABLE | LO_UPGRADABLE | LO_RECURSABLE;
179 if (opts & SX_NOPROFILE)
180 flags |= LO_NOPROFILE;
181 if (!(opts & SX_NOWITNESS))
186 flags |= opts & (SX_ADAPTIVESPIN | SX_RECURSE);
187 sx->sx_lock = SX_LOCK_UNLOCKED;
189 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
193 sx_destroy(struct sx *sx)
196 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
197 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
198 sx->sx_lock = SX_LOCK_DESTROYED;
199 lock_destroy(&sx->lock_object);
203 _sx_slock(struct sx *sx, const char *file, int line)
206 MPASS(curthread != NULL);
207 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
208 ("sx_slock() of destroyed sx @ %s:%d", file, line));
209 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line);
210 __sx_slock(sx, file, line);
211 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
212 WITNESS_LOCK(&sx->lock_object, 0, file, line);
213 curthread->td_locks++;
217 _sx_try_slock(struct sx *sx, const char *file, int line)
222 KASSERT(x != SX_LOCK_DESTROYED,
223 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
224 if ((x & SX_LOCK_SHARED) && atomic_cmpset_acq_ptr(&sx->sx_lock, x,
225 x + SX_ONE_SHARER)) {
226 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
227 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
228 curthread->td_locks++;
232 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
237 _sx_xlock(struct sx *sx, const char *file, int line)
240 MPASS(curthread != NULL);
241 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
242 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
243 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
245 __sx_xlock(sx, curthread, file, line);
246 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, file, line);
247 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
248 curthread->td_locks++;
252 _sx_try_xlock(struct sx *sx, const char *file, int line)
256 MPASS(curthread != NULL);
257 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
258 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
260 if (sx_xlocked(sx) && (sx->lock_object.lo_flags & SX_RECURSE) != 0) {
262 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
265 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
266 (uintptr_t)curthread);
267 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
269 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
271 curthread->td_locks++;
278 _sx_sunlock(struct sx *sx, const char *file, int line)
281 MPASS(curthread != NULL);
282 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
283 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
284 _sx_assert(sx, SX_SLOCKED, file, line);
285 curthread->td_locks--;
286 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
287 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
288 if (SX_SHARERS(sx->sx_lock) == 0)
289 lock_profile_release_lock(&sx->lock_object);
290 __sx_sunlock(sx, file, line);
294 _sx_xunlock(struct sx *sx, const char *file, int line)
297 MPASS(curthread != NULL);
298 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
299 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
300 _sx_assert(sx, SX_XLOCKED, file, line);
301 curthread->td_locks--;
302 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
303 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
305 if (!sx_recursed(sx))
306 lock_profile_release_lock(&sx->lock_object);
307 __sx_xunlock(sx, curthread, file, line);
311 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
312 * This will only succeed if this thread holds a single shared lock.
313 * Return 1 if if the upgrade succeed, 0 otherwise.
316 _sx_try_upgrade(struct sx *sx, const char *file, int line)
321 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
322 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
323 _sx_assert(sx, SX_SLOCKED, file, line);
326 * Try to switch from one shared lock to an exclusive lock. We need
327 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
328 * we will wake up the exclusive waiters when we drop the lock.
330 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
331 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
332 (uintptr_t)curthread | x);
333 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
335 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
341 * Downgrade an unrecursed exclusive lock into a single shared lock.
344 _sx_downgrade(struct sx *sx, const char *file, int line)
348 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
349 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
350 _sx_assert(sx, SX_XLOCKED | SX_NOTRECURSED, file, line);
353 panic("downgrade of a recursed lock");
356 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
359 * Try to switch from an exclusive lock with no shared waiters
360 * to one sharer with no shared waiters. If there are
361 * exclusive waiters, we don't need to lock the sleep queue so
362 * long as we preserve the flag. We do one quick try and if
363 * that fails we grab the sleepq lock to keep the flags from
364 * changing and do it the slow way.
366 * We have to lock the sleep queue if there are shared waiters
367 * so we can wake them up.
370 if (!(x & SX_LOCK_SHARED_WAITERS) &&
371 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
372 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
373 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
378 * Lock the sleep queue so we can read the waiters bits
379 * without any races and wakeup any shared waiters.
381 sleepq_lock(&sx->lock_object);
384 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
385 * shared lock. If there are any shared waiters, wake them up.
388 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
389 (x & SX_LOCK_EXCLUSIVE_WAITERS));
390 if (x & SX_LOCK_SHARED_WAITERS)
391 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1,
394 sleepq_release(&sx->lock_object);
396 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
400 * This function represents the so-called 'hard case' for sx_xlock
401 * operation. All 'easy case' failures are redirected to this. Note
402 * that ideally this would be a static function, but it needs to be
403 * accessible from at least sx.h.
406 _sx_xlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
410 volatile struct thread *owner;
414 uint64_t waitstart = 0;
416 /* If we already hold an exclusive lock, then recurse. */
417 if (sx_xlocked(sx)) {
418 KASSERT((sx->lock_object.lo_flags & SX_RECURSE) != 0,
419 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
420 sx->lock_object.lo_name, file, line));
422 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
423 if (LOCK_LOG_TEST(&sx->lock_object, 0))
424 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
427 lock_profile_obtain_lock_failed(&(sx)->lock_object,
428 &contested, &waitstart);
430 if (LOCK_LOG_TEST(&sx->lock_object, 0))
431 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
432 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
434 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
437 * If the lock is write locked and the owner is
438 * running on another CPU, spin until the owner stops
439 * running or the state of the lock changes.
442 if (!(x & SX_LOCK_SHARED) &&
443 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
445 owner = (struct thread *)x;
446 if (TD_IS_RUNNING(owner)) {
447 if (LOCK_LOG_TEST(&sx->lock_object, 0))
449 "%s: spinning on %p held by %p",
450 __func__, sx, owner);
452 while (SX_OWNER(sx->sx_lock) == x &&
453 TD_IS_RUNNING(owner))
460 sleepq_lock(&sx->lock_object);
464 * If the lock was released while spinning on the
465 * sleep queue chain lock, try again.
467 if (x == SX_LOCK_UNLOCKED) {
468 sleepq_release(&sx->lock_object);
474 * The current lock owner might have started executing
475 * on another CPU (or the lock could have changed
476 * owners) while we were waiting on the sleep queue
477 * chain lock. If so, drop the sleep queue lock and try
480 if (!(x & SX_LOCK_SHARED) &&
481 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
482 owner = (struct thread *)SX_OWNER(x);
483 if (TD_IS_RUNNING(owner)) {
484 sleepq_release(&sx->lock_object);
491 * If an exclusive lock was released with both shared
492 * and exclusive waiters and a shared waiter hasn't
493 * woken up and acquired the lock yet, sx_lock will be
494 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
495 * If we see that value, try to acquire it once. Note
496 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
497 * as there are other exclusive waiters still. If we
498 * fail, restart the loop.
500 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
501 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
502 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
503 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
504 sleepq_release(&sx->lock_object);
505 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
509 sleepq_release(&sx->lock_object);
514 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
515 * than loop back and retry.
517 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
518 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
519 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
520 sleepq_release(&sx->lock_object);
523 if (LOCK_LOG_TEST(&sx->lock_object, 0))
524 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
529 * Since we have been unable to acquire the exclusive
530 * lock and the exclusive waiters flag is set, we have
533 if (LOCK_LOG_TEST(&sx->lock_object, 0))
534 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
538 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
539 SLEEPQ_SX, SQ_EXCLUSIVE_QUEUE);
540 sleepq_wait(&sx->lock_object);
542 if (LOCK_LOG_TEST(&sx->lock_object, 0))
543 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
548 lock_profile_obtain_lock_success(&(sx)->lock_object, contested,
549 waitstart, file, line);
553 * This function represents the so-called 'hard case' for sx_xunlock
554 * operation. All 'easy case' failures are redirected to this. Note
555 * that ideally this would be a static function, but it needs to be
556 * accessible from at least sx.h.
559 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
564 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
566 /* If the lock is recursed, then unrecurse one level. */
567 if (sx_xlocked(sx) && sx_recursed(sx)) {
568 if ((--sx->sx_recurse) == 0)
569 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
570 if (LOCK_LOG_TEST(&sx->lock_object, 0))
571 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
574 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
575 SX_LOCK_EXCLUSIVE_WAITERS));
576 if (LOCK_LOG_TEST(&sx->lock_object, 0))
577 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
579 sleepq_lock(&sx->lock_object);
580 x = SX_LOCK_UNLOCKED;
583 * The wake up algorithm here is quite simple and probably not
584 * ideal. It gives precedence to shared waiters if they are
585 * present. For this condition, we have to preserve the
586 * state of the exclusive waiters flag.
588 if (sx->sx_lock & SX_LOCK_SHARED_WAITERS) {
589 queue = SQ_SHARED_QUEUE;
590 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
592 queue = SQ_EXCLUSIVE_QUEUE;
594 /* Wake up all the waiters for the specific queue. */
595 if (LOCK_LOG_TEST(&sx->lock_object, 0))
596 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
597 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
599 atomic_store_rel_ptr(&sx->sx_lock, x);
600 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1, queue);
604 * This function represents the so-called 'hard case' for sx_slock
605 * operation. All 'easy case' failures are redirected to this. Note
606 * that ideally this would be a static function, but it needs to be
607 * accessible from at least sx.h.
610 _sx_slock_hard(struct sx *sx, const char *file, int line)
614 volatile struct thread *owner;
617 uint64_t waitstart = 0;
620 * As with rwlocks, we don't make any attempt to try to block
621 * shared locks once there is an exclusive waiter.
628 * If no other thread has an exclusive lock then try to bump up
629 * the count of sharers. Since we have to preserve the state
630 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
631 * shared lock loop back and retry.
633 if (x & SX_LOCK_SHARED) {
634 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
635 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
636 x + SX_ONE_SHARER)) {
637 if (SX_SHARERS(x) == 0)
638 lock_profile_obtain_lock_success(
639 &sx->lock_object, contested,
640 waitstart, file, line);
641 if (LOCK_LOG_TEST(&sx->lock_object, 0))
643 "%s: %p succeed %p -> %p", __func__,
645 (void *)(x + SX_ONE_SHARER));
648 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
656 * If the owner is running on another CPU, spin until
657 * the owner stops running or the state of the lock
660 else if (sx->lock_object.lo_flags & SX_ADAPTIVESPIN) {
662 owner = (struct thread *)x;
663 if (TD_IS_RUNNING(owner)) {
664 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
666 if (LOCK_LOG_TEST(&sx->lock_object, 0))
668 "%s: spinning on %p held by %p",
669 __func__, sx, owner);
671 while (SX_OWNER(sx->sx_lock) == x &&
672 TD_IS_RUNNING(owner))
679 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
683 * Some other thread already has an exclusive lock, so
684 * start the process of blocking.
686 sleepq_lock(&sx->lock_object);
690 * The lock could have been released while we spun.
691 * In this case loop back and retry.
693 if (x & SX_LOCK_SHARED) {
694 sleepq_release(&sx->lock_object);
700 * If the owner is running on another CPU, spin until
701 * the owner stops running or the state of the lock
704 if (!(x & SX_LOCK_SHARED) &&
705 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
706 owner = (struct thread *)SX_OWNER(x);
707 if (TD_IS_RUNNING(owner)) {
708 sleepq_release(&sx->lock_object);
715 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
716 * fail to set it drop the sleep queue lock and loop
719 if (!(x & SX_LOCK_SHARED_WAITERS)) {
720 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
721 x | SX_LOCK_SHARED_WAITERS)) {
722 sleepq_release(&sx->lock_object);
725 if (LOCK_LOG_TEST(&sx->lock_object, 0))
726 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
731 * Since we have been unable to acquire the shared lock,
734 if (LOCK_LOG_TEST(&sx->lock_object, 0))
735 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
739 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
740 SLEEPQ_SX, SQ_SHARED_QUEUE);
741 sleepq_wait(&sx->lock_object);
743 if (LOCK_LOG_TEST(&sx->lock_object, 0))
744 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
752 * This function represents the so-called 'hard case' for sx_sunlock
753 * operation. All 'easy case' failures are redirected to this. Note
754 * that ideally this would be a static function, but it needs to be
755 * accessible from at least sx.h.
758 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
766 * We should never have sharers while at least one thread
767 * holds a shared lock.
769 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
770 ("%s: waiting sharers", __func__));
773 * See if there is more than one shared lock held. If
774 * so, just drop one and return.
776 if (SX_SHARERS(x) > 1) {
777 if (atomic_cmpset_ptr(&sx->sx_lock, x,
778 x - SX_ONE_SHARER)) {
779 if (LOCK_LOG_TEST(&sx->lock_object, 0))
781 "%s: %p succeeded %p -> %p",
782 __func__, sx, (void *)x,
783 (void *)(x - SX_ONE_SHARER));
790 * If there aren't any waiters for an exclusive lock,
791 * then try to drop it quickly.
793 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
794 MPASS(x == SX_SHARERS_LOCK(1));
795 if (atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1),
797 lock_profile_release_lock(&sx->lock_object);
798 if (LOCK_LOG_TEST(&sx->lock_object, 0))
799 CTR2(KTR_LOCK, "%s: %p last succeeded",
807 * At this point, there should just be one sharer with
810 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
812 lock_profile_release_lock(&sx->lock_object);
813 sleepq_lock(&sx->lock_object);
816 * Wake up semantic here is quite simple:
817 * Just wake up all the exclusive waiters.
818 * Note that the state of the lock could have changed,
819 * so if it fails loop back and retry.
821 if (!atomic_cmpset_ptr(&sx->sx_lock,
822 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
824 sleepq_release(&sx->lock_object);
827 if (LOCK_LOG_TEST(&sx->lock_object, 0))
828 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
829 "exclusive queue", __func__, sx);
830 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1,
836 #ifdef INVARIANT_SUPPORT
842 * In the non-WITNESS case, sx_assert() can only detect that at least
843 * *some* thread owns an slock, but it cannot guarantee that *this*
844 * thread owns an slock.
847 _sx_assert(struct sx *sx, int what, const char *file, int line)
853 if (panicstr != NULL)
857 case SX_SLOCKED | SX_NOTRECURSED:
858 case SX_SLOCKED | SX_RECURSED:
864 case SX_LOCKED | SX_NOTRECURSED:
865 case SX_LOCKED | SX_RECURSED:
867 witness_assert(&sx->lock_object, what, file, line);
870 * If some other thread has an exclusive lock or we
871 * have one and are asserting a shared lock, fail.
872 * Also, if no one has a lock at all, fail.
874 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
875 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
876 sx_xholder(sx) != curthread)))
877 panic("Lock %s not %slocked @ %s:%d\n",
878 sx->lock_object.lo_name, slocked ? "share " : "",
881 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
882 if (sx_recursed(sx)) {
883 if (what & SX_NOTRECURSED)
884 panic("Lock %s recursed @ %s:%d\n",
885 sx->lock_object.lo_name, file,
887 } else if (what & SX_RECURSED)
888 panic("Lock %s not recursed @ %s:%d\n",
889 sx->lock_object.lo_name, file, line);
894 case SX_XLOCKED | SX_NOTRECURSED:
895 case SX_XLOCKED | SX_RECURSED:
896 if (sx_xholder(sx) != curthread)
897 panic("Lock %s not exclusively locked @ %s:%d\n",
898 sx->lock_object.lo_name, file, line);
899 if (sx_recursed(sx)) {
900 if (what & SX_NOTRECURSED)
901 panic("Lock %s recursed @ %s:%d\n",
902 sx->lock_object.lo_name, file, line);
903 } else if (what & SX_RECURSED)
904 panic("Lock %s not recursed @ %s:%d\n",
905 sx->lock_object.lo_name, file, line);
909 witness_assert(&sx->lock_object, what, file, line);
912 * If we hold an exclusve lock fail. We can't
913 * reliably check to see if we hold a shared lock or
916 if (sx_xholder(sx) == curthread)
917 panic("Lock %s exclusively locked @ %s:%d\n",
918 sx->lock_object.lo_name, file, line);
922 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
926 #endif /* INVARIANT_SUPPORT */
930 db_show_sx(struct lock_object *lock)
935 sx = (struct sx *)lock;
937 db_printf(" state: ");
938 if (sx->sx_lock == SX_LOCK_UNLOCKED)
939 db_printf("UNLOCKED\n");
940 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
941 db_printf("DESTROYED\n");
943 } else if (sx->sx_lock & SX_LOCK_SHARED)
944 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
947 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
948 td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
950 db_printf(" recursed: %d\n", sx->sx_recurse);
953 db_printf(" waiters: ");
955 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
956 case SX_LOCK_SHARED_WAITERS:
957 db_printf("shared\n");
959 case SX_LOCK_EXCLUSIVE_WAITERS:
960 db_printf("exclusive\n");
962 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
963 db_printf("exclusive and shared\n");
971 * Check to see if a thread that is blocked on a sleep queue is actually
972 * blocked on an sx lock. If so, output some details and return true.
973 * If the lock has an exclusive owner, return that in *ownerp.
976 sx_chain(struct thread *td, struct thread **ownerp)
981 * Check to see if this thread is blocked on an sx lock.
982 * First, we check the lock class. If that is ok, then we
983 * compare the lock name against the wait message.
986 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
987 sx->lock_object.lo_name != td->td_wmesg)
990 /* We think we have an sx lock, so output some details. */
991 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
992 *ownerp = sx_xholder(sx);
993 if (sx->sx_lock & SX_LOCK_SHARED)
994 db_printf("SLOCK (count %ju)\n",
995 (uintmax_t)SX_SHARERS(sx->sx_lock));
997 db_printf("XLOCK\n");