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.

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

Kernel timeouts<br />

<strong>Neutrino</strong> lets you have a timeout associated <strong>with</strong> all kernel blocking states. We talked<br />

about the blocking states in the Processes and Threads chapter, in the section “Kernel<br />

states.” Most often, you’ll want to use this <strong>with</strong> message passing; a client will send a<br />

message to a server, but the client won’t want to wait “forever” for the server to<br />

respond. In that case, a kernel timeout is suitable. Kernel timeouts are also useful <strong>with</strong><br />

the pthread_join() function. You might want to wait for a thread to finish, but you<br />

might not want to wait too long.<br />

Here’s the definition for the TimerTimeout() function call, which is the kernel function<br />

responsible for kernel timeouts:<br />

#include <br />

int<br />

TimerTimeout (clockid_t id,<br />

int flags,<br />

const struct sigevent *notify,<br />

const uint64_t *ntime,<br />

uint64_t *otime);<br />

This says that TimerTimeout() returns an integer (a pass/fail indication, <strong>with</strong> -1<br />

meaning the call failed and set errno, and zero indicating success). The time source<br />

(CLOCK_REALTIME, etc.) is passed in id, and the flags parameter gives the relevant<br />

kernel state or states. The notify should always be a notification event of type<br />

SIGEV_UNBLOCK, and the ntime is the relative time when the kernel call should<br />

timeout. The otime parameter indicates the previous value of the timeout — it’s not<br />

used in the vast majority of cases (you can pass NULL).<br />

Kernel timeouts <strong>with</strong> pthread_join()<br />

It’s important to note that the timeout is armed by TimerTimeout(), and triggered on<br />

entry into one of the kernel states specified by flags. Itiscleared upon return from any<br />

kernel call. This means that you must re-arm the timeout before each and every kernel<br />

call that you want to be timeout-aware. You don’t have to clear the timeout after the<br />

kernel call; this is done automagically.<br />

The simplest case to consider is a kernel timeout used <strong>with</strong> the pthread_join() call.<br />

Here’s how you’d set it up:<br />

/*<br />

* part of tt1.c<br />

*/<br />

#include <br />

// 1 billion nanoseconds in a second<br />

#define SEC_NSEC 1000000000LL<br />

int<br />

main (void) // ignore arguments<br />

{<br />

uint64_t timeout;<br />

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

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

Saved successfully!

Ooh no, something went wrong!