Page 1 of 1

chThdSleepUntil() possible bug

Posted: Thu May 29, 2014 11:13 am
by lqv
I see the systime_t type is unsigned then the condition if((time -= chTimeNow()) > 0) is always true and the time value is very long time(in case time < chTimeNow()) . I think it may be fixed like this

Code: Select all

void chThdSleepUntil(systime_t time) {
  systime_t t;
  chSysLock();
  t=time-chTimeNow();
  if (time > chTimeNow())
    chThdSleepS(t);
  chSysUnlock();
}


Re: chThdSleepUntil() possible bug

Posted: Thu May 29, 2014 11:44 am
by Giovanni
Hi,

The implementation assumes that all the specifiable instants are in the future, does it make sense to wait until some instant in the past?

If you need a "sleep until" that is tolerant of deadline violations then this is something different, probably I could add that code to version 3.0 after finding a proper name for it.

Something like chThdWaitUntilButExitIfTheTimeIsInTheRecentPast() :)

Giovanni

Re: chThdSleepUntil() possible bug

Posted: Thu May 29, 2014 12:07 pm
by lqv
OK, it is my misunderstanding :oops: . Sorry to bother you!

Re: chThdSleepUntil() possible bug

Posted: Thu May 29, 2014 1:00 pm
by Giovanni
You are not bothering me, you same opinion has already been expressed several other times, there are at least 2 tickets on the tracker about this and I think the idea is basically good.

I think that an alternate API should be introduced and I am really looking for a meaningful name :)

A serious name could be chThdWaitAtLeastUntil() and your implementation would fit that.

Giovanni

Re: chThdSleepUntil() possible bug

Posted: Thu May 29, 2014 2:30 pm
by steved
Giovanni wrote:A serious name could be chThdWaitAtLeastUntil()
Giovanni

I could certainly use a call like this - I often have to implement something similar myself.

(A real-life application is when there must be a minimum pause between successive operations which may occur randomly)

Re: chThdSleepUntil() possible bug

Posted: Thu May 29, 2014 7:22 pm
by colin
And don't forget that the time point value specified to a chThdWaitAtLeastUntil(systime_t time_point) function must deal with wraparound of the time values. For instance if systime_t is a uint32_t, and it is implemented as milliseconds, then it will wrap around from 0xFFFF FFFF to zero approximately every 50 days. So you can't just do a greater-than comparison to determine if the specified time_point is in the past or not. You might have to add the requirement that time_point is within half the range of systime_t of the current time, so that you can divide the possible systime_t values into two domains of past and present values. Time wraparound is a tricky issue.

sdc_lld.c bug fixes

Posted: Wed Aug 13, 2014 3:32 pm
by Giovanni
Small update.

I added to 3.0 a new function called chThdSleeUntilWindowed() which fixes the problem with slight deadline misses.

Not tested yet but it is a whole 2 lines of code.

Giovanni

Re: sdc_lld.c bug fixes

Posted: Fri Aug 15, 2014 12:46 pm
by Michail
Giovanni,
Very useful function if I understand it correctly. Before this I've used sleepUntil from here.

Code: Select all

sleepUntil(&time_night, S2ST(Timeval_Current));

I am guessing how to use it. Is this right?:

Code: Select all

systime_t time_night = chVTGetSystemTime();
...
time_night = chThdSleepUntilWindowed(time_night,time_night + S2ST(Timeval_Current));
...
time_night = chThdSleepUntilWindowed(time_night,time_night + S2ST(Timeval_Current));

Re: sdc_lld.c bug fixes

Posted: Fri Aug 15, 2014 3:10 pm
by Giovanni
It looks correct, I haven't tested the function yet but it is based on existing functions. It should be like this:

Code: Select all

systime_t prev = chVTGetSystemTime();
while (1) {
  /* Do Something.*/
  prev = chThdSleepUntilWindowed(prev, prev + MS2ST(500));
}


Note that if "Do Something*/ exceeds the time slot then timings become unreliable, the function is just executed as fast as possible, the functionality becomes non deterministic.

Giovanni