chThdSleepUntil() possible bug

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
Post Reply
lqv
Posts: 5
Joined: Mon May 12, 2014 10:14 am

chThdSleepUntil() possible bug

Post 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();
}

User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdSleepUntil() possible bug

Post 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
lqv
Posts: 5
Joined: Mon May 12, 2014 10:14 am

Re: chThdSleepUntil() possible bug

Post by lqv »

OK, it is my misunderstanding :oops: . Sorry to bother you!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chThdSleepUntil() possible bug

Post 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
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: chThdSleepUntil() possible bug

Post 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)
colin
Posts: 149
Joined: Thu Dec 22, 2011 7:44 pm

Re: chThdSleepUntil() possible bug

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

sdc_lld.c bug fixes

Post 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
Michail
Posts: 28
Joined: Mon Aug 04, 2014 6:55 am

Re: sdc_lld.c bug fixes

Post 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));
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: sdc_lld.c bug fixes

Post 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
Post Reply