Skip to content

Commit 88302e8

Browse files
committed
Putting aside std::atomic concerns, use a callback for scheduler alarming.
In the future, async future's .then() might take advantage of this direction.
1 parent 8a531b7 commit 88302e8

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

cores/esp8266/Schedule.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ struct recurrent_fn_t
2424
recurrent_fn_t* mNext = nullptr;
2525
mRecFuncT mFunc;
2626
esp8266::polledTimeout::periodicFastUs callNow;
27-
const std::atomic<bool>* wakeupToken = nullptr;
28-
bool wakeupTokenCmp = false;
27+
std::function<bool()> alarm = nullptr;
2928
recurrent_fn_t(esp8266::polledTimeout::periodicFastUs interval) : callNow(interval) { }
3029
};
3130

@@ -86,8 +85,8 @@ bool schedule_function (const std::function<void(void)>& fn)
8685
return true;
8786
}
8887

89-
bool schedule_recurrent_function_us(const std::function<bool(void)>& fn, uint32_t repeat_us,
90-
const std::atomic<bool>* wakeupToken)
88+
bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
89+
uint32_t repeat_us, std::function<bool()> alarm)
9190
{
9291
assert(repeat_us < decltype(recurrent_fn_t::callNow)::neverExpires); //~26800000us (26.8s)
9392

@@ -100,8 +99,7 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn, uint32_
10099

101100
item->mFunc = fn;
102101
item->mNext = nullptr;
103-
item->wakeupToken = wakeupToken;
104-
item->wakeupTokenCmp = wakeupToken && wakeupToken->load();
102+
item->alarm = std::move(alarm);
105103

106104
esp8266::InterruptLock lockAllInterruptsInThisScope;
107105

@@ -174,8 +172,7 @@ void run_scheduled_recurrent_functions()
174172
do
175173
{
176174
done = current == stop;
177-
const bool wakeupToken = current->wakeupToken && current->wakeupToken->load();
178-
const bool wakeup = current->wakeupTokenCmp != wakeupToken;
175+
const bool wakeup = current->alarm && current->alarm();
179176
bool callNow = current->callNow;
180177

181178
if ((wakeup || callNow) && !current->mFunc())

cores/esp8266/Schedule.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ void run_scheduled_functions();
5959
// functions. However a user function returning false will cancel itself.
6060
// * Long running operations or yield() or delay() are not allowed in the
6161
// recurrent function.
62-
// * If a wakeupToken is used, anytime during scheduling when its value differs
63-
// from the original one in this call, any remaining delay from repeat_us is
64-
// disregarded, and fn is executed.
62+
// * If alarm is used, anytime during scheduling when it returns true,
63+
// any remaining delay from repeat_us is disregarded, and fn is executed.
6564
bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
66-
uint32_t repeat_us, const std::atomic<bool>* wakeupToken = nullptr);
65+
uint32_t repeat_us, std::function<bool()> alarm = nullptr);
6766

6867
// Test recurrence and run recurrent scheduled functions.
6968
// (internally called at every `yield()` and `loop()`)

0 commit comments

Comments
 (0)