From 4b85b05e131fd487406ff2557a3fa720e841e9a0 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sun, 30 Jul 2023 10:13:26 +0200 Subject: [PATCH] Add rationale for HALF_MAX_MICROS --- cores/esp8266/Schedule.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index 4c92e37489..98e64d5701 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -49,6 +49,16 @@ static recurrent_fn_t* rLast = nullptr; // The target time for scheduling the next timed recurrent function static decltype(micros()) rTarget; +// As 32 bit unsigned integer, micros() rolls over every 71.6 minutes. +// For unambiguous earlier/later order between two timestamps, +// despite roll over, there is a limit on the maximum duration +// that can be requested, if full expiration must be observable: +// later - earlier >= 0 for both later >= earlier or (rolled over) later <= earlier +// Also, expiration should remain observable for a useful duration of time: +// now - (start + period) >= 0 for now - start >= 0 despite (start + period) >= now +// A well-balanced solution, not breaking on two's compliment signed arithmetic, +// is limiting durations to the maximum signed value of the same word size +// as the original unsigned word. constexpr decltype(micros()) HALF_MAX_MICROS = ~static_cast(0) >> 1; // Returns a pointer to an unused sched_fn_t,