2 * Copyright (c) 2003,2004,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
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
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
36 * lwkt_token - Implement soft token locks.
38 * Tokens are locks which serialize a thread only while the thread is
39 * running. If the thread blocks all tokens are released, then reacquired
40 * when the thread resumes.
42 * This implementation requires no critical sections or spin locks, but
43 * does use atomic_cmpset_ptr().
45 * Tokens may be recursively acquired by the same thread. However the
46 * caller must be sure to release such tokens in reverse order.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/rtprio.h>
53 #include <sys/queue.h>
54 #include <sys/sysctl.h>
56 #include <sys/kthread.h>
57 #include <machine/cpu.h>
60 #include <sys/spinlock.h>
62 #include <sys/thread2.h>
63 #include <sys/spinlock2.h>
64 #include <sys/mplock2.h>
67 #include <vm/vm_param.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_pager.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_zone.h>
76 #include <machine/stdarg.h>
77 #include <machine/smp.h>
79 extern int lwkt_sched_debug;
81 #ifndef LWKT_NUM_POOL_TOKENS
82 #define LWKT_NUM_POOL_TOKENS 4001 /* prime number */
85 static lwkt_token pool_tokens[LWKT_NUM_POOL_TOKENS];
87 #define TOKEN_STRING "REF=%p TOK=%p TD=%p"
88 #define CONTENDED_STRING "REF=%p TOK=%p TD=%p (contention started)"
89 #define UNCONTENDED_STRING "REF=%p TOK=%p TD=%p (contention stopped)"
90 #if !defined(KTR_TOKENS)
91 #define KTR_TOKENS KTR_ALL
94 KTR_INFO_MASTER(tokens);
95 KTR_INFO(KTR_TOKENS, tokens, fail, 0, TOKEN_STRING, sizeof(void *) * 3);
96 KTR_INFO(KTR_TOKENS, tokens, succ, 1, TOKEN_STRING, sizeof(void *) * 3);
98 KTR_INFO(KTR_TOKENS, tokens, release, 2, TOKEN_STRING, sizeof(void *) * 3);
99 KTR_INFO(KTR_TOKENS, tokens, remote, 3, TOKEN_STRING, sizeof(void *) * 3);
100 KTR_INFO(KTR_TOKENS, tokens, reqremote, 4, TOKEN_STRING, sizeof(void *) * 3);
101 KTR_INFO(KTR_TOKENS, tokens, reqfail, 5, TOKEN_STRING, sizeof(void *) * 3);
102 KTR_INFO(KTR_TOKENS, tokens, drain, 6, TOKEN_STRING, sizeof(void *) * 3);
103 KTR_INFO(KTR_TOKENS, tokens, contention_start, 7, CONTENDED_STRING, sizeof(void *) * 3);
104 KTR_INFO(KTR_TOKENS, tokens, contention_stop, 7, UNCONTENDED_STRING, sizeof(void *) * 3);
107 #define logtoken(name, ref) \
108 KTR_LOG(tokens_ ## name, ref, ref->tr_tok, curthread)
111 * Global tokens. These replace the MP lock for major subsystem locking.
112 * These tokens are initially used to lockup both global and individual
115 * Once individual structures get their own locks these tokens are used
116 * only to protect global lists & other variables and to interlock
117 * allocations and teardowns and such.
119 * The UP initializer causes token acquisition to also acquire the MP lock
120 * for maximum compatibility. The feature may be enabled and disabled at
121 * any time, the MP state is copied to the tokref when the token is acquired
122 * and will not race against sysctl changes.
124 struct lwkt_token mp_token = LWKT_TOKEN_INITIALIZER(mp_token);
125 struct lwkt_token pmap_token = LWKT_TOKEN_INITIALIZER(pmap_token);
126 struct lwkt_token dev_token = LWKT_TOKEN_INITIALIZER(dev_token);
127 struct lwkt_token vm_token = LWKT_TOKEN_INITIALIZER(vm_token);
128 struct lwkt_token vmspace_token = LWKT_TOKEN_INITIALIZER(vmspace_token);
129 struct lwkt_token kvm_token = LWKT_TOKEN_INITIALIZER(kvm_token);
130 struct lwkt_token proc_token = LWKT_TOKEN_INITIALIZER(proc_token);
131 struct lwkt_token tty_token = LWKT_TOKEN_INITIALIZER(tty_token);
132 struct lwkt_token vnode_token = LWKT_TOKEN_INITIALIZER(vnode_token);
133 struct lwkt_token vmobj_token = LWKT_TOKEN_INITIALIZER(vmobj_token);
135 static int lwkt_token_spin = 5;
136 SYSCTL_INT(_lwkt, OID_AUTO, token_spin, CTLFLAG_RW,
137 &lwkt_token_spin, 0, "Decontention spin loops");
138 static int lwkt_token_delay = 0;
139 SYSCTL_INT(_lwkt, OID_AUTO, token_delay, CTLFLAG_RW,
140 &lwkt_token_delay, 0, "Decontention spin delay in ns");
143 * The collision count is bumped every time the LWKT scheduler fails
144 * to acquire needed tokens in addition to a normal lwkt_gettoken()
147 SYSCTL_LONG(_lwkt, OID_AUTO, mp_collisions, CTLFLAG_RW,
148 &mp_token.t_collisions, 0, "Collision counter of mp_token");
149 SYSCTL_LONG(_lwkt, OID_AUTO, pmap_collisions, CTLFLAG_RW,
150 &pmap_token.t_collisions, 0, "Collision counter of pmap_token");
151 SYSCTL_LONG(_lwkt, OID_AUTO, dev_collisions, CTLFLAG_RW,
152 &dev_token.t_collisions, 0, "Collision counter of dev_token");
153 SYSCTL_LONG(_lwkt, OID_AUTO, vm_collisions, CTLFLAG_RW,
154 &vm_token.t_collisions, 0, "Collision counter of vm_token");
155 SYSCTL_LONG(_lwkt, OID_AUTO, vmspace_collisions, CTLFLAG_RW,
156 &vmspace_token.t_collisions, 0, "Collision counter of vmspace_token");
157 SYSCTL_LONG(_lwkt, OID_AUTO, kvm_collisions, CTLFLAG_RW,
158 &kvm_token.t_collisions, 0, "Collision counter of kvm_token");
159 SYSCTL_LONG(_lwkt, OID_AUTO, proc_collisions, CTLFLAG_RW,
160 &proc_token.t_collisions, 0, "Collision counter of proc_token");
161 SYSCTL_LONG(_lwkt, OID_AUTO, tty_collisions, CTLFLAG_RW,
162 &tty_token.t_collisions, 0, "Collision counter of tty_token");
163 SYSCTL_LONG(_lwkt, OID_AUTO, vnode_collisions, CTLFLAG_RW,
164 &vnode_token.t_collisions, 0, "Collision counter of vnode_token");
166 static int _lwkt_getalltokens_sorted(thread_t td);
170 * Acquire the initial mplock
172 * (low level boot only)
175 cpu_get_initial_mplock(void)
177 KKASSERT(mp_token.t_ref == NULL);
178 if (lwkt_trytoken(&mp_token) == FALSE)
179 panic("cpu_get_initial_mplock");
184 * Return a pool token given an address. Use a prime number to reduce
189 _lwkt_token_pool_lookup(void *ptr)
193 i = (u_int)(uintptr_t)ptr % LWKT_NUM_POOL_TOKENS;
194 return(&pool_tokens[i]);
198 * Initialize a tokref_t prior to making it visible in the thread's
203 _lwkt_tokref_init(lwkt_tokref_t ref, lwkt_token_t tok, thread_t td)
211 _lwkt_trytoken_spin(lwkt_token_t tok, lwkt_tokref_t ref)
215 for (n = 0; n < lwkt_token_spin; ++n) {
216 if (tok->t_ref == NULL &&
217 atomic_cmpset_ptr(&tok->t_ref, NULL, ref)) {
220 if (lwkt_token_delay) {
221 tsc_delay(lwkt_token_delay);
232 _lwkt_reltoken_spin(lwkt_token_t tok)
239 * Helper function used by lwkt_getalltokens[_sorted]().
241 * Our attempt to acquire the token has failed. To reduce cache coherency
242 * bandwidth we set our cpu bit in t_collmask then wait for a reasonable
243 * period of time for a hand-off from the current token owner.
247 _lwkt_trytoken_spin(lwkt_token_t tok, lwkt_tokref_t ref)
249 globaldata_t gd = mycpu;
254 * Add our cpu to the collision mask and wait for the token to be
258 atomic_set_cpumask(&tok->t_collmask, gd->gd_cpumask);
259 for (n = 0; n < lwkt_token_spin; ++n) {
261 * Token was released before we set our collision bit.
263 if (tok->t_ref == NULL &&
264 atomic_cmpset_ptr(&tok->t_ref, NULL, ref)) {
265 KKASSERT((tok->t_collmask & gd->gd_cpumask) != 0);
266 atomic_clear_cpumask(&tok->t_collmask, gd->gd_cpumask);
272 * Token was handed-off to us.
274 if (tok->t_ref == &gd->gd_handoff) {
275 KKASSERT((tok->t_collmask & gd->gd_cpumask) == 0);
280 if (lwkt_token_delay)
281 tsc_delay(lwkt_token_delay);
287 * We failed, attempt to clear our bit in the cpumask. We may race
288 * someone handing-off to us. If someone other than us cleared our
289 * cpu bit a handoff is incoming and we must wait for it.
292 mask = tok->t_collmask;
294 if (mask & gd->gd_cpumask) {
295 if (atomic_cmpset_cpumask(&tok->t_collmask,
297 mask & ~gd->gd_cpumask)) {
303 if (tok->t_ref != &gd->gd_handoff) {
314 * Release token with hand-off
318 _lwkt_reltoken_spin(lwkt_token_t tok)
325 if (tok->t_collmask == 0) {
331 sidemask = ~(mycpu->gd_cpumask - 1); /* high bits >= xcpu */
333 mask = tok->t_collmask;
340 cpuid = BSFCPUMASK(mask & sidemask);
342 cpuid = BSFCPUMASK(mask);
343 xgd = globaldata_find(cpuid);
344 if (atomic_cmpset_cpumask(&tok->t_collmask, mask,
345 mask & ~CPUMASK(cpuid))) {
346 tok->t_ref = &xgd->gd_handoff;
357 * Obtain all the tokens required by the specified thread on the current
358 * cpu, return 0 on failure and non-zero on success. If a failure occurs
359 * any partially acquired tokens will be released prior to return.
361 * lwkt_getalltokens is called by the LWKT scheduler to acquire all
362 * tokens that the thread had acquired prior to going to sleep.
364 * If spinning is non-zero this function acquires the tokens in a particular
365 * order to deal with potential deadlocks. We simply use address order for
368 * Called from a critical section.
371 lwkt_getalltokens(thread_t td, int spinning)
378 return(_lwkt_getalltokens_sorted(td));
381 * Acquire tokens in forward order, assign or validate tok->t_ref.
383 for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {
387 * Try to acquire the token if we do not already have
390 * NOTE: If atomic_cmpset_ptr() fails we have to
391 * loop and try again. It just means we
396 if (atomic_cmpset_ptr(&tok->t_ref, NULL,scan))
402 * Someone holds the token.
404 * Test if ref is already recursively held by this
405 * thread. We cannot safely dereference tok->t_ref
406 * (it might belong to another thread and is thus
407 * unstable), but we don't have to. We can simply
410 if (ref >= &td->td_toks_base && ref < td->td_toks_stop)
414 * Try hard to acquire this token before giving up
415 * and releasing the whole lot.
417 if (_lwkt_trytoken_spin(tok, scan))
419 if (lwkt_sched_debug)
420 kprintf("toka %p %s\n", tok, tok->t_desc);
423 * Otherwise we failed to acquire all the tokens.
424 * Release whatever we did get.
426 td->td_wmesg = tok->t_desc;
427 atomic_add_long(&tok->t_collisions, 1);
428 lwkt_relalltokens(td);
438 * Release all tokens owned by the specified thread on the current cpu.
440 * This code is really simple. Even in cases where we own all the tokens
441 * note that t_ref may not match the scan for recursively held tokens which
442 * are held deeper in the stack, or for the case where a lwkt_getalltokens()
445 * Tokens are released in reverse order to reduce chasing race failures.
447 * Called from a critical section.
450 lwkt_relalltokens(thread_t td)
455 for (scan = td->td_toks_stop - 1; scan >= &td->td_toks_base; --scan) {
456 /*for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {*/
458 if (tok->t_ref == scan)
459 _lwkt_reltoken_spin(tok);
464 * This is the decontention version of lwkt_getalltokens(). The tokens are
465 * acquired in address-sorted order to deal with any deadlocks. Ultimately
466 * token failures will spin into the scheduler and get here.
468 * In addition, to reduce hardware cache coherency contention monitor/mwait
469 * is interlocked with gd->gd_reqflags and RQF_SPINNING. Other cores which
470 * release a contended token will clear RQF_SPINNING and cause the mwait
471 * to resume. Any interrupt will also generally set RQF_* flags and cause
472 * mwait to resume (or be a NOP in the first place).
474 * This code is required to set up RQF_SPINNING in case of failure. The
475 * caller may call monitor/mwait on gd->gd_reqflags on failure. We do NOT
476 * want to call mwait here, and doubly so while we are holding tokens.
478 * Called from critical section
482 _lwkt_getalltokens_sorted(thread_t td)
484 /*globaldata_t gd = td->td_gd;*/
485 lwkt_tokref_t sort_array[LWKT_MAXTOKENS];
494 * Sort the token array. Yah yah, I know this isn't fun.
496 * NOTE: Recursively acquired tokens are ordered the same as in the
497 * td_toks_array so we can always get the earliest one first.
500 scan = &td->td_toks_base;
501 while (scan < td->td_toks_stop) {
502 for (j = 0; j < i; ++j) {
503 if (scan->tr_tok < sort_array[j]->tr_tok)
507 bcopy(sort_array + j, sort_array + j + 1,
508 (i - j) * sizeof(lwkt_tokref_t));
510 sort_array[j] = scan;
517 * Acquire tokens in forward order, assign or validate tok->t_ref.
519 for (i = 0; i < n; ++i) {
520 scan = sort_array[i];
524 * Try to acquire the token if we do not already have
527 * NOTE: If atomic_cmpset_ptr() fails we have to
528 * loop and try again. It just means we
533 if (atomic_cmpset_ptr(&tok->t_ref, NULL, scan))
539 * Someone holds the token.
541 * Test if ref is already recursively held by this
542 * thread. We cannot safely dereference tok->t_ref
543 * (it might belong to another thread and is thus
544 * unstable), but we don't have to. We can simply
547 if (ref >= &td->td_toks_base && ref < td->td_toks_stop)
551 * Try hard to acquire this token before giving up
552 * and releasing the whole lot.
554 if (_lwkt_trytoken_spin(tok, scan))
556 if (lwkt_sched_debug)
557 kprintf("tokb %p %s\n", tok, tok->t_desc);
560 * Tokens are released in reverse order to reduce
561 * chasing race failures.
563 td->td_wmesg = tok->t_desc;
564 atomic_add_long(&tok->t_collisions, 1);
566 for (j = i - 1; j >= 0; --j) {
567 /*for (j = 0; j < i; ++j) {*/
568 scan = sort_array[j];
570 if (tok->t_ref == scan)
571 _lwkt_reltoken_spin(tok);
578 * We were successful, there is no need for another core to signal
582 atomic_clear_int(&gd->gd_reqflags, RQF_SPINNING);
588 * Token acquisition helper function. The caller must have already
589 * made nref visible by adjusting td_toks_stop and will be responsible
590 * for the disposition of nref on either success or failure.
592 * When acquiring tokens recursively we want tok->t_ref to point to
593 * the outer (first) acquisition so it gets cleared only on the last
598 _lwkt_trytokref2(lwkt_tokref_t nref, thread_t td, int blocking)
604 * Make sure the compiler does not reorder prior instructions
605 * beyond this demark.
610 * Attempt to gain ownership
615 * Try to acquire the token if we do not already have
616 * it. This is not allowed if we are in a hard code
617 * section (because it 'might' have blocked).
621 KASSERT((blocking == 0 ||
622 td->td_gd->gd_intr_nesting_level == 0 ||
623 panic_cpu_gd == mycpu),
624 ("Attempt to acquire token %p not already "
625 "held in hard code section", tok));
628 * NOTE: If atomic_cmpset_ptr() fails we have to
629 * loop and try again. It just means we
632 if (atomic_cmpset_ptr(&tok->t_ref, NULL, nref))
638 * Test if ref is already recursively held by this
639 * thread. We cannot safely dereference tok->t_ref
640 * (it might belong to another thread and is thus
641 * unstable), but we don't have to. We can simply
644 * It is ok to acquire a token that is already held
645 * by the current thread when in a hard code section.
647 if (ref >= &td->td_toks_base && ref < td->td_toks_stop)
651 * Spin generously. This is preferable to just switching
652 * away unconditionally.
654 if (_lwkt_trytoken_spin(tok, nref))
658 * Otherwise we failed, and it is not ok to attempt to
659 * acquire a token in a hard code section.
661 KASSERT((blocking == 0 ||
662 td->td_gd->gd_intr_nesting_level == 0),
663 ("Attempt to acquire token %p not already "
664 "held in hard code section", tok));
671 * Get a serializing token. This routine can block.
674 lwkt_gettoken(lwkt_token_t tok)
676 thread_t td = curthread;
679 ref = td->td_toks_stop;
680 KKASSERT(ref < &td->td_toks_end);
683 _lwkt_tokref_init(ref, tok, td);
685 if (_lwkt_trytokref2(ref, td, 1) == FALSE) {
687 * Give up running if we can't acquire the token right now.
689 * Since the tokref is already active the scheduler now
690 * takes care of acquisition, so we need only call
693 * Since we failed this was not a recursive token so upon
694 * return tr_tok->t_ref should be assigned to this specific
697 td->td_wmesg = tok->t_desc;
698 atomic_add_long(&tok->t_collisions, 1);
702 KKASSERT(tok->t_ref == ref);
707 lwkt_gettoken_hard(lwkt_token_t tok)
709 thread_t td = curthread;
712 ref = td->td_toks_stop;
713 KKASSERT(ref < &td->td_toks_end);
716 _lwkt_tokref_init(ref, tok, td);
718 if (_lwkt_trytokref2(ref, td, 1) == FALSE) {
720 * Give up running if we can't acquire the token right now.
722 * Since the tokref is already active the scheduler now
723 * takes care of acquisition, so we need only call
726 * Since we failed this was not a recursive token so upon
727 * return tr_tok->t_ref should be assigned to this specific
730 td->td_wmesg = tok->t_desc;
731 atomic_add_long(&tok->t_collisions, 1);
735 KKASSERT(tok->t_ref == ref);
737 crit_enter_hard_gd(td->td_gd);
741 lwkt_getpooltoken(void *ptr)
743 thread_t td = curthread;
747 tok = _lwkt_token_pool_lookup(ptr);
748 ref = td->td_toks_stop;
749 KKASSERT(ref < &td->td_toks_end);
752 _lwkt_tokref_init(ref, tok, td);
754 if (_lwkt_trytokref2(ref, td, 1) == FALSE) {
756 * Give up running if we can't acquire the token right now.
758 * Since the tokref is already active the scheduler now
759 * takes care of acquisition, so we need only call
762 * Since we failed this was not a recursive token so upon
763 * return tr_tok->t_ref should be assigned to this specific
766 td->td_wmesg = tok->t_desc;
767 atomic_add_long(&tok->t_collisions, 1);
771 KKASSERT(tok->t_ref == ref);
777 * Attempt to acquire a token, return TRUE on success, FALSE on failure.
780 lwkt_trytoken(lwkt_token_t tok)
782 thread_t td = curthread;
785 ref = td->td_toks_stop;
786 KKASSERT(ref < &td->td_toks_end);
789 _lwkt_tokref_init(ref, tok, td);
791 if (_lwkt_trytokref2(ref, td, 0) == FALSE) {
793 * Cleanup, deactivate the failed token.
803 * Release a serializing token.
805 * WARNING! All tokens must be released in reverse order. This will be
809 lwkt_reltoken(lwkt_token_t tok)
811 thread_t td = curthread;
815 * Remove ref from thread token list and assert that it matches
816 * the token passed in. Tokens must be released in reverse order.
818 ref = td->td_toks_stop - 1;
819 KKASSERT(ref >= &td->td_toks_base && ref->tr_tok == tok);
822 * Only clear the token if it matches ref. If ref was a recursively
823 * acquired token it may not match. Then adjust td_toks_stop.
825 * Some comparisons must be run prior to adjusting td_toks_stop
826 * to avoid racing against a fast interrupt/ ipi which tries to
829 * We must also be absolutely sure that the compiler does not
830 * reorder the clearing of t_ref and the adjustment of td_toks_stop,
831 * or reorder the adjustment of td_toks_stop against the conditional.
833 * NOTE: The mplock is a token also so sequencing is a bit complex.
835 if (tok->t_ref == ref)
836 _lwkt_reltoken_spin(tok);
839 td->td_toks_stop = ref;
841 KKASSERT(tok->t_ref != ref);
845 lwkt_reltoken_hard(lwkt_token_t tok)
852 * It is faster for users of lwkt_getpooltoken() to use the returned
853 * token and just call lwkt_reltoken(), but for convenience we provide
854 * this function which looks the token up based on the ident.
857 lwkt_relpooltoken(void *ptr)
859 lwkt_token_t tok = _lwkt_token_pool_lookup(ptr);
864 * Return a count of the number of token refs the thread has to the
865 * specified token, whether it currently owns the token or not.
868 lwkt_cnttoken(lwkt_token_t tok, thread_t td)
873 for (scan = &td->td_toks_base; scan < td->td_toks_stop; ++scan) {
874 if (scan->tr_tok == tok)
882 * Pool tokens are used to provide a type-stable serializing token
883 * pointer that does not race against disappearing data structures.
885 * This routine is called in early boot just after we setup the BSP's
886 * globaldata structure.
889 lwkt_token_pool_init(void)
893 for (i = 0; i < LWKT_NUM_POOL_TOKENS; ++i)
894 lwkt_token_init(&pool_tokens[i], "pool");
898 lwkt_token_pool_lookup(void *ptr)
900 return (_lwkt_token_pool_lookup(ptr));
904 * Initialize a token.
907 lwkt_token_init(lwkt_token_t tok, const char *desc)
910 tok->t_collisions = 0;
916 lwkt_token_uninit(lwkt_token_t tok)
922 * Exchange the two most recent tokens on the tokref stack. This allows
923 * you to release a token out of order.
925 * We have to be careful about the case where the top two tokens are
926 * the same token. In this case tok->t_ref will point to the deeper
927 * ref and must remain pointing to the deeper ref. If we were to swap
928 * it the first release would clear the token even though a second
929 * ref is still present.
932 lwkt_token_swap(void)
934 lwkt_tokref_t ref1, ref2;
935 lwkt_token_t tok1, tok2;
936 thread_t td = curthread;
940 ref1 = td->td_toks_stop - 1;
941 ref2 = td->td_toks_stop - 2;
942 KKASSERT(ref1 > &td->td_toks_base);
943 KKASSERT(ref2 > &td->td_toks_base);
950 if (tok1->t_ref == ref1)
952 if (tok2->t_ref == ref2)
961 lwkt_token_is_stale(lwkt_tokref_t ref)
963 lwkt_token_t tok = ref->tr_tok;
965 KKASSERT(tok->t_owner == curthread && ref->tr_state == 1 &&
968 /* Token is not stale */
969 if (tok->t_lastowner == tok->t_owner)
973 * The token is stale. Reset to not stale so that the next call to
974 * lwkt_token_is_stale will return "not stale" unless the token
975 * was acquired in-between by another thread.
977 tok->t_lastowner = tok->t_owner;