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.5 2003/06/27 01:53:25 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>
41 #include <sys/sysctl.h>
42 #include <machine/cpu.h>
45 #include <vm/vm_param.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_object.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_pager.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_zone.h>
54 static int untimely_switch = 0;
55 SYSCTL_INT(_debug, OID_AUTO, untimely_switch, CTLFLAG_RW, &untimely_switch, 0, "");
60 _lwkt_dequeue(thread_t td)
62 if (td->td_flags & TDF_RUNQ) {
63 td->td_flags &= ~TDF_RUNQ;
64 TAILQ_REMOVE(&mycpu->gd_tdrunq, td, td_threadq);
70 _lwkt_enqueue(thread_t td)
72 if ((td->td_flags & TDF_RUNQ) == 0) {
73 td->td_flags |= TDF_RUNQ;
74 TAILQ_INSERT_TAIL(&mycpu->gd_tdrunq, td, td_threadq);
79 * LWKTs operate on a per-cpu basis
81 * YYY implement strict priorities & round-robin at the same priority
84 lwkt_gdinit(struct globaldata *gd)
86 TAILQ_INIT(&gd->gd_tdrunq);
90 * Initialize a thread wait structure prior to first use.
92 * NOTE! called from low level boot code, we cannot do anything fancy!
95 lwkt_init_wait(lwkt_wait_t w)
97 TAILQ_INIT(&w->wa_waitq);
101 * Create a new thread. The thread must be associated with a process context
102 * or LWKT start address before it can be scheduled.
104 * If you intend to create a thread without a process context this function
105 * does everything except load the startup and switcher function.
108 lwkt_alloc_thread(void)
114 if (mycpu->gd_tdfreecount > 0) {
115 --mycpu->gd_tdfreecount;
116 td = TAILQ_FIRST(&mycpu->gd_tdfreeq);
117 KASSERT(td != NULL, ("unexpected null cache td"));
118 TAILQ_REMOVE(&mycpu->gd_tdfreeq, td, td_threadq);
120 stack = td->td_kstack;
123 td = zalloc(thread_zone);
124 stack = (void *)kmem_alloc(kernel_map, UPAGES * PAGE_SIZE);
126 lwkt_init_thread(td, stack);
131 * Initialize a preexisting thread structure. This function is used by
132 * lwkt_alloc_thread() and also used to initialize the per-cpu idlethread.
134 * NOTE! called from low level boot code, we cannot do anything fancy!
137 lwkt_init_thread(thread_t td, void *stack)
139 bzero(td, sizeof(struct thread));
140 lwkt_rwlock_init(&td->td_rwlock);
141 td->td_kstack = stack;
142 pmap_init_thread(td);
146 * Switch to the next runnable lwkt. If no LWKTs are runnable then
147 * switch to the idlethread. Switching must occur within a critical
148 * section to avoid races with the scheduling queue.
150 * We always have full control over our cpu's run queue. Other cpus
151 * that wish to manipulate our queue must use the cpu_*msg() calls to
152 * talk to our cpu, so a critical section is all that is needed and
153 * the result is very, very fast thread switching.
155 * We always 'own' our own thread and the threads on our run queue,l
156 * due to TDF_RUNNING or TDF_RUNQ being set. We can safely clear
157 * TDF_RUNNING while in a critical section.
159 * The td_switch() function must be called while in the critical section.
160 * This function saves as much state as is appropriate for the type of
163 * (self contained on a per cpu basis)
168 thread_t td = curthread;
172 if ((ntd = TAILQ_FIRST(&mycpu->gd_tdrunq)) != NULL) {
173 TAILQ_REMOVE(&mycpu->gd_tdrunq, ntd, td_threadq);
174 TAILQ_INSERT_TAIL(&mycpu->gd_tdrunq, ntd, td_threadq);
176 ntd = &mycpu->gd_idlethread;
179 td->td_flags &= ~TDF_RUNNING;
180 ntd->td_flags |= TDF_RUNNING;
187 * Yield our thread while higher priority threads are pending. This is
188 * typically called when we leave a critical section but it can be safely
189 * called while we are in a critical section.
191 * This function will not generally yield to equal priority threads but it
192 * can occur as a side effect. Note that lwkt_switch() is called from
193 * inside the critical section to pervent its own crit_exit() from reentering
194 * lwkt_yield_quick().
196 * (self contained on a per cpu basis)
199 lwkt_yield_quick(void)
201 thread_t td = curthread;
202 while ((td->td_pri & TDPRI_MASK) < mycpu->gd_reqpri) {
204 cpu_schedule_reqs(); /* resets gd_reqpri */
210 * YYY enabling will cause wakeup() to task-switch, which really
211 * confused the old 4.x code. This is a good way to simulate
212 * preemption and MP without actually doing preemption or MP, because a
213 * lot of code assumes that wakeup() does not block.
215 if (untimely_switch && intr_nesting_level == 0) {
218 * YYY temporary hacks until we disassociate the userland scheduler
219 * from the LWKT scheduler.
221 if (td->td_flags & TDF_RUNQ) {
222 lwkt_switch(); /* will not reenter yield function */
224 lwkt_schedule_self(); /* make sure we are scheduled */
225 lwkt_switch(); /* will not reenter yield function */
226 lwkt_deschedule_self(); /* make sure we are descheduled */
233 * This implements a normal yield which, unlike _quick, will yield to equal
234 * priority threads as well. Note that gd_reqpri tests will be handled by
235 * the crit_exit() call in lwkt_switch().
237 * (self contained on a per cpu basis)
242 lwkt_schedule_self();
247 * Schedule a thread to run. As the current thread we can always safely
248 * schedule ourselves, and a shortcut procedure is provided for that
251 * (non-blocking, self contained on a per cpu basis)
254 lwkt_schedule_self(void)
256 thread_t td = curthread;
259 KASSERT(td->td_wait == NULL, ("lwkt_schedule_self(): td_wait not NULL!"));
260 KASSERT(td->td_flags & TDF_RUNNING, ("lwkt_schedule_self(): TDF_RUNNING not set!"));
266 * Generic schedule. Possibly schedule threads belonging to other cpus and
267 * deal with threads that might be blocked on a wait queue.
269 * This function will queue requests asynchronously when possible, but may
270 * block if no request structures are available. Upon return the caller
271 * should note that the scheduling request may not yet have been processed
274 * YYY this is one of the best places to implement any load balancing code.
275 * Load balancing can be accomplished by requesting other sorts of actions
276 * for the thread in question.
279 lwkt_schedule(thread_t td)
282 if (td == curthread) {
288 * If the thread is on a wait list we have to send our scheduling
289 * request to the owner of the wait structure. Otherwise we send
290 * the scheduling request to the cpu owning the thread. Races
291 * are ok, the target will forward the message as necessary (the
292 * message may chase the thread around before it finally gets
295 * (remember, wait structures use stable storage)
297 if ((w = td->td_wait) != NULL) {
298 if (lwkt_havetoken(&w->wa_token)) {
299 TAILQ_REMOVE(&w->wa_waitq, td, td_threadq);
302 if (td->td_cpu == mycpu->gd_cpu) {
305 panic("lwkt_schedule: cpu mismatch1");
307 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
308 initScheduleReqMsg_Wait(&msg.mu_SchedReq, td, w);
309 cpu_sendnormsg(&msg.mu_Msg);
313 panic("lwkt_schedule: cpu mismatch2");
315 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
316 initScheduleReqMsg_Wait(&msg.mu_SchedReq, td, w);
317 cpu_sendnormsg(&msg.mu_Msg);
322 * If the wait structure is NULL and we own the thread, there
323 * is no race (since we are in a critical section). If we
324 * do not own the thread there might be a race but the
325 * target cpu will deal with it.
327 if (td->td_cpu == mycpu->gd_cpu) {
330 panic("lwkt_schedule: cpu mismatch3");
332 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
333 initScheduleReqMsg_Thread(&msg.mu_SchedReq, td);
334 cpu_sendnormsg(&msg.mu_Msg);
343 * Deschedule a thread.
345 * (non-blocking, self contained on a per cpu basis)
348 lwkt_deschedule_self(void)
350 thread_t td = curthread;
353 KASSERT(td->td_wait == NULL, ("lwkt_schedule_self(): td_wait not NULL!"));
354 KASSERT(td->td_flags & TDF_RUNNING, ("lwkt_schedule_self(): TDF_RUNNING not set!"));
360 * Generic deschedule. Descheduling threads other then your own should be
361 * done only in carefully controlled circumstances. Descheduling is
364 * This function may block if the cpu has run out of messages.
367 lwkt_deschedule(thread_t td)
370 if (td == curthread) {
373 if (td->td_cpu == mycpu->gd_cpu) {
376 panic("lwkt_deschedule: cpu mismatch");
378 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
379 initDescheduleReqMsg_Thread(&msg.mu_DeschedReq, td);
380 cpu_sendnormsg(&msg.mu_Msg);
388 * This function deschedules the current thread and blocks on the specified
389 * wait queue. We obtain ownership of the wait queue in order to block
390 * on it. A generation number is used to interlock the wait queue in case
391 * it gets signalled while we are blocked waiting on the token.
393 * Note: alternatively we could dequeue our thread and then message the
394 * target cpu owning the wait queue. YYY implement as sysctl.
396 * Note: wait queue signals normally ping-pong the cpu as an optimization.
399 lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen)
401 thread_t td = curthread;
403 lwkt_gettoken(&w->wa_token);
404 if (w->wa_gen == *gen) {
406 TAILQ_INSERT_TAIL(&w->wa_waitq, td, td_threadq);
409 td->td_wmesg = wmesg;
412 /* token might be lost, doesn't matter for gen update */
414 lwkt_reltoken(&w->wa_token);
418 * Signal a wait queue. We gain ownership of the wait queue in order to
419 * signal it. Once a thread is removed from the wait queue we have to
420 * deal with the cpu owning the thread.
422 * Note: alternatively we could message the target cpu owning the wait
423 * queue. YYY implement as sysctl.
426 lwkt_signal(lwkt_wait_t w)
431 lwkt_gettoken(&w->wa_token);
434 while ((td = TAILQ_FIRST(&w->wa_waitq)) != NULL && count) {
437 TAILQ_REMOVE(&w->wa_waitq, td, td_threadq);
440 if (td->td_cpu == mycpu->gd_cpu) {
444 lwkt_cpu_msg_union_t msg = lwkt_getcpumsg();
445 initScheduleReqMsg_Thread(&msg.mu_SchedReq, td);
446 cpu_sendnormsg(&msg.mu_Msg);
448 panic("lwkt_signal: cpu mismatch");
450 lwkt_regettoken(&w->wa_token);
452 lwkt_reltoken(&w->wa_token);
456 * Aquire ownership of a token
458 * Aquire ownership of a token. The token may have spl and/or critical
459 * section side effects, depending on its purpose. These side effects
460 * guarentee that you will maintain ownership of the token as long as you
461 * do not block. If you block you may lose access to the token (but you
462 * must still release it even if you lose your access to it).
464 * Note that the spl and critical section characteristics of a token
465 * may not be changed once the token has been initialized.
468 lwkt_gettoken(lwkt_token_t tok)
471 * Prevent preemption so the token can't be taken away from us once
472 * we gain ownership of it. Use a synchronous request which might
473 * block. The request will be forwarded as necessary playing catchup
478 while (tok->t_cpu != mycpu->gd_cpu) {
479 lwkt_cpu_msg_union msg;
480 initTokenReqMsg(&msg.mu_TokenReq);
485 * leave us in a critical section on return. This will be undone
491 * Release your ownership of a token. Releases must occur in reverse
492 * order to aquisitions, eventually so priorities can be unwound properly
493 * like SPLs. At the moment the actual implemention doesn't care.
495 * We can safely hand a token that we own to another cpu without notifying
496 * it, but once we do we can't get it back without requesting it (unless
497 * the other cpu hands it back to us before we check).
499 * We might have lost the token, so check that.
502 lwkt_reltoken(lwkt_token_t tok)
504 if (tok->t_cpu == mycpu->gd_cpu) {
505 tok->t_cpu = tok->t_reqcpu;
511 * Reaquire a token that might have been lost. Returns 1 if we blocked
512 * while reaquiring the token (meaning that you might have lost other
513 * tokens you held when you made this call), return 0 if we did not block.
516 lwkt_regettoken(lwkt_token_t tok)
519 if (tok->t_cpu != mycpu->gd_cpu) {
520 while (tok->t_cpu != mycpu->gd_cpu) {
521 lwkt_cpu_msg_union msg;
522 initTokenReqMsg(&msg.mu_TokenReq);