
A Decent Embedded Operating System 111
Scheduler
Because I use an ordered linked list to maintain the ready list, the scheduler is
easy to implement. It simply checks to see if the running task and the highest-
priority ready task are one and the same. If they are, the scheduler’s job is done.
Otherwise, it will initiate a context switch from the former task to the latter. Here’s
what this looks like when it’s implemented in C++:
/**********************************************************************
*
* Method: schedule()
*
* Description: Select a new task to be run.
*
* Notes: If this routine is called from within an ISR, the
* schedule will be postponed until the nesting level
* returns to zero.
*
* The caller is responsible for disabling interrupts.
*
* Returns: None defined.
*
**********************************************************************/
void
Sched::schedule(void)
{
Task * pOldTask;
Task * pNewTask;
if (state != Started) return;
//
// Postpone rescheduling until all interrupts are completed.
//
if (interruptLevel != 0)
{
bSchedule = 1;
return;
}
//
// If there is a higher-priority ready task, switch to it.
//
if (pRunningTask != readyList.pTop)
{
pOldTask = pRunningTask;
pNewTask = readyList.pTop;
pNewTask->state = Running;
pRunningTask = pNewTask;
if (pOldTask == NULL)