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 {
190 SchedulerItem *next_free;
194 const char *static_name;
206 std::function<void()> callback;
207 uint16_t next_execution_high_;
209#ifdef ESPHOME_THREAD_MULTI_ATOMICS
214 std::atomic<uint8_t> remove{0};
218 NameType name_type_ : 3;
226 NameType name_type_ : 3;
235 next_execution_low_(0),
236 next_execution_high_(0),
237#ifdef ESPHOME_THREAD_MULTI_ATOMICS
240 name_type_(NameType::STATIC_STRING),
245 name_type_(NameType::STATIC_STRING),
248 name_.static_name =
nullptr;
252 ~SchedulerItem() =
default;
255 SchedulerItem(
const SchedulerItem &) =
delete;
256 SchedulerItem &operator=(
const SchedulerItem &) =
delete;
259 SchedulerItem(SchedulerItem &&) =
delete;
260 SchedulerItem &operator=(SchedulerItem &&) =
delete;
264 const char *get_name()
const {
265 return (name_type_ == NameType::STATIC_STRING || name_type_ == NameType::SELF_POINTER) ? name_.static_name
270 uint32_t get_name_hash_or_id()
const {
271 return (name_type_ != NameType::STATIC_STRING && name_type_ != NameType::SELF_POINTER) ? name_.hash_or_id : 0;
275 NameType get_name_type()
const {
return name_type_; }
280 void set_name(NameType
type,
const char *static_name,
uint32_t hash_or_id) {
281 if (
type == NameType::STATIC_STRING ||
type == NameType::SELF_POINTER) {
282 name_.static_name = static_name;
284 name_.hash_or_id = hash_or_id;
289 static bool cmp(SchedulerItem *a, SchedulerItem *b);
294 constexpr uint64_t get_next_execution()
const {
295 return (
static_cast<uint64_t
>(next_execution_high_) << 32) | next_execution_low_;
298 constexpr void set_next_execution(uint64_t value) {
299 next_execution_low_ =
static_cast<uint32_t>(value);
302 next_execution_high_ =
static_cast<uint16_t
>(value >> 32);
304 constexpr const char *get_type_str()
const {
return (
type == TIMEOUT) ?
"timeout" :
"interval"; }
305 const LogString *get_source()
const {
return component ?
component->get_component_log_str() : LOG_STR(
"unknown"); }
310 void set_timer_common_(Component *
component, SchedulerItem::Type
type, NameType name_type,
const char *static_name,
312 bool skip_cancel =
false);
316#pragma GCC diagnostic push
317#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
318 void set_retry_common_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
319 uint32_t initial_wait_time, uint8_t max_attempts, std::function<
RetryResult(uint8_t)> func,
320 float backoff_increase_factor);
321#pragma GCC diagnostic pop
323 bool cancel_retry_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id);
334 uint64_t ESPHOME_ALWAYS_INLINE millis_64_from_(
uint32_t now) {
335#ifdef USE_NATIVE_64BIT_TIME
339 return Millis64Impl::compute(now);
350 inline bool ESPHOME_ALWAYS_INLINE HOT cleanup_() {
351 if (this->to_remove_empty_())
352 return !this->items_.empty();
353 return this->cleanup_slow_path_();
356 bool cleanup_slow_path_();
358 void process_to_add_slow_path_();
362 SchedulerItem *pop_raw_locked_();
365 SchedulerItem *get_item_from_pool_locked_();
370 void shrink_scheduler_vector_(std::vector<SchedulerItem *> *v);
378 bool cancel_item_locked_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
379 SchedulerItem::Type
type,
bool match_retry =
false,
bool find_first =
false);
382 bool cancel_item_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
383 SchedulerItem::Type
type,
bool match_retry =
false);
386 inline bool HOT names_match_static_(
const char *name1,
const char *name2)
const {
391 return (name1 !=
nullptr && name2 !=
nullptr) && ((name1 == name2) || (strcmp(name1, name2) == 0));
397 inline bool HOT matches_item_locked_(SchedulerItem *item, Component *
component, NameType name_type,
398 const char *static_name,
uint32_t hash_or_id, SchedulerItem::Type
type,
399 bool match_retry,
bool skip_removed =
true)
const {
405 if (item->component !=
component || item->type !=
type || (skip_removed && this->is_item_removed_locked_(item)) ||
406 (match_retry && !item->is_retry)) {
410 if (item->get_name_type() != name_type)
414 if (name_type == NameType::STATIC_STRING) {
415 return this->names_match_static_(item->get_name(), static_name);
417 if (name_type == NameType::SELF_POINTER) {
418 return item->name_.static_name == static_name;
420 return item->get_name_hash_or_id() == hash_or_id;
427 bool should_skip_item_(SchedulerItem *item)
const {
428 return is_item_removed_(item) || (item->component !=
nullptr && item->component->is_failed());
437 void recycle_item_main_loop_(SchedulerItem *item);
440 void full_cleanup_removed_items_();
451 is_retry_cancelled_locked_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id);
453#ifdef ESPHOME_DEBUG_SCHEDULER
455 void debug_log_timer_(
const SchedulerItem *item, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
459#ifndef ESPHOME_THREAD_SINGLE
463 inline void ESPHOME_ALWAYS_INLINE HOT process_defer_queue_(
uint32_t &now) {
466 if (this->defer_empty_())
468 this->process_defer_queue_slow_path_(now);
472 void process_defer_queue_slow_path_(
uint32_t &now);
478 inline void cleanup_defer_queue_locked_() {
480 if (this->defer_queue_front_ >= this->defer_queue_.size()) {
482 this->defer_queue_.clear();
486 this->compact_defer_queue_locked_();
488 this->defer_queue_front_ = 0;
494 void __attribute__((noinline)) compact_defer_queue_locked_();
501 bool is_item_removed_(SchedulerItem *item)
const {
502#ifdef ESPHOME_THREAD_MULTI_ATOMICS
504 return item->remove.load(std::memory_order_acquire);
516 bool is_item_removed_locked_(SchedulerItem *item)
const {
517#ifdef ESPHOME_THREAD_MULTI_ATOMICS
519 return item->remove.load(std::memory_order_relaxed);
529 void set_item_removed_(SchedulerItem *item,
bool removed) {
530#ifdef ESPHOME_THREAD_MULTI_ATOMICS
534 item->remove.store(removed ? 1 : 0, removed ? std::memory_order_release : std::memory_order_relaxed);
539 item->remove = removed;
552 inline size_t HOT mark_matching_items_removed_locked_(std::vector<SchedulerItem *> &container, Component *
component,
553 NameType name_type,
const char *static_name,
554 uint32_t hash_or_id, SchedulerItem::Type
type,
bool match_retry,
555 bool find_first =
false) {
556 if (container.empty())
558 return this->mark_matching_items_removed_slow_locked_(container,
component, name_type, static_name, hash_or_id,
559 type, match_retry, find_first);
564 __attribute__((noinline))
size_t mark_matching_items_removed_slow_locked_(
565 std::vector<SchedulerItem *> &container, Component *
component, NameType name_type,
const char *static_name,
566 uint32_t hash_or_id, SchedulerItem::Type
type,
bool match_retry,
bool find_first);
569 std::vector<SchedulerItem *> items_;
570 std::vector<SchedulerItem *> to_add_;
572#ifndef ESPHOME_THREAD_SINGLE
580#ifdef ESPHOME_THREAD_MULTI_ATOMICS
581 std::atomic<uint32_t> to_add_count_{0};
588 bool to_add_empty_()
const {
589#ifdef ESPHOME_THREAD_SINGLE
590 return this->to_add_.empty();
591#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
592 return this->to_add_count_.load(std::memory_order_relaxed) == 0;
594 return __atomic_load_n(&this->to_add_count_, __ATOMIC_RELAXED) == 0;
603 void to_add_count_increment_locked_() {
604#if defined(ESPHOME_THREAD_SINGLE)
606#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
607 this->to_add_count_.fetch_add(1, std::memory_order_relaxed);
609 uint32_t v = __atomic_load_n(&this->to_add_count_, __ATOMIC_RELAXED);
610 __atomic_store_n(&this->to_add_count_, v + 1, __ATOMIC_RELAXED);
615 void to_add_count_clear_locked_() {
616#if defined(ESPHOME_THREAD_SINGLE)
618#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
619 this->to_add_count_.store(0, std::memory_order_relaxed);
621 __atomic_store_n(&this->to_add_count_, 0, __ATOMIC_RELAXED);
625#ifndef ESPHOME_THREAD_SINGLE
629 std::vector<SchedulerItem *> defer_queue_;
630 size_t defer_queue_front_{0};
634#ifdef ESPHOME_THREAD_MULTI_ATOMICS
635 std::atomic<uint32_t> defer_count_{0};
640 bool defer_empty_()
const {
642#ifdef ESPHOME_THREAD_MULTI_ATOMICS
643 return this->defer_count_.load(std::memory_order_relaxed) == 0;
645 return __atomic_load_n(&this->defer_count_, __ATOMIC_RELAXED) == 0;
649 void defer_count_increment_locked_() {
650#ifdef ESPHOME_THREAD_MULTI_ATOMICS
651 this->defer_count_.fetch_add(1, std::memory_order_relaxed);
653 uint32_t v = __atomic_load_n(&this->defer_count_, __ATOMIC_RELAXED);
654 __atomic_store_n(&this->defer_count_, v + 1, __ATOMIC_RELAXED);
658 void defer_count_clear_locked_() {
659#ifdef ESPHOME_THREAD_MULTI_ATOMICS
660 this->defer_count_.store(0, std::memory_order_relaxed);
662 __atomic_store_n(&this->defer_count_, 0, __ATOMIC_RELAXED);
671#ifdef ESPHOME_THREAD_MULTI_ATOMICS
672 std::atomic<uint32_t> to_remove_{0};
678 bool to_remove_empty_()
const {
679#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
680 return this->to_remove_.load(std::memory_order_relaxed) == 0;
681#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
682 return __atomic_load_n(&this->to_remove_, __ATOMIC_RELAXED) == 0;
684 return this->to_remove_ == 0;
688 void to_remove_add_locked_(
uint32_t count) {
689#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
690 this->to_remove_.fetch_add(count, std::memory_order_relaxed);
691#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
692 uint32_t v = __atomic_load_n(&this->to_remove_, __ATOMIC_RELAXED);
693 __atomic_store_n(&this->to_remove_, v + count, __ATOMIC_RELAXED);
695 this->to_remove_ += count;
699 void to_remove_decrement_locked_() {
700#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
701 this->to_remove_.fetch_sub(1, std::memory_order_relaxed);
702#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
703 uint32_t v = __atomic_load_n(&this->to_remove_, __ATOMIC_RELAXED);
704 __atomic_store_n(&this->to_remove_, v - 1, __ATOMIC_RELAXED);
710 void to_remove_clear_locked_() {
711#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
712 this->to_remove_.store(0, std::memory_order_relaxed);
713#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
714 __atomic_store_n(&this->to_remove_, 0, __ATOMIC_RELAXED);
716 this->to_remove_ = 0;
721#if defined(ESPHOME_THREAD_MULTI_ATOMICS)
722 return this->to_remove_.load(std::memory_order_relaxed);
723#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
724 return __atomic_load_n(&this->to_remove_, __ATOMIC_RELAXED);
726 return this->to_remove_;
733 SchedulerItem *scheduler_item_pool_head_{
nullptr};
734 size_t scheduler_item_pool_size_{0};
736#ifdef ESPHOME_DEBUG_SCHEDULER
740 size_t debug_live_items_{0};
744 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