Skip to content

Commit

Permalink
ISR safety.
Browse files Browse the repository at this point in the history
  • Loading branch information
dok-net committed Jul 1, 2023
1 parent 03713a7 commit d87e242
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& 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<uint32_t>(rRemaining) > item->callNow.remaining()))
const int32_t remaining = rTarget - micros();
if (!rFirst || (remaining > 0 && static_cast<uint32_t>(remaining) > item->callNow.remaining()))
{
rTarget = micros() + item->callNow.remaining();
}

esp8266::InterruptLock lockAllInterruptsInThisScope;

if (rLast)
{
rLast->mNext = item;
Expand All @@ -145,8 +145,8 @@ uint32_t get_scheduled_recurrent_delay_us()
{
if (!rFirst) return ~static_cast<uint32_t>(0);
// handle already expired rTarget.
const int32_t rRemaining = rTarget - micros();
return (rRemaining > 0) ? static_cast<uint32_t>(rRemaining) : 0;
const int32_t remaining = rTarget - micros();
return (remaining > 0) ? static_cast<uint32_t>(remaining) : 0;
}

void run_scheduled_functions()
Expand Down Expand Up @@ -248,9 +248,12 @@ void run_scheduled_recurrent_functions()
{
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<uint32_t>(rRemaining) > current->callNow.remaining())
const int32_t remaining = rTarget - micros();
if (remaining > 0 && static_cast<uint32_t>(remaining) > current->callNow.remaining())
{
rTarget = micros() + current->callNow.remaining();
}
Expand Down

0 comments on commit d87e242

Please sign in to comment.