1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-16 10:12:21 +01:00
This commit is contained in:
J. Nick Koston
2025-09-02 22:27:40 -05:00
parent 9a6e678621
commit 1a5402f35c
2 changed files with 18 additions and 24 deletions

View File

@@ -517,7 +517,9 @@ void HOT Scheduler::call(uint32_t now) {
void HOT Scheduler::process_to_add() { void HOT Scheduler::process_to_add() {
LockGuard guard{this->lock_}; LockGuard guard{this->lock_};
for (auto &it : this->to_add_) { 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; continue;
} }
@@ -595,10 +597,14 @@ bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_c
// Check all containers for matching items // Check all containers for matching items
#ifndef ESPHOME_THREAD_SINGLE #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) { if (type == SchedulerItem::TIMEOUT) {
total_cancelled += for (auto &item : this->defer_queue_) {
this->cancel_and_recycle_from_container_(this->defer_queue_, component, name_cstr, type, match_retry); if (this->matches_item_(item, component, name_cstr, type, match_retry)) {
this->mark_item_removed_(item.get());
total_cancelled++;
}
}
} }
#endif /* not ESPHOME_THREAD_SINGLE */ #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 // Cancel items in to_add_
total_cancelled += this->cancel_and_recycle_from_container_(this->to_add_, component, name_cstr, type, match_retry); 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; return total_cancelled > 0;
} }

View File

@@ -291,24 +291,6 @@ class Scheduler {
return false; return false;
} }
// Template helper to cancel and recycle items from a container
template<typename Container>
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_; Mutex lock_;
std::vector<std::unique_ptr<SchedulerItem>> items_; std::vector<std::unique_ptr<SchedulerItem>> items_;
std::vector<std::unique_ptr<SchedulerItem>> to_add_; std::vector<std::unique_ptr<SchedulerItem>> to_add_;