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);
142 inline void HOT process_to_add() {
143 if (this->to_add_empty_())
145 this->process_to_add_slow_path_();
150 enum class NameType : uint8_t {
154 NUMERIC_ID_INTERNAL = 3
158 struct SchedulerItem {
163 const char *static_name;
175 std::function<void()> callback;
176 uint16_t next_execution_high_;
178#ifdef ESPHOME_THREAD_MULTI_ATOMICS
183 std::atomic<uint8_t> remove{0};
187 NameType name_type_ : 2;
195 NameType name_type_ : 2;
204 next_execution_low_(0),
205 next_execution_high_(0),
206#ifdef ESPHOME_THREAD_MULTI_ATOMICS
209 name_type_(NameType::STATIC_STRING),
214 name_type_(NameType::STATIC_STRING),
217 name_.static_name =
nullptr;
221 ~SchedulerItem() =
default;
224 SchedulerItem(
const SchedulerItem &) =
delete;
225 SchedulerItem &operator=(
const SchedulerItem &) =
delete;
228 SchedulerItem(SchedulerItem &&) =
delete;
229 SchedulerItem &operator=(SchedulerItem &&) =
delete;
232 const char *get_name()
const {
return (name_type_ == NameType::STATIC_STRING) ? name_.static_name :
nullptr; }
235 uint32_t get_name_hash_or_id()
const {
return (name_type_ != NameType::STATIC_STRING) ? name_.hash_or_id : 0; }
238 NameType get_name_type()
const {
return name_type_; }
242 void set_name(NameType
type,
const char *static_name,
uint32_t hash_or_id) {
243 if (
type == NameType::STATIC_STRING) {
244 name_.static_name = static_name;
246 name_.hash_or_id = hash_or_id;
251 static bool cmp(SchedulerItem *a, SchedulerItem *b);
256 constexpr uint64_t get_next_execution()
const {
257 return (
static_cast<uint64_t
>(next_execution_high_) << 32) | next_execution_low_;
260 constexpr void set_next_execution(uint64_t value) {
261 next_execution_low_ =
static_cast<uint32_t>(value);
264 next_execution_high_ =
static_cast<uint16_t
>(value >> 32);
266 constexpr const char *get_type_str()
const {
return (
type == TIMEOUT) ?
"timeout" :
"interval"; }
267 const LogString *get_source()
const {
return component ?
component->get_component_log_str() : LOG_STR(
"unknown"); }
272 void set_timer_common_(Component *
component, SchedulerItem::Type
type, NameType name_type,
const char *static_name,
273 uint32_t hash_or_id,
uint32_t delay, std::function<
void()> &&func,
bool is_retry =
false,
274 bool skip_cancel =
false);
278#pragma GCC diagnostic push
279#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
280 void set_retry_common_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
281 uint32_t initial_wait_time, uint8_t max_attempts, std::function<
RetryResult(uint8_t)> func,
282 float backoff_increase_factor);
283#pragma GCC diagnostic pop
285 bool cancel_retry_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id);
290 uint64_t millis_64_from_(
uint32_t now) {
291#ifdef USE_NATIVE_64BIT_TIME
295 return Millis64Impl::compute(now);
306 inline bool HOT cleanup_() {
307 if (this->to_remove_empty_())
308 return !this->items_.empty();
309 return this->cleanup_slow_path_();
312 bool cleanup_slow_path_();
314 void process_to_add_slow_path_();
318 SchedulerItem *pop_raw_locked_();
321 SchedulerItem *get_item_from_pool_locked_();
330 bool cancel_item_locked_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
331 SchedulerItem::Type
type,
bool match_retry =
false,
bool find_first =
false);
334 bool cancel_item_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
335 SchedulerItem::Type
type,
bool match_retry =
false);
338 inline bool HOT names_match_static_(
const char *name1,
const char *name2)
const {
343 return (name1 !=
nullptr && name2 !=
nullptr) && ((name1 == name2) || (strcmp(name1, name2) == 0));
349 inline bool HOT matches_item_locked_(SchedulerItem *item, Component *
component, NameType name_type,
350 const char *static_name,
uint32_t hash_or_id, SchedulerItem::Type
type,
351 bool match_retry,
bool skip_removed =
true)
const {
357 if (item->component !=
component || item->type !=
type || (skip_removed && this->is_item_removed_locked_(item)) ||
358 (match_retry && !item->is_retry)) {
362 if (item->get_name_type() != name_type)
365 if (name_type == NameType::STATIC_STRING) {
366 return this->names_match_static_(item->get_name(), static_name);
368 return item->get_name_hash_or_id() == hash_or_id;
375 bool should_skip_item_(SchedulerItem *item)
const {
376 return is_item_removed_(item) || (item->component !=
nullptr && item->component->is_failed());
385 void recycle_item_main_loop_(SchedulerItem *item);
388 void full_cleanup_removed_items_();
399 is_retry_cancelled_locked_(Component *
component, NameType name_type,
const char *static_name,
uint32_t hash_or_id);
401#ifdef ESPHOME_DEBUG_SCHEDULER
403 void debug_log_timer_(
const SchedulerItem *item, NameType name_type,
const char *static_name,
uint32_t hash_or_id,
404 SchedulerItem::Type
type,
uint32_t delay, uint64_t now);
407#ifndef ESPHOME_THREAD_SINGLE
411 inline void HOT process_defer_queue_(
uint32_t &now) {
414 if (this->defer_empty_())
416 this->process_defer_queue_slow_path_(now);
420 void process_defer_queue_slow_path_(
uint32_t &now);
426 inline void cleanup_defer_queue_locked_() {
428 if (this->defer_queue_front_ >= this->defer_queue_.size()) {
430 this->defer_queue_.clear();
434 this->compact_defer_queue_locked_();
436 this->defer_queue_front_ = 0;
442 void __attribute__((noinline)) compact_defer_queue_locked_();
449 bool is_item_removed_(SchedulerItem *item)
const {
450#ifdef ESPHOME_THREAD_MULTI_ATOMICS
452 return item->remove.load(std::memory_order_acquire);
464 bool is_item_removed_locked_(SchedulerItem *item)
const {
465#ifdef ESPHOME_THREAD_MULTI_ATOMICS
467 return item->remove.load(std::memory_order_relaxed);
477 void set_item_removed_(SchedulerItem *item,
bool removed) {
478#ifdef ESPHOME_THREAD_MULTI_ATOMICS
482 item->remove.store(removed ? 1 : 0, removed ? std::memory_order_release : std::memory_order_relaxed);
487 item->remove = removed;
500 inline size_t HOT mark_matching_items_removed_locked_(std::vector<SchedulerItem *> &container, Component *
component,
501 NameType name_type,
const char *static_name,
502 uint32_t hash_or_id, SchedulerItem::Type
type,
bool match_retry,
503 bool find_first =
false) {
504 if (container.empty())
506 return this->mark_matching_items_removed_slow_locked_(container,
component, name_type, static_name, hash_or_id,
507 type, match_retry, find_first);
512 __attribute__((noinline))
size_t mark_matching_items_removed_slow_locked_(
513 std::vector<SchedulerItem *> &container, Component *
component, NameType name_type,
const char *static_name,
514 uint32_t hash_or_id, SchedulerItem::Type
type,
bool match_retry,
bool find_first);
517 std::vector<SchedulerItem *> items_;
518 std::vector<SchedulerItem *> to_add_;
520#ifndef ESPHOME_THREAD_SINGLE
526#ifdef ESPHOME_THREAD_MULTI_ATOMICS
527 std::atomic<uint32_t> to_add_count_{0};
538 bool to_add_empty_()
const {
539#ifdef ESPHOME_THREAD_SINGLE
540 return this->to_add_.empty();
541#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
542 return this->to_add_count_.load(std::memory_order_relaxed) == 0;
549 void to_add_count_increment_() {
550#ifdef ESPHOME_THREAD_SINGLE
552#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
553 this->to_add_count_.fetch_add(1, std::memory_order_relaxed);
555 this->to_add_count_++;
560 void to_add_count_clear_() {
561#ifdef ESPHOME_THREAD_SINGLE
563#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
564 this->to_add_count_.store(0, std::memory_order_relaxed);
566 this->to_add_count_ = 0;
570#ifndef ESPHOME_THREAD_SINGLE
574 std::vector<SchedulerItem *> defer_queue_;
575 size_t defer_queue_front_{0};
578#ifdef ESPHOME_THREAD_MULTI_ATOMICS
579 std::atomic<uint32_t> defer_count_{0};
584 bool defer_empty_()
const {
587#ifdef ESPHOME_THREAD_MULTI_ATOMICS
588 return this->defer_count_.load(std::memory_order_relaxed) == 0;
594 void defer_count_increment_() {
595#ifdef ESPHOME_THREAD_MULTI_ATOMICS
596 this->defer_count_.fetch_add(1, std::memory_order_relaxed);
598 this->defer_count_++;
602 void defer_count_clear_() {
603#ifdef ESPHOME_THREAD_MULTI_ATOMICS
604 this->defer_count_.store(0, std::memory_order_relaxed);
606 this->defer_count_ = 0;
615#ifdef ESPHOME_THREAD_MULTI_ATOMICS
616 std::atomic<uint32_t> to_remove_{0};
622 bool to_remove_empty_()
const {
623#ifdef ESPHOME_THREAD_MULTI_ATOMICS
624 return this->to_remove_.load(std::memory_order_relaxed) == 0;
625#elif defined(ESPHOME_THREAD_SINGLE)
626 return this->to_remove_ == 0;
632 void to_remove_add_(
uint32_t count) {
633#ifdef ESPHOME_THREAD_MULTI_ATOMICS
634 this->to_remove_.fetch_add(count, std::memory_order_relaxed);
636 this->to_remove_ += count;
640 void to_remove_decrement_() {
641#ifdef ESPHOME_THREAD_MULTI_ATOMICS
642 this->to_remove_.fetch_sub(1, std::memory_order_relaxed);
648 void to_remove_clear_() {
649#ifdef ESPHOME_THREAD_MULTI_ATOMICS
650 this->to_remove_.store(0, std::memory_order_relaxed);
652 this->to_remove_ = 0;
657#ifdef ESPHOME_THREAD_MULTI_ATOMICS
658 return this->to_remove_.load(std::memory_order_relaxed);
660 return this->to_remove_;
672 std::vector<SchedulerItem *> scheduler_item_pool_;
674#ifdef ESPHOME_DEBUG_SCHEDULER
678 size_t debug_live_items_{0};
682 bool debug_verify_no_leak_()
const;
struct @65::@66 __attribute__
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