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 />

Timers delivering signals<br />

Timers creating threads<br />

So far, we’ve seen just about all there is to see <strong>with</strong> timers, except for one small thing.<br />

We’ve been delivering messages (via a pulse), but you can also deliver POSIX signals.<br />

Let’s see how this is done:<br />

timer_create (CLOCK_REALTIME, NULL, &timerid);<br />

This is the simplest way to create a timer that sends you a signal. This method raises<br />

SIGALRM when the timer fires. If we had actually supplied a struct sigevent,we<br />

could specify which signal we actually want to get:<br />

struct sigevent event;<br />

SIGEV_SIGNAL_INIT (&event, SIGUSR1);<br />

timer_create (CLOCK_REALTIME, &event, &timerid);<br />

This hits us <strong>with</strong> SIGUSR1 instead of SIGALRM.<br />

You catch timer signals <strong>with</strong> normal signal handlers, there’s nothing special about<br />

them.<br />

If you’d like to create a new thread every time a timer fires, then you can do so <strong>with</strong><br />

the struct sigevent and all the other timer stuff we just discussed:<br />

struct sigevent event;<br />

SIGEV_THREAD_INIT (&event, maintenance_func, NULL);<br />

You’ll want to be particularly careful <strong>with</strong> this one, because if you specify too short an<br />

interval, you’ll be flooded <strong>with</strong> new threads! This could eat up all your CPU and<br />

memory resources!<br />

<strong>Getting</strong> and setting the realtime clock and more<br />

Apart from using timers, you can also get and set the current realtime clock, and adjust<br />

it gradually. The following functions can be used for these purposes:<br />

Function Type? Description<br />

ClockAdjust() <strong>Neutrino</strong> Gradually adjust the time<br />

ClockCycles() <strong>Neutrino</strong> High-resolution snapshot<br />

clock_getres() POSIX Fetch the base timing resolution<br />

clock_gettime() POSIX Get the current time of day<br />

ClockPeriod() <strong>Neutrino</strong> Get or set the base timing resolution<br />

clock_settime() POSIX Set the current time of day<br />

ClockTime() <strong>Neutrino</strong> Get or set the current time of day<br />

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

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

Saved successfully!

Ooh no, something went wrong!