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.

Pulses<br />

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

The MsgReceivePulse() function<br />

The MsgReceive() and MsgReceivev() functions will receive either a “regular”<br />

message or a pulse. There may be situations where you want to receive only pulses.<br />

The best example of this is in a server where you’ve received a request from a client to<br />

do something, but can’t complete the request just yet (perhaps you have to do a long<br />

hardware operation). In such a design, you’d generally set up the hardware (or a timer,<br />

or whatever) to send you a pulse whenever a significant event occurs.<br />

If you write your server using the classic “wait in an infinite loop for messages”<br />

design, you might run into a situation where one client sends you a request, and then,<br />

while you’re waiting for the pulse to come in (to signal completion of the request),<br />

another client sends you another request. Generally, this is exactly what you want —<br />

after all, you want to be able to service multiple clients at the same time. However,<br />

there might be good reasons why this is not acceptable — servicing a client might be<br />

so resource-intensive that you want to limit the number of clients.<br />

In that case, you now need to be able to “selectively” receive only a pulse, and not a<br />

regular message. This is where MsgReceivePulse() comes into play:<br />

#include <br />

int MsgReceivePulse (int chid,<br />

void *rmsg,<br />

int rbytes,<br />

struct _msg_info *info);<br />

As you can see, you use the same parameters as MsgReceive(); the channel ID, the<br />

buffer (and its size), as well as the info parameter. (We discussed the info parameter<br />

above, in “Who sent the message?”.) Note that the info parameter is not used in the<br />

case of a pulse; you might ask why it’s present in the parameter list. Simple answer: it<br />

was easier to do it that way in the implementation. Just pass a NULL!<br />

The MsgReceivePulse() function will receive nothing but pulses. So, if you had a<br />

channel <strong>with</strong> a number of threads blocked on it via MsgReceivePulse(), (and no<br />

threads blocked on it via MsgReceive()), and a client attempted to send your server a<br />

message, the client would remain SEND-blocked until a thread issued the<br />

MsgReceive() call. Pulses would be transferred via the MsgReceivePulse() functions in<br />

the meantime.<br />

The only thing you can guarantee if you mix both MsgReceivePulse() and<br />

MsgReceive() is that the MsgReceivePulse() will get pulses only. The MsgReceive()<br />

could get pulses or messages! This is because, generally, the use of the<br />

MsgReceivePulse() function is reserved for the cases where you want to exclude<br />

regular message delivery to the server.<br />

This does introduce a bit of confusion. Since the MsgReceive() function can receive<br />

both a message and a pulse, but the MsgReceivePulse() function can receive only a<br />

pulse, how do you deal <strong>with</strong> a server that makes use of both functions? Generally, the<br />

answer here is that you’d have a pool of threads that are performing MsgReceive().<br />

This pool of threads (one or more threads; the number depends on how many clients<br />

you’re prepared to service concurrently) is responsible for handling client calls<br />

116 Chapter 2 • Message Passing April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!