No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / kern_mutex.c
blobca47b17ab29c8e3df10fb2912e8d2cffdfde0d7b
1 /* $NetBSD: kern_mutex.c,v 1.45 2009/01/25 04:45:14 rmind Exp $ */
3 /*-
4 * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
12 * are met:
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
37 * Richard McDougall.
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>
47 #include <sys/proc.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>
54 #include <sys/intr.h>
55 #include <sys/lock.h>
57 #include <dev/lockstat.h>
59 #include <machine/lock.h>
61 #include "opt_sa.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)
69 #define FULL
70 #endif
73 * Debugging support.
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) \
91 do { \
92 if (!(cond)) \
93 MUTEX_ABORT(mtx, "assertion failed: " #cond); \
94 } while (/* CONSTCOND */ 0);
96 #else /* LOCKDEBUG */
98 #define MUTEX_DASSERT(mtx, cond) /* nothing */
100 #endif /* LOCKDEBUG */
102 #if defined(DIAGNOSTIC)
104 #define MUTEX_ASSERT(mtx, cond) \
105 do { \
106 if (!(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
121 #endif
123 #define MUTEX_SPIN_SPLRAISE(mtx) \
124 do { \
125 struct cpu_info *x__ci; \
126 int x__cnt, s; \
127 s = splraiseipl(mtx->mtx_ipl); \
128 x__ci = curcpu(); \
129 x__cnt = x__ci->ci_mtx_count--; \
130 __insn_barrier(); \
131 if (x__cnt == MUTEX_COUNT_BIAS) \
132 x__ci->ci_mtx_oldspl = (s); \
133 } while (/* CONSTCOND */ 0)
135 #define MUTEX_SPIN_SPLRESTORE(mtx) \
136 do { \
137 struct cpu_info *x__ci = curcpu(); \
138 int s = x__ci->ci_mtx_oldspl; \
139 __insn_barrier(); \
140 if (++(x__ci->ci_mtx_count) == MUTEX_COUNT_BIAS) \
141 splx(s); \
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) \
159 do { \
160 if (dodebug) \
161 (mtx)->mtx_owner |= MUTEX_BIT_DEBUG; \
162 } while (/* CONSTCOND */ 0);
164 #define MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl) \
165 do { \
166 (mtx)->mtx_owner = MUTEX_BIT_SPIN; \
167 if (dodebug) \
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) \
174 do { \
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) */
192 static inline int
193 MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
195 int rv;
196 uintptr_t old = 0;
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);
202 MUTEX_RECEIVE(mtx);
203 return rv;
206 static inline int
207 MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
209 int rv;
210 rv = MUTEX_CAS(&mtx->mtx_owner, owner, owner | MUTEX_BIT_WAITERS);
211 MUTEX_RECEIVE(mtx);
212 return rv;
215 static inline void
216 MUTEX_RELEASE(kmutex_t *mtx)
218 uintptr_t new;
220 MUTEX_GIVE(mtx);
221 new = 0;
222 MUTEX_INHERITDEBUG(new, mtx->mtx_owner);
223 mtx->mtx_owner = new;
226 static inline void
227 MUTEX_CLEAR_WAITERS(kmutex_t *mtx)
229 /* nothing */
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
240 #endif
242 #ifndef __HAVE_MUTEX_STUBS
243 __strong_alias(mutex_enter,mutex_vector_enter);
244 __strong_alias(mutex_exit,mutex_vector_exit);
245 #endif
247 #ifndef __HAVE_SPIN_MUTEX_STUBS
248 __strong_alias(mutex_spin_enter,mutex_vector_enter);
249 __strong_alias(mutex_spin_exit,mutex_vector_exit);
250 #endif
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 = {
257 "Mutex",
258 LOCKOPS_SPIN,
259 mutex_dump
262 lockops_t mutex_adaptive_lockops = {
263 "Mutex",
264 LOCKOPS_SLEEP,
265 mutex_dump
268 syncobj_t mutex_syncobj = {
269 SOBJ_SLEEPQ_SORTED,
270 turnstile_unsleep,
271 turnstile_changepri,
272 sleepq_lendpri,
273 (void *)mutex_owner,
277 * mutex_dump:
279 * Dump the contents of a mutex structure.
281 void
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),
288 MUTEX_SPIN_P(mtx));
292 * mutex_abort:
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.
298 void __noinline
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);
307 * mutex_init:
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
313 * mutexes unlocked.
315 void
316 mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
318 bool dodebug;
320 memset(mtx, 0, sizeof(*mtx));
322 switch (type) {
323 case MUTEX_ADAPTIVE:
324 KASSERT(ipl == IPL_NONE);
325 break;
326 case MUTEX_DEFAULT:
327 case MUTEX_DRIVER:
328 if (ipl == IPL_NONE || ipl == IPL_SOFTCLOCK ||
329 ipl == IPL_SOFTBIO || ipl == IPL_SOFTNET ||
330 ipl == IPL_SOFTSERIAL) {
331 type = MUTEX_ADAPTIVE;
332 } else {
333 type = MUTEX_SPIN;
335 break;
336 default:
337 break;
340 switch (type) {
341 case MUTEX_NODEBUG:
342 dodebug = LOCKDEBUG_ALLOC(mtx, NULL,
343 (uintptr_t)__builtin_return_address(0));
344 MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
345 break;
346 case MUTEX_ADAPTIVE:
347 dodebug = LOCKDEBUG_ALLOC(mtx, &mutex_adaptive_lockops,
348 (uintptr_t)__builtin_return_address(0));
349 MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug);
350 break;
351 case MUTEX_SPIN:
352 dodebug = LOCKDEBUG_ALLOC(mtx, &mutex_spin_lockops,
353 (uintptr_t)__builtin_return_address(0));
354 MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
355 break;
356 default:
357 panic("mutex_init: impossible type");
358 break;
363 * mutex_destroy:
365 * Tear down a mutex.
367 void
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));
374 } else {
375 MUTEX_ASSERT(mtx, !__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock));
378 LOCKDEBUG_FREE(MUTEX_DEBUG_P(mtx), mtx);
379 MUTEX_DESTROY(mtx);
383 * mutex_onproc:
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;
398 struct cpu_info *ci;
399 struct lwp *l;
401 if (!MUTEX_OWNED(owner))
402 return 0;
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)
407 goto run;
408 for (CPU_INFO_FOREACH(cii, ci))
409 if (ci->ci_curlwp == l)
410 goto run;
412 /* No: it may be safe to block now. */
413 *cip = NULL;
414 return 0;
416 run:
417 /* Target is running; do we need to block? */
418 *cip = ci;
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.
431 void
432 mutex_vector_enter(kmutex_t *mtx)
434 uintptr_t owner, curthread;
435 turnstile_t *ts;
436 #ifdef MULTIPROCESSOR
437 struct cpu_info *ci = NULL;
438 u_int count;
439 #endif
440 #ifdef KERN_SA
441 int f;
442 #endif
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)
454 u_int spins = 0;
455 #endif
456 MUTEX_SPIN_SPLRAISE(mtx);
457 MUTEX_WANTLOCK(mtx);
458 #ifdef FULL
459 if (__cpu_simple_lock_try(&mtx->mtx_lock)) {
460 MUTEX_LOCKED(mtx);
461 return;
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.
475 do {
476 if (panicstr != NULL)
477 break;
478 while (__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock)) {
479 SPINLOCK_BACKOFF(count);
480 #ifdef LOCKDEBUG
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 */
494 #endif /* FULL */
495 MUTEX_LOCKED(mtx);
496 return;
499 curthread = (uintptr_t)curlwp;
501 MUTEX_DASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
502 MUTEX_ASSERT(mtx, curthread != 0);
503 MUTEX_WANTLOCK(mtx);
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))
528 break;
529 owner = mtx->mtx_owner;
530 continue;
533 if (__predict_false(panicstr != NULL))
534 return;
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;
547 for (;;) {
548 SPINLOCK_BACKOFF(count);
549 owner = mtx->mtx_owner;
550 if (!mutex_onproc(owner, &ci))
551 break;
553 LOCKSTAT_STOP_TIMER(lsflag, spintime);
554 LOCKSTAT_COUNT(spincnt, 1);
555 if (!MUTEX_OWNED(owner))
556 continue;
558 #endif
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)) {
568 turnstile_exit(mtx);
569 owner = mtx->mtx_owner;
570 continue;
573 #ifdef MULTIPROCESSOR
575 * mutex_exit() is permitted to release the mutex without
576 * any interlocking instructions, and the following can
577 * occur as a result:
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
584 * lock cache line ..
585 * verify mutex is held ..
586 * set waiters ..
587 * unlock cache line ..
588 * lose cache line -> acquire cache line
589 * .. clear lock word, waiters
590 * return success
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
599 * assumptions here:
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
604 * or preempted).
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
612 * owner field.
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
662 * released.
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))) {
669 turnstile_exit(mtx);
670 owner = mtx->mtx_owner;
671 continue;
673 #endif /* MULTIPROCESSOR */
675 #ifdef KERN_SA
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;
683 #endif /* KERN_SA */
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);
692 #ifdef KERN_SA
693 curlwp->l_pflag ^= f;
694 #endif /* KERN_SA */
696 owner = mtx->mtx_owner;
699 LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SLEEP1,
700 slpcnt, slptime);
701 LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SPIN,
702 spincnt, spintime);
703 LOCKSTAT_EXIT(lsflag);
705 MUTEX_DASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
706 MUTEX_LOCKED(mtx);
710 * mutex_vector_exit:
712 * Support routine for mutex_exit() that handles all cases.
714 void
715 mutex_vector_exit(kmutex_t *mtx)
717 turnstile_t *ts;
718 uintptr_t curthread;
720 if (MUTEX_SPIN_P(mtx)) {
721 #ifdef FULL
722 if (__predict_false(!__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock))) {
723 if (panicstr != NULL)
724 return;
725 MUTEX_ABORT(mtx, "exiting unheld spin mutex");
727 MUTEX_UNLOCKED(mtx);
728 __cpu_simple_unlock(&mtx->mtx_lock);
729 #endif
730 MUTEX_SPIN_SPLRESTORE(mtx);
731 return;
734 if (__predict_false((uintptr_t)panicstr | cold)) {
735 MUTEX_UNLOCKED(mtx);
736 MUTEX_RELEASE(mtx);
737 return;
740 curthread = (uintptr_t)curlwp;
741 MUTEX_DASSERT(mtx, curthread != 0);
742 MUTEX_ASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
743 MUTEX_UNLOCKED(mtx);
745 #ifdef LOCKDEBUG
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.
752 int s = splhigh();
753 if (!MUTEX_HAS_WAITERS(mtx)) {
754 MUTEX_RELEASE(mtx);
755 splx(s);
756 return;
758 splx(s);
760 #endif
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);
770 if (ts == NULL) {
771 MUTEX_RELEASE(mtx);
772 turnstile_exit(mtx);
773 } else {
774 MUTEX_RELEASE(mtx);
775 turnstile_wakeup(ts, TS_WRITER_Q,
776 TS_WAITERS(ts, TS_WRITER_Q), NULL);
780 #ifndef __HAVE_SIMPLE_MUTEXES
782 * mutex_wakeup:
784 * Support routine for mutex_exit() that wakes up all waiters.
785 * We assume that the mutex has been released, but it need not
786 * be.
788 void
789 mutex_wakeup(kmutex_t *mtx)
791 turnstile_t *ts;
793 ts = turnstile_lookup(mtx);
794 if (ts == NULL) {
795 turnstile_exit(mtx);
796 return;
798 MUTEX_CLEAR_WAITERS(mtx);
799 turnstile_wakeup(ts, TS_WRITER_Q, TS_WAITERS(ts, TS_WRITER_Q), NULL);
801 #endif /* !__HAVE_SIMPLE_MUTEXES */
804 * mutex_owned:
806 * Return true if the current LWP (adaptive) or CPU (spin)
807 * holds the mutex.
810 mutex_owned(kmutex_t *mtx)
813 if (mtx == NULL)
814 return 0;
815 if (MUTEX_ADAPTIVE_P(mtx))
816 return MUTEX_OWNER(mtx->mtx_owner) == (uintptr_t)curlwp;
817 #ifdef FULL
818 return __SIMPLELOCK_LOCKED_P(&mtx->mtx_lock);
819 #else
820 return 1;
821 #endif
825 * mutex_owner:
827 * Return the current owner of an adaptive mutex. Used for
828 * priority inheritance.
830 lwp_t *
831 mutex_owner(kmutex_t *mtx)
834 MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
835 return (struct lwp *)MUTEX_OWNER(mtx->mtx_owner);
839 * mutex_tryenter:
841 * Try to acquire the mutex; return non-zero if we did.
844 mutex_tryenter(kmutex_t *mtx)
846 uintptr_t curthread;
849 * Handle spin mutexes.
851 if (MUTEX_SPIN_P(mtx)) {
852 MUTEX_SPIN_SPLRAISE(mtx);
853 #ifdef FULL
854 if (__cpu_simple_lock_try(&mtx->mtx_lock)) {
855 MUTEX_WANTLOCK(mtx);
856 MUTEX_LOCKED(mtx);
857 return 1;
859 MUTEX_SPIN_SPLRESTORE(mtx);
860 #else
861 MUTEX_WANTLOCK(mtx);
862 MUTEX_LOCKED(mtx);
863 return 1;
864 #endif
865 } else {
866 curthread = (uintptr_t)curlwp;
867 MUTEX_ASSERT(mtx, curthread != 0);
868 if (MUTEX_ACQUIRE(mtx, curthread)) {
869 MUTEX_WANTLOCK(mtx);
870 MUTEX_LOCKED(mtx);
871 MUTEX_DASSERT(mtx,
872 MUTEX_OWNER(mtx->mtx_owner) == curthread);
873 return 1;
877 return 0;
880 #if defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL)
882 * mutex_spin_retry:
884 * Support routine for mutex_spin_enter(). Assumes that the caller
885 * has already raised the SPL, and adjusted counters.
887 void
888 mutex_spin_retry(kmutex_t *mtx)
890 #ifdef MULTIPROCESSOR
891 u_int count;
892 LOCKSTAT_TIMER(spintime);
893 LOCKSTAT_FLAG(lsflag);
894 #ifdef LOCKDEBUG
895 u_int spins = 0;
896 #endif /* LOCKDEBUG */
898 MUTEX_WANTLOCK(mtx);
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.
908 do {
909 if (panicstr != NULL)
910 break;
911 while (__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock)) {
912 SPINLOCK_BACKOFF(count);
913 #ifdef LOCKDEBUG
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);
924 MUTEX_LOCKED(mtx);
925 #else /* MULTIPROCESSOR */
926 MUTEX_ABORT(mtx, "locking against myself");
927 #endif /* MULTIPROCESSOR */
929 #endif /* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */