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 CTASSERT(((SX_ADAPTIVESPIN
| SX_RECURSE
) & LO_CLASSFLAGS
) ==
67 (SX_ADAPTIVESPIN
| SX_RECURSE
));
69 /* Handy macros for sleep queues. */
70 #define SQ_EXCLUSIVE_QUEUE 0
71 #define SQ_SHARED_QUEUE 1
74 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
75 * drop Giant anytime we have to sleep or if we adaptively spin.
77 #define GIANT_DECLARE \
79 WITNESS_SAVE_DECL(Giant) \
81 #define GIANT_SAVE() do { \
82 if (mtx_owned(&Giant)) { \
83 WITNESS_SAVE(&Giant.lock_object, Giant); \
84 while (mtx_owned(&Giant)) { \
91 #define GIANT_RESTORE() do { \
92 if (_giantcnt > 0) { \
93 mtx_assert(&Giant, MA_NOTOWNED); \
96 WITNESS_RESTORE(&Giant.lock_object, Giant); \
101 * Returns true if an exclusive lock is recursed. It assumes
102 * curthread currently has an exclusive lock.
104 #define sx_recurse lock_object.lo_data
105 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
107 static void assert_sx(struct lock_object
*lock
, int what
);
109 static void db_show_sx(struct lock_object
*lock
);
111 static void lock_sx(struct lock_object
*lock
, int how
);
112 static int unlock_sx(struct lock_object
*lock
);
114 struct lock_class lock_class_sx
= {
116 .lc_flags
= LC_SLEEPLOCK
| LC_SLEEPABLE
| LC_RECURSABLE
| LC_UPGRADABLE
,
117 .lc_assert
= assert_sx
,
119 .lc_ddb_show
= db_show_sx
,
122 .lc_unlock
= unlock_sx
,
126 #define _sx_assert(sx, what, file, line)
130 assert_sx(struct lock_object
*lock
, int what
)
133 sx_assert((struct sx
*)lock
, what
);
137 lock_sx(struct lock_object
*lock
, int how
)
141 sx
= (struct sx
*)lock
;
149 unlock_sx(struct lock_object
*lock
)
153 sx
= (struct sx
*)lock
;
154 sx_assert(sx
, SA_LOCKED
| SA_NOTRECURSED
);
155 if (sx_xlocked(sx
)) {
165 sx_sysinit(void *arg
)
167 struct sx_args
*sargs
= arg
;
169 sx_init(sargs
->sa_sx
, sargs
->sa_desc
);
173 sx_init_flags(struct sx
*sx
, const char *description
, int opts
)
177 MPASS((opts
& ~(SX_QUIET
| SX_RECURSE
| SX_NOWITNESS
| SX_DUPOK
|
178 SX_NOPROFILE
| SX_ADAPTIVESPIN
)) == 0);
180 flags
= LO_RECURSABLE
| LO_SLEEPABLE
| LO_UPGRADABLE
;
183 if (opts
& SX_NOPROFILE
)
184 flags
|= LO_NOPROFILE
;
185 if (!(opts
& SX_NOWITNESS
))
190 flags
|= opts
& (SX_ADAPTIVESPIN
| SX_RECURSE
);
191 sx
->sx_lock
= SX_LOCK_UNLOCKED
;
193 lock_init(&sx
->lock_object
, &lock_class_sx
, description
, NULL
, flags
);
197 sx_destroy(struct sx
*sx
)
200 KASSERT(sx
->sx_lock
== SX_LOCK_UNLOCKED
, ("sx lock still held"));
201 KASSERT(sx
->sx_recurse
== 0, ("sx lock still recursed"));
202 sx
->sx_lock
= SX_LOCK_DESTROYED
;
203 lock_destroy(&sx
->lock_object
);
207 _sx_slock(struct sx
*sx
, int opts
, const char *file
, int line
)
211 MPASS(curthread
!= NULL
);
212 KASSERT(sx
->sx_lock
!= SX_LOCK_DESTROYED
,
213 ("sx_slock() of destroyed sx @ %s:%d", file
, line
));
214 WITNESS_CHECKORDER(&sx
->lock_object
, LOP_NEWORDER
, file
, line
);
215 error
= __sx_slock(sx
, opts
, file
, line
);
217 LOCK_LOG_LOCK("SLOCK", &sx
->lock_object
, 0, 0, file
, line
);
218 WITNESS_LOCK(&sx
->lock_object
, 0, file
, line
);
219 curthread
->td_locks
++;
226 _sx_try_slock(struct sx
*sx
, const char *file
, int line
)
232 KASSERT(x
!= SX_LOCK_DESTROYED
,
233 ("sx_try_slock() of destroyed sx @ %s:%d", file
, line
));
234 if (!(x
& SX_LOCK_SHARED
))
236 if (atomic_cmpset_acq_ptr(&sx
->sx_lock
, x
, x
+ SX_ONE_SHARER
)) {
237 LOCK_LOG_TRY("SLOCK", &sx
->lock_object
, 0, 1, file
, line
);
238 WITNESS_LOCK(&sx
->lock_object
, LOP_TRYLOCK
, file
, line
);
239 curthread
->td_locks
++;
244 LOCK_LOG_TRY("SLOCK", &sx
->lock_object
, 0, 0, file
, line
);
249 _sx_xlock(struct sx
*sx
, int opts
, const char *file
, int line
)
253 MPASS(curthread
!= NULL
);
254 KASSERT(sx
->sx_lock
!= SX_LOCK_DESTROYED
,
255 ("sx_xlock() of destroyed sx @ %s:%d", file
, line
));
256 WITNESS_CHECKORDER(&sx
->lock_object
, LOP_NEWORDER
| LOP_EXCLUSIVE
, file
,
258 error
= __sx_xlock(sx
, curthread
, opts
, file
, line
);
260 LOCK_LOG_LOCK("XLOCK", &sx
->lock_object
, 0, sx
->sx_recurse
,
262 WITNESS_LOCK(&sx
->lock_object
, LOP_EXCLUSIVE
, file
, line
);
263 curthread
->td_locks
++;
270 _sx_try_xlock(struct sx
*sx
, const char *file
, int line
)
274 MPASS(curthread
!= NULL
);
275 KASSERT(sx
->sx_lock
!= SX_LOCK_DESTROYED
,
276 ("sx_try_xlock() of destroyed sx @ %s:%d", file
, line
));
278 if (sx_xlocked(sx
) && (sx
->lock_object
.lo_flags
& SX_RECURSE
) != 0) {
280 atomic_set_ptr(&sx
->sx_lock
, SX_LOCK_RECURSED
);
283 rval
= atomic_cmpset_acq_ptr(&sx
->sx_lock
, SX_LOCK_UNLOCKED
,
284 (uintptr_t)curthread
);
285 LOCK_LOG_TRY("XLOCK", &sx
->lock_object
, 0, rval
, file
, line
);
287 WITNESS_LOCK(&sx
->lock_object
, LOP_EXCLUSIVE
| LOP_TRYLOCK
,
289 curthread
->td_locks
++;
296 _sx_sunlock(struct sx
*sx
, const char *file
, int line
)
299 MPASS(curthread
!= NULL
);
300 KASSERT(sx
->sx_lock
!= SX_LOCK_DESTROYED
,
301 ("sx_sunlock() of destroyed sx @ %s:%d", file
, line
));
302 _sx_assert(sx
, SA_SLOCKED
, file
, line
);
303 curthread
->td_locks
--;
304 WITNESS_UNLOCK(&sx
->lock_object
, 0, file
, line
);
305 LOCK_LOG_LOCK("SUNLOCK", &sx
->lock_object
, 0, 0, file
, line
);
306 __sx_sunlock(sx
, file
, line
);
307 lock_profile_release_lock(&sx
->lock_object
);
311 _sx_xunlock(struct sx
*sx
, const char *file
, int line
)
314 MPASS(curthread
!= NULL
);
315 KASSERT(sx
->sx_lock
!= SX_LOCK_DESTROYED
,
316 ("sx_xunlock() of destroyed sx @ %s:%d", file
, line
));
317 _sx_assert(sx
, SA_XLOCKED
, file
, line
);
318 curthread
->td_locks
--;
319 WITNESS_UNLOCK(&sx
->lock_object
, LOP_EXCLUSIVE
, file
, line
);
320 LOCK_LOG_LOCK("XUNLOCK", &sx
->lock_object
, 0, sx
->sx_recurse
, file
,
322 if (!sx_recursed(sx
))
323 lock_profile_release_lock(&sx
->lock_object
);
324 __sx_xunlock(sx
, curthread
, file
, line
);
328 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
329 * This will only succeed if this thread holds a single shared lock.
330 * Return 1 if if the upgrade succeed, 0 otherwise.
333 _sx_try_upgrade(struct sx
*sx
, const char *file
, int line
)
338 KASSERT(sx
->sx_lock
!= SX_LOCK_DESTROYED
,
339 ("sx_try_upgrade() of destroyed sx @ %s:%d", file
, line
));
340 _sx_assert(sx
, SA_SLOCKED
, file
, line
);
343 * Try to switch from one shared lock to an exclusive lock. We need
344 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
345 * we will wake up the exclusive waiters when we drop the lock.
347 x
= sx
->sx_lock
& SX_LOCK_EXCLUSIVE_WAITERS
;
348 success
= atomic_cmpset_ptr(&sx
->sx_lock
, SX_SHARERS_LOCK(1) | x
,
349 (uintptr_t)curthread
| x
);
350 LOCK_LOG_TRY("XUPGRADE", &sx
->lock_object
, 0, success
, file
, line
);
352 WITNESS_UPGRADE(&sx
->lock_object
, LOP_EXCLUSIVE
| LOP_TRYLOCK
,
358 * Downgrade an unrecursed exclusive lock into a single shared lock.
361 _sx_downgrade(struct sx
*sx
, const char *file
, int line
)
366 KASSERT(sx
->sx_lock
!= SX_LOCK_DESTROYED
,
367 ("sx_downgrade() of destroyed sx @ %s:%d", file
, line
));
368 _sx_assert(sx
, SA_XLOCKED
| SA_NOTRECURSED
, file
, line
);
371 panic("downgrade of a recursed lock");
374 WITNESS_DOWNGRADE(&sx
->lock_object
, 0, file
, line
);
377 * Try to switch from an exclusive lock with no shared waiters
378 * to one sharer with no shared waiters. If there are
379 * exclusive waiters, we don't need to lock the sleep queue so
380 * long as we preserve the flag. We do one quick try and if
381 * that fails we grab the sleepq lock to keep the flags from
382 * changing and do it the slow way.
384 * We have to lock the sleep queue if there are shared waiters
385 * so we can wake them up.
388 if (!(x
& SX_LOCK_SHARED_WAITERS
) &&
389 atomic_cmpset_rel_ptr(&sx
->sx_lock
, x
, SX_SHARERS_LOCK(1) |
390 (x
& SX_LOCK_EXCLUSIVE_WAITERS
))) {
391 LOCK_LOG_LOCK("XDOWNGRADE", &sx
->lock_object
, 0, 0, file
, line
);
396 * Lock the sleep queue so we can read the waiters bits
397 * without any races and wakeup any shared waiters.
399 sleepq_lock(&sx
->lock_object
);
402 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
403 * shared lock. If there are any shared waiters, wake them up.
407 atomic_store_rel_ptr(&sx
->sx_lock
, SX_SHARERS_LOCK(1) |
408 (x
& SX_LOCK_EXCLUSIVE_WAITERS
));
409 if (x
& SX_LOCK_SHARED_WAITERS
)
410 wakeup_swapper
= sleepq_broadcast(&sx
->lock_object
, SLEEPQ_SX
,
412 sleepq_release(&sx
->lock_object
);
414 LOCK_LOG_LOCK("XDOWNGRADE", &sx
->lock_object
, 0, 0, file
, line
);
421 * This function represents the so-called 'hard case' for sx_xlock
422 * operation. All 'easy case' failures are redirected to this. Note
423 * that ideally this would be a static function, but it needs to be
424 * accessible from at least sx.h.
427 _sx_xlock_hard(struct sx
*sx
, uintptr_t tid
, int opts
, const char *file
,
432 volatile struct thread
*owner
;
434 uint64_t waittime
= 0;
436 int contested
= 0, error
= 0;
438 /* If we already hold an exclusive lock, then recurse. */
439 if (sx_xlocked(sx
)) {
440 KASSERT((sx
->lock_object
.lo_flags
& SX_RECURSE
) != 0,
441 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
442 sx
->lock_object
.lo_name
, file
, line
));
444 atomic_set_ptr(&sx
->sx_lock
, SX_LOCK_RECURSED
);
445 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
446 CTR2(KTR_LOCK
, "%s: %p recursing", __func__
, sx
);
450 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
451 CTR5(KTR_LOCK
, "%s: %s contested (lock=%p) at %s:%d", __func__
,
452 sx
->lock_object
.lo_name
, (void *)sx
->sx_lock
, file
, line
);
454 while (!atomic_cmpset_acq_ptr(&sx
->sx_lock
, SX_LOCK_UNLOCKED
, tid
)) {
455 lock_profile_obtain_lock_failed(&sx
->lock_object
, &contested
,
459 * If the lock is write locked and the owner is
460 * running on another CPU, spin until the owner stops
461 * running or the state of the lock changes.
464 if (!(x
& SX_LOCK_SHARED
) &&
465 (sx
->lock_object
.lo_flags
& SX_ADAPTIVESPIN
)) {
467 owner
= (struct thread
*)x
;
468 if (TD_IS_RUNNING(owner
)) {
469 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
471 "%s: spinning on %p held by %p",
472 __func__
, sx
, owner
);
474 while (SX_OWNER(sx
->sx_lock
) == x
&&
475 TD_IS_RUNNING(owner
))
482 sleepq_lock(&sx
->lock_object
);
486 * If the lock was released while spinning on the
487 * sleep queue chain lock, try again.
489 if (x
== SX_LOCK_UNLOCKED
) {
490 sleepq_release(&sx
->lock_object
);
496 * The current lock owner might have started executing
497 * on another CPU (or the lock could have changed
498 * owners) while we were waiting on the sleep queue
499 * chain lock. If so, drop the sleep queue lock and try
502 if (!(x
& SX_LOCK_SHARED
) &&
503 (sx
->lock_object
.lo_flags
& SX_ADAPTIVESPIN
)) {
504 owner
= (struct thread
*)SX_OWNER(x
);
505 if (TD_IS_RUNNING(owner
)) {
506 sleepq_release(&sx
->lock_object
);
513 * If an exclusive lock was released with both shared
514 * and exclusive waiters and a shared waiter hasn't
515 * woken up and acquired the lock yet, sx_lock will be
516 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
517 * If we see that value, try to acquire it once. Note
518 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
519 * as there are other exclusive waiters still. If we
520 * fail, restart the loop.
522 if (x
== (SX_LOCK_UNLOCKED
| SX_LOCK_EXCLUSIVE_WAITERS
)) {
523 if (atomic_cmpset_acq_ptr(&sx
->sx_lock
,
524 SX_LOCK_UNLOCKED
| SX_LOCK_EXCLUSIVE_WAITERS
,
525 tid
| SX_LOCK_EXCLUSIVE_WAITERS
)) {
526 sleepq_release(&sx
->lock_object
);
527 CTR2(KTR_LOCK
, "%s: %p claimed by new writer",
531 sleepq_release(&sx
->lock_object
);
536 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
537 * than loop back and retry.
539 if (!(x
& SX_LOCK_EXCLUSIVE_WAITERS
)) {
540 if (!atomic_cmpset_ptr(&sx
->sx_lock
, x
,
541 x
| SX_LOCK_EXCLUSIVE_WAITERS
)) {
542 sleepq_release(&sx
->lock_object
);
545 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
546 CTR2(KTR_LOCK
, "%s: %p set excl waiters flag",
551 * Since we have been unable to acquire the exclusive
552 * lock and the exclusive waiters flag is set, we have
555 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
556 CTR2(KTR_LOCK
, "%s: %p blocking on sleep queue",
560 sleepq_add(&sx
->lock_object
, NULL
, sx
->lock_object
.lo_name
,
561 SLEEPQ_SX
| ((opts
& SX_INTERRUPTIBLE
) ?
562 SLEEPQ_INTERRUPTIBLE
: 0), SQ_EXCLUSIVE_QUEUE
);
563 if (!(opts
& SX_INTERRUPTIBLE
))
564 sleepq_wait(&sx
->lock_object
, 0);
566 error
= sleepq_wait_sig(&sx
->lock_object
, 0);
569 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
571 "%s: interruptible sleep by %p suspended by signal",
575 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
576 CTR2(KTR_LOCK
, "%s: %p resuming from sleep queue",
582 lock_profile_obtain_lock_success(&sx
->lock_object
, contested
,
583 waittime
, file
, line
);
588 * This function represents the so-called 'hard case' for sx_xunlock
589 * operation. All 'easy case' failures are redirected to this. Note
590 * that ideally this would be a static function, but it needs to be
591 * accessible from at least sx.h.
594 _sx_xunlock_hard(struct sx
*sx
, uintptr_t tid
, const char *file
, int line
)
597 int queue
, wakeup_swapper
;
599 MPASS(!(sx
->sx_lock
& SX_LOCK_SHARED
));
601 /* If the lock is recursed, then unrecurse one level. */
602 if (sx_xlocked(sx
) && sx_recursed(sx
)) {
603 if ((--sx
->sx_recurse
) == 0)
604 atomic_clear_ptr(&sx
->sx_lock
, SX_LOCK_RECURSED
);
605 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
606 CTR2(KTR_LOCK
, "%s: %p unrecursing", __func__
, sx
);
609 MPASS(sx
->sx_lock
& (SX_LOCK_SHARED_WAITERS
|
610 SX_LOCK_EXCLUSIVE_WAITERS
));
611 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
612 CTR2(KTR_LOCK
, "%s: %p contested", __func__
, sx
);
614 sleepq_lock(&sx
->lock_object
);
615 x
= SX_LOCK_UNLOCKED
;
618 * The wake up algorithm here is quite simple and probably not
619 * ideal. It gives precedence to shared waiters if they are
620 * present. For this condition, we have to preserve the
621 * state of the exclusive waiters flag.
623 if (sx
->sx_lock
& SX_LOCK_SHARED_WAITERS
) {
624 queue
= SQ_SHARED_QUEUE
;
625 x
|= (sx
->sx_lock
& SX_LOCK_EXCLUSIVE_WAITERS
);
627 queue
= SQ_EXCLUSIVE_QUEUE
;
629 /* Wake up all the waiters for the specific queue. */
630 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
631 CTR3(KTR_LOCK
, "%s: %p waking up all threads on %s queue",
632 __func__
, sx
, queue
== SQ_SHARED_QUEUE
? "shared" :
634 atomic_store_rel_ptr(&sx
->sx_lock
, x
);
635 wakeup_swapper
= sleepq_broadcast(&sx
->lock_object
, SLEEPQ_SX
, 0,
637 sleepq_release(&sx
->lock_object
);
643 * This function represents the so-called 'hard case' for sx_slock
644 * operation. All 'easy case' failures are redirected to this. Note
645 * that ideally this would be a static function, but it needs to be
646 * accessible from at least sx.h.
649 _sx_slock_hard(struct sx
*sx
, int opts
, const char *file
, int line
)
653 volatile struct thread
*owner
;
655 uint64_t waittime
= 0;
661 * As with rwlocks, we don't make any attempt to try to block
662 * shared locks once there is an exclusive waiter.
668 * If no other thread has an exclusive lock then try to bump up
669 * the count of sharers. Since we have to preserve the state
670 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
671 * shared lock loop back and retry.
673 if (x
& SX_LOCK_SHARED
) {
674 MPASS(!(x
& SX_LOCK_SHARED_WAITERS
));
675 if (atomic_cmpset_acq_ptr(&sx
->sx_lock
, x
,
676 x
+ SX_ONE_SHARER
)) {
677 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
679 "%s: %p succeed %p -> %p", __func__
,
681 (void *)(x
+ SX_ONE_SHARER
));
686 lock_profile_obtain_lock_failed(&sx
->lock_object
, &contested
,
691 * If the owner is running on another CPU, spin until
692 * the owner stops running or the state of the lock
695 if (sx
->lock_object
.lo_flags
& SX_ADAPTIVESPIN
) {
697 owner
= (struct thread
*)x
;
698 if (TD_IS_RUNNING(owner
)) {
699 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
701 "%s: spinning on %p held by %p",
702 __func__
, sx
, owner
);
704 while (SX_OWNER(sx
->sx_lock
) == x
&&
705 TD_IS_RUNNING(owner
))
713 * Some other thread already has an exclusive lock, so
714 * start the process of blocking.
716 sleepq_lock(&sx
->lock_object
);
720 * The lock could have been released while we spun.
721 * In this case loop back and retry.
723 if (x
& SX_LOCK_SHARED
) {
724 sleepq_release(&sx
->lock_object
);
730 * If the owner is running on another CPU, spin until
731 * the owner stops running or the state of the lock
734 if (!(x
& SX_LOCK_SHARED
) &&
735 (sx
->lock_object
.lo_flags
& SX_ADAPTIVESPIN
)) {
736 owner
= (struct thread
*)SX_OWNER(x
);
737 if (TD_IS_RUNNING(owner
)) {
738 sleepq_release(&sx
->lock_object
);
745 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
746 * fail to set it drop the sleep queue lock and loop
749 if (!(x
& SX_LOCK_SHARED_WAITERS
)) {
750 if (!atomic_cmpset_ptr(&sx
->sx_lock
, x
,
751 x
| SX_LOCK_SHARED_WAITERS
)) {
752 sleepq_release(&sx
->lock_object
);
755 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
756 CTR2(KTR_LOCK
, "%s: %p set shared waiters flag",
761 * Since we have been unable to acquire the shared lock,
764 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
765 CTR2(KTR_LOCK
, "%s: %p blocking on sleep queue",
769 sleepq_add(&sx
->lock_object
, NULL
, sx
->lock_object
.lo_name
,
770 SLEEPQ_SX
| ((opts
& SX_INTERRUPTIBLE
) ?
771 SLEEPQ_INTERRUPTIBLE
: 0), SQ_SHARED_QUEUE
);
772 if (!(opts
& SX_INTERRUPTIBLE
))
773 sleepq_wait(&sx
->lock_object
, 0);
775 error
= sleepq_wait_sig(&sx
->lock_object
, 0);
778 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
780 "%s: interruptible sleep by %p suspended by signal",
784 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
785 CTR2(KTR_LOCK
, "%s: %p resuming from sleep queue",
789 lock_profile_obtain_lock_success(&sx
->lock_object
, contested
,
790 waittime
, file
, line
);
797 * This function represents the so-called 'hard case' for sx_sunlock
798 * operation. All 'easy case' failures are redirected to this. Note
799 * that ideally this would be a static function, but it needs to be
800 * accessible from at least sx.h.
803 _sx_sunlock_hard(struct sx
*sx
, const char *file
, int line
)
812 * We should never have sharers while at least one thread
813 * holds a shared lock.
815 KASSERT(!(x
& SX_LOCK_SHARED_WAITERS
),
816 ("%s: waiting sharers", __func__
));
819 * See if there is more than one shared lock held. If
820 * so, just drop one and return.
822 if (SX_SHARERS(x
) > 1) {
823 if (atomic_cmpset_ptr(&sx
->sx_lock
, x
,
824 x
- SX_ONE_SHARER
)) {
825 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
827 "%s: %p succeeded %p -> %p",
828 __func__
, sx
, (void *)x
,
829 (void *)(x
- SX_ONE_SHARER
));
836 * If there aren't any waiters for an exclusive lock,
837 * then try to drop it quickly.
839 if (!(x
& SX_LOCK_EXCLUSIVE_WAITERS
)) {
840 MPASS(x
== SX_SHARERS_LOCK(1));
841 if (atomic_cmpset_ptr(&sx
->sx_lock
, SX_SHARERS_LOCK(1),
843 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
844 CTR2(KTR_LOCK
, "%s: %p last succeeded",
852 * At this point, there should just be one sharer with
855 MPASS(x
== (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS
));
857 sleepq_lock(&sx
->lock_object
);
860 * Wake up semantic here is quite simple:
861 * Just wake up all the exclusive waiters.
862 * Note that the state of the lock could have changed,
863 * so if it fails loop back and retry.
865 if (!atomic_cmpset_ptr(&sx
->sx_lock
,
866 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS
,
868 sleepq_release(&sx
->lock_object
);
871 if (LOCK_LOG_TEST(&sx
->lock_object
, 0))
872 CTR2(KTR_LOCK
, "%s: %p waking up all thread on"
873 "exclusive queue", __func__
, sx
);
874 wakeup_swapper
= sleepq_broadcast(&sx
->lock_object
, SLEEPQ_SX
,
875 0, SQ_EXCLUSIVE_QUEUE
);
876 sleepq_release(&sx
->lock_object
);
883 #ifdef INVARIANT_SUPPORT
889 * In the non-WITNESS case, sx_assert() can only detect that at least
890 * *some* thread owns an slock, but it cannot guarantee that *this*
891 * thread owns an slock.
894 _sx_assert(struct sx
*sx
, int what
, const char *file
, int line
)
900 if (panicstr
!= NULL
)
904 case SA_SLOCKED
| SA_NOTRECURSED
:
905 case SA_SLOCKED
| SA_RECURSED
:
911 case SA_LOCKED
| SA_NOTRECURSED
:
912 case SA_LOCKED
| SA_RECURSED
:
914 witness_assert(&sx
->lock_object
, what
, file
, line
);
917 * If some other thread has an exclusive lock or we
918 * have one and are asserting a shared lock, fail.
919 * Also, if no one has a lock at all, fail.
921 if (sx
->sx_lock
== SX_LOCK_UNLOCKED
||
922 (!(sx
->sx_lock
& SX_LOCK_SHARED
) && (slocked
||
923 sx_xholder(sx
) != curthread
)))
924 panic("Lock %s not %slocked @ %s:%d\n",
925 sx
->lock_object
.lo_name
, slocked
? "share " : "",
928 if (!(sx
->sx_lock
& SX_LOCK_SHARED
)) {
929 if (sx_recursed(sx
)) {
930 if (what
& SA_NOTRECURSED
)
931 panic("Lock %s recursed @ %s:%d\n",
932 sx
->lock_object
.lo_name
, file
,
934 } else if (what
& SA_RECURSED
)
935 panic("Lock %s not recursed @ %s:%d\n",
936 sx
->lock_object
.lo_name
, file
, line
);
941 case SA_XLOCKED
| SA_NOTRECURSED
:
942 case SA_XLOCKED
| SA_RECURSED
:
943 if (sx_xholder(sx
) != curthread
)
944 panic("Lock %s not exclusively locked @ %s:%d\n",
945 sx
->lock_object
.lo_name
, file
, line
);
946 if (sx_recursed(sx
)) {
947 if (what
& SA_NOTRECURSED
)
948 panic("Lock %s recursed @ %s:%d\n",
949 sx
->lock_object
.lo_name
, file
, line
);
950 } else if (what
& SA_RECURSED
)
951 panic("Lock %s not recursed @ %s:%d\n",
952 sx
->lock_object
.lo_name
, file
, line
);
956 witness_assert(&sx
->lock_object
, what
, file
, line
);
959 * If we hold an exclusve lock fail. We can't
960 * reliably check to see if we hold a shared lock or
963 if (sx_xholder(sx
) == curthread
)
964 panic("Lock %s exclusively locked @ %s:%d\n",
965 sx
->lock_object
.lo_name
, file
, line
);
969 panic("Unknown sx lock assertion: %d @ %s:%d", what
, file
,
973 #endif /* INVARIANT_SUPPORT */
977 db_show_sx(struct lock_object
*lock
)
982 sx
= (struct sx
*)lock
;
984 db_printf(" state: ");
985 if (sx
->sx_lock
== SX_LOCK_UNLOCKED
)
986 db_printf("UNLOCKED\n");
987 else if (sx
->sx_lock
== SX_LOCK_DESTROYED
) {
988 db_printf("DESTROYED\n");
990 } else if (sx
->sx_lock
& SX_LOCK_SHARED
)
991 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx
->sx_lock
));
994 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td
,
995 td
->td_tid
, td
->td_proc
->p_pid
, td
->td_name
);
997 db_printf(" recursed: %d\n", sx
->sx_recurse
);
1000 db_printf(" waiters: ");
1001 switch(sx
->sx_lock
&
1002 (SX_LOCK_SHARED_WAITERS
| SX_LOCK_EXCLUSIVE_WAITERS
)) {
1003 case SX_LOCK_SHARED_WAITERS
:
1004 db_printf("shared\n");
1006 case SX_LOCK_EXCLUSIVE_WAITERS
:
1007 db_printf("exclusive\n");
1009 case SX_LOCK_SHARED_WAITERS
| SX_LOCK_EXCLUSIVE_WAITERS
:
1010 db_printf("exclusive and shared\n");
1013 db_printf("none\n");
1018 * Check to see if a thread that is blocked on a sleep queue is actually
1019 * blocked on an sx lock. If so, output some details and return true.
1020 * If the lock has an exclusive owner, return that in *ownerp.
1023 sx_chain(struct thread
*td
, struct thread
**ownerp
)
1028 * Check to see if this thread is blocked on an sx lock.
1029 * First, we check the lock class. If that is ok, then we
1030 * compare the lock name against the wait message.
1033 if (LOCK_CLASS(&sx
->lock_object
) != &lock_class_sx
||
1034 sx
->lock_object
.lo_name
!= td
->td_wmesg
)
1037 /* We think we have an sx lock, so output some details. */
1038 db_printf("blocked on sx \"%s\" ", td
->td_wmesg
);
1039 *ownerp
= sx_xholder(sx
);
1040 if (sx
->sx_lock
& SX_LOCK_SHARED
)
1041 db_printf("SLOCK (count %ju)\n",
1042 (uintmax_t)SX_SHARERS(sx
->sx_lock
));
1044 db_printf("XLOCK\n");