From 5cfc4bb57ee3472293f6193c78abb7a96fbaec56 Mon Sep 17 00:00:00 2001 From: Florian Fleissner Date: Sat, 15 Jun 2019 10:13:54 +0200 Subject: [PATCH 1/2] Added a warning against potential pitfalls with hasTimeExpired Signed-off-by: Florian Fleissner --- src/kaleidoscope/Kaleidoscope.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/kaleidoscope/Kaleidoscope.h b/src/kaleidoscope/Kaleidoscope.h index 135b8e7c..9c906873 100644 --- a/src/kaleidoscope/Kaleidoscope.h +++ b/src/kaleidoscope/Kaleidoscope.h @@ -154,6 +154,17 @@ class Kaleidoscope_ { * timer expires (and `hasTimeExpired()` returns true) when the time that * has elapsed since `start_time` exceeds this value. It must be an * integer type that is no bigger than the type of `start_time`. + * + * Warning: When using hasTimeExpired, make sure that the value of ttl + * is at least by the size of the call interval smaller than + * the maximum value that the datatype of timeout can store. + * Otherwise false negatives are likely to occur + * when checking (elapsed_time > ttl). + * + * Example: When both arguments of hasTimeExpired are of type uint16_t and + * the function is called in every cycle with a cycle duration + * of x ms, the value of ttl must not be larger than + * std::numeric_limits::max() - x. */ template bool hasTimeExpired(_Timestamp start_time, _Timeout ttl) { From 2cf6883a16370c8b02024b1972323e37323f23a3 Mon Sep 17 00:00:00 2001 From: Florian Fleissner Date: Sat, 15 Jun 2019 10:15:08 +0200 Subject: [PATCH 2/2] Turned Kaleidoscope::hasTimeExpired static The method does not access any non-static class inventory. This change should bring a small performance gain as not this pointer needs to be passed when the function is called. Signed-off-by: Florian Fleissner --- src/kaleidoscope/Kaleidoscope.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kaleidoscope/Kaleidoscope.h b/src/kaleidoscope/Kaleidoscope.h index 9c906873..4217aac7 100644 --- a/src/kaleidoscope/Kaleidoscope.h +++ b/src/kaleidoscope/Kaleidoscope.h @@ -167,7 +167,7 @@ class Kaleidoscope_ { * std::numeric_limits::max() - x. */ template - bool hasTimeExpired(_Timestamp start_time, _Timeout ttl) { + static bool hasTimeExpired(_Timestamp start_time, _Timeout ttl) { _Timestamp current_time = millis_at_cycle_start_; _Timestamp elapsed_time = current_time - start_time; return (elapsed_time > ttl);