diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index 0074570b93..ec1fd478ab 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -119,15 +119,15 @@ bool schedule_recurrent_function_us(const std::function& fn, item->mFunc = fn; item->alarm = alarm; + esp8266::InterruptLock lockAllInterruptsInThisScope; + // prevent new item overwriting an already expired rTarget. - const int32_t rRemaining = rTarget - micros(); - if (!rFirst || (rRemaining > 0 && static_cast(rRemaining) > item->callNow.remaining())) + const int32_t remaining = rTarget - micros(); + if (!rFirst || (remaining > 0 && static_cast(remaining) > item->callNow.remaining())) { rTarget = micros() + item->callNow.remaining(); } - esp8266::InterruptLock lockAllInterruptsInThisScope; - if (rLast) { rLast->mNext = item; @@ -145,8 +145,8 @@ uint32_t get_scheduled_recurrent_delay_us() { if (!rFirst) return ~static_cast(0); // handle already expired rTarget. - const int32_t rRemaining = rTarget - micros(); - return (rRemaining > 0) ? static_cast(rRemaining) : 0; + const int32_t remaining = rTarget - micros(); + return (remaining > 0) ? static_cast(remaining) : 0; } void run_scheduled_functions() @@ -209,12 +209,18 @@ void run_scheduled_recurrent_functions() fence = true; } - rTarget = micros() + current->callNow.remaining(); + decltype(rLast) stop; recurrent_fn_t* prev = nullptr; - // prevent scheduling of new functions during this run - auto stop = rLast; - bool done; + + { + esp8266::InterruptLock lockAllInterruptsInThisScope; + + // prevent scheduling of new functions during this run + stop = rLast; + rTarget = micros() + (~static_cast(0) >> 1); + } + do { done = current == stop; @@ -246,14 +252,16 @@ void run_scheduled_recurrent_functions() } else { - prev = current; - current = current->mNext; + esp8266::InterruptLock lockAllInterruptsInThisScope; + // prevent current item overwriting an already expired rTarget. - const int32_t rRemaining = rTarget - micros(); - if (rRemaining > 0 && static_cast(rRemaining) > current->callNow.remaining()) + const int32_t remaining = rTarget - micros(); + if (remaining > 0 && static_cast(remaining) > current->callNow.remaining()) { rTarget = micros() + current->callNow.remaining(); } + prev = current; + current = current->mNext; } if (yieldNow)