Skip to content

Commit 40dbcc1

Browse files
authored
Merge pull request #6 from dok-net/d-a-v/recurrentscheduledfunctions
Proposed changes from review
2 parents 24474c8 + df839c2 commit 40dbcc1

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

cores/esp8266/Schedule.cpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ typedef std::function<bool(void)> mFuncT;
99

1010
struct scheduled_fn_t
1111
{
12-
scheduled_fn_t* mNext;
12+
scheduled_fn_t* mNext = nullptr;
1313
mFuncT mFunc;
1414
esp8266::polledTimeout::periodicFastUs callNow;
1515

16-
scheduled_fn_t(): callNow(esp8266::polledTimeout::periodicFastUs::alwaysExpired) { }
16+
scheduled_fn_t() : callNow(esp8266::polledTimeout::periodicFastUs::alwaysExpired) { }
1717
};
1818

1919
static scheduled_fn_t* sFirst = nullptr;
@@ -32,26 +32,26 @@ static scheduled_fn_t* get_fn_unsafe()
3232
{
3333
result = sUnused;
3434
sUnused = sUnused->mNext;
35+
result->mNext = nullptr;
3536
}
3637
// if no unused items, and count not too high, allocate a new one
3738
else if (sCount < SCHEDULED_FN_MAX_COUNT)
3839
{
3940
result = new scheduled_fn_t;
4041
++sCount;
4142
}
42-
result->mNext = nullptr;
4343
return result;
4444
}
4545

4646
static void recycle_fn_unsafe(scheduled_fn_t* fn)
4747
{
48-
fn->mFunc = mFuncT();
48+
fn->mFunc = nullptr; // special overload in c++ std lib
4949
fn->mNext = sUnused;
5050
sUnused = fn;
5151
}
5252

53-
IRAM_ATTR // called from ISR
54-
bool schedule_function_us(const mFuncT& fn, uint32_t repeat_us)
53+
IRAM_ATTR // (not only) called from ISR
54+
bool schedule_function_us(std::function<bool(void)>&& fn, uint32_t repeat_us)
5555
{
5656
assert(repeat_us < decltype(scheduled_fn_t::callNow)::neverExpires); //~26800000us (26.8s)
5757

@@ -74,10 +74,20 @@ bool schedule_function_us(const mFuncT& fn, uint32_t repeat_us)
7474
return true;
7575
}
7676

77+
bool ICACHE_RAM_ATTR schedule_function_us(const std::function<bool(void)>& fn, uint32_t repeat_us)
78+
{
79+
return schedule_function_us(std::function<bool(void)>(fn), repeat_us);
80+
}
81+
7782
IRAM_ATTR // called from ISR
78-
bool schedule_function(const std::function<void(void)>& fn)
83+
bool schedule_function(std::function<void(void)>&& fn)
84+
{
85+
return schedule_function_us([fn]() { fn(); return false; }, 0);
86+
}
87+
88+
bool ICACHE_RAM_ATTR schedule_function(const std::function<void(void)>& fn)
7989
{
80-
return schedule_function_us([fn](){ fn(); return false; }, 0);
90+
return schedule_function(std::function<void(void)>(fn));
8191
}
8292

8393
void run_scheduled_functions()

cores/esp8266/Schedule.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <functional>
55

66
#define SCHEDULED_FN_MAX_COUNT 32
7-
#define SCHEDULED_FN_INITIAL_COUNT 4
87

98
// This API was not considered stable but is now stabilizing.
109
// Function signatures may change, queue must stay FIFO.
@@ -17,11 +16,13 @@
1716
// Note: there is no mechanism for cancelling scheduled functions.
1817
// Keep that in mind when binding functions to objects which may have short lifetime.
1918
// Returns false if the number of scheduled functions exceeds SCHEDULED_FN_MAX_COUNT.
19+
//bool schedule_function(std::function<void(void)>&& fn);
2020
bool schedule_function(const std::function<void(void)>& fn);
2121

2222
// Run given function periodically about every <repeat_us> microseconds until it returns false.
2323
// Note that it may be more than <repeat_us> microseconds between calls if `yield` is not called
2424
// frequently, and therefore should not be used for timing critical operations.
25+
//bool schedule_function_us(std::function<bool(void)>&& fn, uint32_t repeat_us);
2526
bool schedule_function_us(const std::function<bool(void)>& fn, uint32_t repeat_us);
2627

2728
// Run all scheduled functions.

0 commit comments

Comments
 (0)