Timer with callback

It is not working for me… :frowning: .Any significance of scavenger_pulse_code? I
see that it is not declared here …and it is not declared in any of the
system header files.I see that all the return values are Success ,but i
don’t see that the timer function being called.

Sreekanth

“Xiaodan Tang” <xtang@qnx.com> wrote in message
news:amtlrd$lo$1@nntp.qnx.com

I would still suggest the “pulse way”. Here is the piece code
send to Sreekanth via email (note inside the io-net, the dpp
is already created and passed down…):


int callback(message_context_t *ctp, int code, unsigned flags, void hdl)
{
/
do timeout process */
return 0;
}

int main()
{
dispatch_t *dpp;
int coid;
struct sigevent ev;
timer_t callback_timer;

dpp = dispatch_create();
coid = message_connect(dpp, 0);
ev.sigev_code = scavenger_pulsecode = pulse_attach(dpp,
MSG_FLAG_ALLOC_PULSE, 0, callback, 0);
ev.sigev_notify = SIGEV_PULSE;
ev.sigev_value.sival_int = 0;
ev.sigev_coid = coid;
ev.sigev_priority = 1;
timer_create(CLOCK_REALTIME, &ev, &callback_timer);

/* you normal work */
}

-xtang



Chris McKillop <> cdm@qnx.com> > wrote:
If you are doing this within io-net you will probably need to use the
SIGEV_SIGNAL_THREAD() to ensure that the signal is sent to a specific
thread so that io-net’s signal thread doesn’t handle the signal behind
your back.

chris


Sreekanth <> nospam@nospam.com> > wrote:
I used the following.
alarm(x);
signal(SIGALRM,myfunc);
This does not seem to work.One more thing worth mentioning, It is not a
independent application.It is part of io-net process(A filter more
precisely).

Sreekanth

“Chris McKillop” <> cdm@qnx.com> > wrote in message
news:amqve3$j3s$> 1@nntp.qnx.com> …
Yes, that is what I told you to do. Setup a signal handler
(signal()),
setup your sigevent structure (SIGEV_SIGNAL()) and then create your
timer
(timer_create()).

chris

Sreekanth <> nospam@nospam.com> > wrote:
Probably I didn’t make it clear.I need that my function be called
once
the
timer hits.I don’t want to create another thread to wait for the
message(or
pulse).
I need something like this…
create_timer(TimerID,callback,…);
void callback(void *data) {

printf(“got the timer”);
return;
}
Now is this kind of API available?

Thanks
Sreekanth

“Xiaodan Tang” <> xtang@qnx.com> > wrote in message
news:amqqub$dmq$> 1@nntp.qnx.com> …
Chris McKillop <> cdm@qnx.com> > wrote:
The timer_*() API on QNX will do this for you. You want to setup
the
sigevent struct to fire a signal instead of a pulse. You use the
signal()
API to setup a handler and the SIGEV_SIGNAL() to setup the
sigevent
structure.

Or, of cause, you using pulse, and dispather, have pulse code
generated by pulse_attach().

Then when timeout, a pulse will send to you, and the function
pulse_attach() will be called.

-xtang

Sreekanth <> nospam@nospam.com> > wrote:
Is it possible in qnx to start a timer with a callback function
instead
of
message handler? i need something like
CreateTimer(timer_id,callback).Where
after the timer duration the function callback is called with an
argument.All i see now is to get the timer to send a message. I
know
something like that is possible in Windows.I didn’t see any API
in
documentation in QNX.

Any help is greatly appreciated.

Sreekanth





\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I
get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

\

No…I did set the timer.Now i am seeing that the message_connect is failing
with errno of ESRCH (May be i over looked it earlier).But message_connect
is not supposed to return this error.Here is my entire code

