Timing

Some operating systems use a periodic timer bound to a tick counter to manage time smoothly throughout the system. The didactic version of OpenEPOS you are currently working with, follows this strategy:

1.
2.
3.
4.
5.
void Alarm::timer_handler(void)
{
    _elapsed++;
    ...
}

To implement the timed event concept, EPOS-- uses a event queue that is handled by the timer's interrupt handling routine as follows:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
  void Alarm::timer_handler(void)
{
        static Tick next;
        static Handler handler;

        _elapsed++;

        if(next)
                next--;
        if(!next) {
                if(handler)
                        handler();
                if(_requests.empty())
                        handler = 0;
                else {
                        Queue::Element * e = _requests.remove();
                        Alarm * alarm = e->object();
                        next = alarm->_ticks;
                        handler = alarm->_handler;
                        if(alarm->_times != -1)
                                alarm->_times--;
                        if(alarm->_times) {
                                e->rank(alarm->_ticks);
                                _requests.insert(e);
                        }
                }
        }
}

In order to avoid searching the requests queue on every time tick, the queue is kept ordered and its elements are ranked relatively to each other (i.e. the rank of each element is an offset to the previous).

The main issue in this implementation is that the handler indicated by the user to be invoked when the given time arrives is executed within the context of the interrupt handling routing, thus compromising the system timin.

To do

You are requested to modify the implementation of Alarm to eliminate the busy-waiting in method delay() and also to remodel the interrupt handling routine so that user's handler functions no longer have a chance to disrupt the system timing.