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

}<br />

}<br />

return;<br />

fprintf (stderr, "Table full, message from rcvid %d ignored, "<br />

"client blocked\n", rcvid);<br />

break;<br />

// client <strong>with</strong> data<br />

case MT_SEND_DATA:<br />

// see if we can find another client to reply to <strong>with</strong><br />

// this client’s data<br />

for (i = 0; i < MAX_CLIENT; i++) {<br />

if (clients [i].in_use) {<br />

// found one -- reuse the incoming message<br />

// as an outgoing message<br />

msg -> messageType = MT_OK;<br />

// reply to BOTH CLIENTS!<br />

MsgReply (clients [i].rcvid, EOK, msg,<br />

sizeof (*msg));<br />

MsgReply (rcvid, EOK, msg, sizeof (*msg));<br />

}<br />

}<br />

clients [i].in_use = 0;<br />

return;<br />

}<br />

}<br />

fprintf (stderr, "Table empty, message from rcvid %d ignored, "<br />

"client blocked\n", rcvid);<br />

break;<br />

Notes<br />

Some general notes about the code:<br />

• If there’s no one waiting and a data message arrives, or there’s no room in the list<br />

for a new waiter client, we print a message to standard error, but never reply to the<br />

client. This means that some clients could be sitting there, REPLY-blocked forever<br />

— we’ve lost their receive ID, so we have no way to reply to them later.<br />

This is intentional in the design. You could modify this to add MT_NO_WAITERS<br />

and MT_NO_SPACE messages, respectively, which can be returned whenever these<br />

errors were detected.<br />

• When a waiter client is waiting, and a data-supplying client sends to it, we reply to<br />

both clients. This is crucial, because we want both clients to unblock.<br />

• We reused the data-supplying client’s buffer for both replies. This again is a style<br />

issue — in a larger application you’d probably have to have multiple types of return<br />

values, in which case you may not want to reuse the same buffer.<br />

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

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

Saved successfully!

Ooh no, something went wrong!