ipfw: Use netisr wrappers
[dragonfly.git] / sys / kern / kern_mutex.c
blob0499e4bf08ae391d55e4165896d932825e6efa22
1 /*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 * Implement fast persistent locks based on atomic_cmpset_int() with
36 * semantics similar to lockmgr locks but faster and taking up much less
37 * space. Taken from HAMMER's lock implementation.
39 * These are meant to complement our LWKT tokens. Tokens are only held
40 * while the thread is running. Mutexes can be held across blocking
41 * conditions.
43 * - Exclusive priority over shared to prevent SMP starvation.
44 * - locks can be aborted (async callback, if any, will be made w/ENOLCK).
45 * - locks can be asynchronous.
46 * - synchronous fast path if no blocking occurs (async callback is not
47 * made in this case).
49 * Generally speaking any caller-supplied link state must be properly
50 * initialized before use.
52 * Most of the support is in sys/mutex[2].h. We mostly provide backoff
53 * functions here.
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/sysctl.h>
60 #include <sys/thread.h>
62 #include <machine/cpufunc.h>
64 #include <sys/thread2.h>
65 #include <sys/mutex2.h>
67 static int mtx_chain_link_ex(mtx_t *mtx, u_int olock);
68 static int mtx_chain_link_sh(mtx_t *mtx, u_int olock, int addcount);
69 static void mtx_delete_link(mtx_t *mtx, mtx_link_t *link);
72 * Exclusive-lock a mutex, block until acquired unless link is async.
73 * Recursion is allowed.
75 * Returns 0 on success, the tsleep() return code on failure, EINPROGRESS
76 * if async. If immediately successful an async exclusive lock will return 0
77 * and not issue the async callback or link the link structure. The caller
78 * must handle this case (typically this is an optimal code path).
80 * A tsleep() error can only be returned if PCATCH is specified in the flags.
82 static __inline int
83 __mtx_lock_ex(mtx_t *mtx, mtx_link_t *link, int flags, int to)
85 thread_t td;
86 u_int lock;
87 u_int nlock;
88 int error;
89 int isasync;
91 for (;;) {
92 lock = mtx->mtx_lock;
93 cpu_ccfence();
95 if (lock == 0) {
96 nlock = MTX_EXCLUSIVE | 1;
97 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
98 mtx->mtx_owner = curthread;
99 link->state = MTX_LINK_ACQUIRED;
100 error = 0;
101 break;
103 continue;
105 if ((lock & MTX_EXCLUSIVE) && mtx->mtx_owner == curthread) {
106 KKASSERT((lock & MTX_MASK) != MTX_MASK);
107 nlock = lock + 1;
108 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
109 link->state = MTX_LINK_ACQUIRED;
110 error = 0;
111 break;
113 continue;
117 * We need MTX_LINKSPIN to manipulate exlink or
118 * shlink.
120 * We must set MTX_EXWANTED with MTX_LINKSPIN to indicate
121 * pending shared requests. It cannot be set as a separate
122 * operation prior to acquiring MTX_LINKSPIN.
124 * To avoid unnecessary cpu cache traffic we poll
125 * for collisions. It is also possible that EXWANTED
126 * state failing the above test was spurious, so all the
127 * tests must be repeated if we cannot obtain LINKSPIN
128 * with the prior state tests intact (i.e. don't reload
129 * the (lock) variable here, for heaven's sake!).
131 if (lock & MTX_LINKSPIN) {
132 cpu_pause();
133 continue;
135 td = curthread;
136 nlock = lock | MTX_EXWANTED | MTX_LINKSPIN;
137 ++td->td_critcount;
138 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock) == 0) {
139 --td->td_critcount;
140 continue;
144 * Check for early abort.
146 if (link->state == MTX_LINK_ABORTED) {
147 if (mtx->mtx_exlink == NULL) {
148 atomic_clear_int(&mtx->mtx_lock,
149 MTX_LINKSPIN |
150 MTX_EXWANTED);
151 } else {
152 atomic_clear_int(&mtx->mtx_lock,
153 MTX_LINKSPIN);
155 --td->td_critcount;
156 link->state = MTX_LINK_IDLE;
157 error = ENOLCK;
158 break;
162 * Add our link to the exlink list and release LINKSPIN.
164 link->owner = td;
165 link->state = MTX_LINK_LINKED_EX;
166 if (mtx->mtx_exlink) {
167 link->next = mtx->mtx_exlink;
168 link->prev = link->next->prev;
169 link->next->prev = link;
170 link->prev->next = link;
171 } else {
172 link->next = link;
173 link->prev = link;
174 mtx->mtx_exlink = link;
176 isasync = (link->callback != NULL);
177 atomic_clear_int(&mtx->mtx_lock, MTX_LINKSPIN);
178 --td->td_critcount;
181 * If asynchronous lock request return without
182 * blocking, leave link structure linked.
184 if (isasync) {
185 error = EINPROGRESS;
186 break;
190 * Wait for lock
192 error = mtx_wait_link(mtx, link, flags, to);
193 break;
195 return (error);
199 _mtx_lock_ex_link(mtx_t *mtx, mtx_link_t *link, int flags, int to)
201 return(__mtx_lock_ex(mtx, link, flags, to));
205 _mtx_lock_ex(mtx_t *mtx, int flags, int to)
207 mtx_link_t link;
209 mtx_link_init(&link);
210 return(__mtx_lock_ex(mtx, &link, flags, to));
214 _mtx_lock_ex_quick(mtx_t *mtx)
216 mtx_link_t link;
218 mtx_link_init(&link);
219 return(__mtx_lock_ex(mtx, &link, 0, 0));
223 * Share-lock a mutex, block until acquired. Recursion is allowed.
225 * Returns 0 on success, or the tsleep() return code on failure.
226 * An error can only be returned if PCATCH is specified in the flags.
228 * NOTE: Shared locks get a mass-wakeup so if the tsleep fails we
229 * do not have to chain the wakeup().
231 static __inline int
232 __mtx_lock_sh(mtx_t *mtx, mtx_link_t *link, int flags, int to)
234 thread_t td;
235 u_int lock;
236 u_int nlock;
237 int error;
238 int isasync;
240 for (;;) {
241 lock = mtx->mtx_lock;
242 cpu_ccfence();
244 if (lock == 0) {
245 nlock = 1;
246 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
247 error = 0;
248 link->state = MTX_LINK_ACQUIRED;
249 break;
251 continue;
253 if ((lock & (MTX_EXCLUSIVE | MTX_EXWANTED)) == 0) {
254 KKASSERT((lock & MTX_MASK) != MTX_MASK);
255 nlock = lock + 1;
256 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
257 error = 0;
258 link->state = MTX_LINK_ACQUIRED;
259 break;
261 continue;
265 * We need MTX_LINKSPIN to manipulate exlink or
266 * shlink.
268 * We must set MTX_SHWANTED with MTX_LINKSPIN to indicate
269 * pending shared requests. It cannot be set as a separate
270 * operation prior to acquiring MTX_LINKSPIN.
272 * To avoid unnecessary cpu cache traffic we poll
273 * for collisions. It is also possible that EXWANTED
274 * state failing the above test was spurious, so all the
275 * tests must be repeated if we cannot obtain LINKSPIN
276 * with the prior state tests intact (i.e. don't reload
277 * the (lock) variable here, for heaven's sake!).
279 if (lock & MTX_LINKSPIN) {
280 cpu_pause();
281 continue;
283 td = curthread;
284 nlock = lock | MTX_SHWANTED | MTX_LINKSPIN;
285 ++td->td_critcount;
286 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock) == 0) {
287 --td->td_critcount;
288 continue;
292 * Check for early abort.
294 if (link->state == MTX_LINK_ABORTED) {
295 if (mtx->mtx_exlink == NULL) {
296 atomic_clear_int(&mtx->mtx_lock,
297 MTX_LINKSPIN |
298 MTX_SHWANTED);
299 } else {
300 atomic_clear_int(&mtx->mtx_lock,
301 MTX_LINKSPIN);
303 --td->td_critcount;
304 link->state = MTX_LINK_IDLE;
305 error = ENOLCK;
306 break;
310 * Add our link to the exlink list and release LINKSPIN.
312 link->owner = td;
313 link->state = MTX_LINK_LINKED_SH;
314 if (mtx->mtx_shlink) {
315 link->next = mtx->mtx_shlink;
316 link->prev = link->next->prev;
317 link->next->prev = link;
318 link->prev->next = link;
319 } else {
320 link->next = link;
321 link->prev = link;
322 mtx->mtx_shlink = link;
324 isasync = (link->callback != NULL);
325 atomic_clear_int(&mtx->mtx_lock, MTX_LINKSPIN);
326 --td->td_critcount;
329 * If asynchronous lock request return without
330 * blocking, leave link structure linked.
332 if (isasync) {
333 error = EINPROGRESS;
334 break;
338 * Wait for lock
340 error = mtx_wait_link(mtx, link, flags, to);
341 break;
343 return (error);
347 _mtx_lock_sh_link(mtx_t *mtx, mtx_link_t *link, int flags, int to)
349 return(__mtx_lock_sh(mtx, link, flags, to));
353 _mtx_lock_sh(mtx_t *mtx, int flags, int to)
355 mtx_link_t link;
357 mtx_link_init(&link);
358 return(__mtx_lock_sh(mtx, &link, flags, to));
362 _mtx_lock_sh_quick(mtx_t *mtx)
364 mtx_link_t link;
366 mtx_link_init(&link);
367 return(__mtx_lock_sh(mtx, &link, 0, 0));
371 * Get an exclusive spinlock the hard way.
373 void
374 _mtx_spinlock(mtx_t *mtx)
376 u_int lock;
377 u_int nlock;
378 int bb = 1;
379 int bo;
381 for (;;) {
382 lock = mtx->mtx_lock;
383 if (lock == 0) {
384 nlock = MTX_EXCLUSIVE | 1;
385 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
386 mtx->mtx_owner = curthread;
387 break;
389 } else if ((lock & MTX_EXCLUSIVE) &&
390 mtx->mtx_owner == curthread) {
391 KKASSERT((lock & MTX_MASK) != MTX_MASK);
392 nlock = lock + 1;
393 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
394 break;
395 } else {
396 /* MWAIT here */
397 if (bb < 1000)
398 ++bb;
399 cpu_pause();
400 for (bo = 0; bo < bb; ++bo)
403 cpu_pause();
408 * Attempt to acquire a spinlock, if we fail we must undo the
409 * gd->gd_spinlocks/gd->gd_curthead->td_critcount predisposition.
411 * Returns 0 on success, EAGAIN on failure.
414 _mtx_spinlock_try(mtx_t *mtx)
416 globaldata_t gd = mycpu;
417 u_int lock;
418 u_int nlock;
419 int res = 0;
421 for (;;) {
422 lock = mtx->mtx_lock;
423 if (lock == 0) {
424 nlock = MTX_EXCLUSIVE | 1;
425 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
426 mtx->mtx_owner = gd->gd_curthread;
427 break;
429 } else if ((lock & MTX_EXCLUSIVE) &&
430 mtx->mtx_owner == gd->gd_curthread) {
431 KKASSERT((lock & MTX_MASK) != MTX_MASK);
432 nlock = lock + 1;
433 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
434 break;
435 } else {
436 --gd->gd_spinlocks;
437 cpu_ccfence();
438 --gd->gd_curthread->td_critcount;
439 res = EAGAIN;
440 break;
442 cpu_pause();
444 return res;
447 #if 0
449 void
450 _mtx_spinlock_sh(mtx_t *mtx)
452 u_int lock;
453 u_int nlock;
454 int bb = 1;
455 int bo;
457 for (;;) {
458 lock = mtx->mtx_lock;
459 if ((lock & MTX_EXCLUSIVE) == 0) {
460 KKASSERT((lock & MTX_MASK) != MTX_MASK);
461 nlock = lock + 1;
462 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
463 break;
464 } else {
465 /* MWAIT here */
466 if (bb < 1000)
467 ++bb;
468 cpu_pause();
469 for (bo = 0; bo < bb; ++bo)
472 cpu_pause();
476 #endif
479 _mtx_lock_ex_try(mtx_t *mtx)
481 u_int lock;
482 u_int nlock;
483 int error;
485 for (;;) {
486 lock = mtx->mtx_lock;
487 if (lock == 0) {
488 nlock = MTX_EXCLUSIVE | 1;
489 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
490 mtx->mtx_owner = curthread;
491 error = 0;
492 break;
494 } else if ((lock & MTX_EXCLUSIVE) &&
495 mtx->mtx_owner == curthread) {
496 KKASSERT((lock & MTX_MASK) != MTX_MASK);
497 nlock = lock + 1;
498 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
499 error = 0;
500 break;
502 } else {
503 error = EAGAIN;
504 break;
506 cpu_pause();
508 return (error);
512 _mtx_lock_sh_try(mtx_t *mtx)
514 u_int lock;
515 u_int nlock;
516 int error = 0;
518 for (;;) {
519 lock = mtx->mtx_lock;
520 if ((lock & MTX_EXCLUSIVE) == 0) {
521 KKASSERT((lock & MTX_MASK) != MTX_MASK);
522 nlock = lock + 1;
523 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
524 break;
525 } else {
526 error = EAGAIN;
527 break;
529 cpu_pause();
531 return (error);
535 * If the lock is held exclusively it must be owned by the caller. If the
536 * lock is already a shared lock this operation is a NOP. A panic will
537 * occur if the lock is not held either shared or exclusive.
539 * The exclusive count is converted to a shared count.
541 void
542 _mtx_downgrade(mtx_t *mtx)
544 u_int lock;
545 u_int nlock;
547 for (;;) {
548 lock = mtx->mtx_lock;
549 cpu_ccfence();
552 * NOP if already shared.
554 if ((lock & MTX_EXCLUSIVE) == 0) {
555 KKASSERT((lock & MTX_MASK) > 0);
556 break;
560 * Transfer count to shared. Any additional pending shared
561 * waiters must be woken up.
563 if (lock & MTX_SHWANTED) {
564 if (mtx_chain_link_sh(mtx, lock, 1))
565 break;
566 /* retry */
567 } else {
568 nlock = lock & ~MTX_EXCLUSIVE;
569 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
570 break;
571 /* retry */
573 cpu_pause();
578 * Upgrade a shared lock to an exclusive lock. The upgrade will fail if
579 * the shared lock has a count other then 1. Optimize the most likely case
580 * but note that a single cmpset can fail due to WANTED races.
582 * If the lock is held exclusively it must be owned by the caller and
583 * this function will simply return without doing anything. A panic will
584 * occur if the lock is held exclusively by someone other then the caller.
586 * Returns 0 on success, EDEADLK on failure.
589 _mtx_upgrade_try(mtx_t *mtx)
591 u_int lock;
592 u_int nlock;
593 int error = 0;
595 for (;;) {
596 lock = mtx->mtx_lock;
598 if ((lock & ~MTX_EXWANTED) == 1) {
599 nlock = lock | MTX_EXCLUSIVE;
600 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
601 mtx->mtx_owner = curthread;
602 break;
604 } else if (lock & MTX_EXCLUSIVE) {
605 KKASSERT(mtx->mtx_owner == curthread);
606 break;
607 } else {
608 error = EDEADLK;
609 break;
611 cpu_pause();
613 return (error);
617 * Unlock a lock. The caller must hold the lock either shared or exclusive.
619 * On the last release we handle any pending chains.
621 void
622 _mtx_unlock(mtx_t *mtx)
624 u_int lock;
625 u_int nlock;
627 for (;;) {
628 lock = mtx->mtx_lock;
629 cpu_ccfence();
631 switch(lock) {
632 case MTX_EXCLUSIVE | 1:
634 * Last release, exclusive lock.
635 * No exclusive or shared requests pending.
637 mtx->mtx_owner = NULL;
638 nlock = 0;
639 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
640 goto done;
641 break;
642 case MTX_EXCLUSIVE | MTX_EXWANTED | 1:
643 case MTX_EXCLUSIVE | MTX_EXWANTED | MTX_SHWANTED | 1:
645 * Last release, exclusive lock.
646 * Exclusive requests pending.
647 * Exclusive requests have priority over shared reqs.
649 if (mtx_chain_link_ex(mtx, lock))
650 goto done;
651 break;
652 case MTX_EXCLUSIVE | MTX_SHWANTED | 1:
654 * Last release, exclusive lock.
656 * Shared requests are pending. Transfer our count (1)
657 * to the first shared request, wakeup all shared reqs.
659 if (mtx_chain_link_sh(mtx, lock, 0))
660 goto done;
661 break;
662 case 1:
664 * Last release, shared lock.
665 * No exclusive or shared requests pending.
667 nlock = 0;
668 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
669 goto done;
670 break;
671 case MTX_EXWANTED | 1:
672 case MTX_EXWANTED | MTX_SHWANTED | 1:
674 * Last release, shared lock.
676 * Exclusive requests are pending. Transfer our
677 * count (1) to the next exclusive request.
679 * Exclusive requests have priority over shared reqs.
681 if (mtx_chain_link_ex(mtx, lock))
682 goto done;
683 break;
684 case MTX_SHWANTED | 1:
686 * Last release, shared lock.
687 * Shared requests pending.
689 if (mtx_chain_link_sh(mtx, lock, 0))
690 goto done;
691 break;
692 default:
694 * We have to loop if this is the last release but
695 * someone is fiddling with LINKSPIN.
697 if ((lock & MTX_MASK) == 1) {
698 KKASSERT(lock & MTX_LINKSPIN);
699 break;
703 * Not the last release (shared or exclusive)
705 nlock = lock - 1;
706 KKASSERT((nlock & MTX_MASK) != MTX_MASK);
707 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
708 goto done;
709 break;
711 /* loop try again */
712 cpu_pause();
714 done:
719 * Chain pending links. Called on the last release of an exclusive or
720 * shared lock when the appropriate WANTED bit is set. mtx_lock old state
721 * is passed in with the count left at 1, which we can inherit, and other
722 * bits which we must adjust in a single atomic operation.
724 * Return non-zero on success, 0 if caller needs to retry.
726 * NOTE: It's ok if MTX_EXWANTED is in an indeterminant state while we are
727 * acquiring LINKSPIN as all other cases will also need to acquire
728 * LINKSPIN when handling the EXWANTED case.
730 static int
731 mtx_chain_link_ex(mtx_t *mtx, u_int olock)
733 thread_t td = curthread;
734 mtx_link_t *link;
735 u_int nlock;
737 olock &= ~MTX_LINKSPIN;
738 nlock = olock | MTX_LINKSPIN | MTX_EXCLUSIVE;
739 ++td->td_critcount;
740 if (atomic_cmpset_int(&mtx->mtx_lock, olock, nlock)) {
741 link = mtx->mtx_exlink;
742 KKASSERT(link != NULL);
743 if (link->next == link) {
744 mtx->mtx_exlink = NULL;
745 nlock = MTX_LINKSPIN | MTX_EXWANTED; /* to clear */
746 } else {
747 mtx->mtx_exlink = link->next;
748 link->next->prev = link->prev;
749 link->prev->next = link->next;
750 nlock = MTX_LINKSPIN; /* to clear */
752 KKASSERT(link->state == MTX_LINK_LINKED_EX);
753 mtx->mtx_owner = link->owner;
754 cpu_sfence();
757 * WARNING! The callback can only be safely
758 * made with LINKSPIN still held
759 * and in a critical section.
761 * WARNING! The link can go away after the
762 * state is set, or after the
763 * callback.
765 if (link->callback) {
766 link->state = MTX_LINK_CALLEDBACK;
767 link->callback(link, link->arg, 0);
768 } else {
769 link->state = MTX_LINK_ACQUIRED;
770 wakeup(link);
772 atomic_clear_int(&mtx->mtx_lock, nlock);
773 --td->td_critcount;
774 return 1;
776 /* retry */
777 --td->td_critcount;
778 return 0;
782 * Flush waiting shared locks. The lock's prior state is passed in and must
783 * be adjusted atomically only if it matches.
785 * If addcount is 0, the count for the first shared lock in the chain is
786 * assumed to have already been accounted for.
788 * If addcount is 1, the count for the first shared lock in the chain has
789 * not yet been accounted for.
791 static int
792 mtx_chain_link_sh(mtx_t *mtx, u_int olock, int addcount)
794 thread_t td = curthread;
795 mtx_link_t *link;
796 u_int nlock;
798 olock &= ~MTX_LINKSPIN;
799 nlock = olock | MTX_LINKSPIN;
800 nlock &= ~MTX_EXCLUSIVE;
801 ++td->td_critcount;
802 if (atomic_cmpset_int(&mtx->mtx_lock, olock, nlock)) {
803 KKASSERT(mtx->mtx_shlink != NULL);
804 for (;;) {
805 link = mtx->mtx_shlink;
806 atomic_add_int(&mtx->mtx_lock, addcount);
807 KKASSERT(link->state == MTX_LINK_LINKED_SH);
808 if (link->next == link) {
809 mtx->mtx_shlink = NULL;
810 cpu_sfence();
813 * WARNING! The callback can only be safely
814 * made with LINKSPIN still held
815 * and in a critical section.
817 * WARNING! The link can go away after the
818 * state is set, or after the
819 * callback.
821 if (link->callback) {
822 link->state = MTX_LINK_CALLEDBACK;
823 link->callback(link, link->arg, 0);
824 } else {
825 link->state = MTX_LINK_ACQUIRED;
826 wakeup(link);
828 break;
830 mtx->mtx_shlink = link->next;
831 link->next->prev = link->prev;
832 link->prev->next = link->next;
833 cpu_sfence();
834 link->state = MTX_LINK_ACQUIRED;
835 /* link can go away */
836 wakeup(link);
837 addcount = 1;
839 atomic_clear_int(&mtx->mtx_lock, MTX_LINKSPIN |
840 MTX_SHWANTED);
841 --td->td_critcount;
842 return 1;
844 /* retry */
845 --td->td_critcount;
846 return 0;
850 * Delete a link structure after tsleep has failed. This code is not
851 * in the critical path as most exclusive waits are chained.
853 static
854 void
855 mtx_delete_link(mtx_t *mtx, mtx_link_t *link)
857 thread_t td = curthread;
858 u_int lock;
859 u_int nlock;
862 * Acquire MTX_LINKSPIN.
864 * Do not use cmpxchg to wait for LINKSPIN to clear as this might
865 * result in too much cpu cache traffic.
867 ++td->td_critcount;
868 for (;;) {
869 lock = mtx->mtx_lock;
870 if (lock & MTX_LINKSPIN) {
871 cpu_pause();
872 continue;
874 nlock = lock | MTX_LINKSPIN;
875 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
876 break;
877 cpu_pause();
881 * Delete the link and release LINKSPIN.
883 nlock = MTX_LINKSPIN; /* to clear */
885 switch(link->state) {
886 case MTX_LINK_LINKED_EX:
887 if (link->next == link) {
888 mtx->mtx_exlink = NULL;
889 nlock |= MTX_EXWANTED; /* to clear */
890 } else {
891 mtx->mtx_exlink = link->next;
892 link->next->prev = link->prev;
893 link->prev->next = link->next;
895 break;
896 case MTX_LINK_LINKED_SH:
897 if (link->next == link) {
898 mtx->mtx_shlink = NULL;
899 nlock |= MTX_SHWANTED; /* to clear */
900 } else {
901 mtx->mtx_shlink = link->next;
902 link->next->prev = link->prev;
903 link->prev->next = link->next;
905 break;
906 default:
907 /* no change */
908 break;
910 atomic_clear_int(&mtx->mtx_lock, nlock);
911 --td->td_critcount;
915 * Wait for async lock completion or abort. Returns ENOLCK if an abort
916 * occurred.
919 mtx_wait_link(mtx_t *mtx, mtx_link_t *link, int flags, int to)
921 int error;
924 * Sleep. Handle false wakeups, interruptions, etc.
925 * The link may also have been aborted.
927 error = 0;
928 while (link->state & MTX_LINK_LINKED) {
929 tsleep_interlock(link, 0);
930 cpu_lfence();
931 if (link->state & MTX_LINK_LINKED) {
932 if (link->state & MTX_LINK_LINKED_SH)
933 mycpu->gd_cnt.v_lock_name[0] = 'S';
934 else
935 mycpu->gd_cnt.v_lock_name[0] = 'X';
936 strncpy(mycpu->gd_cnt.v_lock_name + 1,
937 mtx->mtx_ident,
938 sizeof(mycpu->gd_cnt.v_lock_name) - 2);
939 ++mycpu->gd_cnt.v_lock_colls;
941 error = tsleep(link, flags | PINTERLOCKED,
942 mtx->mtx_ident, to);
943 if (error)
944 break;
949 * We are done, make sure the link structure is unlinked.
950 * It may still be on the list due to e.g. EINTR or
951 * EWOULDBLOCK.
953 * It is possible for the tsleep to race an ABORT and cause
954 * error to be 0.
956 * The tsleep() can be woken up for numerous reasons and error
957 * might be zero in situations where we intend to return an error.
959 * (This is the synchronous case so state cannot be CALLEDBACK)
961 switch(link->state) {
962 case MTX_LINK_ACQUIRED:
963 case MTX_LINK_CALLEDBACK:
964 error = 0;
965 break;
966 case MTX_LINK_ABORTED:
967 error = ENOLCK;
968 break;
969 case MTX_LINK_LINKED_EX:
970 case MTX_LINK_LINKED_SH:
971 mtx_delete_link(mtx, link);
972 /* fall through */
973 default:
974 if (error == 0)
975 error = EWOULDBLOCK;
976 break;
980 * Clear state on status returned.
982 link->state = MTX_LINK_IDLE;
984 return error;
988 * Abort a mutex locking operation, causing mtx_lock_ex_link() to
989 * return ENOLCK. This may be called at any time after the mtx_link
990 * is initialized or the status from a previous lock has been
991 * returned. If called prior to the next (non-try) lock attempt, the
992 * next lock attempt using this link structure will abort instantly.
994 * Caller must still wait for the operation to complete, either from a
995 * blocking call that is still in progress or by calling mtx_wait_link().
997 * If an asynchronous lock request is possibly in-progress, the caller
998 * should call mtx_wait_link() synchronously. Note that the asynchronous
999 * lock callback will NOT be called if a successful abort occurred. XXX
1001 void
1002 mtx_abort_link(mtx_t *mtx, mtx_link_t *link)
1004 thread_t td = curthread;
1005 u_int lock;
1006 u_int nlock;
1009 * Acquire MTX_LINKSPIN
1011 ++td->td_critcount;
1012 for (;;) {
1013 lock = mtx->mtx_lock;
1014 if (lock & MTX_LINKSPIN) {
1015 cpu_pause();
1016 continue;
1018 nlock = lock | MTX_LINKSPIN;
1019 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
1020 break;
1021 cpu_pause();
1025 * Do the abort.
1027 * WARNING! Link structure can disappear once link->state is set.
1029 nlock = MTX_LINKSPIN; /* to clear */
1031 switch(link->state) {
1032 case MTX_LINK_IDLE:
1034 * Link not started yet
1036 link->state = MTX_LINK_ABORTED;
1037 break;
1038 case MTX_LINK_LINKED_EX:
1040 * de-link, mark aborted, and potentially wakeup the thread
1041 * or issue the callback.
1043 if (link->next == link) {
1044 if (mtx->mtx_exlink == link) {
1045 mtx->mtx_exlink = NULL;
1046 nlock |= MTX_EXWANTED; /* to clear */
1048 } else {
1049 if (mtx->mtx_exlink == link)
1050 mtx->mtx_exlink = link->next;
1051 link->next->prev = link->prev;
1052 link->prev->next = link->next;
1056 * When aborting the async callback is still made. We must
1057 * not set the link status to ABORTED in the callback case
1058 * since there is nothing else to clear its status if the
1059 * link is reused.
1061 if (link->callback) {
1062 link->state = MTX_LINK_CALLEDBACK;
1063 link->callback(link, link->arg, ENOLCK);
1064 } else {
1065 link->state = MTX_LINK_ABORTED;
1066 wakeup(link);
1068 break;
1069 case MTX_LINK_LINKED_SH:
1071 * de-link, mark aborted, and potentially wakeup the thread
1072 * or issue the callback.
1074 if (link->next == link) {
1075 if (mtx->mtx_shlink == link) {
1076 mtx->mtx_shlink = NULL;
1077 nlock |= MTX_SHWANTED; /* to clear */
1079 } else {
1080 if (mtx->mtx_shlink == link)
1081 mtx->mtx_shlink = link->next;
1082 link->next->prev = link->prev;
1083 link->prev->next = link->next;
1087 * When aborting the async callback is still made. We must
1088 * not set the link status to ABORTED in the callback case
1089 * since there is nothing else to clear its status if the
1090 * link is reused.
1092 if (link->callback) {
1093 link->state = MTX_LINK_CALLEDBACK;
1094 link->callback(link, link->arg, ENOLCK);
1095 } else {
1096 link->state = MTX_LINK_ABORTED;
1097 wakeup(link);
1099 break;
1100 case MTX_LINK_ACQUIRED:
1101 case MTX_LINK_CALLEDBACK:
1103 * Too late, the lock was acquired. Let it complete.
1105 break;
1106 default:
1108 * link already aborted, do nothing.
1110 break;
1112 atomic_clear_int(&mtx->mtx_lock, nlock);
1113 --td->td_critcount;