#include <signal.h>
#include <time.h>
#include <sys/dispatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int timer_func(message_context_t *ctp,int code,unsigned flags,void *hdl);
int timer_func(message_context_t *ctp,int code,unsigned flags,void *hdl) {
printf(“Timer Hit”);
return 0;
}
main() {
dispatch_t *dpp;
int coid;
struct sigevent ev;
timer_t timer_callb;
struct itimerspec itime;
dpp = dispatch_create();
if(dpp == NULL)
printf(“Dispatch create failed:%d\n”,errno);
coid = message_connect(dpp,0);
if(coid == -1)
printf(“Message Connect failed %d\n”,errno);
ev.sigev_code=pulse_attach(dpp,MSG_FLAG_ALLOC_PULSE,0,timer_func,0);
if(ev.sigev_code == -1)
perror(“Pulse attach failed”);
ev.sigev_notify=SIGEV_PULSE;
ev.sigev_value.sival_int=0;
ev.sigev_coid=coid;
ev.sigev_priority=1;
if(timer_create(CLOCK_REALTIME,&ev,&timer_callb) == -1)
perror(“Timer Create failed.”);
itime.it_value.tv_sec=2;
itime.it_value.tv_nsec=0;
itime.it_interval.tv_sec=2;
itime.it_interval.tv_nsec=2;
if(timer_settime(timer_callb,0,&itime,NULL) == -1) {
perror(“timer settime failed”);
return -1;
}
sleep(30);
}
“Rennie Allen” <rallen@csical.com> wrote in message
news:3D92EA11.2060901@csical.com

Sreekanth wrote:
It is not working for me… > :frowning: > .Any significance of
scavenger_pulse_code? I
see that it is not declared here …and it is not declared in any of the
system header files.I see that all the return values are Success ,but i
don’t see that the timer function being called.

Perhaps you are not setting the timer ? Xiaodans example creates the
timer, but does not set it.

Xiaodans’ example assumes that you know how to use timers in general,
and that you just need the specifics of creating a timer to deliver a
pulse.

Sreekanth

“Xiaodan Tang” <> xtang@qnx.com> > wrote in message
news:amtlrd$lo$> 1@nntp.qnx.com> …

I would still suggest the “pulse way”. Here is the piece code
send to Sreekanth via email (note inside the io-net, the dpp
is already created and passed down…):


int callback(message_context_t *ctp, int code, unsigned flags, void
hdl)
{
/
do timeout process */
return 0;
}

int main()
{
dispatch_t *dpp;
int coid;
struct sigevent ev;
timer_t callback_timer;

dpp = dispatch_create();
coid = message_connect(dpp, 0);
ev.sigev_code = scavenger_pulsecode = pulse_attach(dpp,

MSG_FLAG_ALLOC_PULSE, 0, callback, 0);

ev.sigev_notify = SIGEV_PULSE;
ev.sigev_value.sival_int = 0;
ev.sigev_coid = coid;
ev.sigev_priority = 1;
timer_create(CLOCK_REALTIME, &ev, &callback_timer);

/* you normal work */
}

Sreekanth <nospam@nospam.com> wrote:

No…I did set the timer.Now i am seeing that the message_connect is failing
with errno of ESRCH (May be i over looked it earlier).But message_connect
is not supposed to return this error.Here is my entire code

Sorry, if you create the dpp yourself, you need to resmgr_attach() it.
Otherwise, the dispatcher is not “activated”. Something like:

dpp = dispatch_create();
resmgr_attach(dpp, 0, 0, 0, 0, 0, 0, 0);
coid = message_connect(dpp, 0);

Again, if you are in the io-net module, you only need to
message_connect().

-xtang

#include <signal.h
#include <time.h
#include <sys/dispatch.h
#include <stdio.h
#include <stdlib.h
#include <errno.h
int timer_func(message_context_t *ctp,int code,unsigned flags,void *hdl);
int timer_func(message_context_t *ctp,int code,unsigned flags,void *hdl) {
printf(“Timer Hit”);
return 0;
}
main() {
dispatch_t *dpp;
int coid;
struct sigevent ev;
timer_t timer_callb;
struct itimerspec itime;
dpp = dispatch_create();
if(dpp == NULL)
printf(“Dispatch create failed:%d\n”,errno);
coid = message_connect(dpp,0);
if(coid == -1)
printf(“Message Connect failed %d\n”,errno);
ev.sigev_code=pulse_attach(dpp,MSG_FLAG_ALLOC_PULSE,0,timer_func,0);
if(ev.sigev_code == -1)
perror(“Pulse attach failed”);
ev.sigev_notify=SIGEV_PULSE;
ev.sigev_value.sival_int=0;
ev.sigev_coid=coid;
ev.sigev_priority=1;
if(timer_create(CLOCK_REALTIME,&ev,&timer_callb) == -1)
perror(“Timer Create failed.”);
itime.it_value.tv_sec=2;
itime.it_value.tv_nsec=0;
itime.it_interval.tv_sec=2;
itime.it_interval.tv_nsec=2;
if(timer_settime(timer_callb,0,&itime,NULL) == -1) {
perror(“timer settime failed”);
return -1;
}
sleep(30);
}
“Rennie Allen” <> rallen@csical.com> > wrote in message
news:> 3D92EA11.2060901@csical.com> …
Sreekanth wrote:
It is not working for me… > :frowning: > .Any significance of
scavenger_pulse_code? I
see that it is not declared here …and it is not declared in any of the
system header files.I see that all the return values are Success ,but i
don’t see that the timer function being called.

