15static const char *
const TAG =
"scheduler";
24static constexpr size_t MAX_POOL_SIZE = 5;
29static constexpr uint32_t MAX_LOGICALLY_DELETED_ITEMS = 5;
31static constexpr uint32_t MAX_INTERVAL_DELAY = 5000;
36void Scheduler::SchedulerItemDeleter::operator()(SchedulerItem *ptr)
const noexcept {
delete ptr; }
38#if defined(ESPHOME_LOG_HAS_VERBOSE) || defined(ESPHOME_DEBUG_SCHEDULER)
42struct SchedulerNameLog {
47 const char *format(Scheduler::NameType name_type,
const char *static_name, uint32_t hash_or_id) {
48 using NameType = Scheduler::NameType;
49 if (name_type == NameType::STATIC_STRING) {
53 ESPHOME_strncpy_P(buffer, ESPHOME_PSTR(
"(null)"),
sizeof(buffer));
55 }
else if (name_type == NameType::HASHED_STRING) {
56 ESPHOME_snprintf_P(buffer,
sizeof(buffer), ESPHOME_PSTR(
"hash:0x%08" PRIX32), hash_or_id);
58 }
else if (name_type == NameType::NUMERIC_ID) {
59 ESPHOME_snprintf_P(buffer,
sizeof(buffer), ESPHOME_PSTR(
"id:%" PRIu32), hash_or_id);
62 ESPHOME_snprintf_P(buffer,
sizeof(buffer), ESPHOME_PSTR(
"iid:%" PRIu32), hash_or_id);
72#ifdef ESPHOME_DEBUG_SCHEDULER
74static void validate_static_string(
const char *name) {
80 uintptr_t addr =
reinterpret_cast<uintptr_t
>(name);
84 uintptr_t stack_addr =
reinterpret_cast<uintptr_t
>(&stack_var);
88 if (addr > (stack_addr - 0x2000) && addr < (stack_addr + 0x2000)) {
90 "WARNING: Scheduler name '%s' at %p appears to be on the stack - this is unsafe!\n"
91 " Stack reference at %p",
92 name, name, &stack_var);
97 static const char *static_str =
"test";
98 uintptr_t static_addr =
reinterpret_cast<uintptr_t
>(static_str);
101 if (addr > static_addr + 0x100000 || (static_addr > 0x100000 && addr < static_addr - 0x100000)) {
102 ESP_LOGW(TAG,
"WARNING: Scheduler name '%s' at %p might be on heap (static ref at %p)", name, name, static_str);
115uint32_t Scheduler::calculate_interval_offset_(uint32_t
delay) {
122bool Scheduler::is_retry_cancelled_locked_(Component *
component, NameType name_type,
const char *static_name,
123 uint32_t hash_or_id) {
124 for (
auto *container : {&this->items_, &this->to_add_}) {
125 for (
auto &item : *container) {
126 if (item && this->is_item_removed_locked_(item.get()) &&
127 this->matches_item_locked_(item,
component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
138void HOT Scheduler::set_timer_common_(Component *
component, SchedulerItem::Type
type, NameType name_type,
139 const char *static_name, uint32_t hash_or_id, uint32_t
delay,
140 std::function<
void()> &&func,
bool is_retry,
bool skip_cancel) {
144 LockGuard guard{this->lock_};
145 this->cancel_item_locked_(
component, name_type, static_name, hash_or_id,
type);
151 LockGuard guard{this->lock_};
154 auto item = this->get_item_from_pool_locked_();
156 item->set_name(name_type, static_name, hash_or_id);
158 item->callback = std::move(func);
160 this->set_item_removed_(item.get(),
false);
161 item->is_retry = is_retry;
165 auto *target = &this->to_add_;
167#ifndef ESPHOME_THREAD_SINGLE
170 if (
delay == 0 &&
type == SchedulerItem::TIMEOUT) {
172 target = &this->defer_queue_;
180 if (
type == SchedulerItem::INTERVAL) {
181 item->interval =
delay;
184 item->set_next_execution(now_64 + offset);
185#ifdef ESPHOME_LOG_HAS_VERBOSE
186 SchedulerNameLog name_log;
187 ESP_LOGV(TAG,
"Scheduler interval for %s is %" PRIu32
"ms, offset %" PRIu32
"ms",
188 name_log.format(name_type, static_name, hash_or_id),
delay, offset);
192 item->set_next_execution(now_64 +
delay);
195#ifdef ESPHOME_DEBUG_SCHEDULER
196 this->debug_log_timer_(item.get(), name_type, static_name, hash_or_id,
type,
delay, now_64);
201 if (is_retry && (name_type != NameType::STATIC_STRING || static_name !=
nullptr) &&
202 type == SchedulerItem::TIMEOUT &&
203 this->is_retry_cancelled_locked_(
component, name_type, static_name, hash_or_id)) {
205#ifdef ESPHOME_DEBUG_SCHEDULER
206 SchedulerNameLog skip_name_log;
207 ESP_LOGD(TAG,
"Skipping retry '%s' - found cancelled item",
208 skip_name_log.format(name_type, static_name, hash_or_id));
216 this->cancel_item_locked_(
component, name_type, static_name, hash_or_id,
type);
218 target->push_back(std::move(item));
221void HOT Scheduler::set_timeout(Component *
component,
const char *name, uint32_t timeout,
222 std::function<
void()> &&func) {
223 this->set_timer_common_(
component, SchedulerItem::TIMEOUT, NameType::STATIC_STRING, name, 0, timeout,
227void HOT Scheduler::set_timeout(Component *
component,
const std::string &name, uint32_t timeout,
228 std::function<
void()> &&func) {
229 this->set_timer_common_(
component, SchedulerItem::TIMEOUT, NameType::HASHED_STRING,
nullptr,
fnv1a_hash(name),
230 timeout, std::move(func));
232void HOT Scheduler::set_timeout(Component *
component, uint32_t
id, uint32_t timeout, std::function<
void()> &&func) {
233 this->set_timer_common_(
component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID,
nullptr,
id, timeout,
236bool HOT Scheduler::cancel_timeout(Component *
component,
const std::string &name) {
237 return this->cancel_item_(
component, NameType::HASHED_STRING,
nullptr,
fnv1a_hash(name), SchedulerItem::TIMEOUT);
239bool HOT Scheduler::cancel_timeout(Component *
component,
const char *name) {
240 return this->cancel_item_(
component, NameType::STATIC_STRING, name, 0, SchedulerItem::TIMEOUT);
242bool HOT Scheduler::cancel_timeout(Component *
component, uint32_t
id) {
243 return this->cancel_item_(
component, NameType::NUMERIC_ID,
nullptr,
id, SchedulerItem::TIMEOUT);
245void HOT Scheduler::set_interval(Component *
component,
const std::string &name, uint32_t interval,
246 std::function<
void()> &&func) {
247 this->set_timer_common_(
component, SchedulerItem::INTERVAL, NameType::HASHED_STRING,
nullptr,
fnv1a_hash(name),
248 interval, std::move(func));
251void HOT Scheduler::set_interval(Component *
component,
const char *name, uint32_t interval,
252 std::function<
void()> &&func) {
253 this->set_timer_common_(
component, SchedulerItem::INTERVAL, NameType::STATIC_STRING, name, 0, interval,
256void HOT Scheduler::set_interval(Component *
component, uint32_t
id, uint32_t interval, std::function<
void()> &&func) {
257 this->set_timer_common_(
component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID,
nullptr,
id, interval,
260bool HOT Scheduler::cancel_interval(Component *
component,
const std::string &name) {
261 return this->cancel_item_(
component, NameType::HASHED_STRING,
nullptr,
fnv1a_hash(name), SchedulerItem::INTERVAL);
263bool HOT Scheduler::cancel_interval(Component *
component,
const char *name) {
264 return this->cancel_item_(
component, NameType::STATIC_STRING, name, 0, SchedulerItem::INTERVAL);
266bool HOT Scheduler::cancel_interval(Component *
component, uint32_t
id) {
267 return this->cancel_item_(
component, NameType::NUMERIC_ID,
nullptr,
id, SchedulerItem::INTERVAL);
272#pragma GCC diagnostic push
273#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
279 Scheduler *scheduler;
282 const char *static_name;
286 float backoff_increase_factor;
287 Scheduler::NameType name_type;
288 uint8_t retry_countdown;
292 RetryResult const retry_result = args->func(--args->retry_countdown);
298 const char *static_name = (args->name_type == Scheduler::NameType::STATIC_STRING) ? args->name_.static_name :
nullptr;
299 uint32_t hash_or_id = (args->name_type != Scheduler::NameType::STATIC_STRING) ? args->name_.hash_or_id : 0;
300 args->scheduler->set_timer_common_(
301 args->component, Scheduler::SchedulerItem::TIMEOUT, args->name_type, static_name, hash_or_id,
302 args->current_interval, [args]() { retry_handler(args); },
305 args->current_interval *= args->backoff_increase_factor;
308void HOT Scheduler::set_retry_common_(Component *
component, NameType name_type,
const char *static_name,
309 uint32_t hash_or_id, uint32_t initial_wait_time, uint8_t max_attempts,
310 std::function<
RetryResult(uint8_t)> func,
float backoff_increase_factor) {
311 this->cancel_retry_(
component, name_type, static_name, hash_or_id);
316#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
318 SchedulerNameLog name_log;
319 ESP_LOGVV(TAG,
"set_retry(name='%s', initial_wait_time=%" PRIu32
", max_attempts=%u, backoff_factor=%0.1f)",
320 name_log.format(name_type, static_name, hash_or_id), initial_wait_time, max_attempts,
321 backoff_increase_factor);
325 if (backoff_increase_factor < 0.0001) {
326 ESP_LOGE(TAG,
"set_retry: backoff_factor %0.1f too small, using 1.0: %s", backoff_increase_factor,
327 (name_type == NameType::STATIC_STRING && static_name) ? static_name :
"");
328 backoff_increase_factor = 1;
331 auto args = std::make_shared<RetryArgs>();
332 args->func = std::move(func);
334 args->scheduler =
this;
335 args->name_type = name_type;
336 if (name_type == NameType::STATIC_STRING) {
337 args->name_.static_name = static_name;
339 args->name_.hash_or_id = hash_or_id;
341 args->current_interval = initial_wait_time;
343 args->retry_countdown = max_attempts;
346 this->set_timer_common_(
347 component, SchedulerItem::TIMEOUT, name_type, static_name, hash_or_id, 0, [args]() {
retry_handler(args); },
351void HOT Scheduler::set_retry(Component *
component,
const char *name, uint32_t initial_wait_time, uint8_t max_attempts,
352 std::function<
RetryResult(uint8_t)> func,
float backoff_increase_factor) {
353 this->set_retry_common_(
component, NameType::STATIC_STRING, name, 0, initial_wait_time, max_attempts, std::move(func),
354 backoff_increase_factor);
357bool HOT Scheduler::cancel_retry_(Component *
component, NameType name_type,
const char *static_name,
358 uint32_t hash_or_id) {
359 return this->cancel_item_(
component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
362bool HOT Scheduler::cancel_retry(Component *
component,
const char *name) {
363 return this->cancel_retry_(
component, NameType::STATIC_STRING, name, 0);
366void HOT Scheduler::set_retry(Component *
component,
const std::string &name, uint32_t initial_wait_time,
367 uint8_t max_attempts, std::function<
RetryResult(uint8_t)> func,
368 float backoff_increase_factor) {
369 this->set_retry_common_(
component, NameType::HASHED_STRING,
nullptr,
fnv1a_hash(name), initial_wait_time,
370 max_attempts, std::move(func), backoff_increase_factor);
373bool HOT Scheduler::cancel_retry(Component *
component,
const std::string &name) {
377void HOT Scheduler::set_retry(Component *
component, uint32_t
id, uint32_t initial_wait_time, uint8_t max_attempts,
378 std::function<
RetryResult(uint8_t)> func,
float backoff_increase_factor) {
379 this->set_retry_common_(
component, NameType::NUMERIC_ID,
nullptr,
id, initial_wait_time, max_attempts,
380 std::move(func), backoff_increase_factor);
383bool HOT Scheduler::cancel_retry(Component *
component, uint32_t
id) {
384 return this->cancel_retry_(
component, NameType::NUMERIC_ID,
nullptr,
id);
387#pragma GCC diagnostic pop
389optional<uint32_t> HOT Scheduler::next_schedule_in(uint32_t now) {
395 if (this->cleanup_() == 0)
398 auto &item = this->items_[0];
399 const auto now_64 = this->millis_64_from_(now);
400 const uint64_t next_exec = item->get_next_execution();
401 if (next_exec < now_64)
403 return next_exec - now_64;
406void Scheduler::full_cleanup_removed_items_() {
412 LockGuard guard{this->lock_};
416 for (
size_t read = 0; read < this->items_.size(); ++read) {
417 if (!is_item_removed_locked_(this->items_[read].get())) {
419 this->items_[write] = std::move(this->items_[read]);
423 this->recycle_item_main_loop_(std::move(this->items_[read]));
426 this->items_.erase(this->items_.begin() + write, this->items_.end());
428 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
429 this->to_remove_ = 0;
432#ifndef ESPHOME_THREAD_SINGLE
433void Scheduler::compact_defer_queue_locked_() {
445 size_t remaining = this->defer_queue_.size() - this->defer_queue_front_;
446 for (
size_t i = 0; i < remaining; i++) {
447 this->defer_queue_[i] = std::move(this->defer_queue_[this->defer_queue_front_ + i]);
451 this->defer_queue_.erase(this->defer_queue_.begin() + remaining, this->defer_queue_.end());
455void HOT Scheduler::call(uint32_t now) {
456#ifndef ESPHOME_THREAD_SINGLE
457 this->process_defer_queue_(now);
461 const auto now_64 = this->millis_64_from_(now);
462 this->process_to_add();
465 bool has_added_items =
false;
467#ifdef ESPHOME_DEBUG_SCHEDULER
468 static uint64_t last_print = 0;
470 if (now_64 - last_print > 2000) {
472 std::vector<SchedulerItemPtr> old_items;
473 ESP_LOGD(TAG,
"Items: count=%zu, pool=%zu, now=%" PRIu64, this->items_.size(), this->scheduler_item_pool_.size(),
477 while (!this->items_.empty()) {
478 SchedulerItemPtr item;
480 LockGuard guard{this->lock_};
481 item = this->pop_raw_locked_();
484 SchedulerNameLog name_log;
485 bool is_cancelled = is_item_removed_(item.get());
486 ESP_LOGD(TAG,
" %s '%s/%s' interval=%" PRIu32
" next_execution in %" PRIu64
"ms at %" PRIu64
"%s",
487 item->get_type_str(), LOG_STR_ARG(item->get_source()),
488 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
489 item->get_next_execution() - now_64, item->get_next_execution(), is_cancelled ?
" [CANCELLED]" :
"");
491 old_items.push_back(std::move(item));
496 LockGuard guard{this->lock_};
497 this->items_ = std::move(old_items);
499 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
510 if (this->to_remove_ >= MAX_LOGICALLY_DELETED_ITEMS) {
511 this->full_cleanup_removed_items_();
513 while (!this->items_.empty()) {
515 auto &item = this->items_[0];
516 if (item->get_next_execution() > now_64) {
521 if (item->component !=
nullptr && item->component->is_failed()) {
522 LockGuard guard{this->lock_};
523 this->recycle_item_main_loop_(this->pop_raw_locked_());
531#ifdef ESPHOME_THREAD_MULTI_NO_ATOMICS
534 LockGuard guard{this->lock_};
535 if (is_item_removed_locked_(item.get())) {
536 this->recycle_item_main_loop_(this->pop_raw_locked_());
543 if (is_item_removed_(item.get())) {
544 LockGuard guard{this->lock_};
545 this->recycle_item_main_loop_(this->pop_raw_locked_());
551#ifdef ESPHOME_DEBUG_SCHEDULER
553 SchedulerNameLog name_log;
554 ESP_LOGV(TAG,
"Running %s '%s/%s' with interval=%" PRIu32
" next_execution=%" PRIu64
" (now=%" PRIu64
")",
555 item->get_type_str(), LOG_STR_ARG(item->get_source()),
556 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
557 item->get_next_execution(), now_64);
564 now = this->execute_item_(item.get(), now);
566 LockGuard guard{this->lock_};
570 auto executed_item = this->pop_raw_locked_();
572 if (this->is_item_removed_locked_(executed_item.get())) {
575 this->recycle_item_main_loop_(std::move(executed_item));
579 if (executed_item->type == SchedulerItem::INTERVAL) {
580 executed_item->set_next_execution(now_64 + executed_item->interval);
583 this->to_add_.push_back(std::move(executed_item));
586 this->recycle_item_main_loop_(std::move(executed_item));
589 has_added_items |= !this->to_add_.empty();
592 if (has_added_items) {
593 this->process_to_add();
596void HOT Scheduler::process_to_add() {
597 LockGuard guard{this->lock_};
598 for (
auto &it : this->to_add_) {
599 if (is_item_removed_locked_(it.get())) {
601 this->recycle_item_main_loop_(std::move(it));
605 this->items_.push_back(std::move(it));
606 std::push_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
608 this->to_add_.clear();
610size_t HOT Scheduler::cleanup_() {
618 if (this->to_remove_ == 0)
619 return this->items_.size();
629 LockGuard guard{this->lock_};
630 while (!this->items_.empty()) {
631 auto &item = this->items_[0];
632 if (!this->is_item_removed_locked_(item.get()))
635 this->recycle_item_main_loop_(this->pop_raw_locked_());
637 return this->items_.size();
639Scheduler::SchedulerItemPtr HOT Scheduler::pop_raw_locked_() {
640 std::pop_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
643 auto item = std::move(this->items_.back());
645 this->items_.pop_back();
650uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) {
652 WarnIfComponentBlockingGuard guard{item->component, now};
654 return guard.finish();
658bool HOT Scheduler::cancel_item_(Component *
component, NameType name_type,
const char *static_name, uint32_t hash_or_id,
659 SchedulerItem::Type
type,
bool match_retry) {
660 LockGuard guard{this->lock_};
661 return this->cancel_item_locked_(
component, name_type, static_name, hash_or_id,
type, match_retry);
666bool HOT Scheduler::cancel_item_locked_(Component *
component, NameType name_type,
const char *static_name,
667 uint32_t hash_or_id, SchedulerItem::Type
type,
bool match_retry) {
669 if (name_type == NameType::STATIC_STRING && static_name ==
nullptr) {
673 size_t total_cancelled = 0;
675#ifndef ESPHOME_THREAD_SINGLE
677 if (
type == SchedulerItem::TIMEOUT) {
678 total_cancelled += this->mark_matching_items_removed_locked_(this->defer_queue_,
component, name_type, static_name,
679 hash_or_id,
type, match_retry);
688 if (!this->items_.empty()) {
689 size_t heap_cancelled = this->mark_matching_items_removed_locked_(this->items_,
component, name_type, static_name,
690 hash_or_id,
type, match_retry);
691 total_cancelled += heap_cancelled;
692 this->to_remove_ += heap_cancelled;
696 total_cancelled += this->mark_matching_items_removed_locked_(this->to_add_,
component, name_type, static_name,
697 hash_or_id,
type, match_retry);
699 return total_cancelled > 0;
702bool HOT Scheduler::SchedulerItem::cmp(
const SchedulerItemPtr &a,
const SchedulerItemPtr &b) {
705 return (a->next_execution_high_ ==
b->next_execution_high_) ? (a->next_execution_low_ >
b->next_execution_low_)
706 : (a->next_execution_high_ >
b->next_execution_high_);
713void Scheduler::recycle_item_main_loop_(SchedulerItemPtr item) {
717 if (this->scheduler_item_pool_.size() < MAX_POOL_SIZE) {
719 item->callback =
nullptr;
720 this->scheduler_item_pool_.push_back(std::move(item));
721#ifdef ESPHOME_DEBUG_SCHEDULER
722 ESP_LOGD(TAG,
"Recycled item to pool (pool size now: %zu)", this->scheduler_item_pool_.size());
725#ifdef ESPHOME_DEBUG_SCHEDULER
726 ESP_LOGD(TAG,
"Pool full (size: %zu), deleting item", this->scheduler_item_pool_.size());
732#ifdef ESPHOME_DEBUG_SCHEDULER
733void Scheduler::debug_log_timer_(
const SchedulerItem *item, NameType name_type,
const char *static_name,
734 uint32_t hash_or_id, SchedulerItem::Type
type, uint32_t
delay, uint64_t now) {
736 if (name_type == NameType::STATIC_STRING && static_name !=
nullptr) {
737 validate_static_string(static_name);
741 SchedulerNameLog name_log;
742 const char *type_str = (
type == SchedulerItem::TIMEOUT) ?
"timeout" :
"interval";
743 if (
type == SchedulerItem::TIMEOUT) {
744 ESP_LOGD(TAG,
"set_%s(name='%s/%s', %s=%" PRIu32
")", type_str, LOG_STR_ARG(item->get_source()),
745 name_log.format(name_type, static_name, hash_or_id), type_str,
delay);
747 ESP_LOGD(TAG,
"set_%s(name='%s/%s', %s=%" PRIu32
", offset=%" PRIu32
")", type_str, LOG_STR_ARG(item->get_source()),
748 name_log.format(name_type, static_name, hash_or_id), type_str,
delay,
749 static_cast<uint32_t>(item->get_next_execution() - now));
756Scheduler::SchedulerItemPtr Scheduler::get_item_from_pool_locked_() {
757 SchedulerItemPtr item;
758 if (!this->scheduler_item_pool_.empty()) {
759 item = std::move(this->scheduler_item_pool_.back());
760 this->scheduler_item_pool_.pop_back();
761#ifdef ESPHOME_DEBUG_SCHEDULER
762 ESP_LOGD(TAG,
"Reused item from pool (pool size now: %zu)", this->scheduler_item_pool_.size());
765 item = SchedulerItemPtr(
new SchedulerItem());
766#ifdef ESPHOME_DEBUG_SCHEDULER
767 ESP_LOGD(TAG,
"Allocated new item (pool empty)");
void set_current_component(Component *component)
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> float backoff_increase_factor
const Component * component
Providing packet encoding functions for exchanging data with a remote host.
float random_float()
Return a random float between 0 and 1.
void retry_handler(const std::shared_ptr< RetryArgs > &args)
void HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint32_t fnv1a_hash(const char *str)
Calculate a FNV-1a hash of str.
constexpr uint32_t SCHEDULER_DONT_RUN