7#ifdef ESPHOME_THREAD_MULTI_ATOMICS
26 friend void ::esphome::retry_handler(
const std::shared_ptr<RetryArgs> &args);
31 template<
typename... Ts>
friend class DelayAction;
36 ESPDEPRECATED(
"Use const char* or uint32_t overload instead. Removed in 2026.7.0",
"2026.1.0")
37 void set_timeout(Component *
component, const std::
string &name,
uint32_t timeout, std::function<
void()> &&func);
47 void set_timeout(Component *
component, const
char *name,
uint32_t timeout, std::function<
void()> &&func);
51 void set_timeout(Component *
component, InternalSchedulerID
id,
uint32_t timeout, std::function<
void()> &&func) {
52 this->set_timer_common_(
component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID_INTERNAL,
nullptr,
53 static_cast<uint32_t>(
id), timeout, std::move(func));
56 ESPDEPRECATED(
"Use const char* or uint32_t overload instead. Removed in 2026.7.0",
"2026.1.0")
57 bool cancel_timeout(Component *
component, const std::
string &name);
58 bool cancel_timeout(Component *
component, const
char *name);
60 bool cancel_timeout(Component *
component, InternalSchedulerID
id) {
61 return this->cancel_item_(
component, NameType::NUMERIC_ID_INTERNAL,
nullptr,
static_cast<uint32_t>(
id),
62 SchedulerItem::TIMEOUT);
65 ESPDEPRECATED(
"Use const char* or uint32_t overload instead. Removed in 2026.7.0",
"2026.1.0")
66 void set_interval(Component *
component, const std::
string &name,
uint32_t interval, std::function<
void()> &&func);
76 void set_interval(Component *
component, const
char *name,
uint32_t interval, std::function<
void()> &&func);
80 void set_interval(Component *
component, InternalSchedulerID
id,
uint32_t interval, std::function<
void()> &&func) {
81 this->set_timer_common_(
component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID_INTERNAL,
nullptr,
82 static_cast<uint32_t>(
id), interval, std::move(func));
85 ESPDEPRECATED(
"Use const char* or uint32_t overload instead. Removed in 2026.7.0",
"2026.1.0")
86 bool cancel_interval(Component *
component, const std::
string &name);
87 bool cancel_interval(Component *
component, const
char *name);
89 bool cancel_interval(Component *
component, InternalSchedulerID
id) {
90 return this->cancel_item_(
component, NameType::NUMERIC_ID_INTERNAL,
nullptr,
static_cast<uint32_t>(
id),
91 SchedulerItem::INTERVAL);
95 ESPDEPRECATED(
"set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
97 void set_retry(Component *
component, const std::
string &name,
uint32_t initial_wait_time, uint8_t max_attempts,
98 std::function<RetryResult(uint8_t)> func,
float backoff_increase_factor = 1.0f);
100 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
102 void set_retry(Component *
component, const
char *name,
uint32_t initial_wait_time, uint8_t max_attempts,
103 std::function<RetryResult(uint8_t)> func,
float backoff_increase_factor = 1.0f);
105 ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.",
108 std::function<RetryResult(uint8_t)> func,
float backoff_increase_factor = 1.0f);
111 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
112 bool cancel_retry(Component *
component, const std::
string &name);
114 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
115 bool cancel_retry(Component *
component, const
char *name);
117 ESPDEPRECATED("cancel_retry is deprecated and will be removed in 2026.8.0.", "2026.2.0")
128 optional<uint32_t> next_schedule_in(
uint32_t now);
139 void trim_freelist();
148 inline void ESPHOME_ALWAYS_INLINE HOT process_to_add() {
149 if (this->to_add_empty_())
151 this->process_to_add_slow_path_();
157 enum class NameType : uint8_t {
161 NUMERIC_ID_INTERNAL = 3,
178 void set_timeout(
const void *self,
uint32_t timeout, std::function<
void()> &&func);
180 void set_interval(
const void *self,
uint32_t interval, std::function<
void()> &&func);
181 bool cancel_timeout(
const void *self);
182 bool cancel_interval(
const void *self);
185 struct SchedulerItem {
191 SchedulerItem *next_free;
195 const char *static_name;
207 std::function<void()> callback;
208 uint16_t next_execution_high_;
210#ifdef ESPHOME_THREAD_MULTI_ATOMICS
215 std::atomic<uint8_t> remove{0};
219 NameType name_type_ : 3;
227 NameType name_type_ : 3;
236 next_execution_low_(0),
237 next_execution_high_(0),
238#ifdef ESPHOME_THREAD_MULTI_ATOMICS
241 name_type_(NameType::STATIC_STRING),
246 name_type_(NameType::STATIC_STRING),
249 name_.static_name =
nullptr;
253 ~SchedulerItem() =
default;
256 SchedulerItem(
const SchedulerItem &) =
delete;
257 SchedulerItem &operator=(
const SchedulerItem &) =
delete;
260 SchedulerItem(SchedulerItem &&) =
delete;
261 SchedulerItem &operator=(SchedulerItem &&) =
delete;
265 const char *get_name()
const {
266 return (name_type_ == NameType::STATIC_STRING || name_type_ == NameType::SELF_POINTER) ? name_.static_name
271 uint32_t get_name_hash_or_id()
const {
272 return (name_type_ != NameType::STATIC_STRING && name_type_ != NameType::SELF_POINTER) ? name_.hash_or_id : 0;
276 NameType get_name_type()
const {
return name_type_; }
281 void set_name(NameType
type,
const char *static_name,
uint32_t hash_or_id) {
282 if (
type == NameType::STATIC_STRING ||
type == NameType::SELF_POINTER) {
283 name_.static_name = static_name;
285 name_.hash_or_id = hash_or_id;
290 static bool cmp(SchedulerItem *a, SchedulerItem *b);
295 constexpr uint64_t get_next_execution()
const {
296 return (
static_cast<uint64_t
>(next_execution_high_) << 32) | next_execution_low_;
299 constexpr void set_next_execution(uint64_t value) {
300 next_execution_low_ =
static_cast<uint32_t>(value);
303 next_execution_high_ =
static_cast<uint16_t
>(value >> 32);
305 constexpr const char *get_type_str()
const {
return (
type == TIMEOUT) ?
"timeout" :
"interval"; }
308 Component *get_component()
const {
return name_type_ == NameType::SELF_POINTER ? nullptr :
component; }
309 const LogString *get_source()
const {
311 if (name_type_ == NameType::SELF_POINTER)
313 return component !=
nullptr ?
component->get_component_log_str() : LOG_STR(
"unknown");
320 void set_timer_common_(Component *
component, SchedulerItem::Type
type, NameType name_type,
const char *static_name,
322 bool skip_cancel =
false,
const LogString *source =
nullptr);
326#pragma GCC diagnostic push
327#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
328 void set_retry_common_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
329 uint32_t initial_wait_time, uint8_t max_attempts, std::function<
RetryResult(uint8_t)> func,
330 float backoff_increase_factor);
331#pragma GCC diagnostic pop
333 bool cancel_retry_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id);
344 uint64_t ESPHOME_ALWAYS_INLINE millis_64_from_(
uint32_t now) {
345#ifdef USE_NATIVE_64BIT_TIME
349 return Millis64Impl::compute(now);
360 inline bool ESPHOME_ALWAYS_INLINE HOT cleanup_() {
361 if (this->to_remove_empty_())
362 return !this->items_.empty();
363 return this->cleanup_slow_path_();
366 bool cleanup_slow_path_();
368 void process_to_add_slow_path_();
372 SchedulerItem *pop_raw_locked_();
375 SchedulerItem *get_item_from_pool_locked_();
380 void shrink_scheduler_vector_(std::vector<SchedulerItem *> *v);
388 bool cancel_item_locked_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
389 SchedulerItem::Type
type,
bool match_retry =
false,
bool find_first =
false);
392 bool cancel_item_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
393 SchedulerItem::Type
type,
bool match_retry =
false);
396 inline bool HOT names_match_static_(
const char *name1,
const char *name2)
const {
401 return (name1 !=
nullptr && name2 !=
nullptr) && ((name1 == name2) || (strcmp(name1, name2) == 0));
407 inline bool HOT matches_item_locked_(SchedulerItem *item, Component *
component, NameType name_type,
408 const char *static_name,
uint32_t hash_or_id, SchedulerItem::Type
type,
409 bool match_retry,
bool skip_removed =
true)
const {
417 if (item->get_component() !=
component || item->type !=
type ||
418 (skip_removed && this->is_item_removed_locked_(item)) || (match_retry && !item->is_retry)) {
422 if (item->get_name_type() != name_type)
426 if (name_type == NameType::STATIC_STRING) {
427 return this->names_match_static_(item->get_name(), static_name);
429 if (name_type == NameType::SELF_POINTER) {
430 return item->name_.static_name == static_name;
432 return item->get_name_hash_or_id() == hash_or_id;
440 bool is_item_failed_(SchedulerItem *item)
const {
441 Component *
component = item->get_component();
446 bool should_skip_item_(SchedulerItem *item)
const {
return is_item_removed_(item) || this->is_item_failed_(item); }
454 void recycle_item_main_loop_(SchedulerItem *item);
457 void full_cleanup_removed_items_();
468 is_retry_cancelled_locked_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id);
470#ifdef ESPHOME_DEBUG_SCHEDULER
472 void debug_log_timer_(
const SchedulerItem *item, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
476#ifndef ESPHOME_THREAD_SINGLE
480 inline void ESPHOME_ALWAYS_INLINE HOT process_defer_queue_(
uint32_t &now) {
483 if (this->defer_empty_())
485 this->process_defer_queue_slow_path_(now);
489 void process_defer_queue_slow_path_(
uint32_t &now);
495 inline void cleanup_defer_queue_locked_() {
497 if (this->defer_queue_front_ >= this->defer_queue_.size()) {
499 this->defer_queue_.clear();
503 this->compact_defer_queue_locked_();
505 this->defer_queue_front_ = 0;
511 void __attribute__((noinline)) compact_defer_queue_locked_();
518 bool is_item_removed_(SchedulerItem *item)
const {
519#ifdef ESPHOME_THREAD_MULTI_ATOMICS
521 return item->remove.load(std::memory_order_acquire);
533 bool is_item_removed_locked_(SchedulerItem *item)
const {
534#ifdef ESPHOME_THREAD_MULTI_ATOMICS
536 return item->remove.load(std::memory_order_relaxed);
546 void set_item_removed_(SchedulerItem *item,
bool removed) {
547#ifdef ESPHOME_THREAD_MULTI_ATOMICS
551 item->remove.store(removed ? 1 : 0, removed ? std::memory_order_release : std::memory_order_relaxed);
556 item->remove = removed;
569 inline size_t HOT mark_matching_items_removed_locked_(std::vector<SchedulerItem *> &container, Component *
component,
570 NameType name_type,
const char *static_name,
571 uint32_t hash_or_id, SchedulerItem::Type
type,
bool match_retry,
572 bool find_first =
false) {
573 if (container.empty())
575 return this->mark_matching_items_removed_slow_locked_(container,
component, name_type, static_name, hash_or_id,
576 type, match_retry, find_first);
581 __attribute__((noinline))
size_t mark_matching_items_removed_slow_locked_(
582 std::vector<SchedulerItem *> &container, Component *
component, NameType name_type,
const char *static_name,
583 uint32_t hash_or_id, SchedulerItem::Type
type,
bool match_retry,
bool find_first);
586 std::vector<SchedulerItem *> items_;
587 std::vector<SchedulerItem *> to_add_;
589#ifndef ESPHOME_THREAD_SINGLE
597#ifdef ESPHOME_THREAD_MULTI_ATOMICS
598 std::atomic<uint32_t> to_add_count_{0};
605 bool to_add_empty_()
const {
606#ifdef ESPHOME_THREAD_SINGLE
607 return this->to_add_.empty();
608#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
609 return this->to_add_count_.load(std::memory_order_relaxed) == 0;
611 return __atomic_load_n(&this->to_add_count_, __ATOMIC_RELAXED) == 0;
620 void to_add_count_increment_locked_() {
621#if defined(ESPHOME_THREAD_SINGLE)
623#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
624 this->to_add_count_.fetch_add(1, std::memory_order_relaxed);
626 uint32_t v = __atomic_load_n(&this->to_add_count_, __ATOMIC_RELAXED);
627 __atomic_store_n(&this->to_add_count_, v + 1, __ATOMIC_RELAXED);
632 void to_add_count_clear_locked_() {
633#if defined(ESPHOME_THREAD_SINGLE)
635#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
636 this->to_add_count_.store(0, std::memory_order_relaxed);
638 __atomic_store_n(&this->to_add_count_, 0, __ATOMIC_RELAXED);
642#ifndef ESPHOME_THREAD_SINGLE
646 std::vector<SchedulerItem *> defer_queue_;
647 size_t defer_queue_front_{0};
651#ifdef ESPHOME_THREAD_MULTI_ATOMICS
652 std::atomic<uint32_t> defer_count_{0};
657 bool defer_empty_()
const {
659#ifdef ESPHOME_THREAD_MULTI_ATOMICS
660 return this->defer_count_.load(std::memory_order_relaxed) == 0;
662 return __atomic_load_n(&this->defer_count_, __ATOMIC_RELAXED) == 0;
666 void defer_count_increment_locked_() {
667#ifdef ESPHOME_THREAD_MULTI_ATOMICS
668 this->defer_count_.fetch_add(1, std::memory_order_relaxed);
670 uint32_t v = __atomic_load_n(&this->defer_count_, __ATOMIC_RELAXED);
671 __atomic_store_n(&this->defer_count_, v + 1, __ATOMIC_RELAXED);
675 void defer_count_clear_locked_() {
676#ifdef ESPHOME_THREAD_MULTI_ATOMICS
677 this->defer_count_.store(0, std::memory_order_relaxed);
679 __atomic_store_n(&this->defer_count_, 0, __ATOMIC_RELAXED);
688#ifdef ESPHOME_THREAD_MULTI_ATOMICS
689 std::atomic<uint32_t> to_remove_{0};
695 bool to_remove_empty_()
const {
696#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
697 return this->to_remove_.load(std::memory_order_relaxed) == 0;
698#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
699 return __atomic_load_n(&this->to_remove_, __ATOMIC_RELAXED) == 0;
701 return this->to_remove_ == 0;
705 void to_remove_add_locked_(
uint32_t count) {
706#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
707 this->to_remove_.fetch_add(count, std::memory_order_relaxed);
708#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
709 uint32_t v = __atomic_load_n(&this->to_remove_, __ATOMIC_RELAXED);
710 __atomic_store_n(&this->to_remove_, v + count, __ATOMIC_RELAXED);
712 this->to_remove_ += count;
716 void to_remove_decrement_locked_() {
717#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
718 this->to_remove_.fetch_sub(1, std::memory_order_relaxed);
719#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
720 uint32_t v = __atomic_load_n(&this->to_remove_, __ATOMIC_RELAXED);
721 __atomic_store_n(&this->to_remove_, v - 1, __ATOMIC_RELAXED);
727 void to_remove_clear_locked_() {
728#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
729 this->to_remove_.store(0, std::memory_order_relaxed);
730#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
731 __atomic_store_n(&this->to_remove_, 0, __ATOMIC_RELAXED);
733 this->to_remove_ = 0;
738#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
739 return this->to_remove_.load(std::memory_order_relaxed);
740#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
741 return __atomic_load_n(&this->to_remove_, __ATOMIC_RELAXED);
743 return this->to_remove_;
750 SchedulerItem *scheduler_item_pool_head_{
nullptr};
751 size_t scheduler_item_pool_size_{0};
753#ifdef ESPHOME_DEBUG_SCHEDULER
757 size_t debug_live_items_{0};
761 bool debug_verify_no_leak_()
const;
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
const Component * component
void delay(unsigned long ms)
void retry_handler(const std::shared_ptr< RetryArgs > &args)
const char int const __FlashStringHelper va_list args