I post this message on behalf of a work colleague:
I created a design that used multiple threads and each thread needed a
timeout while waiting for h/w to respond using setTimer() and OnTimer().
I discovered the hard way that thread.cpp does not provide the
millisecond resolution that the documentation suggests as it uses
alarm() which has a resolution of 1 sec causing a timer set to < 1000ms
to be rounded to 0 secs and thus not set the timer at all. I modified
the code to use setittimer/getitimer (on RH 6.1 Linux) which provide
(notionally) a 1 microsecond resolution. I have only tested this code on
this target, however it appears to work providing sub-second timing
events.
387,394c387
< struct itimerval fine_time; // for finer time resolution
<
< memset(&fine_time, 0, sizeof fine_time);// clear timers
<
< // assume that timer will fit in microseconds field (~2000 seconds)
< fine_time.it_value.tv_usec = timer * 1000;
<
< // timer /= 1000;
---
> timer /= 1000;
403,404c396
< setitimer(ITIMER_REAL,&fine_time, NULL);// set hires timer
< // alarm(timer);
---
> alarm(timer);
410,411d401
< struct itimerval fine_time; // for finer time resolution
<
415,421c405,406
< // time(&now);
< // return (timeout_t)(((now - _alarm) * 1000) + 500);
<
< getitimer(ITIMER_REAL, &fine_time);// get the hires time
<
< // convert to millisecs
< return (timeout_t)(fine_time.it_value.tv_sec * 1000 + fine_time.it_value.tv_usec / 1000);
---
> time(&now);
> return (timeout_t)(((now - _alarm) * 1000) + 500);
427,428d411
< static const struct itimerval fine_time = {{0,0},{0,0}}; // for finer time resolution
<
434,435c417
< // alarm(0);
< setitimer(ITIMER_REAL, &fine_time, NULL);// set timer to 0 which stops it
---
> alarm(0);
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I post this message on behalf of a work colleague:
I created a design that used multiple threads and each thread needed a
timeout while waiting for h/w to respond using setTimer() and OnTimer().
I discovered the hard way that thread.cpp does not provide the
millisecond resolution that the documentation suggests as it uses
alarm() which has a resolution of 1 sec causing a timer set to < 1000ms
to be rounded to 0 secs and thus not set the timer at all. I modified
the code to use setittimer/getitimer (on RH 6.1 Linux) which provide
(notionally) a 1 microsecond resolution. I have only tested this code on
this target, however it appears to work providing sub-second timing
events.
387,394c387
< struct itimerval fine_time; // for finer time resolution
<
< memset(&fine_time, 0, sizeof fine_time);// clear timers
<
< // assume that timer will fit in microseconds field (~2000 seconds)
< fine_time.it_value.tv_usec = timer * 1000;
<
< // timer /= 1000;
---
> timer /= 1000;
403,404c396
< setitimer(ITIMER_REAL,&fine_time, NULL);// set hires timer
< // alarm(timer);
---
> alarm(timer);
410,411d401
< struct itimerval fine_time; // for finer time resolution
<
415,421c405,406
< // time(&now);
< // return (timeout_t)(((now - _alarm) * 1000) + 500);
<
< getitimer(ITIMER_REAL, &fine_time);// get the hires time
<
< // convert to millisecs
< return (timeout_t)(fine_time.it_value.tv_sec * 1000 + fine_time.it_value.tv_usec / 1000);
---
> time(&now);
> return (timeout_t)(((now - _alarm) * 1000) + 500);
427,428d411
< static const struct itimerval fine_time = {{0,0},{0,0}}; // for finer time resolution
<
434,435c417
< // alarm(0);
< setitimer(ITIMER_REAL, &fine_time, NULL);// set timer to 0 which stops it
---
> alarm(0);
I will see if I can add a autoconf test for setitimer and a clean alternate build with #ifdef's for those systems that might have this...