1 /* $NetBSD: kern_mutex.c,v 1.45 2009/01/25 04:45:14 rmind Exp $ */
4 * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Kernel mutex implementation, modeled after those found in Solaris,
34 * a description of which can be found in:
36 * Solaris Internals: Core Kernel Architecture, Jim Mauro and
40 #define __MUTEX_PRIVATE
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.45 2009/01/25 04:45:14 rmind Exp $");
45 #include <sys/param.h>
46 #include <sys/atomic.h>
48 #include <sys/mutex.h>
49 #include <sys/sched.h>
50 #include <sys/sleepq.h>
51 #include <sys/systm.h>
52 #include <sys/lockdebug.h>
53 #include <sys/kernel.h>
57 #include <dev/lockstat.h>
59 #include <machine/lock.h>
64 * When not running a debug kernel, spin mutexes are not much
65 * more than an splraiseipl() and splx() pair.
68 #if defined(DIAGNOSTIC) || defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
76 #define MUTEX_WANTLOCK(mtx) \
77 LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx), \
78 (uintptr_t)__builtin_return_address(0), false, false)
79 #define MUTEX_LOCKED(mtx) \
80 LOCKDEBUG_LOCKED(MUTEX_DEBUG_P(mtx), (mtx), NULL, \
81 (uintptr_t)__builtin_return_address(0), 0)
82 #define MUTEX_UNLOCKED(mtx) \
83 LOCKDEBUG_UNLOCKED(MUTEX_DEBUG_P(mtx), (mtx), \
84 (uintptr_t)__builtin_return_address(0), 0)
85 #define MUTEX_ABORT(mtx, msg) \
86 mutex_abort(mtx, __func__, msg)
88 #if defined(LOCKDEBUG)
90 #define MUTEX_DASSERT(mtx, cond) \
93 MUTEX_ABORT(mtx, "assertion failed: " #cond); \
94 } while (/* CONSTCOND */ 0);
98 #define MUTEX_DASSERT(mtx, cond) /* nothing */
100 #endif /* LOCKDEBUG */
102 #if defined(DIAGNOSTIC)
104 #define MUTEX_ASSERT(mtx, cond) \
107 MUTEX_ABORT(mtx, "assertion failed: " #cond); \
108 } while (/* CONSTCOND */ 0)
110 #else /* DIAGNOSTIC */
112 #define MUTEX_ASSERT(mtx, cond) /* nothing */
114 #endif /* DIAGNOSTIC */
117 * Spin mutex SPL save / restore.
119 #ifndef MUTEX_COUNT_BIAS
120 #define MUTEX_COUNT_BIAS 0
123 #define MUTEX_SPIN_SPLRAISE(mtx) \
125 struct cpu_info *x__ci; \
127 s = splraiseipl(mtx->mtx_ipl); \
129 x__cnt = x__ci->ci_mtx_count--; \
131 if (x__cnt == MUTEX_COUNT_BIAS) \
132 x__ci->ci_mtx_oldspl = (s); \
133 } while (/* CONSTCOND */ 0)
135 #define MUTEX_SPIN_SPLRESTORE(mtx) \
137 struct cpu_info *x__ci = curcpu(); \
138 int s = x__ci->ci_mtx_oldspl; \
140 if (++(x__ci->ci_mtx_count) == MUTEX_COUNT_BIAS) \
142 } while (/* CONSTCOND */ 0)
145 * For architectures that provide 'simple' mutexes: they provide a
146 * CAS function that is either MP-safe, or does not need to be MP
147 * safe. Adaptive mutexes on these architectures do not require an
148 * additional interlock.
151 #ifdef __HAVE_SIMPLE_MUTEXES
153 #define MUTEX_OWNER(owner) \
154 (owner & MUTEX_THREAD)
155 #define MUTEX_HAS_WAITERS(mtx) \
156 (((int)(mtx)->mtx_owner & MUTEX_BIT_WAITERS) != 0)
158 #define MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug) \
161 (mtx)->mtx_owner |= MUTEX_BIT_DEBUG; \
162 } while (/* CONSTCOND */ 0);
164 #define MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl) \
166 (mtx)->mtx_owner = MUTEX_BIT_SPIN; \
168 (mtx)->mtx_owner |= MUTEX_BIT_DEBUG; \
169 (mtx)->mtx_ipl = makeiplcookie((ipl)); \
170 __cpu_simple_lock_init(&(mtx)->mtx_lock); \
171 } while (/* CONSTCOND */ 0)
173 #define MUTEX_DESTROY(mtx) \
175 (mtx)->mtx_owner = MUTEX_THREAD; \
176 } while (/* CONSTCOND */ 0);
178 #define MUTEX_SPIN_P(mtx) \
179 (((mtx)->mtx_owner & MUTEX_BIT_SPIN) != 0)
180 #define MUTEX_ADAPTIVE_P(mtx) \
181 (((mtx)->mtx_owner & MUTEX_BIT_SPIN) == 0)
183 #define MUTEX_DEBUG_P(mtx) (((mtx)->mtx_owner & MUTEX_BIT_DEBUG) != 0)
184 #if defined(LOCKDEBUG)
185 #define MUTEX_OWNED(owner) (((owner) & ~MUTEX_BIT_DEBUG) != 0)
186 #define MUTEX_INHERITDEBUG(new, old) (new) |= (old) & MUTEX_BIT_DEBUG
187 #else /* defined(LOCKDEBUG) */
188 #define MUTEX_OWNED(owner) ((owner) != 0)
189 #define MUTEX_INHERITDEBUG(new, old) /* nothing */
190 #endif /* defined(LOCKDEBUG) */
193 MUTEX_ACQUIRE(kmutex_t
*mtx
, uintptr_t curthread
)
197 uintptr_t new = curthread
;
199 MUTEX_INHERITDEBUG(old
, mtx
->mtx_owner
);
200 MUTEX_INHERITDEBUG(new, old
);
201 rv
= MUTEX_CAS(&mtx
->mtx_owner
, old
, new);
207 MUTEX_SET_WAITERS(kmutex_t
*mtx
, uintptr_t owner
)
210 rv
= MUTEX_CAS(&mtx
->mtx_owner
, owner
, owner
| MUTEX_BIT_WAITERS
);
216 MUTEX_RELEASE(kmutex_t
*mtx
)
222 MUTEX_INHERITDEBUG(new, mtx
->mtx_owner
);
223 mtx
->mtx_owner
= new;
227 MUTEX_CLEAR_WAITERS(kmutex_t
*mtx
)
231 #endif /* __HAVE_SIMPLE_MUTEXES */
234 * Patch in stubs via strong alias where they are not available.
237 #if defined(LOCKDEBUG)
238 #undef __HAVE_MUTEX_STUBS
239 #undef __HAVE_SPIN_MUTEX_STUBS
242 #ifndef __HAVE_MUTEX_STUBS
243 __strong_alias(mutex_enter
,mutex_vector_enter
);
244 __strong_alias(mutex_exit
,mutex_vector_exit
);
247 #ifndef __HAVE_SPIN_MUTEX_STUBS
248 __strong_alias(mutex_spin_enter
,mutex_vector_enter
);
249 __strong_alias(mutex_spin_exit
,mutex_vector_exit
);
252 void mutex_abort(kmutex_t
*, const char *, const char *);
253 void mutex_dump(volatile void *);
254 int mutex_onproc(uintptr_t, struct cpu_info
**);
256 lockops_t mutex_spin_lockops
= {
262 lockops_t mutex_adaptive_lockops
= {
268 syncobj_t mutex_syncobj
= {
279 * Dump the contents of a mutex structure.
282 mutex_dump(volatile void *cookie
)
284 volatile kmutex_t
*mtx
= cookie
;
286 printf_nolog("owner field : %#018lx wait/spin: %16d/%d\n",
287 (long)MUTEX_OWNER(mtx
->mtx_owner
), MUTEX_HAS_WAITERS(mtx
),
294 * Dump information about an error and panic the system. This
295 * generates a lot of machine code in the DIAGNOSTIC case, so
296 * we ask the compiler to not inline it.
299 mutex_abort(kmutex_t
*mtx
, const char *func
, const char *msg
)
302 LOCKDEBUG_ABORT(mtx
, (MUTEX_SPIN_P(mtx
) ?
303 &mutex_spin_lockops
: &mutex_adaptive_lockops
), func
, msg
);
309 * Initialize a mutex for use. Note that adaptive mutexes are in
310 * essence spin mutexes that can sleep to avoid deadlock and wasting
311 * CPU time. We can't easily provide a type of mutex that always
312 * sleeps - see comments in mutex_vector_enter() about releasing
316 mutex_init(kmutex_t
*mtx
, kmutex_type_t type
, int ipl
)
320 memset(mtx
, 0, sizeof(*mtx
));
324 KASSERT(ipl
== IPL_NONE
);
328 if (ipl
== IPL_NONE
|| ipl
== IPL_SOFTCLOCK
||
329 ipl
== IPL_SOFTBIO
|| ipl
== IPL_SOFTNET
||
330 ipl
== IPL_SOFTSERIAL
) {
331 type
= MUTEX_ADAPTIVE
;
342 dodebug
= LOCKDEBUG_ALLOC(mtx
, NULL
,
343 (uintptr_t)__builtin_return_address(0));
344 MUTEX_INITIALIZE_SPIN(mtx
, dodebug
, ipl
);
347 dodebug
= LOCKDEBUG_ALLOC(mtx
, &mutex_adaptive_lockops
,
348 (uintptr_t)__builtin_return_address(0));
349 MUTEX_INITIALIZE_ADAPTIVE(mtx
, dodebug
);
352 dodebug
= LOCKDEBUG_ALLOC(mtx
, &mutex_spin_lockops
,
353 (uintptr_t)__builtin_return_address(0));
354 MUTEX_INITIALIZE_SPIN(mtx
, dodebug
, ipl
);
357 panic("mutex_init: impossible type");
368 mutex_destroy(kmutex_t
*mtx
)
371 if (MUTEX_ADAPTIVE_P(mtx
)) {
372 MUTEX_ASSERT(mtx
, !MUTEX_OWNED(mtx
->mtx_owner
) &&
373 !MUTEX_HAS_WAITERS(mtx
));
375 MUTEX_ASSERT(mtx
, !__SIMPLELOCK_LOCKED_P(&mtx
->mtx_lock
));
378 LOCKDEBUG_FREE(MUTEX_DEBUG_P(mtx
), mtx
);
385 * Return true if an adaptive mutex owner is running on a CPU in the
386 * system. If the target is waiting on the kernel big lock, then we
387 * must release it. This is necessary to avoid deadlock.
389 * Note that we can't use the mutex owner field as an LWP pointer. We
390 * don't have full control over the timing of our execution, and so the
391 * pointer could be completely invalid by the time we dereference it.
393 #ifdef MULTIPROCESSOR
395 mutex_onproc(uintptr_t owner
, struct cpu_info
**cip
)
397 CPU_INFO_ITERATOR cii
;
401 if (!MUTEX_OWNED(owner
))
403 l
= (struct lwp
*)MUTEX_OWNER(owner
);
405 /* See if the target is running on a CPU somewhere. */
406 if ((ci
= *cip
) != NULL
&& ci
->ci_curlwp
== l
)
408 for (CPU_INFO_FOREACH(cii
, ci
))
409 if (ci
->ci_curlwp
== l
)
412 /* No: it may be safe to block now. */
417 /* Target is running; do we need to block? */
419 return ci
->ci_biglock_wanted
!= l
;
421 #endif /* MULTIPROCESSOR */
424 * mutex_vector_enter:
426 * Support routine for mutex_enter() that must handle all cases. In
427 * the LOCKDEBUG case, mutex_enter() is always aliased here, even if
428 * fast-path stubs are available. If an mutex_spin_enter() stub is
429 * not available, then it is also aliased directly here.
432 mutex_vector_enter(kmutex_t
*mtx
)
434 uintptr_t owner
, curthread
;
436 #ifdef MULTIPROCESSOR
437 struct cpu_info
*ci
= NULL
;
443 LOCKSTAT_COUNTER(spincnt
);
444 LOCKSTAT_COUNTER(slpcnt
);
445 LOCKSTAT_TIMER(spintime
);
446 LOCKSTAT_TIMER(slptime
);
447 LOCKSTAT_FLAG(lsflag
);
450 * Handle spin mutexes.
452 if (MUTEX_SPIN_P(mtx
)) {
453 #if defined(LOCKDEBUG) && defined(MULTIPROCESSOR)
456 MUTEX_SPIN_SPLRAISE(mtx
);
459 if (__cpu_simple_lock_try(&mtx
->mtx_lock
)) {
463 #if !defined(MULTIPROCESSOR)
464 MUTEX_ABORT(mtx
, "locking against myself");
465 #else /* !MULTIPROCESSOR */
467 LOCKSTAT_ENTER(lsflag
);
468 LOCKSTAT_START_TIMER(lsflag
, spintime
);
469 count
= SPINLOCK_BACKOFF_MIN
;
472 * Spin testing the lock word and do exponential backoff
473 * to reduce cache line ping-ponging between CPUs.
476 if (panicstr
!= NULL
)
478 while (__SIMPLELOCK_LOCKED_P(&mtx
->mtx_lock
)) {
479 SPINLOCK_BACKOFF(count
);
481 if (SPINLOCK_SPINOUT(spins
))
482 MUTEX_ABORT(mtx
, "spinout");
483 #endif /* LOCKDEBUG */
485 } while (!__cpu_simple_lock_try(&mtx
->mtx_lock
));
487 if (count
!= SPINLOCK_BACKOFF_MIN
) {
488 LOCKSTAT_STOP_TIMER(lsflag
, spintime
);
489 LOCKSTAT_EVENT(lsflag
, mtx
,
490 LB_SPIN_MUTEX
| LB_SPIN
, 1, spintime
);
492 LOCKSTAT_EXIT(lsflag
);
493 #endif /* !MULTIPROCESSOR */
499 curthread
= (uintptr_t)curlwp
;
501 MUTEX_DASSERT(mtx
, MUTEX_ADAPTIVE_P(mtx
));
502 MUTEX_ASSERT(mtx
, curthread
!= 0);
505 if (panicstr
== NULL
) {
506 LOCKDEBUG_BARRIER(&kernel_lock
, 1);
509 LOCKSTAT_ENTER(lsflag
);
512 * Adaptive mutex; spin trying to acquire the mutex. If we
513 * determine that the owner is not running on a processor,
514 * then we stop spinning, and sleep instead.
516 for (owner
= mtx
->mtx_owner
;;) {
517 if (!MUTEX_OWNED(owner
)) {
519 * Mutex owner clear could mean two things:
521 * * The mutex has been released.
522 * * The owner field hasn't been set yet.
524 * Try to acquire it again. If that fails,
525 * we'll just loop again.
527 if (MUTEX_ACQUIRE(mtx
, curthread
))
529 owner
= mtx
->mtx_owner
;
533 if (__predict_false(panicstr
!= NULL
))
535 if (__predict_false(MUTEX_OWNER(owner
) == curthread
))
536 MUTEX_ABORT(mtx
, "locking against myself");
538 #ifdef MULTIPROCESSOR
540 * Check to see if the owner is running on a processor.
541 * If so, then we should just spin, as the owner will
542 * likely release the lock very soon.
544 if (mutex_onproc(owner
, &ci
)) {
545 LOCKSTAT_START_TIMER(lsflag
, spintime
);
546 count
= SPINLOCK_BACKOFF_MIN
;
548 SPINLOCK_BACKOFF(count
);
549 owner
= mtx
->mtx_owner
;
550 if (!mutex_onproc(owner
, &ci
))
553 LOCKSTAT_STOP_TIMER(lsflag
, spintime
);
554 LOCKSTAT_COUNT(spincnt
, 1);
555 if (!MUTEX_OWNED(owner
))
560 ts
= turnstile_lookup(mtx
);
563 * Once we have the turnstile chain interlock, mark the
564 * mutex has having waiters. If that fails, spin again:
565 * chances are that the mutex has been released.
567 if (!MUTEX_SET_WAITERS(mtx
, owner
)) {
569 owner
= mtx
->mtx_owner
;
573 #ifdef MULTIPROCESSOR
575 * mutex_exit() is permitted to release the mutex without
576 * any interlocking instructions, and the following can
579 * CPU 1: MUTEX_SET_WAITERS() CPU2: mutex_exit()
580 * ---------------------------- ----------------------------
581 * .. acquire cache line
582 * .. test for waiters
583 * acquire cache line <- lose cache line
585 * verify mutex is held ..
587 * unlock cache line ..
588 * lose cache line -> acquire cache line
589 * .. clear lock word, waiters
592 * There is a another race that can occur: a third CPU could
593 * acquire the mutex as soon as it is released. Since
594 * adaptive mutexes are primarily spin mutexes, this is not
595 * something that we need to worry about too much. What we
596 * do need to ensure is that the waiters bit gets set.
598 * To allow the unlocked release, we need to make some
601 * o Release is the only non-atomic/unlocked operation
602 * that can be performed on the mutex. (It must still
603 * be atomic on the local CPU, e.g. in case interrupted
606 * o At any given time, MUTEX_SET_WAITERS() can only ever
607 * be in progress on one CPU in the system - guaranteed
608 * by the turnstile chain lock.
610 * o No other operations other than MUTEX_SET_WAITERS()
611 * and release can modify a mutex with a non-zero
614 * o The result of a successful MUTEX_SET_WAITERS() call
615 * is an unbuffered write that is immediately visible
616 * to all other processors in the system.
618 * o If the holding LWP switches away, it posts a store
619 * fence before changing curlwp, ensuring that any
620 * overwrite of the mutex waiters flag by mutex_exit()
621 * completes before the modification of curlwp becomes
622 * visible to this CPU.
624 * o mi_switch() posts a store fence before setting curlwp
625 * and before resuming execution of an LWP.
627 * o _kernel_lock() posts a store fence before setting
628 * curcpu()->ci_biglock_wanted, and after clearing it.
629 * This ensures that any overwrite of the mutex waiters
630 * flag by mutex_exit() completes before the modification
631 * of ci_biglock_wanted becomes visible.
633 * We now post a read memory barrier (after setting the
634 * waiters field) and check the lock holder's status again.
635 * Some of the possible outcomes (not an exhaustive list):
637 * 1. The onproc check returns true: the holding LWP is
638 * running again. The lock may be released soon and
639 * we should spin. Importantly, we can't trust the
640 * value of the waiters flag.
642 * 2. The onproc check returns false: the holding LWP is
643 * not running. We now have the opportunity to check
644 * if mutex_exit() has blatted the modifications made
645 * by MUTEX_SET_WAITERS().
647 * 3. The onproc check returns false: the holding LWP may
648 * or may not be running. It has context switched at
649 * some point during our check. Again, we have the
650 * chance to see if the waiters bit is still set or
651 * has been overwritten.
653 * 4. The onproc check returns false: the holding LWP is
654 * running on a CPU, but wants the big lock. It's OK
655 * to check the waiters field in this case.
657 * 5. The has-waiters check fails: the mutex has been
658 * released, the waiters flag cleared and another LWP
659 * now owns the mutex.
661 * 6. The has-waiters check fails: the mutex has been
664 * If the waiters bit is not set it's unsafe to go asleep,
665 * as we might never be awoken.
667 if ((membar_consumer(), mutex_onproc(owner
, &ci
)) ||
668 (membar_consumer(), !MUTEX_HAS_WAITERS(mtx
))) {
670 owner
= mtx
->mtx_owner
;
673 #endif /* MULTIPROCESSOR */
677 * Sleeping for a mutex should not generate an upcall.
678 * So set LP_SA_NOBLOCK to indicate this.
679 * f indicates if we should clear LP_SA_NOBLOCK when done.
681 f
= ~curlwp
->l_pflag
& LP_SA_NOBLOCK
;
682 curlwp
->l_pflag
|= LP_SA_NOBLOCK
;
685 LOCKSTAT_START_TIMER(lsflag
, slptime
);
687 turnstile_block(ts
, TS_WRITER_Q
, mtx
, &mutex_syncobj
);
689 LOCKSTAT_STOP_TIMER(lsflag
, slptime
);
690 LOCKSTAT_COUNT(slpcnt
, 1);
693 curlwp
->l_pflag
^= f
;
696 owner
= mtx
->mtx_owner
;
699 LOCKSTAT_EVENT(lsflag
, mtx
, LB_ADAPTIVE_MUTEX
| LB_SLEEP1
,
701 LOCKSTAT_EVENT(lsflag
, mtx
, LB_ADAPTIVE_MUTEX
| LB_SPIN
,
703 LOCKSTAT_EXIT(lsflag
);
705 MUTEX_DASSERT(mtx
, MUTEX_OWNER(mtx
->mtx_owner
) == curthread
);
712 * Support routine for mutex_exit() that handles all cases.
715 mutex_vector_exit(kmutex_t
*mtx
)
720 if (MUTEX_SPIN_P(mtx
)) {
722 if (__predict_false(!__SIMPLELOCK_LOCKED_P(&mtx
->mtx_lock
))) {
723 if (panicstr
!= NULL
)
725 MUTEX_ABORT(mtx
, "exiting unheld spin mutex");
728 __cpu_simple_unlock(&mtx
->mtx_lock
);
730 MUTEX_SPIN_SPLRESTORE(mtx
);
734 if (__predict_false((uintptr_t)panicstr
| cold
)) {
740 curthread
= (uintptr_t)curlwp
;
741 MUTEX_DASSERT(mtx
, curthread
!= 0);
742 MUTEX_ASSERT(mtx
, MUTEX_OWNER(mtx
->mtx_owner
) == curthread
);
747 * Avoid having to take the turnstile chain lock every time
748 * around. Raise the priority level to splhigh() in order
749 * to disable preemption and so make the following atomic.
753 if (!MUTEX_HAS_WAITERS(mtx
)) {
763 * Get this lock's turnstile. This gets the interlock on
764 * the sleep queue. Once we have that, we can clear the
765 * lock. If there was no turnstile for the lock, there
766 * were no waiters remaining.
768 ts
= turnstile_lookup(mtx
);
775 turnstile_wakeup(ts
, TS_WRITER_Q
,
776 TS_WAITERS(ts
, TS_WRITER_Q
), NULL
);
780 #ifndef __HAVE_SIMPLE_MUTEXES
784 * Support routine for mutex_exit() that wakes up all waiters.
785 * We assume that the mutex has been released, but it need not
789 mutex_wakeup(kmutex_t
*mtx
)
793 ts
= turnstile_lookup(mtx
);
798 MUTEX_CLEAR_WAITERS(mtx
);
799 turnstile_wakeup(ts
, TS_WRITER_Q
, TS_WAITERS(ts
, TS_WRITER_Q
), NULL
);
801 #endif /* !__HAVE_SIMPLE_MUTEXES */
806 * Return true if the current LWP (adaptive) or CPU (spin)
810 mutex_owned(kmutex_t
*mtx
)
815 if (MUTEX_ADAPTIVE_P(mtx
))
816 return MUTEX_OWNER(mtx
->mtx_owner
) == (uintptr_t)curlwp
;
818 return __SIMPLELOCK_LOCKED_P(&mtx
->mtx_lock
);
827 * Return the current owner of an adaptive mutex. Used for
828 * priority inheritance.
831 mutex_owner(kmutex_t
*mtx
)
834 MUTEX_ASSERT(mtx
, MUTEX_ADAPTIVE_P(mtx
));
835 return (struct lwp
*)MUTEX_OWNER(mtx
->mtx_owner
);
841 * Try to acquire the mutex; return non-zero if we did.
844 mutex_tryenter(kmutex_t
*mtx
)
849 * Handle spin mutexes.
851 if (MUTEX_SPIN_P(mtx
)) {
852 MUTEX_SPIN_SPLRAISE(mtx
);
854 if (__cpu_simple_lock_try(&mtx
->mtx_lock
)) {
859 MUTEX_SPIN_SPLRESTORE(mtx
);
866 curthread
= (uintptr_t)curlwp
;
867 MUTEX_ASSERT(mtx
, curthread
!= 0);
868 if (MUTEX_ACQUIRE(mtx
, curthread
)) {
872 MUTEX_OWNER(mtx
->mtx_owner
) == curthread
);
880 #if defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL)
884 * Support routine for mutex_spin_enter(). Assumes that the caller
885 * has already raised the SPL, and adjusted counters.
888 mutex_spin_retry(kmutex_t
*mtx
)
890 #ifdef MULTIPROCESSOR
892 LOCKSTAT_TIMER(spintime
);
893 LOCKSTAT_FLAG(lsflag
);
896 #endif /* LOCKDEBUG */
900 LOCKSTAT_ENTER(lsflag
);
901 LOCKSTAT_START_TIMER(lsflag
, spintime
);
902 count
= SPINLOCK_BACKOFF_MIN
;
905 * Spin testing the lock word and do exponential backoff
906 * to reduce cache line ping-ponging between CPUs.
909 if (panicstr
!= NULL
)
911 while (__SIMPLELOCK_LOCKED_P(&mtx
->mtx_lock
)) {
912 SPINLOCK_BACKOFF(count
);
914 if (SPINLOCK_SPINOUT(spins
))
915 MUTEX_ABORT(mtx
, "spinout");
916 #endif /* LOCKDEBUG */
918 } while (!__cpu_simple_lock_try(&mtx
->mtx_lock
));
920 LOCKSTAT_STOP_TIMER(lsflag
, spintime
);
921 LOCKSTAT_EVENT(lsflag
, mtx
, LB_SPIN_MUTEX
| LB_SPIN
, 1, spintime
);
922 LOCKSTAT_EXIT(lsflag
);
925 #else /* MULTIPROCESSOR */
926 MUTEX_ABORT(mtx
, "locking against myself");
927 #endif /* MULTIPROCESSOR */
929 #endif /* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */