[core] Pass std::function by rvalue reference in scheduler (#14260)

This commit is contained in:
J. Nick Koston
2026-02-25 13:08:07 +13:00
committed by GitHub
parent 4dc6b12ec5
commit 08dc487b5b
2 changed files with 17 additions and 16 deletions
+8 -7
View File
@@ -135,7 +135,7 @@ bool Scheduler::is_retry_cancelled_locked_(Component *component, NameType name_t
// name_type determines storage type: STATIC_STRING uses static_name, others use hash_or_id // name_type determines storage type: STATIC_STRING uses static_name, others use hash_or_id
void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type type, NameType name_type, void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type type, NameType name_type,
const char *static_name, uint32_t hash_or_id, uint32_t delay, const char *static_name, uint32_t hash_or_id, uint32_t delay,
std::function<void()> func, bool is_retry, bool skip_cancel) { std::function<void()> &&func, bool is_retry, bool skip_cancel) {
if (delay == SCHEDULER_DONT_RUN) { if (delay == SCHEDULER_DONT_RUN) {
// Still need to cancel existing timer if we have a name/id // Still need to cancel existing timer if we have a name/id
if (!skip_cancel) { if (!skip_cancel) {
@@ -216,17 +216,18 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
target->push_back(std::move(item)); target->push_back(std::move(item));
} }
void HOT Scheduler::set_timeout(Component *component, const char *name, uint32_t timeout, std::function<void()> func) { void HOT Scheduler::set_timeout(Component *component, const char *name, uint32_t timeout,
std::function<void()> &&func) {
this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::STATIC_STRING, name, 0, timeout, this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::STATIC_STRING, name, 0, timeout,
std::move(func)); std::move(func));
} }
void HOT Scheduler::set_timeout(Component *component, const std::string &name, uint32_t timeout, void HOT Scheduler::set_timeout(Component *component, const std::string &name, uint32_t timeout,
std::function<void()> func) { std::function<void()> &&func) {
this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
timeout, std::move(func)); timeout, std::move(func));
} }
void HOT Scheduler::set_timeout(Component *component, uint32_t id, uint32_t timeout, std::function<void()> func) { void HOT Scheduler::set_timeout(Component *component, uint32_t id, uint32_t timeout, std::function<void()> &&func) {
this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID, nullptr, id, timeout, this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID, nullptr, id, timeout,
std::move(func)); std::move(func));
} }
@@ -240,17 +241,17 @@ bool HOT Scheduler::cancel_timeout(Component *component, uint32_t id) {
return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::TIMEOUT); return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::TIMEOUT);
} }
void HOT Scheduler::set_interval(Component *component, const std::string &name, uint32_t interval, void HOT Scheduler::set_interval(Component *component, const std::string &name, uint32_t interval,
std::function<void()> func) { std::function<void()> &&func) {
this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
interval, std::move(func)); interval, std::move(func));
} }
void HOT Scheduler::set_interval(Component *component, const char *name, uint32_t interval, void HOT Scheduler::set_interval(Component *component, const char *name, uint32_t interval,
std::function<void()> func) { std::function<void()> &&func) {
this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::STATIC_STRING, name, 0, interval, this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::STATIC_STRING, name, 0, interval,
std::move(func)); std::move(func));
} }
void HOT Scheduler::set_interval(Component *component, uint32_t id, uint32_t interval, std::function<void()> func) { void HOT Scheduler::set_interval(Component *component, uint32_t id, uint32_t interval, std::function<void()> &&func) {
this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID, nullptr, id, interval, this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID, nullptr, id, interval,
std::move(func)); std::move(func));
} }
+9 -9
View File
@@ -33,7 +33,7 @@ class Scheduler {
// std::string overload - deprecated, use const char* or uint32_t instead // std::string overload - deprecated, use const char* or uint32_t instead
// Remove before 2026.7.0 // Remove before 2026.7.0
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function<void()> func); void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function<void()> &&func);
/** Set a timeout with a const char* name. /** Set a timeout with a const char* name.
* *
@@ -43,11 +43,11 @@ class Scheduler {
* - A static const char* variable * - A static const char* variable
* - A pointer with lifetime >= the scheduled task * - A pointer with lifetime >= the scheduled task
*/ */
void set_timeout(Component *component, const char *name, uint32_t timeout, std::function<void()> func); void set_timeout(Component *component, const char *name, uint32_t timeout, std::function<void()> &&func);
/// Set a timeout with a numeric ID (zero heap allocation) /// Set a timeout with a numeric ID (zero heap allocation)
void set_timeout(Component *component, uint32_t id, uint32_t timeout, std::function<void()> func); void set_timeout(Component *component, uint32_t id, uint32_t timeout, std::function<void()> &&func);
/// Set a timeout with an internal scheduler ID (separate namespace from component NUMERIC_ID) /// Set a timeout with an internal scheduler ID (separate namespace from component NUMERIC_ID)
void set_timeout(Component *component, InternalSchedulerID id, uint32_t timeout, std::function<void()> func) { void set_timeout(Component *component, InternalSchedulerID id, uint32_t timeout, std::function<void()> &&func) {
this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID_INTERNAL, nullptr, this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID_INTERNAL, nullptr,
static_cast<uint32_t>(id), timeout, std::move(func)); static_cast<uint32_t>(id), timeout, std::move(func));
} }
@@ -62,7 +62,7 @@ class Scheduler {
} }
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0")
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function<void()> func); void set_interval(Component *component, const std::string &name, uint32_t interval, std::function<void()> &&func);
/** Set an interval with a const char* name. /** Set an interval with a const char* name.
* *
@@ -72,11 +72,11 @@ class Scheduler {
* - A static const char* variable * - A static const char* variable
* - A pointer with lifetime >= the scheduled task * - A pointer with lifetime >= the scheduled task
*/ */
void set_interval(Component *component, const char *name, uint32_t interval, std::function<void()> func); void set_interval(Component *component, const char *name, uint32_t interval, std::function<void()> &&func);
/// Set an interval with a numeric ID (zero heap allocation) /// Set an interval with a numeric ID (zero heap allocation)
void set_interval(Component *component, uint32_t id, uint32_t interval, std::function<void()> func); void set_interval(Component *component, uint32_t id, uint32_t interval, std::function<void()> &&func);
/// Set an interval with an internal scheduler ID (separate namespace from component NUMERIC_ID) /// Set an interval with an internal scheduler ID (separate namespace from component NUMERIC_ID)
void set_interval(Component *component, InternalSchedulerID id, uint32_t interval, std::function<void()> func) { void set_interval(Component *component, InternalSchedulerID id, uint32_t interval, std::function<void()> &&func) {
this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID_INTERNAL, nullptr, this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID_INTERNAL, nullptr,
static_cast<uint32_t>(id), interval, std::move(func)); static_cast<uint32_t>(id), interval, std::move(func));
} }
@@ -255,7 +255,7 @@ class Scheduler {
// Common implementation for both timeout and interval // Common implementation for both timeout and interval
// name_type determines storage type: STATIC_STRING uses static_name, others use hash_or_id // name_type determines storage type: STATIC_STRING uses static_name, others use hash_or_id
void set_timer_common_(Component *component, SchedulerItem::Type type, NameType name_type, const char *static_name, void set_timer_common_(Component *component, SchedulerItem::Type type, NameType name_type, const char *static_name,
uint32_t hash_or_id, uint32_t delay, std::function<void()> func, bool is_retry = false, uint32_t hash_or_id, uint32_t delay, std::function<void()> &&func, bool is_retry = false,
bool skip_cancel = false); bool skip_cancel = false);
// Common implementation for retry - Remove before 2026.8.0 // Common implementation for retry - Remove before 2026.8.0