Skip to content

Fix for scheduling recurrent functions while inside scheduled function #6307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct recurrent_fn_t
recurrent_fn_t (esp8266::polledTimeout::periodicFastUs interval): callNow(interval) { }
};

static recurrent_fn_t* rFirst = nullptr; // fifo not needed
static recurrent_fn_t rFirst(0); // fifo not needed

// Returns a pointer to an unused sched_fn_t,
// or if none are available allocates a new one,
Expand Down Expand Up @@ -62,6 +62,9 @@ static void recycle_fn_unsafe (scheduled_fn_t* fn)
IRAM_ATTR // (not only) called from ISR
bool schedule_function (const std::function<void(void)>& fn)
{
if (!fn)
return false;

esp8266::InterruptLock lockAllInterruptsInThisScope;

scheduled_fn_t* item = get_fn_unsafe();
Expand All @@ -84,6 +87,9 @@ bool schedule_recurrent_function_us (const std::function<bool(void)>& fn, uint32
{
assert(repeat_us < decltype(recurrent_fn_t::callNow)::neverExpires); //~26800000us (26.8s)

if (!fn)
return false;

esp8266::InterruptLock lockAllInterruptsInThisScope;

recurrent_fn_t* item = new (std::nothrow) recurrent_fn_t(repeat_us);
Expand All @@ -92,10 +98,8 @@ bool schedule_recurrent_function_us (const std::function<bool(void)>& fn, uint32

item->mFunc = fn;

if (rFirst)
item->mNext = rFirst;

rFirst = item;
item->mNext = rFirst.mNext;
rFirst.mNext = item;

return true;
}
Expand Down Expand Up @@ -136,7 +140,8 @@ void run_scheduled_recurrent_functions ()
// its purpose is that it is never called from an interrupt
// (always on cont stack).

if (!rFirst)
recurrent_fn_t* current = rFirst.mNext;
if (!current)
return;

static bool fence = false;
Expand All @@ -152,10 +157,9 @@ void run_scheduled_recurrent_functions ()
fence = true;
}

recurrent_fn_t* prev = nullptr;
recurrent_fn_t* current = rFirst;
recurrent_fn_t* prev = &rFirst;

while (current)
do
{
if (current->callNow && !current->mFunc())
{
Expand All @@ -164,16 +168,11 @@ void run_scheduled_recurrent_functions ()

auto to_ditch = current;

if (prev)
{
current = current->mNext;
prev->mNext = current;
}
else
{
rFirst = rFirst->mNext;
current = rFirst;
}
// current function recursively scheduled something
if (prev->mNext != current) prev = prev->mNext;

current = current->mNext;
prev->mNext = current;

delete(to_ditch);
}
Expand All @@ -183,6 +182,7 @@ void run_scheduled_recurrent_functions ()
current = current->mNext;
}
}
while (current);

fence = false;
}