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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using timers<br />

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

Signal notification<br />

Using timers<br />

Creating a timer<br />

Suppose that, on the other hand, you’re performing some kind of work, but don’t want<br />

that work to go on forever. For example, you may be waiting for some function call to<br />

return, but you can’t predict how long it takes.<br />

In this case, using a signal as the notification scheme, <strong>with</strong> perhaps a signal handler, is<br />

a good choice (another choice we’ll discuss later is to use kernel timeouts; see<br />

_NTO_CHF_UNBLOCK in the Message Passing chapter as well). In the “Using<br />

timers” section below, we’ll see a sample that uses signals.<br />

Alternatively, a signal <strong>with</strong> sigwait() is cheaper than creating a channel to receive a<br />

pulse on, if you’re not going to be receiving messages in your application anyway.<br />

Having looked at all this wonderful theory, let’s turn our attention to some specific<br />

code samples to see what you can do <strong>with</strong> timers.<br />

To work <strong>with</strong> a timer, you must:<br />

1 Create the timer object.<br />

2 Decide how you wish to be notified (signal, pulse, or thread creation), and create<br />

the notification structure (the struct sigevent).<br />

3 Decide what kind of timer you wish to have (relative versus absolute, and<br />

one-shot versus periodic).<br />

4 Start it.<br />

Let’s look at these in order.<br />

The first step is to create the timer <strong>with</strong> timer_create():<br />

#include <br />

#include <br />

int<br />

timer_create (clockid_t clock_id,<br />

struct sigevent *event,<br />

timer_t *timerid);<br />

The clock_id argument tells the timer_create() function which time base you’re<br />

creating this timer for. This is a POSIX thing — POSIX says that on different<br />

platforms you can have multiple time bases, but that every platform must support at<br />

least the CLOCK_REALTIME time base. Under <strong>Neutrino</strong>, there are three time bases to<br />

choose from:<br />

• CLOCK_REALTIME<br />

• CLOCK_SOFTTIME<br />

146 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!