-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Discussion on PolledTimeout #5964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
* polledTimeout: add option to use CPU count instead of millis() * use more "using" alias * more c++/clear code, using typename (thanks @devyte) * rename class name to include unit, introduce timeMax() and check it with assert() * remove useless defines * improve api readability, add micro-second unit * update example * mock: emulate getCycleCount, add/fix polledTimeout CI test * + nano-seconds, assert -> message, comments, host test * allow 0 for timeout (enables immediate timeout, fix division by 0) * typo, set member instead of local variable * unify error message * slight change on checkExpired() allows "never expired" also removed printed message, add YieldAndDelay, simplify calculations * remove traces of debug.h/cpp in this PR * include missing <limits> header * back to original expired test, introduce boolean _neverExpires, fix reset(), getTimeout() is invalid * fix expiredOneShot with _timeout==0 check * reenable getTimeout() * expose checkExpired with unit conversion * fix timing comments, move critical code to iram * add member ::neverExpires and use it where relevant * improve clarity * remove exposed checkExpired(), adapt LEAmDNS with equivalent * add API ::resetToNeverExpires(), use it in LEAmDNS * remove offending constness from ::flagged() LEAmDNS (due do API fix in PolledTimeout) * simplify "Fast" base classes * minor variable rename * Fix examples * compliance with good c++ manners * minor changes for consistency * add missing const * expired() and bool() moved to iram * constexpr compensation computing * add/update comments * move neverExpires and alwaysExpired
Sorry for being away for so long, migrating to a new phone was rather painful. First and foremost: all variables that hold time values are of type timeType, with the sole exception of "n", which is forcibly unsigned long. In the case of millis(), timeType is unsigned long, hence the same type. unsigned long n = (current - _start) / _timeout; The minus op is always >=0 , then the division is always >=0, and n is always >=0. The reason for the existence of "n" is to account for the case when polling skips over more than "timeout" time, in which case the _start has to be advanced a multiple of _timeout instead of just one _timeout. Notice that the calculation has zero drift in the time calculation, but allows for jitter in the poll moment, and also allows skipping multiples of timeout without winding. This is particularly important for periodic. With normal use (construct, then test), the calculation is always correct if the time elapsed between checks is less than timsSource::timeMax (in truth it's less strict than that, but timeMax is defined the way it is because Reasons). About this: current is of type timeType TBH, I don't remember why n is unsigned long. In theory, if timeType is constrained to unsigned types, then I guess n could also be of type timeType. About this: The same applies as above: About this: The entire code assumes that the time() function in the TimeSource policy class returns an unsigned type. That assumption isn't enforced anywhere, and if it is not met, then the calculation code has to be quite different. This is why I proposed to enforce always meeting the assumption with the above assert, to make sure that the code won't compile if anyone ever implements a new TimeSource::time() that returns a signed type. I'm closing this, because I don't see an issue, but I'll be happy to address further questions. |
@devyte are the proposed type-cast relevant for reader's proper understanding ? |
In general, casting should be done when there is a type change involved, to clarify in an explicit way what is being done, and i don't think that's the case here.
You're right! I had thought to just use a pr, but this is where the discussion is. Reopening. |
Following
Regarding 2:
I made some test with gcc and it seems OK.
However, I totally agree it is not clear enough.
To ensure we stay within (unsigned)
timeType
bounds (with implicit modulo over range), I would propose to changeto
Also, regarding 1)
to
This is supposed to be already implicit but it becomes clear and explicit this way, what do you think ?
and finally to add somewhere (a proposal from @devyte)
The text was updated successfully, but these errors were encountered: