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.

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

Signal, pulse, or thread?<br />

What kind of timer?<br />

• CLOCK_MONOTONIC<br />

For now, we’ll ignore CLOCK_SOFTTIME and CLOCK_MONOTONIC but we will<br />

come back to them in the “Other clock sources” section, below.<br />

The second parameter is a pointer to a struct sigevent data structure. This data<br />

structure is used to inform the kernel about what kind of event the timer should deliver<br />

whenever it “fires.” We discussed how to fill in the struct sigevent above in the<br />

discussion of signals versus pulses versus thread creation.<br />

So, you’d call timer_create() <strong>with</strong> CLOCK_REALTIME and a pointer to your struct<br />

sigevent data structure, and the kernel would create a timer object for you (which<br />

gets returned in the last argument). This timer object is just a small integer that acts as<br />

an index into the kernel’s timer tables; think of it as a “handle.”<br />

At this point, nothing else is going to happen. You’ve only just created the timer; you<br />

haven’t triggered it yet.<br />

Having created the timer, you now have to decide what kind of timer it is. This is done<br />

by a combination of arguments to timer_settime(), the function used to actually start<br />

the timer:<br />

#include <br />

int<br />

timer_settime (timer_t timerid,<br />

int flags,<br />

struct itimerspec *value,<br />

struct itimerspec *oldvalue);<br />

The timerid argument is the value that you got back from the timer_create() function<br />

call — you can create a bunch of timers, and then call timer_settime() on them<br />

individually to set and start them at your convenience.<br />

The flags argument is where you specify absolute versus relative.<br />

If you pass the constant TIMER_ABSTIME, then it’s absolute, pretty much as you’d<br />

expect. You then pass the actual date and time when you want the timer to go off.<br />

If you pass a zero, then the timer is considered relative to the current time.<br />

Let’s look at how you specify the times. Here are key portions of two data structures<br />

(in ):<br />

struct timespec {<br />

long tv_sec,<br />

tv_nsec;<br />

};<br />

struct itimerspec {<br />

struct timespec it_value,<br />

it_interval;<br />

};<br />

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

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

Saved successfully!

Ooh no, something went wrong!