Perhaps you are not setting the timer ? Xiaodans example creates the
timer, but does not set it.

Xiaodans’ example assumes that you know how to use timers in general,
and that you just need the specifics of creating a timer to deliver a
pulse.

Sreekanth

“Xiaodan Tang” <> xtang@qnx.com> > wrote in message
news:amtlrd$lo$> 1@nntp.qnx.com> …

I would still suggest the “pulse way”. Here is the piece code
send to Sreekanth via email (note inside the io-net, the dpp
is already created and passed down…):


int callback(message_context_t *ctp, int code, unsigned flags, void
hdl)
{
/
do timeout process */
return 0;
}

int main()
{
dispatch_t *dpp;
int coid;
struct sigevent ev;
timer_t callback_timer;

dpp = dispatch_create();
coid = message_connect(dpp, 0);
ev.sigev_code = scavenger_pulsecode = pulse_attach(dpp,

MSG_FLAG_ALLOC_PULSE, 0, callback, 0);

ev.sigev_notify = SIGEV_PULSE;
ev.sigev_value.sival_int = 0;
ev.sigev_coid = coid;
ev.sigev_priority = 1;
timer_create(CLOCK_REALTIME, &ev, &callback_timer);

/* you normal work */
}

“Akhilesh Mritunjai” ~mritun~@REMOVETHISBIT.me.iitb.ac.in.ANDTHISLASTBIT wrote
in message news:amv91t$47u$1@inn.qnx.com

If one were trying to
be strictly POSIX-compliant, couldn’t you also call sem_post()
(which is async signal safe) from the signal handler to wake up a
thread?

Yes you can.

On the one hand, structuring it using sem_post() is more
portable, and portability is an important goal of the project; but,
is there an efficiency tradeoff?

Well, if you’re not using multiple users, using pthread_mutex_* and
pthread_cond_* will be more efficient.

Do you mean to say that it is okay to call pthread_cond_signal(), for
example, from a signal handler under QNX? I didn’t realize that. But
this function is not required by POSIX to be signal safe, so this
wouldn’t necessarily be portable to all POSIX-conforming systems.

From ‘Programming With POSIX Threads’ [Butenhof]:

“To awaken a thread from a POSIX signal-catching function, you need a
mechanism that’s reentrant with respect to POSIX signals (async-signal
safe). POSIX provides relatively few of these functions, and none
of the Pthreads functions is included.”

Use semaphores only when you need it.

Right. Butenhof goes on to say:

“If you’ve got Pthreads, you only need semaphores to handle this one
specialized function–waking a waiting thread from a signal-catching
function.”

So I guess I’m trying to figure out whether, portablility-wise, I am
better off with the sem_post approach. We want this code to compile
with as few changes as possible under IRIX and Linux, among others.
Someday. But, if QNX allows me to call pthread_cond_* and
friends from a signal_handler (and I see now from the documentation that
many of them are listed as signal safe)… maybe I’ll just use that
for now and try to leave myself some rope for a fallback implementation
that uses sem_post() on other OSes.

(Of course, the other possible choice is to have a thread wait
synchronously for the timer signals using sigwait(). But I’m not sure
our API will allow that, and this thread has been about asynchronous
callbacks, anyway…)

Sounds like I better figure out what the heck a ‘pulse’ is… > :wink:

A pulse is a QNX IPC primitive… just like messages. The differences
are:
snip

Thank you for the detailed description. I think I understand now.


HTH
Yes, quite a lot!

  • Dave

“Rennie Allen” <rallen@csical.com> wrote in message
news:3D92FF89.8070509@csical.com

David Wolfe wrote:

(Of course, the other possible choice is to have a thread wait
synchronously for the timer signals using sigwait(). But I’m not
sure our API will allow that, and this thread has been about
asynchronous callbacks, anyway…)

Sigh… I was hoping that most people would interpret this thread as
being about not using asynchronous callbacks.

Oh well, to each his own…

