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);
141 inline void ESPHOME_ALWAYS_INLINE HOT process_to_add() {
142 if (this->to_add_empty_())
144 this->process_to_add_slow_path_();
149 enum class NameType : uint8_t {
153 NUMERIC_ID_INTERNAL = 3
157 struct SchedulerItem {
162 const char *static_name;
174 std::function<void()> callback;
175 uint16_t next_execution_high_;
177#ifdef ESPHOME_THREAD_MULTI_ATOMICS
182 std::atomic<uint8_t> remove{0};
186 NameType name_type_ : 2;
194 NameType name_type_ : 2;
203 next_execution_low_(0),
204 next_execution_high_(0),
205#ifdef ESPHOME_THREAD_MULTI_ATOMICS
208 name_type_(NameType::STATIC_STRING),
213 name_type_(NameType::STATIC_STRING),
216 name_.static_name =
nullptr;
220 ~SchedulerItem() =
default;
223 SchedulerItem(
const SchedulerItem &) =
delete;
224 SchedulerItem &operator=(
const SchedulerItem &) =
delete;
227 SchedulerItem(SchedulerItem &&) =
delete;
228 SchedulerItem &operator=(SchedulerItem &&) =
delete;
231 const char *get_name()
const {
return (name_type_ == NameType::STATIC_STRING) ? name_.static_name :
nullptr; }
234 uint32_t get_name_hash_or_id()
const {
return (name_type_ != NameType::STATIC_STRING) ? name_.hash_or_id : 0; }
237 NameType get_name_type()
const {
return name_type_; }
241 void set_name(NameType
type,
const char *static_name,
uint32_t hash_or_id) {
242 if (
type == NameType::STATIC_STRING) {
243 name_.static_name = static_name;
245 name_.hash_or_id = hash_or_id;
250 static bool cmp(SchedulerItem *a, SchedulerItem *b);
255 constexpr uint64_t get_next_execution()
const {
256 return (
static_cast<uint64_t
>(next_execution_high_) << 32) | next_execution_low_;
259 constexpr void set_next_execution(uint64_t value) {
260 next_execution_low_ =
static_cast<uint32_t>(value);
263 next_execution_high_ =
static_cast<uint16_t
>(value >> 32);
265 constexpr const char *get_type_str()
const {
return (
type == TIMEOUT) ?
"timeout" :
"interval"; }
266 const LogString *get_source()
const {
return component ?
component->get_component_log_str() : LOG_STR(
"unknown"); }
271 void set_timer_common_(Component *
component, SchedulerItem::Type
type, NameType name_type,
const char *static_name,
272 uint32_t hash_or_id,
uint32_t delay, std::function<
void()> &&func,
bool is_retry =
false,
273 bool skip_cancel =
false);
277#pragma GCC diagnostic push
278#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
279 void set_retry_common_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
280 uint32_t initial_wait_time, uint8_t max_attempts, std::function<
RetryResult(uint8_t)> func,
281 float backoff_increase_factor);
282#pragma GCC diagnostic pop
284 bool cancel_retry_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id);
289 uint64_t ESPHOME_ALWAYS_INLINE millis_64_from_(
uint32_t now) {
290#ifdef USE_NATIVE_64BIT_TIME
294 return Millis64Impl::compute(now);
305 inline bool ESPHOME_ALWAYS_INLINE HOT cleanup_() {
306 if (this->to_remove_empty_())
307 return !this->items_.empty();
308 return this->cleanup_slow_path_();
311 bool cleanup_slow_path_();
313 void process_to_add_slow_path_();
317 SchedulerItem *pop_raw_locked_();
320 SchedulerItem *get_item_from_pool_locked_();
329 bool cancel_item_locked_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
330 SchedulerItem::Type
type,
bool match_retry =
false,
bool find_first =
false);
333 bool cancel_item_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
334 SchedulerItem::Type
type,
bool match_retry =
false);
337 inline bool HOT names_match_static_(
const char *name1,
const char *name2)
const {
342 return (name1 !=
nullptr && name2 !=
nullptr) && ((name1 == name2) || (strcmp(name1, name2) == 0));
348 inline bool HOT matches_item_locked_(SchedulerItem *item, Component *
component, NameType name_type,
349 const char *static_name,
uint32_t hash_or_id, SchedulerItem::Type
type,
350 bool match_retry,
bool skip_removed =
true)
const {
356 if (item->component !=
component || item->type !=
type || (skip_removed && this->is_item_removed_locked_(item)) ||
357 (match_retry && !item->is_retry)) {
361 if (item->get_name_type() != name_type)
364 if (name_type == NameType::STATIC_STRING) {
365 return this->names_match_static_(item->get_name(), static_name);
367 return item->get_name_hash_or_id() == hash_or_id;
374 bool should_skip_item_(SchedulerItem *item)
const {
375 return is_item_removed_(item) || (item->component !=
nullptr && item->component->is_failed());
384 void recycle_item_main_loop_(SchedulerItem *item);
387 void full_cleanup_removed_items_();
398 is_retry_cancelled_locked_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id);
400#ifdef ESPHOME_DEBUG_SCHEDULER
402 void debug_log_timer_(
const SchedulerItem *item, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
403 SchedulerItem::Type
type,
uint32_t delay, uint64_t now);
406#ifndef ESPHOME_THREAD_SINGLE
410 inline void ESPHOME_ALWAYS_INLINE HOT process_defer_queue_(
uint32_t &now) {
413 if (this->defer_empty_())
415 this->process_defer_queue_slow_path_(now);
419 void process_defer_queue_slow_path_(
uint32_t &now);
425 inline void cleanup_defer_queue_locked_() {
427 if (this->defer_queue_front_ >= this->defer_queue_.size()) {
429 this->defer_queue_.clear();
433 this->compact_defer_queue_locked_();
435 this->defer_queue_front_ = 0;
441 void __attribute__((noinline)) compact_defer_queue_locked_();
448 bool is_item_removed_(SchedulerItem *item)
const {
449#ifdef ESPHOME_THREAD_MULTI_ATOMICS
451 return item->remove.load(std::memory_order_acquire);
463 bool is_item_removed_locked_(SchedulerItem *item)
const {
464#ifdef ESPHOME_THREAD_MULTI_ATOMICS
466 return item->remove.load(std::memory_order_relaxed);
476 void set_item_removed_(SchedulerItem *item,
bool removed) {
477#ifdef ESPHOME_THREAD_MULTI_ATOMICS
481 item->remove.store(removed ? 1 : 0, removed ? std::memory_order_release : std::memory_order_relaxed);
486 item->remove = removed;
499 inline size_t HOT mark_matching_items_removed_locked_(std::vector<SchedulerItem *> &container, Component *
component,
500 NameType name_type,
const char *static_name,
501 uint32_t hash_or_id, SchedulerItem::Type
type,
bool match_retry,
502 bool find_first =
false) {
503 if (container.empty())
505 return this->mark_matching_items_removed_slow_locked_(container,
component, name_type, static_name, hash_or_id,
506 type, match_retry, find_first);
511 __attribute__((noinline))
size_t mark_matching_items_removed_slow_locked_(
512 std::vector<SchedulerItem *> &container, Component *
component, NameType name_type,
const char *static_name,
513 uint32_t hash_or_id, SchedulerItem::Type
type,
bool match_retry,
bool find_first);
516 std::vector<SchedulerItem *> items_;
517 std::vector<SchedulerItem *> to_add_;
519#ifndef ESPHOME_THREAD_SINGLE
525#ifdef ESPHOME_THREAD_MULTI_ATOMICS
526 std::atomic<uint32_t> to_add_count_{0};
537 bool to_add_empty_()
const {
538#ifdef ESPHOME_THREAD_SINGLE
539 return this->to_add_.empty();
540#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
541 return this->to_add_count_.load(std::memory_order_relaxed) == 0;
548 void to_add_count_increment_() {
549#ifdef ESPHOME_THREAD_SINGLE
551#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
552 this->to_add_count_.fetch_add(1, std::memory_order_relaxed);
554 this->to_add_count_++;
559 void to_add_count_clear_() {
560#ifdef ESPHOME_THREAD_SINGLE
562#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
563 this->to_add_count_.store(0, std::memory_order_relaxed);
565 this->to_add_count_ = 0;
569#ifndef ESPHOME_THREAD_SINGLE
573 std::vector<SchedulerItem *> defer_queue_;
574 size_t defer_queue_front_{0};
577#ifdef ESPHOME_THREAD_MULTI_ATOMICS
578 std::atomic<uint32_t> defer_count_{0};
583 bool defer_empty_()
const {
586#ifdef ESPHOME_THREAD_MULTI_ATOMICS
587 return this->defer_count_.load(std::memory_order_relaxed) == 0;
593 void defer_count_increment_() {
594#ifdef ESPHOME_THREAD_MULTI_ATOMICS
595 this->defer_count_.fetch_add(1, std::memory_order_relaxed);
597 this->defer_count_++;
601 void defer_count_clear_() {
602#ifdef ESPHOME_THREAD_MULTI_ATOMICS
603 this->defer_count_.store(0, std::memory_order_relaxed);
605 this->defer_count_ = 0;
614#ifdef ESPHOME_THREAD_MULTI_ATOMICS
615 std::atomic<uint32_t> to_remove_{0};
621 bool to_remove_empty_()
const {
622#ifdef ESPHOME_THREAD_MULTI_ATOMICS
623 return this->to_remove_.load(std::memory_order_relaxed) == 0;
624#elif defined(ESPHOME_THREAD_SINGLE)
625 return this->to_remove_ == 0;
631 void to_remove_add_(
uint32_t count) {
632#ifdef ESPHOME_THREAD_MULTI_ATOMICS
633 this->to_remove_.fetch_add(count, std::memory_order_relaxed);
635 this->to_remove_ += count;
639 void to_remove_decrement_() {
640#ifdef ESPHOME_THREAD_MULTI_ATOMICS
641 this->to_remove_.fetch_sub(1, std::memory_order_relaxed);
647 void to_remove_clear_() {
648#ifdef ESPHOME_THREAD_MULTI_ATOMICS
649 this->to_remove_.store(0, std::memory_order_relaxed);
651 this->to_remove_ = 0;
656#ifdef ESPHOME_THREAD_MULTI_ATOMICS
657 return this->to_remove_.load(std::memory_order_relaxed);
659 return this->to_remove_;
671 std::vector<SchedulerItem *> scheduler_item_pool_;
673#ifdef ESPHOME_DEBUG_SCHEDULER
677 size_t debug_live_items_{0};
681 bool debug_verify_no_leak_()
const;
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
const Component * component
ESPDEPRECATED("Use modbus::helpers::value_type_is_float() instead. Removed in 2026.10.0", "2026.4.0") inline bool value_type_is_float(SensorValueType v)
Providing packet encoding functions for exchanging data with a remote host.
void retry_handler(const std::shared_ptr< RetryArgs > &args)
const char int const __FlashStringHelper va_list args