Idle task never runs

- idle task becomes a pseudo task which is never scheduled. It is never put on
  any run queue and never enters userspace. An entry for this task still remains
  in the process table for time accounting

- Instead of panicing if there is not process to schedule, pick_proc() returns
  NULL which is a signal to put the cpu in an idle state and set everything in
  such a way that after receiving and interrupt it looks like idle task was
  preempted

- idle task is set non-preemptible to avoid handling in the timer interrupt code
  which make userspace scheduling simpler as idle task does not need to be
  handled as a special case.
This commit is contained in:
Tomas Hruby 2009-11-12 08:42:18 +00:00
parent 37a7e1b76b
commit ad4dcaab71
5 changed files with 32 additions and 16 deletions

View file

@ -30,7 +30,7 @@
.globl phys_memset /* write pattern anywhere in memory */ .globl phys_memset /* write pattern anywhere in memory */
.globl mem_rdw /* copy one word from [segment:offset] */ .globl mem_rdw /* copy one word from [segment:offset] */
.globl reset /* reset the system */ .globl reset /* reset the system */
.globl idle_task /* task executed when there is no work */ .globl halt_cpu/* halts the current cpu when idle */
.globl level0 /* call a function at level 0 */ .globl level0 /* call a function at level 0 */
.globl read_cpu_flags /* read the cpu flags */ .globl read_cpu_flags /* read the cpu flags */
.globl read_cr0 /* read cr0 */ .globl read_cr0 /* read cr0 */
@ -436,18 +436,14 @@ idt_zero:
/*===========================================================================*/ /*===========================================================================*/
/* idle_task */ /* halt_cpu */
/*===========================================================================*/ /*===========================================================================*/
idle_task:
/* /*
* This task is called when the system has nothing else to do. The HLT * PUBLIC void halt_cpu(void);
* instruction puts the processor in a state where it draws minimum power. * reanables interrupts and puts the cpu in the halts state. Once an interrupt
* is handled the execution resumes by disabling interrupts and continues
*/ */
push $halt halt_cpu:
call level0 /* level0(halt) */
add $4, %esp
jmp idle_task
halt:
sti sti
hlt hlt
cli cli

View file

@ -178,6 +178,8 @@ PUBLIC void main()
/* Set ready. The HARDWARE task is never ready. */ /* Set ready. The HARDWARE task is never ready. */
if (rp->p_nr == HARDWARE) RTS_SET(rp, RTS_PROC_STOP); if (rp->p_nr == HARDWARE) RTS_SET(rp, RTS_PROC_STOP);
/* IDLE task is never put on a run queue as it is never ready to run */
if (rp->p_nr == IDLE) RTS_SET(rp, RTS_PROC_STOP);
RTS_UNSET(rp, RTS_SLOT_FREE); /* remove RTS_SLOT_FREE and schedule */ RTS_UNSET(rp, RTS_SLOT_FREE); /* remove RTS_SLOT_FREE and schedule */
alloc_segments(rp); alloc_segments(rp);
} }

View file

@ -155,7 +155,19 @@ not_runnable_pick_new:
/* this enqueues the process again */ /* this enqueues the process again */
if (proc_no_quantum(proc_ptr)) if (proc_no_quantum(proc_ptr))
RTS_UNSET(proc_ptr, RTS_NO_QUANTUM); RTS_UNSET(proc_ptr, RTS_NO_QUANTUM);
proc_ptr = pick_proc();
/*
* if we have no process to run, set IDLE as the current process for
* time accounting and put the cpu in and idle state. After the next
* timer interrupt the execution resumes here and we can pick another
* process. If there is still nothing runnable we "schedule" IDLE again
*/
while (!(proc_ptr = pick_proc())) {
proc_ptr = proc_addr(IDLE);
if (priv(proc_ptr)->s_flags & BILLABLE)
bill_ptr = proc_ptr;
halt_cpu();
}
check_misc_flags: check_misc_flags:
@ -1388,7 +1400,6 @@ PRIVATE struct proc * pick_proc(void)
bill_ptr = rp; /* bill for system time */ bill_ptr = rp; /* bill for system time */
return rp; return rp;
} }
minix_panic("no runnable processes", NO_NUM);
return NULL; return NULL;
} }

View file

@ -136,7 +136,7 @@ _PROTOTYPE( vir_bytes alloc_remote_segment, (u32_t *, segframe_t *,
int, phys_bytes, vir_bytes, int)); int, phys_bytes, vir_bytes, int));
_PROTOTYPE( int intr_init, (int) ); _PROTOTYPE( int intr_init, (int) );
_PROTOTYPE( int intr_disabled, (void) ); _PROTOTYPE( int intr_disabled, (void) );
_PROTOTYPE( void idle_task, (void) ); _PROTOTYPE( void halt_cpu, (void) );
_PROTOTYPE( void arch_init, (void) ); _PROTOTYPE( void arch_init, (void) );
_PROTOTYPE( void ser_putc, (char) ); _PROTOTYPE( void ser_putc, (char) );
_PROTOTYPE( void arch_shutdown, (int) ); _PROTOTYPE( void arch_shutdown, (int) );

View file

@ -45,7 +45,14 @@
PUBLIC char *t_stack[TOT_STACK_SPACE / sizeof(char *)]; PUBLIC char *t_stack[TOT_STACK_SPACE / sizeof(char *)];
/* Define flags for the various process types. */ /* Define flags for the various process types. */
#define IDL_F (SYS_PROC | PREEMPTIBLE | BILLABLE) /* idle task */ #define IDL_F (SYS_PROC | BILLABLE) /* idle task is not preemptible as we
* don't want it to interfere with the
* timer tick interrupt handler code.
* Unlike other processes idle task is
* handled in a special way and is
* preempted always if timer tick occurs
* and there is another runnable process
*/
#define TSK_F (SYS_PROC) /* kernel tasks */ #define TSK_F (SYS_PROC) /* kernel tasks */
#define SRV_F (SYS_PROC | PREEMPTIBLE) /* system services */ #define SRV_F (SYS_PROC | PREEMPTIBLE) /* system services */
#define VM_F (SYS_PROC) /* vm */ #define VM_F (SYS_PROC) /* vm */
@ -113,7 +120,7 @@ PRIVATE int
PUBLIC struct boot_image image[] = { PUBLIC struct boot_image image[] = {
/* process nr, pc,flags, qs, queue, stack, traps, ipcto, call, name */ /* process nr, pc,flags, qs, queue, stack, traps, ipcto, call, name */
{IDLE, idle_task,IDL_F, 8, IDLE_Q, IDL_S, 0, 0, no_c,"idle" }, {IDLE, NULL,IDL_F, 0, IDLE_Q, IDL_S, 0, 0, no_c,"idle" },
{CLOCK,clock_task,TSK_F, 8, TASK_Q, TSK_S, TSK_T, 0, no_c,"clock" }, {CLOCK,clock_task,TSK_F, 8, TASK_Q, TSK_S, TSK_T, 0, no_c,"clock" },
{SYSTEM, sys_task,TSK_F, 8, TASK_Q, TSK_S, TSK_T, 0, no_c,"system"}, {SYSTEM, sys_task,TSK_F, 8, TASK_Q, TSK_S, TSK_T, 0, no_c,"system"},
{HARDWARE, 0,TSK_F, 8, TASK_Q, HRD_S, 0, 0, no_c,"kernel"}, {HARDWARE, 0,TSK_F, 8, TASK_Q, HRD_S, 0, 0, no_c,"kernel"},