diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index 99d27d0e55..ceb5ca064c 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -9,11 +9,11 @@ typedef std::function mFuncT; struct scheduled_fn_t { - scheduled_fn_t* mNext; + scheduled_fn_t* mNext = nullptr; mFuncT mFunc; esp8266::polledTimeout::periodicFastUs callNow; - scheduled_fn_t(): callNow(esp8266::polledTimeout::periodicFastUs::alwaysExpired) { } + scheduled_fn_t() : callNow(esp8266::polledTimeout::periodicFastUs::alwaysExpired) { } }; static scheduled_fn_t* sFirst = nullptr; @@ -32,6 +32,7 @@ static scheduled_fn_t* get_fn_unsafe() { result = sUnused; sUnused = sUnused->mNext; + result->mNext = nullptr; } // if no unused items, and count not too high, allocate a new one else if (sCount < SCHEDULED_FN_MAX_COUNT) @@ -39,19 +40,18 @@ static scheduled_fn_t* get_fn_unsafe() result = new scheduled_fn_t; ++sCount; } - result->mNext = nullptr; return result; } static void recycle_fn_unsafe(scheduled_fn_t* fn) { - fn->mFunc = mFuncT(); + fn->mFunc = nullptr; // special overload in c++ std lib fn->mNext = sUnused; sUnused = fn; } -IRAM_ATTR // called from ISR -bool schedule_function_us(const mFuncT& fn, uint32_t repeat_us) +IRAM_ATTR // (not only) called from ISR +bool schedule_function_us(std::function&& fn, uint32_t repeat_us) { assert(repeat_us < decltype(scheduled_fn_t::callNow)::neverExpires); //~26800000us (26.8s) @@ -74,10 +74,20 @@ bool schedule_function_us(const mFuncT& fn, uint32_t repeat_us) return true; } +bool ICACHE_RAM_ATTR schedule_function_us(const std::function& fn, uint32_t repeat_us) +{ + return schedule_function_us(std::function(fn), repeat_us); +} + IRAM_ATTR // called from ISR -bool schedule_function(const std::function& fn) +bool schedule_function(std::function&& fn) +{ + return schedule_function_us([fn]() { fn(); return false; }, 0); +} + +bool ICACHE_RAM_ATTR schedule_function(const std::function& fn) { - return schedule_function_us([fn](){ fn(); return false; }, 0); + return schedule_function(std::function(fn)); } void run_scheduled_functions() diff --git a/cores/esp8266/Schedule.h b/cores/esp8266/Schedule.h index 62b5d84f84..bda3ebf48e 100644 --- a/cores/esp8266/Schedule.h +++ b/cores/esp8266/Schedule.h @@ -4,7 +4,6 @@ #include #define SCHEDULED_FN_MAX_COUNT 32 -#define SCHEDULED_FN_INITIAL_COUNT 4 // This API was not considered stable but is now stabilizing. // Function signatures may change, queue must stay FIFO. @@ -17,11 +16,13 @@ // Note: there is no mechanism for cancelling scheduled functions. // Keep that in mind when binding functions to objects which may have short lifetime. // Returns false if the number of scheduled functions exceeds SCHEDULED_FN_MAX_COUNT. +//bool schedule_function(std::function&& fn); bool schedule_function(const std::function& fn); // Run given function periodically about every microseconds until it returns false. // Note that it may be more than microseconds between calls if `yield` is not called // frequently, and therefore should not be used for timing critical operations. +//bool schedule_function_us(std::function&& fn, uint32_t repeat_us); bool schedule_function_us(const std::function& fn, uint32_t repeat_us); // Run all scheduled functions.