Skip to content

Commit 3458547

Browse files
committed
Adapt the scheduled recurrent functions' invoke to altered MultiDelegate non-queue (event multiplexer) mode.
1 parent 0ac3b81 commit 3458547

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

cores/esp8266/Schedule.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "coredecls.h"
2626

2727
using mSchedFuncT = Delegate<void(), void*>;
28+
// queue specialization: cache erased nodes, call each once next round.
2829
MultiDelegate<mSchedFuncT, true> schedFuncs;
2930

3031
class mRecFuncT : public Delegate<bool(), void*>
@@ -36,13 +37,15 @@ class mRecFuncT : public Delegate<bool(), void*>
3637
using base_type::operator=;
3738
esp8266::polledTimeout::periodicFastUs callNow;
3839
Delegate<bool(), void*> alarm = nullptr;
40+
// return true to keep, false to erase.
3941
bool IRAM_ATTR operator()()
4042
{
4143
const bool wakeup = alarm && alarm();
4244
bool callNow = this->callNow;
4345
return !(wakeup || callNow) || base_type::operator()();
4446
}
4547
};
48+
// non-queue specialization: heap new/delete, use explicit erase.
4649
MultiDelegate<mRecFuncT> recFuncs;
4750

4851
IRAM_ATTR // (not only) called from ISR
@@ -74,5 +77,25 @@ void run_scheduled_functions()
7477

7578
void run_scheduled_recurrent_functions()
7679
{
77-
recFuncs();
80+
auto it = recFuncs.begin();
81+
if (!it)
82+
return;
83+
84+
static std::atomic<bool> fence(false);
85+
// prevent recursive calls
86+
if (fence.load()) return;
87+
fence.store(true);
88+
89+
auto end = recFuncs.end();
90+
do
91+
{
92+
if ((*it)())
93+
++it;
94+
else
95+
it = recFuncs.erase(it);
96+
// running callbacks might last too long for watchdog etc.
97+
optimistic_yield(10000);
98+
} while (it != end);
99+
100+
fence.store(false);
78101
}

0 commit comments

Comments
 (0)