Skip to content

Commit 4b85b05

Browse files
committed
Add rationale for HALF_MAX_MICROS
1 parent 8518563 commit 4b85b05

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

cores/esp8266/Schedule.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ static recurrent_fn_t* rLast = nullptr;
4949
// The target time for scheduling the next timed recurrent function
5050
static decltype(micros()) rTarget;
5151

52+
// As 32 bit unsigned integer, micros() rolls over every 71.6 minutes.
53+
// For unambiguous earlier/later order between two timestamps,
54+
// despite roll over, there is a limit on the maximum duration
55+
// that can be requested, if full expiration must be observable:
56+
// later - earlier >= 0 for both later >= earlier or (rolled over) later <= earlier
57+
// Also, expiration should remain observable for a useful duration of time:
58+
// now - (start + period) >= 0 for now - start >= 0 despite (start + period) >= now
59+
// A well-balanced solution, not breaking on two's compliment signed arithmetic,
60+
// is limiting durations to the maximum signed value of the same word size
61+
// as the original unsigned word.
5262
constexpr decltype(micros()) HALF_MAX_MICROS = ~static_cast<decltype(micros())>(0) >> 1;
5363

5464
// Returns a pointer to an unused sched_fn_t,

0 commit comments

Comments
 (0)