
130 Chapter 9: Putting It All Together
int
Timer::waitfor()
{
if (state != Active)
{
return (-1);
}
//
// Wait for the timer to expire.
//
pMutex->take();
//
// Restart or idle the timer, depending on its type.
//
if (type == Periodic)
{
state = Active;
timerList.insert(this);
}
else
{
pMutex->release();
state = Idle;
}
return (0);
} /* waitfor() */
When the timer does eventually expire, the interrupt service routine will release
the mutex and the calling task will awake inside waitfor. In the process of wak-
ing, the mutex will already be taken for the next run of the timer. The mutex need
only be released if the timer is of type OneShot and, because of that, not automat-
ically restarted.
Printing “Hello, World!”
The other part of our example application is a task that prints the text string
“Hello, World!” to one of the serial ports at a regular interval. Again, the timer
driver is used to create the periodicity. However, this task also depends on a serial
port driver that we haven’t seen before. The guts of the serial driver will be
described in the final two sections of this chapter, but the task that uses it is
shown here. The only thing you need to know about serial ports to understand
this task is that a SerialPort is a C++ class and that the puts method is used to
print a string of characters from that port.
#include "timer.h"
#include "serial.h"