26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using timers<br />

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG.<br />

There are two members in struct itimerspec:<br />

it_value<br />

it_interval<br />

the one-shot value<br />

the reload value<br />

The it_value specifies either how long from now the timer should go off (in the case of<br />

a relative timer), or when the timer should go off (in the case of an absolute timer).<br />

Once the timer fires, the it_interval value specifies a relative value to reload the timer<br />

<strong>with</strong> so that it can trigger again. Note that specifying a value of zero for the it_interval<br />

makes it into a one-shot timer. You might expect that to create a “pure” periodic timer,<br />

you’d just set the it_interval to the reload value, and set it_value to zero.<br />

Unfortunately, the last part of that statement is false — setting the it_value to zero<br />

disables the timer. If you want to create a pure periodic timer, set it_value equal to<br />

it_interval and create the timer as a relative timer. This will fire once (for the it_value<br />

delay) and then keep reloading <strong>with</strong> the it_interval delay.<br />

Both the it_value and it_interval members are actually structures of type struct<br />

timespec, another POSIX thing. The structure lets you specify sub-second<br />

resolutions. The first member, tv_sec, is the number of seconds; the second member,<br />

tv_nsec, is the number of nanoseconds in the current second. (What this means is that<br />

you should never set tv_nsec past the value 1 billion — this would imply more than a<br />

one-second offset.)<br />

Here are some examples:<br />

it_value.tv_sec = 5;<br />

it_value.tv_nsec = 500000000;<br />

it_interval.tv_sec = 0;<br />

it_interval.tv_nsec = 0;<br />

This creates a one-shot timer that goes off in 5.5 seconds. (We got the “.5” because of<br />

the 500,000,000 nanoseconds value.)<br />

We’re assuming that this is used as a relative timer, because if it weren’t, then that time<br />

would have elapsed long ago (5.5 seconds past January 1, 1970, 00:00 GMT).<br />

Here’s another example:<br />

it_value.tv_sec = 987654321;<br />

it_value.tv_nsec = 0;<br />

it_interval.tv_sec = 0;<br />

it_interval.tv_nsec = 0;<br />

This creates a one-shot timer that goes off Thursday, April 19, 2001 at 00:25:21 EDT.<br />

(There are a bunch of functions that help you convert between the human-readable date<br />

and the “number of seconds since January 1, 1970, 00:00:00 GMT” representation.<br />

Take a look in the C library at time(), asctime(), ctime(), mktime(), strftime(), etc.)<br />

For this example, we’re assuming that it’s an absolute timer, because of the huge<br />

number of seconds that we’d be waiting if it were relative (987654321 seconds is<br />

about 31.3 years).<br />

148 Chapter 3 • Clocks, Timers, and <strong>Getting</strong> a Kick Every So Often April 30, 2009

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!