Download O`Reilly - Programming Embedded Systems in C and C++
Transcript
The scheduler in ADEOS is implemented in a class called Sched:
class Sched
{
public:
Sched();
void
void
start();
schedule();
void
void
enterIsr();
exitIsr();
static Task *
static TaskList
pRunningTask;
readyList;
enum SchedState { Uninitialized, Initialized, Started };
private:
static
static
static
static
SchedState
Task
int
int
state;
idleTask;
interruptLevel;
bSchedule;
};
After defining this class, an object of this type is instantiated within one of the operating system modules. That way,
users of ADEOS need only link the file sched.obj to include an instance of the scheduler. This instance is called os
and is declared as follows:
extern Sched os;
References to this global variable can be made from within any part of the application program. But you'll soon see
that only one such reference will be necessary per application.
8.2.2.1 Scheduling points
Simply stated, the scheduling points are the set of operating system events that result in an invocation of the
scheduler. We have already encountered two such events: task creation and task deletion. During each of these
events, the method os.schedule is called to select the next task to be run. If the currently executing task still has the
highest priority of all the ready tasks, it will be allowed to continue using the processor. Otherwise, the highest
priority ready task will be executed next. Of course, in the case of task deletion a new task is always selected: the
currently running task is no longer ready, by virtue of the fact that it no longer exists!
A third scheduling point is called the clock tick. The clock tick is a periodic event that is triggered by a timer
interrupt. The clock tick provides an opportunity to awake tasks that are waiting for a software timer to expire. This
is almost exactly the same as the timer tick we saw in the previous chapter. In fact, support for software timers is a
common feature of embedded operating systems. During the clock tick, the operating system decrements and checks
each of the active software timers. When a timer expires, all of the tasks that are waiting for it to complete are
changed from the waiting state to the ready state. Then the scheduler is invoked to see if one of these newly
awakened tasks has a higher priority than the task that was running prior to the timer interrupt.
The clock tick routine in ADEOS is almost exactly the same as the one in Chapter 7. In fact, we still use the same
Timer class. Only the implementation of this class has been changed, and that only slightly. These changes are
meant to account for the fact that multiple tasks might be waiting for the same software timer. In addition, all of the
calls to disable and enable have been replaced by enterCS and exitCS, and the length of a clock tick has been
increased from 1 ms to 10 ms.
8.2.2.2 Ready list
The scheduler uses a data structure called the ready list to track the tasks that are in the ready state. In ADEOS, the
ready list is implemented as an ordinary linked list, ordered by priority. So the head of this list is always the highest
priority task that is ready to run. Following a call to the scheduler, this will be the same as the currently running task.
In fact, the only time that won't be the case is during a reschedule. Figure 8-2 shows what the ready list might look
like while the operating system is in use.