Timer with callback

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

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.

chris


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/

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

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

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/

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/

I wrote this with the description provided by Chris in previous message


#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

void cb (int sig)
{
static int counter = 0;

printf(“counter %d\n”, ++counter);
}

int main ()
{
struct sigevent event;
timer_t timer_id;
struct itimerspec itime;

signal(SIGALRM, cb);
SIGEV_SIGNAL_INIT(&event, SIGALRM);
timer_create(CLOCK_REALTIME, &event, &timer_id);

itime.it_value.tv_sec = 1;
itime.it_value.tv_nsec = 0;
itime.it_interval.tv_sec = 1;
itime.it_interval.tv_nsec = 0;
timer_settime(timer_id, 0, &itime, NULL);

for (;:wink:
sleep(1000);

return 0;
}

Stepan Hejny wrote:

Chris neglected to mention, however, that since the “callback” is a
signal handler, it has all of the restrictions of a signal handler, and
in fact, even the trivial example you provide is illegal (since printf
isn’t signal safe).

The bottom line is: the proper way to execute a timer based function is
to set the timer to generate a pulse, and handle it as a normal dispatch
(as Xiaodan suggests). This way there are no restrictions on what calls
can be made.

I wrote this with the description provided by Chris in previous message


#include <stdio.h
#include <signal.h
#include <time.h
#include <unistd.h

void cb (int sig)
{
static int counter = 0;

printf(“counter %d\n”, ++counter);
}

int main ()
{
struct sigevent event;
timer_t timer_id;
struct itimerspec itime;

signal(SIGALRM, cb);
SIGEV_SIGNAL_INIT(&event, SIGALRM);
timer_create(CLOCK_REALTIME, &event, &timer_id);

itime.it_value.tv_sec = 1;
itime.it_value.tv_nsec = 0;
itime.it_interval.tv_sec = 1;
itime.it_interval.tv_nsec = 0;
timer_settime(timer_id, 0, &itime, NULL);

for (;:wink:
sleep(1000);

return 0;
}

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.

Perhaps the closest equivalent to what you’ve seen in Windows is
Photon’s RtTimerCreate().

Of course, RtTimerCreate() is no magic. It’s just a combination of
PtAppCreatePulse(), timer_create(), and PtAppAddInput(). You could
write a similar function yourself.


Wojtek Lerch QNX Software Systems Ltd.

Rennie Allen <rallen@csical.com> wrote:

Stepan Hejny wrote:

Chris neglected to mention, however, that since the “callback” is a
signal handler, it has all of the restrictions of a signal handler, and
in fact, even the trivial example you provide is illegal (since printf
isn’t signal safe).

Actually, since sleep() is signal-safe, the example is OK. It’s legal
to call signal-unsafe functions from a signal handler if you know that
the signal didn’t interrupt any signal-unsafe function.

\

Wojtek Lerch QNX Software Systems Ltd.

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

The bottom line is: the proper way to execute a timer based function
is to set the timer to generate a pulse, and handle it as a normal
dispatch (as Xiaodan suggests). This way there are no restrictions on
what calls can be made.

I’m new to QNX, so this piqued my curiousity. 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? I’m
just beginning a new project and trying to decide which approach to use.
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?

Is the native QNX way far simpler and/or more efficient? Or does QNX
just take the sem_post() method and distill it down to this
pulse/dispatch thing ‘under the covers’?

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

  • Dave

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/

\


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

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

This example is legal. Of course printf isn’t signal safe, it’s
documented as well as is documented what signal safe means …


Rennie Allen wrote:

Stepan Hejny wrote:

Chris neglected to mention, however, that since the “callback” is a
signal handler, it has all of the restrictions of a signal handler, and
in fact, even the trivial example you provide is illegal (since printf
isn’t signal safe).

The bottom line is: the proper way to execute a timer based function is
to set the timer to generate a pulse, and handle it as a normal dispatch
(as Xiaodan suggests). This way there are no restrictions on what calls
can be made.

I wrote this with the description provided by Chris in previous message


#include <stdio.h
#include <signal.h
#include <time.h
#include <unistd.h

void cb (int sig)
{
static int counter = 0;
printf(“counter %d\n”, ++counter);
}

int main ()
{
struct sigevent event;
timer_t timer_id;
struct itimerspec itime;

signal(SIGALRM, cb);
SIGEV_SIGNAL_INIT(&event, SIGALRM);
timer_create(CLOCK_REALTIME, &event, &timer_id);
itime.it_value.tv_sec = 1;
itime.it_value.tv_nsec = 0;
itime.it_interval.tv_sec = 1;
itime.it_interval.tv_nsec = 0;
timer_settime(timer_id, 0, &itime, NULL);

for (;:wink:
sleep(1000);

return 0;
}

Stepan Hejny wrote:

This example is legal. Of course printf isn’t signal safe, it’s
documented as well as is documented what signal safe means …

OK, so let’s say I accept that the handler is legal. If this is true,
then the main thread can’t legally call printf. Either the handler is
illegal or calling a standard library function from the main thread has
been made illegal. IMO using an idiom that makes calling a standard
library function from anywhere in your main thread illegal; is, if not
an illegal idiom, certainly an unethical one (since it is highly likely
to bite some unsuspecting innocent programmer maintaining your code in
the future).

The intention of my post was not to critique your code, but to discredit
the idea of using a signal handler as a general purpose timer callback.

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

I’m new to QNX, so this piqued my curiousity. 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? I’m
Yes you can.



just beginning a new project and trying to decide which approach to use.
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.
Use semaphores only when you need it.

Is the native QNX way far simpler and/or more efficient? Or does QNX
just take the sem_post() method and distill it down to this
pulse/dispatch thing ‘under the covers’?

No. sem_* functions are built upon some user space code and kernel calls
SyncCreate() etc.
You can actually see how they’re implemented by digging into libc.

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:

  1. There is no reply mechanism.
  2. They are nonblocking (owing to 1)
  3. Over QNET network, delivery of pulse is not guaranteed. Such situation
    arises when server process dies or connection breaks between making
    connection and sending pulse. QNET on local node has no idea whether the
    remote server died and since pulses are non-blocking, it has to reply
    immediately… so it returns success… however later QNET may fail to
    actually deliver the pulse to remote server (eg if server died or connection
    broke).
  4. A pulse carries only a very small payload (8 bit code and 32 bit data).


    HTH

Regards

  • Akhilesh Mritunjai

Sreekanth 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

Yikes ! There is a lot of code missing from your test program.

What follows is a working example.

Remember, if you are inside io-net most of this work is already done for
you, and Xiaodans’ paraphrased code is close to what you need.


#include <signal.h>
#include <time.h>
#include <sys/iofunc.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)
{
printf(“Timer Hit\n”);
return 0;
}

static resmgr_connect_funcs_t connect_funcs;
static resmgr_io_funcs_t io_funcs;
static iofunc_attr_t attr;

main(int argc, char **argv)
{
dispatch_t *dpp;
int coid;
struct sigevent ev;
timer_t timer_callb;
struct itimerspec itime;
resmgr_attr_t resmgr_attr;
dispatch_context_t *ctp;
int id;

/* initialize dispatch interface */
if ( (dpp = dispatch_create()) == NULL ) {
fprintf( stderr, “%s: Unable to allocate
dispatch handle.\n”, argv[0] );
return EXIT_FAILURE;
}

/* initialize resource manager attributes */
memset( &resmgr_attr, 0, sizeof resmgr_attr );
resmgr_attr.nparts_max = 1;
resmgr_attr.msg_max_size = 2048;

/* initialize functions for handling messages */
iofunc_func_init( _RESMGR_CONNECT_NFUNCS, &connect_funcs,
_RESMGR_IO_NFUNCS, &io_funcs );

/* initialize attribute structure */
iofunc_attr_init( &attr, S_IFNAM | 0666, 0, 0 );

if((id=resmgr_attach(dpp, &resmgr_attr, “/testpulse”,
_FTYPE_ANY, 0, &connect_funcs,
&io_funcs, &attr)) == -1 ) {
fprintf( stderr, “%s: Unable to attach name.\n”,
argv[0] );
return EXIT_FAILURE;
}

ev.sigev_coid=message_connect(dpp, 0);
if(ev.sigev_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_priority=32;
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=0;
if(timer_settime(timer_callb,0, &itime, NULL) == -1) {
perror(“timer settime failed”);
return -1;
}

ctp=dispatch_context_alloc(dpp);

while(1) {
if((ctp = dispatch_block(ctp)) == NULL) {
fprintf(stderr, “block error\n”);
return EXIT_FAILURE;
}
dispatch_handler(ctp);
}
}

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…

Let me try to help people understand why some things AREN’T signal safe.

Any function that has/uses static local data has the right to assume that
that data won’t get screwed with. If there are two instances of the same
function running concurrently in the same program, then they will probably
both try to initialize that same buffer of data. I.E. the data got screwed
with. So this function isn’t signal safe, thread safe, or ISR safe.

Also any function that calls another function that isn’t signal safe, is
itself also not signal safe.

There may be additional reasons why a function isn’t signal safe. But I
know I’ve been bitten in the butt by this.


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

Stepan Hejny wrote:
This example is legal. Of course printf isn’t signal safe, it’s
documented as well as is documented what signal safe means …

OK, so let’s say I accept that the handler is legal. If this is true,
then the main thread can’t legally call printf. Either the handler is
illegal or calling a standard library function from the main thread has
been made illegal. IMO using an idiom that makes calling a standard
library function from anywhere in your main thread illegal; is, if not
an illegal idiom, certainly an unethical one (since it is highly likely
to bite some unsuspecting innocent programmer maintaining your code in
the future).

The intention of my post was not to critique your code, but to discredit
the idea of using a signal handler as a general purpose timer callback.