From 1a5402f35c674015d64b1e35816e4d61e310d073 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 2 Sep 2025 22:27:40 -0500 Subject: [PATCH] preen --- esphome/core/scheduler.cpp | 24 ++++++++++++++++++------ esphome/core/scheduler.h | 18 ------------------ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index f463e87996..4745cf9ecc 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -517,7 +517,9 @@ void HOT Scheduler::call(uint32_t now) { void HOT Scheduler::process_to_add() { LockGuard guard{this->lock_}; for (auto &it : this->to_add_) { - if (it->remove) { + if (is_item_removed_(it.get())) { + // Recycle cancelled items + this->recycle_item_(std::move(it)); continue; } @@ -595,10 +597,14 @@ bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_c // Check all containers for matching items #ifndef ESPHOME_THREAD_SINGLE - // Cancel and immediately recycle items in defer queue + // Mark items in defer queue as cancelled (they'll be skipped when processed) if (type == SchedulerItem::TIMEOUT) { - total_cancelled += - this->cancel_and_recycle_from_container_(this->defer_queue_, component, name_cstr, type, match_retry); + for (auto &item : this->defer_queue_) { + if (this->matches_item_(item, component, name_cstr, type, match_retry)) { + this->mark_item_removed_(item.get()); + total_cancelled++; + } + } } #endif /* not ESPHOME_THREAD_SINGLE */ @@ -622,8 +628,14 @@ bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_c } } - // Cancel and immediately recycle items in to_add_ since they're not in heap yet - total_cancelled += this->cancel_and_recycle_from_container_(this->to_add_, component, name_cstr, type, match_retry); + // Cancel items in to_add_ + for (auto &item : this->to_add_) { + if (this->matches_item_(item, component, name_cstr, type, match_retry)) { + this->mark_item_removed_(item.get()); + total_cancelled++; + // Don't track removals for to_add_ items + } + } return total_cancelled > 0; } diff --git a/esphome/core/scheduler.h b/esphome/core/scheduler.h index e241e7a4ec..85cfaab2e0 100644 --- a/esphome/core/scheduler.h +++ b/esphome/core/scheduler.h @@ -291,24 +291,6 @@ class Scheduler { return false; } - // Template helper to cancel and recycle items from a container - template - size_t cancel_and_recycle_from_container_(Container &container, Component *component, const char *name_cstr, - SchedulerItem::Type type, bool match_retry) { - size_t cancelled = 0; - for (auto it = container.begin(); it != container.end();) { - if (this->matches_item_(*it, component, name_cstr, type, match_retry)) { - // Recycle the cancelled item immediately - this->recycle_item_(std::move(*it)); - it = container.erase(it); - cancelled++; - } else { - ++it; - } - } - return cancelled; - } - Mutex lock_; std::vector> items_; std::vector> to_add_;