This commit is contained in:
J. Nick Koston 2025-07-06 18:01:21 -05:00
parent 758e5b89bb
commit e355ce04f7
No known key found for this signature in database
2 changed files with 11 additions and 62 deletions

View File

@ -68,12 +68,7 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
if (delay == SCHEDULER_DONT_RUN) {
// Still need to cancel existing timer if name is not empty
if (name_cstr != nullptr && name_cstr[0] != '\0') {
LockGuard guard{this->lock_};
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);
}
this->cancel_item_(component, is_static_string, name_ptr, type);
}
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_cstr != nullptr && name_cstr[0] != '\0') {
// Cancel existing items
if (delay == 0 && type == SchedulerItem::TIMEOUT) {
// 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);
}
this->cancel_item_locked_(component, name_cstr, type);
}
// Add new item directly to to_add_
// 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);
}
// Cancel heap items (items_ and to_add_)
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;
}
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
// 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,
SchedulerItem::Type type) {
size_t cancelled_count = 0;
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
for (auto &item : this->defer_queue_) {
if (item->component != component || item->type != type || item->remove) {
continue;
@ -489,12 +443,9 @@ size_t HOT Scheduler::cancel_deferred_item_locked_(Component *component, const c
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;
}
#endif
// 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) {
@ -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
bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type,
uint32_t delay) {
bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type) {
size_t total_cancelled = 0;
// Check all containers for matching items
if (delay == 0 && type == SchedulerItem::TIMEOUT) {
// Cancel deferred items only
total_cancelled += this->cancel_deferred_item_locked_(component, name_cstr, type);
} else {
// Cancel heap items (items_ and to_add_)
total_cancelled += this->cancel_heap_item_locked_(component, name_cstr, type);
}
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
total_cancelled += this->cancel_deferred_item_locked_(component, name_cstr, type);
#endif
total_cancelled += this->cancel_heap_item_locked_(component, name_cstr, type);
return total_cancelled > 0;
}

View File

@ -140,8 +140,10 @@ class Scheduler {
// Helper to cancel items by name - must be called with lock held
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)
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
size_t cancel_heap_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type);