Page 1 of 1
chTheSleepUntil revisited
Posted: Mon Nov 12, 2012 4:21 am
by iggarpe
I know this topic has been discussed but I still need some clarification. Sorry to bug you.
The issue is that if you call chThdSleepUntil passing a time which is in the past, the function will block seemingly forever. This is because chThdSleepUntil substracts chTimeNow() from the passed value, and since the result is treated as unsigned, a time two ticks in the past will result in a wait of 0xFFFFFFFE system ticks.
Now, I understand that chThdSleepUntil should not be used with times in the past. Period. But this is how I see it: even considering this an erroneous usage as a result of an anomalous program behaviour, there are two possible outcomes:
a) chThdSleepUntil blocks forever (in practice).
b) chThdSleepUntil returns immediately.
So, given a problematic situation in which we already missed the deadline, the (b) outcome is much better than (a) (which I consider catastrophic because makes any reasonable recovery IMPOSSIBLE). Since for normal usage there is no difference between both implementations, why just not do it in a way that will cause the less harm when the deadline is missed?.
Just curious.
Re: chTheSleepUntil revisited
Posted: Mon Nov 12, 2012 9:30 am
by Giovanni
The problem is that the whole range 1...0xFFFFFFFF are future instants so you can't possibly specify a past time.
The solution is to check the condition beforehand and then call chThdSleepS() like chThdSleepntil() does.
Giovanni
Re: chTheSleepUntil revisited
Posted: Mon Nov 12, 2012 1:47 pm
by iggarpe
The problem is that the whole range 1...0xFFFFFFFF are future instants so you can't possibly specify a past time.
Yeah, sure, but that's the internal calculation in chThdSleepUntil. The passed parameter is supposed to be an absolute instant in time, and thus it makes utter sense to pass an absolute instant in the past.
Anyway, that would be a semantic debate. IMO, it all boils down to the fact that:
a) I CANNOT think a situation in which passing an instant in the past and getting chThdSleepUntil blocked forever (in practice) is USEFUL.
b) I CAN think (and in fact have seen myself in) of quite a few situations in which passing an instant in the past and chThdSleepUntil returning immediately is either:
b.1) Lesser evil.
b.2) Useful (*)
Which can be summarized as: I can't see one reason to prefer chThdSleepUntil to behave as it does instead of as I propose (besides the purist semantic argument which is anyway questionable as I said at the beginning).
(*) I have a thread which I can afford to miss a deadline once in a while when some other higher priority task needs (very seldom) to run for an extended period, but it is important that the average period is correct. Sure, may not be a frequent scenario, but sure it's more frequent that an scenario where a total block in chThdSleepUntil is useful in ANY way.
Re: chTheSleepUntil revisited
Posted: Mon Nov 12, 2012 2:44 pm
by Giovanni
There are several reasons:
1) The systime_t type is not necessarily a 32bits wide type, in some port it is 16bits and it could be 8bits too in very small systems. Halving that limited usable range could be a bad idea.
2) Implementing the fix you proposed would require a type of the same type of systime_t but signed, casting to "int" is not portable unless (sizeof systime_t == sizeof int) which is not a given. Adding a new signed type would require a change in all ports.
3) The behavior of the function has been defined for a while, changing the behavior could break compatibility with existing code somewhere out there.
Giovanni
Re: chTheSleepUntil revisited
Posted: Mon Nov 12, 2012 3:16 pm
by jscott
iggarpe,
I understand you position. It does seem that it could be a problem in some cases.
Perhaps this is an opportunity for your to contribute a helper function to check if a supplied time has already passed. Then it could be used to skip the chThdSleepUntil call if it is.
This seems to be a case of needing to keep a odd behaving functions as-is because fixing it will "break many other things" / "cost too much for the benefit".
Thanks for pointing this out, Giovanni's explanation helped me understand why your solution would not work. Now the whole community is a little more clear on a seemingly dark corner of Chibios.
-John Scott
atl123.com
Re: chTheSleepUntil revisited
Posted: Mon Nov 12, 2012 3:20 pm
by Giovanni
Hi,
He already proposed two implementations:
https://sourceforge.net/tracker/?func=d ... tid=995489Both assume a 32bits type so that would not apply to all architectures, this is why I mentioned the need to have a specific signed type defined in the port layer.
I'd use this one:
Code: Select all
chSysLock();
if ((int32_t)(time -= chTimeNow()) > 0)
chThdSleepS(time);
chSysUnlock();
Giovanni
Re: chTheSleepUntil revisited
Posted: Mon Nov 12, 2012 3:24 pm
by Giovanni
BTW, can anybody think a way to check for negativity in an unsigned variable of arbitrary size? that would at least fix portability without adding more types.
Giovanni
Re: chTheSleepUntil revisited
Posted: Tue Nov 13, 2012 6:36 pm
by iggarpe
1) The systime_t type is not necessarily a 32bits wide type, in some port it is 16bits and it could be 8bits too in very small systems. Halving that limited usable range could be a bad idea.
Sorry, but IMHO a real time system with a 8 bit time counter does not make much sense. It imposes huge limitations either on the system tick speed or in the amount of time that can be specified in a timeout. Just to save a couple of cycles per reference to a system time variable.
The rationale can be extended for 16 bit systems vs 32 bit. I doubt the few cycles gained by using a 16 bit var versus a compiler emulated 32 bit is worth the trouble, most being executed most (if not all) the time in the entry of a function that is meant to sleep for a certain amount of time.
Trying to make everyone happy guarantees no one will be happy. You need to draw a line of minimum requirements somewhere, and honestly I think a 8 bit systime_t provides tiny gains in corner cases and comes with a boatload of implementation limitations, like the one we're discussing.
Again, just my opinion.
2) Implementing the fix you proposed would require a type of the same type of systime_t but signed, casting to "int" is not portable unless (sizeof systime_t == sizeof int) which is not a given. Adding a new signed type would require a change in all ports.
No. You just need to temporarly calculate the time interval as a signed variable INSIDE chThdSleepUntil:
Code: Select all
int32_t interval;
chSysLock();
interval = time - chTimeNow();
if (interval > 0)
chThdSleepS(interval);
chSysUnlock();
3) The behavior of the function has been defined for a while, changing the behavior could break compatibility with existing code somewhere out there.
Now that's something I agree is definitely a no-no. Let's see what would break;
a) Code that relies on chThdSleepUntil blocking forever when passed a time in the past.
SERIOUSLY ?. No, definitely this would not be a problem.
b) Code passing an absolute time more than 2^31 ticks in the future (for 32 bit systems).
I doubt anyone is using such huge delays. Seriously. And if he is, he would be using chThdSleep instead.
Now, I agree for 16 bit systems using a 16 bit systime_t the above might not be so unthinkable and maybe cause some trouble, and of course for 8 bit the issue is worse, but then again, I think an 8-bit systime_t makes no sense.
Rgds.
Re: chTheSleepUntil revisited
Posted: Tue Nov 13, 2012 7:10 pm
by Giovanni
iggarpe wrote:Sorry, but IMHO a real time system with a 8 bit time counter does not make much sense. It imposes huge limitations either on the system tick speed or in the amount of time that can be specified in a timeout. Just to save a couple of cycles per reference to a system time variable.
8bits are never actually used if not in some tests, the minimum is 16 but the problem does not change. Changing the range from 65 seconds down to 32 seconds can have impact.
iggarpe wrote:Trying to make everyone happy guarantees no one will be happy. You need to draw a line of minimum requirements somewhere, and honestly I think a 8 bit systime_t provides tiny gains in corner cases and comes with a boatload of implementation limitations, like the one we're discussing.
Exactly which boatload of limitations are you talking about? what we are talking about is a minor annoyance with an easy workaround and the fix would require changes in two dozens of ports.
iggarpe wrote:No. You just need to temporarly calculate the time interval as a signed variable INSIDE chThdSleepUntil:
This small thing can bring in 32bits arithmetic libraries in a system that does not use 32bits anywhere. So far I avoided using explicit 32bits types.
Considering the above points what would you suggest?
Giovanni