How do you setup a pulse handler using pulse_attach?

I have been programming for several years in QNX4 (Way to long!) and have
started programming in RTP. In QNX 4 we use proxies with a receive()
function to determine which proxy needs to run.

In RTP we can also use something like in QNX4 as follows…


chid = ChannelCreate(0);

setupPulseandTimer(); // the usual set-ups to do this for one second pulse
rate

for (;:wink: {
rcvid = MsgReceive (chid, &msg, sizeof (msg), NULL);

// determine who the message came from
if (rcvid == 0)
{
// production code should check “code” field…
gotAPulse ();
} else {
gotAMessage (rcvid, &msg.msg);
}
}

// you’ll never get here
return (EXIT_SUCCESS);
}

I have tried this and it works fine but, In Rob Krtens Book “QNX Neutrino 2”
page 394 and 395 states A better way to do this using a pulse handler as
follows…


dpp = dispatch_create();
pulse_attach(dpp,0,MyPulseCode,gotAPulse,NULL);

//
// MyPulseCode is defined as ?
// gotAPulse is pulse handle defines as it should be
//


This would be a much better way to let the OS call the handler function to
handle each pulse or message etc…

Has anyone got this working in the RTP? and if so How?


Greg Orvets
VP Engineering
Lambda Tech International
www.lambdatech.com

Greg Orvets <orvets@lambdatech.com> wrote:

I have been programming for several years in QNX4 (Way to long!) and have
started programming in RTP. In QNX 4 we use proxies with a receive()
function to determine which proxy needs to run.

In RTP we can also use something like in QNX4 as follows…


chid = ChannelCreate(0);

setupPulseandTimer(); // the usual set-ups to do this for one second pulse
rate

for (;:wink: {
rcvid = MsgReceive (chid, &msg, sizeof (msg), NULL);

// determine who the message came from
if (rcvid == 0)
{
// production code should check “code” field…
gotAPulse ();
} else {
gotAMessage (rcvid, &msg.msg);
}
}

// you’ll never get here
return (EXIT_SUCCESS);
}

I have tried this and it works fine but, In Rob Krtens Book “QNX Neutrino 2”
page 394 and 395 states A better way to do this using a pulse handler as
follows…


dpp = dispatch_create();
pulse_attach(dpp,0,MyPulseCode,gotAPulse,NULL);

You actually want to:

struct sigevent ev;

ev.coid = message_connect(dpp, 0);
ev.sigev_code = pulse_attach(dpp, MSG_FLAG_ALLOC_PULSE, 0, gotAPulse, 0);
ev.sigev_notify = SIGEV_PULSE;
ev.sigev_priority = SIGEV_PULSE_PRIO_INHERIT;

timer_create(CLOCK_REALTIME, &ev, &timerid);

Make sure you check all the return value is correct :slight_smile:

-xtang

//
// MyPulseCode is defined as ?
// gotAPulse is pulse handle defines as it should be
//


This would be a much better way to let the OS call the handler function to
handle each pulse or message etc…

Has anyone got this working in the RTP? and if so How?


Greg Orvets
VP Engineering
Lambda Tech International
www.lambdatech.com

“Greg Orvets” <orvets@lambdatech.com> wrote in message
news:9ifgm7$4bj$1@inn.qnx.com

I have been programming for several years in QNX4 (Way to long!) and have
started programming in RTP. In QNX 4 we use proxies with a receive()
function to determine which proxy needs to run.

page 394 and 395 states A better way to do this using a pulse handler as
follows…


dpp = dispatch_create();
pulse_attach(dpp,0,MyPulseCode,gotAPulse,NULL);

//
// MyPulseCode is defined as ?
// gotAPulse is pulse handle defines as it should be
//


This would be a much better way to let the OS call the handler function to
handle each pulse or message etc…

Has anyone got this working in the RTP?

Sure!

and if so How?

Your main loop must look like:

dispatch_context_t *ctp, *new_ctp;

ctp = dispatch_context_alloc (dpp);

while (1) {


if ( (new_ctp = dispatch_block ( ctp) ) != NULL) {

ctp = new_ctp;



if ( dispatch_handler (ctp) == -1 ) {

dprintf( 3, “Unknow message\n”);

}

}


The idea is that the pulse_attach is part of a framework call “dispatch”.




Greg Orvets
VP Engineering
Lambda Tech International
www.lambdatech.com

Greg Orvets <orvets@lambdatech.com> wrote:

I have been programming for several years in QNX4 (Way to long!) and have
started programming in RTP. In QNX 4 we use proxies with a receive()
function to determine which proxy needs to run.

In RTP we can also use something like in QNX4 as follows…



for (;:wink: {
rcvid = MsgReceive (chid, &msg, sizeof (msg), NULL);
if (rcvid == 0)
{
gotAPulse ();
} else {
gotAMessage (rcvid, &msg.msg);
}
}

I have tried this and it works fine but, In Rob Krtens Book “QNX Neutrino 2”
page 394 and 395 states A better way to do this using a pulse handler as
follows…


dpp = dispatch_create();
pulse_attach(dpp,0,MyPulseCode,gotAPulse,NULL);

Rob’s code is the equivalent of doing proxy handling in a QNX4 I/O manager.

It is using the dispatch (resource manager) framework to handle this.

Neither way is neccessarily more/less correct than the other – they
are just two different choices.

If porting QNX4 code, you may find it easier to not re-architect it to
fit in the resource manager (dispatch) framework.

-David

QNX Training Services
dagibbs@qnx.com