2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Each cpu in a system has its own self-contained light weight kernel
27 * thread scheduler, which means that generally speaking we only need
28 * to use a critical section to prevent hicups.
30 * $DragonFly: src/sys/kern/lwkt_thread.c,v 1.15 2003/07/06 21:23:51 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/rtprio.h>
38 #include <sys/queue.h>
39 #include <sys/thread2.h>
40 #include <sys/sysctl.h>
41 #include <sys/kthread.h>
42 #include <machine/cpu.h>
46 #include <vm/vm_param.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_pager.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_zone.h>
55 #include <machine/stdarg.h>
57 static int untimely_switch = 0;
58 SYSCTL_INT(_lwkt, OID_AUTO, untimely_switch, CTLFLAG_RW, &untimely_switch, 0, "");
59 static quad_t switch_count = 0;
60 SYSCTL_QUAD(_lwkt, OID_AUTO, switch_count, CTLFLAG_RW, &switch_count, 0, "");
61 static quad_t preempt_hit = 0;
62 SYSCTL_QUAD(_lwkt, OID_AUTO, preempt_hit, CTLFLAG_RW, &preempt_hit, 0, "");
63 static quad_t preempt_miss = 0;
64 SYSCTL_QUAD(_lwkt, OID_AUTO, preempt_miss, CTLFLAG_RW, &preempt_miss, 0, "");
65 static quad_t preempt_weird = 0;
66 SYSCTL_QUAD(_lwkt, OID_AUTO, preempt_weird, CTLFLAG_RW, &preempt_weird, 0, "");
69 * These helper procedures handle the runq, they can only be called from
70 * within a critical section.
74 _lwkt_dequeue(thread_t td)
76 if (td->td_flags & TDF_RUNQ) {
77 int nq = td->td_pri & TDPRI_MASK;
78 struct globaldata *gd = mycpu;
80 td->td_flags &= ~TDF_RUNQ;
81 TAILQ_REMOVE(&gd->gd_tdrunq[nq], td, td_threadq);
82 /* runqmask is passively cleaned up by the switcher */
88 _lwkt_enqueue(thread_t td)
90 if ((td->td_flags & TDF_RUNQ) == 0) {
91 int nq = td->td_pri & TDPRI_MASK;
92 struct globaldata *gd = mycpu;
94 td->td_flags |= TDF_RUNQ;
95 TAILQ_INSERT_TAIL(&gd->gd_tdrunq[nq], td, td_threadq);
96 gd->gd_runqmask |= 1 << nq;
99 * YYY needs cli/sti protection? gd_reqpri set by interrupt
100 * when made pending. need better mechanism.
102 if (gd->gd_reqpri < (td->td_pri & TDPRI_MASK))
103 gd->gd_reqpri = (td->td_pri & TDPRI_MASK);
109 * LWKTs operate on a per-cpu basis
111 * WARNING! Called from early boot, 'mycpu' may not work yet.
114 lwkt_gdinit(struct globaldata *gd)
118 for (i = 0; i < sizeof(gd->gd_tdrunq)/sizeof(gd->gd_tdrunq[0]); ++i)
119 TAILQ_INIT(&gd->gd_tdrunq[i]);
121 TAILQ_INIT(&gd->gd_tdallq);
125 * Initialize a thread wait structure prior to first use.
127 * NOTE! called from low level boot code, we cannot do anything fancy!
130 lwkt_init_wait(lwkt_wait_t w)
132 TAILQ_INIT(&w->wa_waitq);
136 * Create a new thread. The thread must be associated with a process context
137 * or LWKT start address before it can be scheduled.
139 * If you intend to create a thread without a process context this function
140 * does everything except load the startup and switcher function.
143 lwkt_alloc_thread(struct thread *td)
150 if (mycpu->gd_tdfreecount > 0) {
151 --mycpu->gd_tdfreecount;
152 td = TAILQ_FIRST(&mycpu->gd_tdfreeq);
153 KASSERT(td != NULL && (td->td_flags & TDF_EXITED),
154 ("lwkt_alloc_thread: unexpected NULL or corrupted td"));
155 TAILQ_REMOVE(&mycpu->gd_tdfreeq, td, td_threadq);
157 stack = td->td_kstack;
158 flags = td->td_flags & (TDF_ALLOCATED_STACK|TDF_ALLOCATED_THREAD);
161 td = zalloc(thread_zone);
162 td->td_kstack = NULL;
163 flags |= TDF_ALLOCATED_THREAD;
166 if ((stack = td->td_kstack) == NULL) {
167 stack = (void *)kmem_alloc(kernel_map, UPAGES * PAGE_SIZE);
168 flags |= TDF_ALLOCATED_STACK;
170 lwkt_init_thread(td, stack, flags, mycpu);
175 * Initialize a preexisting thread structure. This function is used by
176 * lwkt_alloc_thread() and also used to initialize the per-cpu idlethread.
178 * NOTE! called from low level boot code, we cannot do anything fancy!
181 lwkt_init_thread(thread_t td, void *stack, int flags, struct globaldata *gd)
183 bzero(td, sizeof(struct thread));
184 td->td_kstack = stack;
185 td->td_flags |= flags;
187 td->td_pri = TDPRI_CRIT;
188 td->td_cpu = gd->gd_cpuid; /* YYY don't need this if have td_gd */
189 pmap_init_thread(td);
191 TAILQ_INSERT_TAIL(&mycpu->gd_tdallq, td, td_allq);
196 lwkt_set_comm(thread_t td, const char *ctl, ...)
201 vsnprintf(td->td_comm, sizeof(td->td_comm), ctl, va);
206 lwkt_hold(thread_t td)
212 lwkt_rele(thread_t td)
214 KKASSERT(td->td_refs > 0);
219 lwkt_wait_free(thread_t td)
222 tsleep(td, PWAIT, "tdreap", hz);
226 lwkt_free_thread(thread_t td)
228 struct globaldata *gd = mycpu;
230 KASSERT(td->td_flags & TDF_EXITED,
231 ("lwkt_free_thread: did not exit! %p", td));
234 TAILQ_REMOVE(&gd->gd_tdallq, td, td_allq);
235 if (gd->gd_tdfreecount < CACHE_NTHREADS &&
236 (td->td_flags & TDF_ALLOCATED_THREAD)
238 ++gd->gd_tdfreecount;
239 TAILQ_INSERT_HEAD(&gd->gd_tdfreeq, td, td_threadq);
243 if (td->td_kstack && (td->td_flags & TDF_ALLOCATED_STACK)) {
244 kmem_free(kernel_map,
245 (vm_offset_t)td->td_kstack, UPAGES * PAGE_SIZE);
247 td->td_kstack = NULL;
249 if (td->td_flags & TDF_ALLOCATED_THREAD)
250 zfree(thread_zone, td);
256 * Switch to the next runnable lwkt. If no LWKTs are runnable then
257 * switch to the idlethread. Switching must occur within a critical
258 * section to avoid races with the scheduling queue.
260 * We always have full control over our cpu's run queue. Other cpus
261 * that wish to manipulate our queue must use the cpu_*msg() calls to
262 * talk to our cpu, so a critical section is all that is needed and
263 * the result is very, very fast thread switching.
265 * We always 'own' our own thread and the threads on our run queue,l
266 * due to TDF_RUNNING or TDF_RUNQ being set. We can safely clear
267 * TDF_RUNNING while in a critical section.
269 * The td_switch() function must be called while in the critical section.
270 * This function saves as much state as is appropriate for the type of
273 * (self contained on a per cpu basis)
278 struct globaldata *gd;
279 thread_t td = curthread;
285 if (mycpu->gd_intr_nesting_level && td->td_preempted == NULL)
286 panic("lwkt_switch: cannot switch from within an interrupt, yet\n");
293 * td_mpcount cannot be used to determine if we currently hold the
294 * MP lock because get_mplock() will increment it prior to attempting
295 * to get the lock, and switch out if it can't. Look at the actual lock.
297 mpheld = MP_LOCK_HELD();
299 if ((ntd = td->td_preempted) != NULL) {
301 * We had preempted another thread on this cpu, resume the preempted
302 * thread. This occurs transparently, whether the preempted thread
303 * was scheduled or not (it may have been preempted after descheduling
306 * We have to setup the MP lock for the original thread after backing
307 * out the adjustment that was made to curthread when the original
310 KKASSERT(ntd->td_flags & TDF_PREEMPT_LOCK);
312 if (ntd->td_mpcount) {
313 td->td_mpcount -= ntd->td_mpcount;
314 KKASSERT(td->td_mpcount >= 0);
317 ntd->td_flags |= TDF_PREEMPT_DONE;
318 /* YYY release mp lock on switchback if original doesn't need it */
321 * Priority queue / round-robin at each priority. Note that user
322 * processes run at a fixed, low priority and the user process
323 * scheduler deals with interactions between user processes
324 * by scheduling and descheduling them from the LWKT queue as
327 * We have to adjust the MP lock for the target thread. If we
328 * need the MP lock and cannot obtain it we try to locate a
329 * thread that does not need the MP lock.
333 if (gd->gd_runqmask) {
334 int nq = bsrl(gd->gd_runqmask);
335 if ((ntd = TAILQ_FIRST(&gd->gd_tdrunq[nq])) == NULL) {
336 gd->gd_runqmask &= ~(1 << nq);
340 if (ntd->td_mpcount && mpheld == 0 && !cpu_try_mplock()) {
342 * Target needs MP lock and we couldn't get it.
344 u_int32_t rqmask = gd->gd_runqmask;
346 TAILQ_FOREACH(ntd, &gd->gd_tdrunq[nq], td_threadq) {
347 if (ntd->td_mpcount == 0)
352 rqmask &= ~(1 << nq);
358 TAILQ_REMOVE(&gd->gd_tdrunq[nq], ntd, td_threadq);
359 TAILQ_INSERT_TAIL(&gd->gd_tdrunq[nq], ntd, td_threadq);
362 TAILQ_REMOVE(&gd->gd_tdrunq[nq], ntd, td_threadq);
363 TAILQ_INSERT_TAIL(&gd->gd_tdrunq[nq], ntd, td_threadq);
366 TAILQ_REMOVE(&gd->gd_tdrunq[nq], ntd, td_threadq);
367 TAILQ_INSERT_TAIL(&gd->gd_tdrunq[nq], ntd, td_threadq);
373 KASSERT(ntd->td_pri >= TDPRI_CRIT,
374 ("priority problem in lwkt_switch %d %d", td->td_pri, ntd->td_pri));
377 * Do the actual switch. If the new target does not need the MP lock
378 * and we are holding it, release the MP lock. If the new target requires
379 * the MP lock we have already acquired it for the target.
382 if (ntd->td_mpcount == 0 ) {
386 ASSERT_MP_LOCK_HELD();
397 * Request that the target thread preempt the current thread. This only
400 * + We aren't trying to preempt ourselves (it can happen!)
401 * + We are not currently being preempted
402 * + The target is not currently being preempted
403 * + The target either does not need the MP lock or we can get it
404 * for the target immediately.
406 * XXX at the moment we run the target thread in a critical section during
407 * the preemption in order to prevent the target from taking interrupts
408 * that *WE* can't. Preemption is strictly limited to interrupt threads
409 * and interrupt-like threads, outside of a critical section, and the
410 * preempted source thread will be resumed the instant the target blocks
411 * whether or not the source is scheduled (i.e. preemption is supposed to
412 * be as transparent as possible).
414 * This call is typically made from an interrupt handler like sched_ithd()
415 * which will only run if the current thread is not in a critical section,
416 * so we optimize the priority check a bit.
418 * CAREFUL! either we or the target thread may get interrupted during the
421 * The target thread inherits our MP count (added to its own) for the
422 * duration of the preemption in order to preserve the atomicy of the
426 lwkt_preempt(thread_t ntd, int id)
428 thread_t td = curthread;
434 * The caller has put us in a critical section, and in order to have
435 * gotten here in the first place the thread the caller interrupted
436 * cannot have been in a critical section before.
438 KASSERT(ntd->td_pri >= TDPRI_CRIT, ("BADCRIT0 %d", ntd->td_pri));
439 KASSERT((td->td_pri & ~TDPRI_MASK) == TDPRI_CRIT, ("BADPRI %d", td->td_pri));
441 if (td == ntd || ((td->td_flags | ntd->td_flags) & TDF_PREEMPT_LOCK)) {
445 if (ntd->td_preempted) {
449 if ((ntd->td_pri & TDPRI_MASK) <= (td->td_pri & TDPRI_MASK)) {
454 mpheld = MP_LOCK_HELD();
455 ntd->td_mpcount += td->td_mpcount;
456 if (mpheld == 0 && ntd->td_mpcount && !cpu_try_mplock()) {
457 ntd->td_mpcount -= td->td_mpcount;
464 ntd->td_preempted = td;
465 td->td_flags |= TDF_PREEMPT_LOCK;
467 KKASSERT(ntd->td_preempted && (td->td_flags & TDF_PREEMPT_DONE));
468 ntd->td_preempted = NULL;
469 td->td_flags &= ~(TDF_PREEMPT_LOCK|TDF_PREEMPT_DONE);
473 * Yield our thread while higher priority threads are pending. This is
474 * typically called when we leave a critical section but it can be safely
475 * called while we are in a critical section.
477 * This function will not generally yield to equal priority threads but it
478 * can occur as a side effect. Note that lwkt_switch() is called from
479 * inside the critical section to pervent its own crit_exit() from reentering
480 * lwkt_yield_quick().
482 * gd_reqpri indicates that *something* changed, e.g. an interrupt or softint
483 * came along but was blocked and made pending.
485 * (self contained on a per cpu basis)
488 lwkt_yield_quick(void)
490 thread_t td = curthread;
492 if ((td->td_pri & TDPRI_MASK) < mycpu->gd_reqpri) {
493 mycpu->gd_reqpri = 0;
498 * YYY enabling will cause wakeup() to task-switch, which really
499 * confused the old 4.x code. This is a good way to simulate
500 * preemption and MP without actually doing preemption or MP, because a
501 * lot of code assumes that wakeup() does not block.
503 if (untimely_switch && mycpu->gd_intr_nesting_level == 0) {
506 * YYY temporary hacks until we disassociate the userland scheduler
507 * from the LWKT scheduler.
509 if (td->td_flags & TDF_RUNQ) {
510 lwkt_switch(); /* will not reenter yield function */
512 lwkt_schedule_self(); /* make sure we are scheduled */
513 lwkt_switch(); /* will not reenter yield function */
514 lwkt_deschedule_self(); /* make sure we are descheduled */
521 * This implements a normal yield which, unlike _quick, will yield to equal
522 * priority threads as well. Note that gd_reqpri tests will be handled by
523 * the crit_exit() call in lwkt_switch().
525 * (self contained on a per cpu basis)
530 lwkt_schedule_self();
535 * Schedule a thread to run. As the current thread we can always safely
536 * schedule ourselves, and a shortcut procedure is provided for that
539 * (non-blocking, self contained on a per cpu basis)
542 lwkt_schedule_self(void)
544 thread_t td = curthread;
547 KASSERT(td->td_wait == NULL, ("lwkt_schedule_self(): td_wait not NULL!"));
549 if (td->td_proc && td->td_proc->p_stat == SSLEEP)
550 panic("SCHED SELF PANIC");
555 * Generic schedule. Possibly schedule threads belonging to other cpus and
556 * deal with threads that might be blocked on a wait queue.
558 * This function will queue requests asynchronously when possible, but may
559 * block if no request structures are available. Upon return the caller
560 * should note that the scheduling request may not yet have been processed
563 * YYY this is one of the best places to implement any load balancing code.
564 * Load balancing can be accomplished by requesting other sorts of actions
565 * for the thread in question.
568 lwkt_schedule(thread_t td)
570 if ((td->td_flags & TDF_PREEMPT_LOCK) == 0 && td->td_proc
571 && td->td_proc->p_stat == SSLEEP
573 printf("PANIC schedule curtd = %p (%d %d) target %p (%d %d)\n",
575 curthread->td_proc ? curthread->td_proc->p_pid : -1,
576 curthread->td_proc ? curthread->td_proc->p_stat : -1,
578 td->td_proc ? curthread->td_proc->p_pid : -1,
579 td->td_proc ? curthread->td_proc->p_stat : -1
581 panic("SCHED PANIC");
584 if (td == curthread) {
590 * If the thread is on a wait list we have to send our scheduling
591 * request to the owner of the wait structure. Otherwise we send
592 * the scheduling request to the cpu owning the thread. Races
593 * are ok, the target will forward the message as necessary (the
594 * message may chase the thread around before it finally gets
597 * (remember, wait structures use stable storage)
599 if ((w = td->td_wait) != NULL) {
600 if (lwkt_havetoken(&w->wa_token)) {
601 TAILQ_REMOVE(&w->wa_waitq, td, td_threadq);
604 if (td->td_cpu == mycpu->gd_cpuid) {
607 panic("lwkt_schedule: cpu mismatch1");
609 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
610 initScheduleReqMsg_Wait(&msg.mu_SchedReq, td, w);
611 cpu_sendnormsg(&msg.mu_Msg);
615 panic("lwkt_schedule: cpu mismatch2");
617 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
618 initScheduleReqMsg_Wait(&msg.mu_SchedReq, td, w);
619 cpu_sendnormsg(&msg.mu_Msg);
624 * If the wait structure is NULL and we own the thread, there
625 * is no race (since we are in a critical section). If we
626 * do not own the thread there might be a race but the
627 * target cpu will deal with it.
629 if (td->td_cpu == mycpu->gd_cpuid) {
632 panic("lwkt_schedule: cpu mismatch3");
634 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
635 initScheduleReqMsg_Thread(&msg.mu_SchedReq, td);
636 cpu_sendnormsg(&msg.mu_Msg);
645 * Deschedule a thread.
647 * (non-blocking, self contained on a per cpu basis)
650 lwkt_deschedule_self(void)
652 thread_t td = curthread;
655 KASSERT(td->td_wait == NULL, ("lwkt_schedule_self(): td_wait not NULL!"));
661 * Generic deschedule. Descheduling threads other then your own should be
662 * done only in carefully controlled circumstances. Descheduling is
665 * This function may block if the cpu has run out of messages.
668 lwkt_deschedule(thread_t td)
671 if (td == curthread) {
674 if (td->td_cpu == mycpu->gd_cpuid) {
677 panic("lwkt_deschedule: cpu mismatch");
679 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
680 initDescheduleReqMsg_Thread(&msg.mu_DeschedReq, td);
681 cpu_sendnormsg(&msg.mu_Msg);
689 * Set the target thread's priority. This routine does not automatically
690 * switch to a higher priority thread, LWKT threads are not designed for
691 * continuous priority changes. Yield if you want to switch.
693 * We have to retain the critical section count which uses the high bits
694 * of the td_pri field. The specified priority may also indicate zero or
695 * more critical sections by adding TDPRI_CRIT*N.
698 lwkt_setpri(thread_t td, int pri)
702 if (td->td_flags & TDF_RUNQ) {
704 td->td_pri = (td->td_pri & ~TDPRI_MASK) + pri;
707 td->td_pri = (td->td_pri & ~TDPRI_MASK) + pri;
713 lwkt_setpri_self(int pri)
715 thread_t td = curthread;
717 KKASSERT(pri >= 0 && pri <= TDPRI_MAX);
719 if (td->td_flags & TDF_RUNQ) {
721 td->td_pri = (td->td_pri & ~TDPRI_MASK) + pri;
724 td->td_pri = (td->td_pri & ~TDPRI_MASK) + pri;
730 lwkt_preempted_proc(void)
732 thread_t td = curthread;
733 while (td->td_preempted)
734 td = td->td_preempted;
740 * This function deschedules the current thread and blocks on the specified
741 * wait queue. We obtain ownership of the wait queue in order to block
742 * on it. A generation number is used to interlock the wait queue in case
743 * it gets signalled while we are blocked waiting on the token.
745 * Note: alternatively we could dequeue our thread and then message the
746 * target cpu owning the wait queue. YYY implement as sysctl.
748 * Note: wait queue signals normally ping-pong the cpu as an optimization.
751 lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen)
753 thread_t td = curthread;
755 lwkt_gettoken(&w->wa_token);
756 if (w->wa_gen == *gen) {
758 TAILQ_INSERT_TAIL(&w->wa_waitq, td, td_threadq);
761 td->td_wmesg = wmesg;
764 /* token might be lost, doesn't matter for gen update */
766 lwkt_reltoken(&w->wa_token);
770 * Signal a wait queue. We gain ownership of the wait queue in order to
771 * signal it. Once a thread is removed from the wait queue we have to
772 * deal with the cpu owning the thread.
774 * Note: alternatively we could message the target cpu owning the wait
775 * queue. YYY implement as sysctl.
778 lwkt_signal(lwkt_wait_t w)
783 lwkt_gettoken(&w->wa_token);
786 while ((td = TAILQ_FIRST(&w->wa_waitq)) != NULL && count) {
789 TAILQ_REMOVE(&w->wa_waitq, td, td_threadq);
792 if (td->td_cpu == mycpu->gd_cpuid) {
796 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
797 initScheduleReqMsg_Thread(&msg.mu_SchedReq, td);
798 cpu_sendnormsg(&msg.mu_Msg);
800 panic("lwkt_signal: cpu mismatch");
802 lwkt_regettoken(&w->wa_token);
804 lwkt_reltoken(&w->wa_token);
808 * Aquire ownership of a token
810 * Aquire ownership of a token. The token may have spl and/or critical
811 * section side effects, depending on its purpose. These side effects
812 * guarentee that you will maintain ownership of the token as long as you
813 * do not block. If you block you may lose access to the token (but you
814 * must still release it even if you lose your access to it).
816 * Note that the spl and critical section characteristics of a token
817 * may not be changed once the token has been initialized.
820 lwkt_gettoken(lwkt_token_t tok)
823 * Prevent preemption so the token can't be taken away from us once
824 * we gain ownership of it. Use a synchronous request which might
825 * block. The request will be forwarded as necessary playing catchup
830 while (tok->t_cpu != mycpu->gd_cpuid) {
831 lwkt_cpu_msg_union msg;
832 initTokenReqMsg(&msg.mu_TokenReq);
837 * leave us in a critical section on return. This will be undone
838 * by lwkt_reltoken(). Bump the generation number.
840 return(++tok->t_gen);
844 * Release your ownership of a token. Releases must occur in reverse
845 * order to aquisitions, eventually so priorities can be unwound properly
846 * like SPLs. At the moment the actual implemention doesn't care.
848 * We can safely hand a token that we own to another cpu without notifying
849 * it, but once we do we can't get it back without requesting it (unless
850 * the other cpu hands it back to us before we check).
852 * We might have lost the token, so check that.
855 lwkt_reltoken(lwkt_token_t tok)
857 if (tok->t_cpu == mycpu->gd_cpuid) {
858 tok->t_cpu = tok->t_reqcpu;
864 * Reacquire a token that might have been lost and compare and update the
865 * generation number. 0 is returned if the generation has not changed
866 * (nobody else obtained the token while we were blocked, on this cpu or
869 * This function returns with the token re-held whether the generation
870 * number changed or not.
873 lwkt_gentoken(lwkt_token_t tok, int *gen)
875 if (lwkt_regettoken(tok) == *gen) {
885 * Reacquire a token that might have been lost. Returns the generation
886 * number of the token.
889 lwkt_regettoken(lwkt_token_t tok)
892 if (tok->t_cpu != mycpu->gd_cpuid) {
893 while (tok->t_cpu != mycpu->gd_cpuid) {
894 lwkt_cpu_msg_union msg;
895 initTokenReqMsg(&msg.mu_TokenReq);
904 lwkt_inittoken(lwkt_token_t tok)
907 * Zero structure and set cpu owner and reqcpu to cpu 0.
909 bzero(tok, sizeof(*tok));
913 * Create a kernel process/thread/whatever. It shares it's address space
914 * with proc0 - ie: kernel only.
916 * XXX should be renamed to lwkt_create()
918 * The thread will be entered with the MP lock held.
921 lwkt_create(void (*func)(void *), void *arg,
922 struct thread **tdp, thread_t template, int tdflags,
923 const char *fmt, ...)
928 td = *tdp = lwkt_alloc_thread(template);
929 cpu_set_thread_handler(td, kthread_exit, func, arg);
930 td->td_flags |= TDF_VERBOSE | tdflags;
936 * Set up arg0 for 'ps' etc
939 vsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
943 * Schedule the thread to run
945 if ((td->td_flags & TDF_STOPREQ) == 0)
948 td->td_flags &= ~TDF_STOPREQ;
953 * Destroy an LWKT thread. Warning! This function is not called when
954 * a process exits, cpu_proc_exit() directly calls cpu_thread_exit() and
955 * uses a different reaping mechanism.
960 thread_t td = curthread;
962 if (td->td_flags & TDF_VERBOSE)
963 printf("kthread %p %s has exited\n", td, td->td_comm);
965 lwkt_deschedule_self();
966 ++mycpu->gd_tdfreecount;
967 TAILQ_INSERT_TAIL(&mycpu->gd_tdfreeq, td, td_threadq);
972 * Create a kernel process/thread/whatever. It shares it's address space
973 * with proc0 - ie: kernel only. 5.x compatible.
976 kthread_create(void (*func)(void *), void *arg,
977 struct thread **tdp, const char *fmt, ...)
982 td = *tdp = lwkt_alloc_thread(NULL);
983 cpu_set_thread_handler(td, kthread_exit, func, arg);
984 td->td_flags |= TDF_VERBOSE;
990 * Set up arg0 for 'ps' etc
993 vsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap);
997 * Schedule the thread to run
1006 thread_t td = curthread;
1007 int lpri = td->td_pri;
1010 panic("td_pri is/would-go negative! %p %d", td, lpri);
1014 * Destroy an LWKT thread. Warning! This function is not called when
1015 * a process exits, cpu_proc_exit() directly calls cpu_thread_exit() and
1016 * uses a different reaping mechanism.
1018 * XXX duplicates lwkt_exit()