mirror of
https://github.com/esphome/esphome.git
synced 2025-08-06 02:17:45 +00:00
preen
This commit is contained in:
parent
758e5b89bb
commit
e355ce04f7
@ -68,12 +68,7 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
|
|||||||
if (delay == SCHEDULER_DONT_RUN) {
|
if (delay == SCHEDULER_DONT_RUN) {
|
||||||
// Still need to cancel existing timer if name is not empty
|
// Still need to cancel existing timer if name is not empty
|
||||||
if (name_cstr != nullptr && name_cstr[0] != '\0') {
|
if (name_cstr != nullptr && name_cstr[0] != '\0') {
|
||||||
LockGuard guard{this->lock_};
|
this->cancel_item_(component, is_static_string, name_ptr, type);
|
||||||
if (delay == 0 && type == SchedulerItem::TIMEOUT) {
|
|
||||||
this->cancel_deferred_item_locked_(component, is_static_string, name_ptr, type);
|
|
||||||
} else {
|
|
||||||
this->cancel_heap_item_locked_(component, is_static_string, name_ptr, type);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -132,16 +127,7 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
|
|||||||
// If name is provided, do atomic cancel-and-add
|
// If name is provided, do atomic cancel-and-add
|
||||||
if (name_cstr != nullptr && name_cstr[0] != '\0') {
|
if (name_cstr != nullptr && name_cstr[0] != '\0') {
|
||||||
// Cancel existing items
|
// Cancel existing items
|
||||||
if (delay == 0 && type == SchedulerItem::TIMEOUT) {
|
this->cancel_item_locked_(component, name_cstr, type);
|
||||||
// For defer (delay=0), only cancel from defer queue
|
|
||||||
this->cancel_deferred_item_locked_(component, name_cstr, type);
|
|
||||||
} else if (type == SchedulerItem::TIMEOUT) {
|
|
||||||
// For regular timeouts, check all containers since we don't know where it might be
|
|
||||||
this->cancel_item_locked_(component, name_cstr, type);
|
|
||||||
} else {
|
|
||||||
// For intervals, only check heap items
|
|
||||||
this->cancel_heap_item_locked_(component, name_cstr, type);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Add new item directly to to_add_
|
// Add new item directly to to_add_
|
||||||
// since we have the lock held
|
// since we have the lock held
|
||||||
@ -442,43 +428,11 @@ bool HOT Scheduler::cancel_item_(Component *component, bool is_static_string, co
|
|||||||
return this->cancel_item_locked_(component, name_cstr, type);
|
return this->cancel_item_locked_(component, name_cstr, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cancel heap items (items_ and to_add_)
|
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
||||||
bool HOT Scheduler::cancel_heap_item_(Component *component, bool is_static_string, const void *name_ptr,
|
|
||||||
SchedulerItem::Type type) {
|
|
||||||
// Get the name as const char*
|
|
||||||
const char *name_cstr =
|
|
||||||
is_static_string ? static_cast<const char *>(name_ptr) : static_cast<const std::string *>(name_ptr)->c_str();
|
|
||||||
|
|
||||||
// Handle null or empty names
|
|
||||||
if (name_cstr == nullptr)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// obtain lock because this function iterates and can be called from non-loop task context
|
|
||||||
LockGuard guard{this->lock_};
|
|
||||||
return this->cancel_heap_item_locked_(component, name_cstr, type) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cancel deferred items (defer_queue_)
|
|
||||||
bool HOT Scheduler::cancel_deferred_item_(Component *component, bool is_static_string, const void *name_ptr,
|
|
||||||
SchedulerItem::Type type) {
|
|
||||||
// Get the name as const char*
|
|
||||||
const char *name_cstr =
|
|
||||||
is_static_string ? static_cast<const char *>(name_ptr) : static_cast<const std::string *>(name_ptr)->c_str();
|
|
||||||
|
|
||||||
// Handle null or empty names
|
|
||||||
if (name_cstr == nullptr)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// obtain lock because this function iterates and can be called from non-loop task context
|
|
||||||
LockGuard guard{this->lock_};
|
|
||||||
return this->cancel_deferred_item_locked_(component, name_cstr, type) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to mark deferred items for cancellation (no to_remove_ tracking needed)
|
// Helper to mark deferred items for cancellation (no to_remove_ tracking needed)
|
||||||
size_t HOT Scheduler::cancel_deferred_item_locked_(Component *component, const char *name_cstr,
|
size_t HOT Scheduler::cancel_deferred_item_locked_(Component *component, const char *name_cstr,
|
||||||
SchedulerItem::Type type) {
|
SchedulerItem::Type type) {
|
||||||
size_t cancelled_count = 0;
|
size_t cancelled_count = 0;
|
||||||
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
|
||||||
for (auto &item : this->defer_queue_) {
|
for (auto &item : this->defer_queue_) {
|
||||||
if (item->component != component || item->type != type || item->remove) {
|
if (item->component != component || item->type != type || item->remove) {
|
||||||
continue;
|
continue;
|
||||||
@ -489,12 +443,9 @@ size_t HOT Scheduler::cancel_deferred_item_locked_(Component *component, const c
|
|||||||
cancelled_count++;
|
cancelled_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
// On platforms without defer queue, defer items go to the heap
|
|
||||||
cancelled_count = this->cancel_heap_item_locked_(component, name_cstr, type);
|
|
||||||
#endif
|
|
||||||
return cancelled_count;
|
return cancelled_count;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Helper to mark heap items for cancellation and update to_remove_ count
|
// Helper to mark heap items for cancellation and update to_remove_ count
|
||||||
size_t HOT Scheduler::cancel_heap_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type) {
|
size_t HOT Scheduler::cancel_heap_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type) {
|
||||||
@ -530,18 +481,14 @@ size_t HOT Scheduler::cancel_heap_item_locked_(Component *component, const char
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper to cancel items by name - must be called with lock held
|
// Helper to cancel items by name - must be called with lock held
|
||||||
bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type,
|
bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type) {
|
||||||
uint32_t delay) {
|
|
||||||
size_t total_cancelled = 0;
|
size_t total_cancelled = 0;
|
||||||
|
|
||||||
// Check all containers for matching items
|
// Check all containers for matching items
|
||||||
if (delay == 0 && type == SchedulerItem::TIMEOUT) {
|
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
||||||
// Cancel deferred items only
|
total_cancelled += this->cancel_deferred_item_locked_(component, name_cstr, type);
|
||||||
total_cancelled += this->cancel_deferred_item_locked_(component, name_cstr, type);
|
#endif
|
||||||
} else {
|
total_cancelled += this->cancel_heap_item_locked_(component, name_cstr, type);
|
||||||
// Cancel heap items (items_ and to_add_)
|
|
||||||
total_cancelled += this->cancel_heap_item_locked_(component, name_cstr, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
return total_cancelled > 0;
|
return total_cancelled > 0;
|
||||||
}
|
}
|
||||||
|
@ -140,8 +140,10 @@ class Scheduler {
|
|||||||
// Helper to cancel items by name - must be called with lock held
|
// Helper to cancel items by name - must be called with lock held
|
||||||
bool cancel_item_locked_(Component *component, const char *name, SchedulerItem::Type type);
|
bool cancel_item_locked_(Component *component, const char *name, SchedulerItem::Type type);
|
||||||
|
|
||||||
|
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
||||||
// Helper to mark deferred items for cancellation (no to_remove_ tracking needed)
|
// Helper to mark deferred items for cancellation (no to_remove_ tracking needed)
|
||||||
size_t cancel_deferred_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type);
|
size_t cancel_deferred_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type);
|
||||||
|
#endif
|
||||||
|
|
||||||
// Helper to mark heap items for cancellation and update to_remove_ count
|
// Helper to mark heap items for cancellation and update to_remove_ count
|
||||||
size_t cancel_heap_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type);
|
size_t cancel_heap_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user