LOL. But, seriously, I guess that really should be the message. If
the API I have to code to permits it, that is what I’ll likely end up
doing…

  • Dave

“Rennie Allen” <rallen@csical.com> wrote in message
news:3D92FE66.4090506@csical.com

What follows is a working example.

Thanks a million for that complete, working program. It compiled with
no problems. I am dissecting it now to see how this ‘pulse’ stuff
works. :slight_smile:

  • Dave

Thanks everybody,especially xiaodan and chris.
I have been able to use the code to generate timer callbacks in my code

Sreekanth

“Xiaodan Tang” <xtang@qnx.com> wrote in message
news:amvknr$dte$1@nntp.qnx.com

Sreekanth <> nospam@nospam.com> > wrote:
No…I did set the timer.Now i am seeing that the message_connect is
failing
with errno of ESRCH (May be i over looked it earlier).But
message_connect
is not supposed to return this error.Here is my entire code

Sorry, if you create the dpp yourself, you need to resmgr_attach() it.
Otherwise, the dispatcher is not “activated”. Something like:

dpp = dispatch_create();
resmgr_attach(dpp, 0, 0, 0, 0, 0, 0, 0);
coid = message_connect(dpp, 0);

Again, if you are in the io-net module, you only need to
message_connect().

-xtang
#include <signal.h
#include <time.h
#include <sys/dispatch.h
#include <stdio.h
#include <stdlib.h
#include <errno.h
int timer_func(message_context_t *ctp,int code,unsigned flags,void
*hdl);
int timer_func(message_context_t *ctp,int code,unsigned flags,void *hdl)
{
printf(“Timer Hit”);
return 0;
}
main() {
dispatch_t *dpp;
int coid;
struct sigevent ev;
timer_t timer_callb;
struct itimerspec itime;
dpp = dispatch_create();
if(dpp == NULL)
printf(“Dispatch create failed:%d\n”,errno);
coid = message_connect(dpp,0);
if(coid == -1)
printf(“Message Connect failed %d\n”,errno);
ev.sigev_code=pulse_attach(dpp,MSG_FLAG_ALLOC_PULSE,0,timer_func,0);
if(ev.sigev_code == -1)
perror(“Pulse attach failed”);
ev.sigev_notify=SIGEV_PULSE;
ev.sigev_value.sival_int=0;
ev.sigev_coid=coid;
ev.sigev_priority=1;
if(timer_create(CLOCK_REALTIME,&ev,&timer_callb) == -1)
perror(“Timer Create failed.”);
itime.it_value.tv_sec=2;
itime.it_value.tv_nsec=0;
itime.it_interval.tv_sec=2;
itime.it_interval.tv_nsec=2;
if(timer_settime(timer_callb,0,&itime,NULL) == -1) {
perror(“timer settime failed”);
return -1;
}
sleep(30);
}
“Rennie Allen” <> rallen@csical.com> > wrote in message
news:> 3D92EA11.2060901@csical.com> …
Sreekanth wrote:
It is not working for me… > :frowning: > .Any significance of
scavenger_pulse_code? I
see that it is not declared here …and it is not declared in any of
the
system header files.I see that all the return values are Success ,but
i
don’t see that the timer function being called.

Perhaps you are not setting the timer ? Xiaodans example creates the
timer, but does not set it.

Xiaodans’ example assumes that you know how to use timers in general,
and that you just need the specifics of creating a timer to deliver a
pulse.

Sreekanth

“Xiaodan Tang” <> xtang@qnx.com> > wrote in message
news:amtlrd$lo$> 1@nntp.qnx.com> …

I would still suggest the “pulse way”. Here is the piece code
send to Sreekanth via email (note inside the io-net, the dpp
is already created and passed down…):


int callback(message_context_t *ctp, int code, unsigned flags, void
hdl)
{
/
do timeout process */
return 0;
}

int main()
{
dispatch_t *dpp;
int coid;
struct sigevent ev;
timer_t callback_timer;

dpp = dispatch_create();
coid = message_connect(dpp, 0);
ev.sigev_code = scavenger_pulsecode = pulse_attach(dpp,

MSG_FLAG_ALLOC_PULSE, 0, callback, 0);

ev.sigev_notify = SIGEV_PULSE;
ev.sigev_value.sival_int = 0;
ev.sigev_coid = coid;
ev.sigev_priority = 1;
timer_create(CLOCK_REALTIME, &ev, &callback_timer);

/* you normal work */
}
\