From 5ae08f4e84f91735b334f86b17ed28fcf67a57c2 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Thu, 20 Jul 2023 02:14:30 +0200 Subject: [PATCH] Worked out input from PR review. --- cores/esp8266/Schedule.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index 2652051a30..4a4a5d69e4 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -47,7 +47,8 @@ struct recurrent_fn_t static recurrent_fn_t* rFirst = nullptr; static recurrent_fn_t* rLast = nullptr; -static uint32_t rTarget; +// The target time for scheduling the next timed recurrent function +static decltype(micros()) rTarget; // Returns a pointer to an unused sched_fn_t, // or if none are available allocates a new one, @@ -122,10 +123,12 @@ bool schedule_recurrent_function_us(const std::function& fn, esp8266::InterruptLock lockAllInterruptsInThisScope; // prevent new item overwriting an already expired rTarget. - const int32_t remaining = rTarget - micros(); - if (!rFirst || (remaining > 0 && static_cast(remaining) > item->callNow.remaining())) + const auto now = micros(); + const auto itemRemaining = item->callNow.remaining(); + const int32_t remaining = rTarget - now; + if (!rFirst || (remaining > 0 && static_cast(remaining) > itemRemaining)) { - rTarget = micros() + item->callNow.remaining(); + rTarget = now + itemRemaining; } if (rLast) @@ -218,13 +221,9 @@ void run_scheduled_recurrent_functions() recurrent_fn_t* prev = nullptr; bool done; - { - esp8266::InterruptLock lockAllInterruptsInThisScope; - - // prevent scheduling of new functions during this run - stop = rLast; - rTarget = micros() + (~static_cast(0) >> 1); - } + // prevent scheduling of new functions during this run + stop = rLast; + rTarget = micros() + (~static_cast(0) >> 1); do { @@ -260,11 +259,14 @@ void run_scheduled_recurrent_functions() esp8266::InterruptLock lockAllInterruptsInThisScope; // prevent current item overwriting an already expired rTarget. - const int32_t remaining = rTarget - micros(); - if (remaining > 0 && static_cast(remaining) > current->callNow.remaining()) + const auto now = micros(); + const auto currentRemaining = current->callNow.remaining(); + const int32_t remaining = rTarget - now; + if (remaining > 0 && static_cast(remaining) > currentRemaining) { - rTarget = micros() + current->callNow.remaining(); + rTarget = now + currentRemaining; } + prev = current; current = current->mNext; }