ESPHome 2026.6.0
Loading...
Searching...
No Matches
scheduler.cpp
Go to the documentation of this file.
1#include "scheduler.h"
2
3#include "application.h"
5#include "esphome/core/hal.h"
7#include "esphome/core/log.h"
9#include <algorithm>
10#include <cinttypes>
11#include <cstring>
12
13namespace esphome {
14
15static const char *const TAG = "scheduler";
16
17// Maximum number of logically deleted (cancelled) items before forcing cleanup.
18// Empirically chosen to balance cleanup overhead against tombstone accumulation in items_.
19static constexpr uint32_t MAX_LOGICALLY_DELETED_ITEMS = 5;
20// max delay to start an interval sequence
21static constexpr uint32_t MAX_INTERVAL_DELAY = 5000;
22
23#if defined(ESPHOME_LOG_HAS_VERBOSE) || defined(ESPHOME_DEBUG_SCHEDULER)
24// Helper struct for formatting scheduler item names consistently in logs
25// Uses a stack buffer to avoid heap allocation
26// Uses ESPHOME_snprintf_P/ESPHOME_PSTR for ESP8266 to keep format strings in flash
27struct SchedulerNameLog {
28 // Sized for the widest formatted output: "self:0x" + 16 hex digits (64-bit pointer) + nul.
29 // Also covers "id:4294967295", "hash:0xFFFFFFFF", "iid:4294967295", "(null)".
30 char buffer[28];
31
32 // Format a scheduler item name for logging
33 // Returns pointer to formatted string (either static_name or internal buffer)
34 const char *format(Scheduler::NameType name_type, const char *static_name, uint32_t hash_or_id) {
35 using NameType = Scheduler::NameType;
36 if (name_type == NameType::STATIC_STRING) {
37 if (static_name)
38 return static_name;
39 // Copy "(null)" to buffer to keep it in flash on ESP8266
40 ESPHOME_strncpy_P(buffer, ESPHOME_PSTR("(null)"), sizeof(buffer));
41 return buffer;
42 } else if (name_type == NameType::HASHED_STRING) {
43 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("hash:0x%08" PRIX32), hash_or_id);
44 return buffer;
45 } else if (name_type == NameType::NUMERIC_ID) {
46 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("id:%" PRIu32), hash_or_id);
47 return buffer;
48 } else if (name_type == NameType::NUMERIC_ID_INTERNAL) {
49 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("iid:%" PRIu32), hash_or_id);
50 return buffer;
51 } else { // SELF_POINTER
52 // static_name carries the void* key for SELF_POINTER (pointer-width union slot).
53 // %p is specified as void* (not const void*), so strip const for the varargs call.
54 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("self:%p"),
55 const_cast<void *>(static_cast<const void *>(static_name)));
56 return buffer;
57 }
58 }
59};
60#endif
61
62// Uncomment to debug scheduler
63// #define ESPHOME_DEBUG_SCHEDULER
64
65#ifdef ESPHOME_DEBUG_SCHEDULER
66// Helper to validate that a pointer looks like it's in static memory
67static void validate_static_string(const char *name) {
68 if (name == nullptr)
69 return;
70
71 // This is a heuristic check - stack and heap pointers are typically
72 // much higher in memory than static data
73 uintptr_t addr = reinterpret_cast<uintptr_t>(name);
74
75 // Create a stack variable to compare against
76 int stack_var;
77 uintptr_t stack_addr = reinterpret_cast<uintptr_t>(&stack_var);
78
79 // If the string pointer is near our stack variable, it's likely on the stack
80 // Using 8KB range as ESP32 main task stack is typically 8192 bytes
81 if (addr > (stack_addr - 0x2000) && addr < (stack_addr + 0x2000)) {
82 ESP_LOGW(TAG,
83 "WARNING: Scheduler name '%s' at %p appears to be on the stack - this is unsafe!\n"
84 " Stack reference at %p",
85 name, name, &stack_var);
86 }
87
88 // Also check if it might be on the heap by seeing if it's in a very different range
89 // This is platform-specific but generally heap is allocated far from static memory
90 static const char *static_str = "test";
91 uintptr_t static_addr = reinterpret_cast<uintptr_t>(static_str);
92
93 // If the address is very far from known static memory, it might be heap
94 if (addr > static_addr + 0x100000 || (static_addr > 0x100000 && addr < static_addr - 0x100000)) {
95 ESP_LOGW(TAG, "WARNING: Scheduler name '%s' at %p might be on heap (static ref at %p)", name, name, static_str);
96 }
97}
98#endif /* ESPHOME_DEBUG_SCHEDULER */
99
100// A note on locking: the `lock_` lock protects the `items_` and `to_add_` containers. It must be taken when writing to
101// them (i.e. when adding/removing items, but not when changing items). As items are only deleted from the loop task,
102// iterating over them from the loop task is fine; but iterating from any other context requires the lock to be held to
103// avoid the main thread modifying the list while it is being accessed.
104
105// Calculate random offset for interval timers
106// Extracted from set_timer_common_ to reduce code size - only needed for intervals, not timeouts
107uint32_t Scheduler::calculate_interval_offset_(uint32_t delay) {
108 uint32_t max_offset = std::min(delay / 2, MAX_INTERVAL_DELAY);
109 // Multiply-and-shift: uniform random in [0, max_offset) without floating point
110 return static_cast<uint32_t>((static_cast<uint64_t>(random_uint32()) * max_offset) >> 32);
111}
112
113// Check if a retry was already cancelled in items_ or to_add_
114// Extracted from set_timer_common_ to reduce code size - retry path is cold and deprecated
115// Remove before 2026.8.0 along with all retry code
116bool Scheduler::is_retry_cancelled_locked_(Component *component, NameType name_type, const char *static_name,
117 uint32_t hash_or_id) {
118 for (auto *container : {&this->items_, &this->to_add_}) {
119 for (auto *item : *container) {
120 if (item != nullptr && this->is_item_removed_locked_(item) &&
121 this->matches_item_locked_(item, component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
122 /* match_retry= */ true, /* skip_removed= */ false)) {
123 return true;
124 }
125 }
126 }
127 return false;
128}
129
130// Common implementation for both timeout and interval
131// name_type determines storage type: STATIC_STRING uses static_name, others use hash_or_id
132void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type type, NameType name_type,
133 const char *static_name, uint32_t hash_or_id, uint32_t delay,
134 std::function<void()> &&func, bool is_retry, bool skip_cancel,
135 const LogString *source) {
136 if (delay == SCHEDULER_DONT_RUN) {
137 // Still need to cancel existing timer if we have a name/id
138 if (!skip_cancel) {
139 LockGuard guard{this->lock_};
140 this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type, /* match_retry= */ false,
141 /* find_first= */ true);
142 }
143 return;
144 }
145
146 // An interval of 0 means "fire every tick forever," which is misuse: the
147 // item would always be due, causing Scheduler::call() to spin and starve
148 // the main loop (WDT reset in the field). Coerce to 1ms so existing code
149 // using update_interval=0ms as a pseudo-loop() continues to work at ~1kHz,
150 // and warn so authors can migrate to HighFrequencyLoopRequester which is
151 // the intended mechanism for running fast in the main loop. Zero-delay
152 // timeouts (defer) remain legitimate one-shots and are not affected.
153 if (type == SchedulerItem::INTERVAL && delay == 0) [[unlikely]] {
154 ESP_LOGE(TAG, "[%s] set_interval(0) would spin main loop - coercing to 1ms (use HighFrequencyLoopRequester)",
155 component ? LOG_STR_ARG(component->get_component_log_str()) : LOG_STR_LITERAL("?"));
156 delay = 1;
157 }
158
159 // Take lock early to protect scheduler_item_pool_head_ access and retry-cancelled check
160 LockGuard guard{this->lock_};
161
162 // For retries, check if there's a cancelled timeout first - before allocating an item.
163 // Skip check for anonymous retries (STATIC_STRING with nullptr) - they can't be cancelled by name
164 // Skip check for defer (delay=0) - deferred retries bypass the cancellation check
165 if (is_retry && delay != 0 && (name_type != NameType::STATIC_STRING || static_name != nullptr) &&
166 type == SchedulerItem::TIMEOUT &&
167 this->is_retry_cancelled_locked_(component, name_type, static_name, hash_or_id)) {
168#ifdef ESPHOME_DEBUG_SCHEDULER
169 SchedulerNameLog skip_name_log;
170 ESP_LOGD(TAG, "Skipping retry '%s' - found cancelled item",
171 skip_name_log.format(name_type, static_name, hash_or_id));
172#endif
173 return;
174 }
175
176 // Create and populate the scheduler item
177 SchedulerItem *item = this->get_item_from_pool_locked_();
178 // SELF_POINTER items store the source name (owning script) in the union slot instead of a component.
179 if (name_type == NameType::SELF_POINTER) {
180 item->source_name = source;
181 } else {
182 item->component = component;
183 }
184 item->set_name(name_type, static_name, hash_or_id);
185 item->type = type;
186 // Use destroy + placement-new instead of move-assignment.
187 // GCC's std::function::operator=(function&&) does a full swap dance even when the
188 // target is empty. Since recycled/new items always have an empty callback, we can
189 // destroy the empty one (no-op) and move-construct directly, saving ~40 bytes of
190 // swap/destructor code on Xtensa.
191 item->callback.~function();
192 new (&item->callback) std::function<void()>(std::move(func));
193 // Reset remove flag - recycled items may have been cancelled (remove=true) in previous use
194 this->set_item_removed_(item, false);
195 item->is_retry = is_retry;
196
197 // Determine target container: defer_queue_ for deferred items, to_add_ for everything else.
198 // Using a pointer lets both paths share the cancel + push_back epilogue.
199 auto *target = &this->to_add_;
200
201#ifndef ESPHOME_THREAD_SINGLE
202 // Special handling for defer() (delay = 0, type = TIMEOUT)
203 // Single-core platforms don't need thread-safe defer handling
204 if (delay == 0 && type == SchedulerItem::TIMEOUT) {
205 // Put in defer queue for guaranteed FIFO execution
206 target = &this->defer_queue_;
207 } else
208#endif /* not ESPHOME_THREAD_SINGLE */
209 {
210 // Only non-defer items need a timestamp for scheduling
211 const uint64_t now_64 = millis_64();
212
213 // Type-specific setup
214 if (type == SchedulerItem::INTERVAL) {
215 item->interval = delay;
216 // first execution happens immediately after a random smallish offset
217 uint32_t offset = this->calculate_interval_offset_(delay);
218 item->set_next_execution(now_64 + offset);
219#ifdef ESPHOME_LOG_HAS_VERBOSE
220 SchedulerNameLog name_log;
221 ESP_LOGV(TAG, "Scheduler interval for %s is %" PRIu32 "ms, offset %" PRIu32 "ms",
222 name_log.format(name_type, static_name, hash_or_id), delay, offset);
223#endif
224 } else {
225 item->interval = 0;
226 item->set_next_execution(now_64 + delay);
227 }
228
229#ifdef ESPHOME_DEBUG_SCHEDULER
230 this->debug_log_timer_(item, name_type, static_name, hash_or_id, type, delay, now_64);
231#endif /* ESPHOME_DEBUG_SCHEDULER */
232 }
233
234 // Common epilogue: atomic cancel-and-add (unless skip_cancel is true or anonymous)
235 // Anonymous items (STATIC_STRING with nullptr) can never match anything, so skip the scan.
236 if (!skip_cancel && (name_type != NameType::STATIC_STRING || static_name != nullptr)) {
237 this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type, /* match_retry= */ false,
238 /* find_first= */ true);
239 }
240 target->push_back(item);
241 if (target == &this->to_add_) {
242 this->to_add_count_increment_locked_();
243 }
244#ifndef ESPHOME_THREAD_SINGLE
245 else {
246 this->defer_count_increment_locked_();
247 }
248#endif
249}
250
251void HOT Scheduler::set_timeout(Component *component, const char *name, uint32_t timeout,
252 std::function<void()> &&func) {
253 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::STATIC_STRING, name, 0, timeout,
254 std::move(func));
255}
256
257void HOT Scheduler::set_timeout(Component *component, const std::string &name, uint32_t timeout,
258 std::function<void()> &&func) {
259 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
260 timeout, std::move(func));
261}
262void HOT Scheduler::set_timeout(Component *component, uint32_t id, uint32_t timeout, std::function<void()> &&func) {
263 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID, nullptr, id, timeout,
264 std::move(func));
265}
266bool HOT Scheduler::cancel_timeout(Component *component, const std::string &name) {
267 return this->cancel_item_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), SchedulerItem::TIMEOUT);
268}
269bool HOT Scheduler::cancel_timeout(Component *component, const char *name) {
270 return this->cancel_item_(component, NameType::STATIC_STRING, name, 0, SchedulerItem::TIMEOUT);
271}
272bool HOT Scheduler::cancel_timeout(Component *component, uint32_t id) {
273 return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::TIMEOUT);
274}
275void HOT Scheduler::set_interval(Component *component, const std::string &name, uint32_t interval,
276 std::function<void()> &&func) {
277 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
278 interval, std::move(func));
279}
280
281void HOT Scheduler::set_interval(Component *component, const char *name, uint32_t interval,
282 std::function<void()> &&func) {
283 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::STATIC_STRING, name, 0, interval,
284 std::move(func));
285}
286void HOT Scheduler::set_interval(Component *component, uint32_t id, uint32_t interval, std::function<void()> &&func) {
287 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID, nullptr, id, interval,
288 std::move(func));
289}
290bool HOT Scheduler::cancel_interval(Component *component, const std::string &name) {
291 return this->cancel_item_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), SchedulerItem::INTERVAL);
292}
293bool HOT Scheduler::cancel_interval(Component *component, const char *name) {
294 return this->cancel_item_(component, NameType::STATIC_STRING, name, 0, SchedulerItem::INTERVAL);
295}
296bool HOT Scheduler::cancel_interval(Component *component, uint32_t id) {
297 return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::INTERVAL);
298}
299
300// Self-keyed scheduler API. The cancellation key is `self` (typically the caller's `this`),
301// passed through the existing static_name pointer slot. Matching is by raw pointer equality
302// (see matches_item_locked_'s SELF_POINTER branch). No Component pointer is stored, so
303// is_failed() skip and component-based log attribution don't apply.
304void HOT Scheduler::set_timeout(const void *self, uint32_t timeout, std::function<void()> &&func) {
305 this->set_timer_common_(nullptr, SchedulerItem::TIMEOUT, NameType::SELF_POINTER, static_cast<const char *>(self), 0,
306 timeout, std::move(func));
307}
308void HOT Scheduler::set_interval(const void *self, uint32_t interval, std::function<void()> &&func) {
309 this->set_timer_common_(nullptr, SchedulerItem::INTERVAL, NameType::SELF_POINTER, static_cast<const char *>(self), 0,
310 interval, std::move(func));
311}
312bool HOT Scheduler::cancel_timeout(const void *self) {
313 return this->cancel_item_(nullptr, NameType::SELF_POINTER, static_cast<const char *>(self), 0,
314 SchedulerItem::TIMEOUT);
315}
316bool HOT Scheduler::cancel_interval(const void *self) {
317 return this->cancel_item_(nullptr, NameType::SELF_POINTER, static_cast<const char *>(self), 0,
318 SchedulerItem::INTERVAL);
319}
320
321// Suppress deprecation warnings for RetryResult usage in the still-present (but deprecated) retry implementation.
322// Remove before 2026.8.0 along with all retry code.
323#pragma GCC diagnostic push
324#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
325
326struct RetryArgs {
327 // Ordered to minimize padding on 32-bit systems
328 std::function<RetryResult(uint8_t)> func;
329 Component *component;
330 Scheduler *scheduler;
331 // Union for name storage - only one is used based on name_type
332 union {
333 const char *static_name; // For STATIC_STRING
334 uint32_t hash_or_id; // For HASHED_STRING or NUMERIC_ID
335 } name_;
336 uint32_t current_interval;
337 float backoff_increase_factor;
338 Scheduler::NameType name_type; // Discriminator for name_ union
339 uint8_t retry_countdown;
340};
341
342void retry_handler(const std::shared_ptr<RetryArgs> &args) {
343 RetryResult const retry_result = args->func(--args->retry_countdown);
344 if (retry_result == RetryResult::DONE || args->retry_countdown <= 0)
345 return;
346 // second execution of `func` happens after `initial_wait_time`
347 // args->name_ is owned by the shared_ptr<RetryArgs>
348 // which is captured in the lambda and outlives the SchedulerItem
349 const char *static_name = (args->name_type == Scheduler::NameType::STATIC_STRING) ? args->name_.static_name : nullptr;
350 uint32_t hash_or_id = (args->name_type != Scheduler::NameType::STATIC_STRING) ? args->name_.hash_or_id : 0;
351 args->scheduler->set_timer_common_(
352 args->component, Scheduler::SchedulerItem::TIMEOUT, args->name_type, static_name, hash_or_id,
353 args->current_interval, [args]() { retry_handler(args); },
354 /* is_retry= */ true);
355 // backoff_increase_factor applied to third & later executions
356 args->current_interval *= args->backoff_increase_factor;
357}
358
359void HOT Scheduler::set_retry_common_(Component *component, NameType name_type, const char *static_name,
360 uint32_t hash_or_id, uint32_t initial_wait_time, uint8_t max_attempts,
361 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
362 this->cancel_retry_(component, name_type, static_name, hash_or_id);
363
364 if (initial_wait_time == SCHEDULER_DONT_RUN)
365 return;
366
367#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
368 {
369 SchedulerNameLog name_log;
370 ESP_LOGVV(TAG, "set_retry(name='%s', initial_wait_time=%" PRIu32 ", max_attempts=%u, backoff_factor=%0.1f)",
371 name_log.format(name_type, static_name, hash_or_id), initial_wait_time, max_attempts,
372 backoff_increase_factor);
373 }
374#endif
375
376 if (backoff_increase_factor < 0.0001) {
377 ESP_LOGE(TAG, "set_retry: backoff_factor %0.1f too small, using 1.0: %s", backoff_increase_factor,
378 (name_type == NameType::STATIC_STRING && static_name) ? static_name : "");
379 backoff_increase_factor = 1;
380 }
381
382 auto args = std::make_shared<RetryArgs>();
383 args->func = std::move(func);
384 args->component = component;
385 args->scheduler = this;
386 args->name_type = name_type;
387 if (name_type == NameType::STATIC_STRING) {
388 args->name_.static_name = static_name;
389 } else {
390 args->name_.hash_or_id = hash_or_id;
391 }
392 args->current_interval = initial_wait_time;
393 args->backoff_increase_factor = backoff_increase_factor;
394 args->retry_countdown = max_attempts;
395
396 // First execution of `func` immediately - use set_timer_common_ with is_retry=true
397 this->set_timer_common_(
398 component, SchedulerItem::TIMEOUT, name_type, static_name, hash_or_id, 0, [args]() { retry_handler(args); },
399 /* is_retry= */ true);
400}
401
402void HOT Scheduler::set_retry(Component *component, const char *name, uint32_t initial_wait_time, uint8_t max_attempts,
403 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
404 this->set_retry_common_(component, NameType::STATIC_STRING, name, 0, initial_wait_time, max_attempts, std::move(func),
405 backoff_increase_factor);
406}
407
408bool HOT Scheduler::cancel_retry_(Component *component, NameType name_type, const char *static_name,
409 uint32_t hash_or_id) {
410 return this->cancel_item_(component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
411 /* match_retry= */ true);
412}
413bool HOT Scheduler::cancel_retry(Component *component, const char *name) {
414 return this->cancel_retry_(component, NameType::STATIC_STRING, name, 0);
415}
416
417void HOT Scheduler::set_retry(Component *component, const std::string &name, uint32_t initial_wait_time,
418 uint8_t max_attempts, std::function<RetryResult(uint8_t)> func,
419 float backoff_increase_factor) {
420 this->set_retry_common_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), initial_wait_time,
421 max_attempts, std::move(func), backoff_increase_factor);
422}
423
424bool HOT Scheduler::cancel_retry(Component *component, const std::string &name) {
425 return this->cancel_retry_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name));
426}
427
428void HOT Scheduler::set_retry(Component *component, uint32_t id, uint32_t initial_wait_time, uint8_t max_attempts,
429 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
430 this->set_retry_common_(component, NameType::NUMERIC_ID, nullptr, id, initial_wait_time, max_attempts,
431 std::move(func), backoff_increase_factor);
432}
433
434bool HOT Scheduler::cancel_retry(Component *component, uint32_t id) {
435 return this->cancel_retry_(component, NameType::NUMERIC_ID, nullptr, id);
436}
437
438#pragma GCC diagnostic pop // End suppression of deprecated RetryResult warnings
439
440optional<uint32_t> HOT Scheduler::next_schedule_in(uint32_t now) {
441 // IMPORTANT: This method should only be called from the main thread (loop task).
442 // Accesses items_[0] and the fast-path empty checks without holding a lock, which
443 // is only safe from the main thread. Other threads must not call this method.
444 //
445 // Note: cleanup_() is only invoked on the items_[0] path below. The early returns
446 // skip it because they don't read items_[0], and Scheduler::call() at the top of
447 // every loop iteration already performs its own cleanup before the next sleep-
448 // duration computation happens.
449
450#ifndef ESPHOME_THREAD_SINGLE
451 // defer() items live in a separate queue that is drained at the top of every
452 // loop tick via process_defer_queue_(). If any are pending, the next loop
453 // iteration has work to do right now -- don't let the caller sleep.
454 if (!this->defer_empty_())
455 return 0;
456#else
457 // On single-threaded builds, defer() routes through set_timeout(..., 0) which
458 // stages in to_add_. process_to_add() runs at the top of every scheduler.call(),
459 // so anything in to_add_ becomes runnable on the next iteration; don't sleep.
460 if (!this->to_add_empty_())
461 return 0;
462#endif
463
464 // If no items, return empty optional
465 if (!this->cleanup_())
466 return {};
467
468 SchedulerItem *item = this->items_[0];
469 const auto now_64 = this->millis_64_from_(now);
470 const uint64_t next_exec = item->get_next_execution();
471 if (next_exec < now_64)
472 return 0;
473 return next_exec - now_64;
474}
475
476void Scheduler::full_cleanup_removed_items_() {
477 // We hold the lock for the entire cleanup operation because:
478 // 1. We're rebuilding the entire items_ list, so we need exclusive access throughout
479 // 2. Other threads must see either the old state or the new state, not intermediate states
480 // 3. The operation is already expensive (O(n)), so lock overhead is negligible
481 // 4. No operations inside can block or take other locks, so no deadlock risk
482 LockGuard guard{this->lock_};
483
484 // Compact in-place: move valid items forward, recycle removed ones
485 size_t write = 0;
486 for (size_t read = 0; read < this->items_.size(); ++read) {
487 if (!is_item_removed_locked_(this->items_[read])) {
488 if (write != read) {
489 this->items_[write] = this->items_[read];
490 }
491 ++write;
492 } else {
493 this->recycle_item_main_loop_(this->items_[read]);
494 }
495 }
496 this->items_.erase(this->items_.begin() + write, this->items_.end());
497 // Rebuild the heap structure since items are no longer in heap order
498 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
499 this->to_remove_clear_locked_();
500}
501
502#ifndef ESPHOME_THREAD_SINGLE
503void Scheduler::compact_defer_queue_locked_() {
504 // Rare case: new items were added during processing - compact the vector
505 // This only happens when:
506 // 1. A deferred callback calls defer() again, or
507 // 2. Another thread calls defer() while we're processing
508 //
509 // Move unprocessed items (added during this loop) to the front for next iteration
510 //
511 // SAFETY: Compacted items may include cancelled items (marked for removal via
512 // cancel_item_locked_() during execution). This is safe because should_skip_item_()
513 // checks is_item_removed_() before executing, so cancelled items will be skipped
514 // and recycled on the next loop iteration.
515 size_t remaining = this->defer_queue_.size() - this->defer_queue_front_;
516 for (size_t i = 0; i < remaining; i++) {
517 this->defer_queue_[i] = this->defer_queue_[this->defer_queue_front_ + i];
518 }
519 // Use erase() instead of resize() to avoid instantiating _M_default_append
520 // (saves ~156 bytes flash). Erasing from the end is O(1) - no shifting needed.
521 this->defer_queue_.erase(this->defer_queue_.begin() + remaining, this->defer_queue_.end());
522}
523void HOT Scheduler::process_defer_queue_slow_path_(uint32_t &now) {
524 // Process defer queue to guarantee FIFO execution order for deferred items.
525 // Previously, defer() used the heap which gave undefined order for equal timestamps,
526 // causing race conditions on multi-core systems (ESP32, BK7200).
527 // With the defer queue:
528 // - Deferred items (delay=0) go directly to defer_queue_ in set_timer_common_
529 // - Items execute in exact order they were deferred (FIFO guarantee)
530 // - No deferred items exist in to_add_, so processing order doesn't affect correctness
531 // Single-core platforms don't use this queue and fall back to the heap-based approach.
532 //
533 // Note: Items cancelled via cancel_item_locked_() are marked with remove=true but still
534 // processed here. They are skipped during execution by should_skip_item_().
535 // This is intentional - no memory leak occurs.
536 //
537 // We use an index (defer_queue_front_) to track the read position instead of calling
538 // erase() on every pop, which would be O(n). The queue is processed once per loop -
539 // any items added during processing are left for the next loop iteration.
540
541 // Merge lock acquisitions: instead of separate locks for move-out and recycle (2N+1 total),
542 // recycle each item after re-acquiring the lock for the next iteration (N+1 total).
543 // The lock is held across: recycle → loop condition → move-out, then released for execution.
544 SchedulerItem *item;
545
546 this->lock_.lock();
547 // Reset counter and snapshot queue end under lock
548 this->defer_count_clear_locked_();
549 size_t defer_queue_end = this->defer_queue_.size();
550 if (this->defer_queue_front_ >= defer_queue_end) {
551 this->lock_.unlock();
552 return;
553 }
554 while (this->defer_queue_front_ < defer_queue_end) {
555 // Take ownership of the item, leaving nullptr in the vector slot.
556 // This is safe because:
557 // 1. The vector is only cleaned up by cleanup_defer_queue_locked_() at the end of this function
558 // 2. Any code iterating defer_queue_ MUST check for nullptr items (see mark_matching_items_removed_locked_)
559 // 3. The lock protects concurrent access, but the nullptr remains until cleanup
560 item = this->defer_queue_[this->defer_queue_front_];
561 this->defer_queue_[this->defer_queue_front_] = nullptr;
562 this->defer_queue_front_++;
563 this->lock_.unlock();
564
565 // Execute callback without holding lock to prevent deadlocks
566 // if the callback tries to call defer() again
567 if (!this->should_skip_item_(item)) {
568 now = this->execute_item_(item, now);
569 }
570
571 this->lock_.lock();
572 this->recycle_item_main_loop_(item);
573 }
574 // Clean up the queue (lock already held from last recycle or initial acquisition)
575 this->cleanup_defer_queue_locked_();
576 this->lock_.unlock();
577}
578#endif /* not ESPHOME_THREAD_SINGLE */
579
580uint32_t HOT Scheduler::call(uint32_t now) {
581#ifndef ESPHOME_THREAD_SINGLE
582 this->process_defer_queue_(now);
583#endif /* not ESPHOME_THREAD_SINGLE */
584
585 // Extend the caller's 32-bit timestamp to 64-bit for scheduler operations
586 const auto now_64 = this->millis_64_from_(now);
587 this->process_to_add();
588
589 // Track if any items were added to to_add_ during callbacks
590 bool has_added_items = false;
591
592#ifdef ESPHOME_DEBUG_SCHEDULER
593 static uint64_t last_print = 0;
594
595 if (now_64 - last_print > 2000) {
596 last_print = now_64;
597 std::vector<SchedulerItem *> old_items;
598 ESP_LOGD(TAG, "Items: count=%zu, pool=%zu, now=%" PRIu64, this->items_.size(), this->scheduler_item_pool_size_,
599 now_64);
600 // Cleanup before debug output
601 this->cleanup_();
602 while (!this->items_.empty()) {
603 SchedulerItem *item;
604 {
605 LockGuard guard{this->lock_};
606 item = this->pop_raw_locked_();
607 }
608
609 SchedulerNameLog name_log;
610 bool is_cancelled = is_item_removed_(item);
611 ESP_LOGD(TAG, " %s '%s/%s' interval=%" PRIu32 " next_execution in %" PRIu64 "ms at %" PRIu64 "%s",
612 item->get_type_str(), LOG_STR_ARG(item->get_source()),
613 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
614 item->get_next_execution() - now_64, item->get_next_execution(), is_cancelled ? " [CANCELLED]" : "");
615
616 old_items.push_back(item);
617 }
618 ESP_LOGD(TAG, "\n");
619
620 {
621 LockGuard guard{this->lock_};
622 this->items_ = std::move(old_items);
623 // Rebuild heap after moving items back
624 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
625 }
626 }
627#endif /* ESPHOME_DEBUG_SCHEDULER */
628
629 // Cleanup removed items before processing
630 // First try to clean items from the top of the heap (fast path)
631 this->cleanup_();
632
633 // If we still have too many cancelled items, do a full cleanup
634 // This only happens if cancelled items are stuck in the middle/bottom of the heap
635 if (this->to_remove_count_() >= MAX_LOGICALLY_DELETED_ITEMS) {
636 this->full_cleanup_removed_items_();
637 }
638 // IMPORTANT: This loop uses index-based access (items_[0]), NOT iterators.
639 // This is intentional — fired intervals are pushed back into items_ via
640 // push_back() + push_heap() below, which may reallocate the vector's storage.
641 // Index-based access is safe across reallocations because we re-read items_[0]
642 // at the top of each iteration. Do NOT convert this to a range-based for loop
643 // or iterator-based loop, as that would break when items are added.
644 while (!this->items_.empty()) {
645 // Don't copy-by value yet
646 SchedulerItem *item = this->items_[0];
647 if (item->get_next_execution() > now_64) {
648 // Not reached timeout yet, done for this call
649 break;
650 }
651 // Don't run on failed components (is_item_failed_ exempts SELF_POINTER delays).
652 if (this->is_item_failed_(item)) {
653 LockGuard guard{this->lock_};
654 this->recycle_item_main_loop_(this->pop_raw_locked_());
655 continue;
656 }
657
658 // Check if item is marked for removal
659 // This handles two cases:
660 // 1. Item was marked for removal after cleanup_() but before we got here
661 // 2. Item is marked for removal but wasn't at the front of the heap during cleanup_()
662#ifdef ESPHOME_THREAD_MULTI_NO_ATOMICS
663 // Multi-threaded platforms without atomics: must take lock to safely read remove flag
664 {
665 LockGuard guard{this->lock_};
666 if (is_item_removed_locked_(item)) {
667 this->recycle_item_main_loop_(this->pop_raw_locked_());
668 this->to_remove_decrement_locked_();
669 continue;
670 }
671 }
672#else
673 // Single-threaded or multi-threaded with atomics: can check without lock
674 if (is_item_removed_(item)) {
675 LockGuard guard{this->lock_};
676 this->recycle_item_main_loop_(this->pop_raw_locked_());
677 this->to_remove_decrement_locked_();
678 continue;
679 }
680#endif
681
682#ifdef ESPHOME_DEBUG_SCHEDULER
683 {
684 SchedulerNameLog name_log;
685 ESP_LOGV(TAG, "Running %s '%s/%s' with interval=%" PRIu32 " next_execution=%" PRIu64 " (now=%" PRIu64 ")",
686 item->get_type_str(), LOG_STR_ARG(item->get_source()),
687 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
688 item->get_next_execution(), now_64);
689 }
690#endif /* ESPHOME_DEBUG_SCHEDULER */
691
692 // Warning: During callback(), a lot of stuff can happen, including:
693 // - timeouts/intervals get added, potentially invalidating vector pointers
694 // - timeouts/intervals get cancelled
695 now = this->execute_item_(item, now);
696
697 LockGuard guard{this->lock_};
698
699 // Only pop after function call, this ensures we were reachable
700 // during the function call and know if we were cancelled.
701 SchedulerItem *executed_item = this->pop_raw_locked_();
702
703 if (this->is_item_removed_locked_(executed_item)) {
704 // We were removed/cancelled in the function call, recycle and continue
705 this->to_remove_decrement_locked_();
706 this->recycle_item_main_loop_(executed_item);
707 continue;
708 }
709
710 if (executed_item->type == SchedulerItem::INTERVAL) {
711 executed_item->set_next_execution(now_64 + executed_item->interval);
712 // Push directly back into the heap instead of routing through to_add_.
713 // This is safe because:
714 // 1. We're on the main loop and already hold the lock
715 // 2. The item was already popped from items_ via pop_raw_locked_() above
716 // 3. The while loop uses index-based access (items_[0]), not iterators,
717 // so push_back() reallocation cannot invalidate our iteration
718 // 4. push_heap() restores the heap invariant before the next iteration
719 // peeks at items_[0]
720 // This avoids the to_add_ detour and the overhead of
721 // process_to_add_slow_path_() (lock acquisition, vector iteration, clear).
722 this->items_.push_back(executed_item);
723 std::push_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
724 } else {
725 // Timeout completed - recycle it
726 this->recycle_item_main_loop_(executed_item);
727 }
728
729 has_added_items |= !this->to_add_.empty();
730 }
731
732 if (has_added_items) {
733 this->process_to_add();
734 }
735
736#ifdef ESPHOME_DEBUG_SCHEDULER
737 // Verify no items were leaked during this call() cycle.
738 // All items must be in items_, to_add_, defer_queue_, or the pool.
739 // Safe to check here because:
740 // - process_defer_queue_ has already run its cleanup_defer_queue_locked_(),
741 // so defer_queue_ contains no nullptr slots inflating the count.
742 // - The while loop above has finished, so no items are held in local variables;
743 // every item has been returned to a container (items_, to_add_, or pool).
744 // Lock needed to get a consistent snapshot of all containers.
745 {
746 LockGuard guard{this->lock_};
747 this->debug_verify_no_leak_();
748 }
749#endif
750 // execute_item_() advances `now` as items fire; return it so the caller
751 // stays monotonic with last_wdt_feed_.
752 return now;
753}
754void HOT Scheduler::process_to_add_slow_path_() {
755 LockGuard guard{this->lock_};
756 for (auto *&it : this->to_add_) {
757 if (is_item_removed_locked_(it)) {
758 // Recycle cancelled items
759 this->recycle_item_main_loop_(it);
760 it = nullptr;
761 continue;
762 }
763
764 this->items_.push_back(it);
765 std::push_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
766 }
767 this->to_add_.clear();
768 this->to_add_count_clear_locked_();
769}
770bool HOT Scheduler::cleanup_slow_path_() {
771 // We must hold the lock for the entire cleanup operation because:
772 // 1. We're modifying items_ (via pop_raw_locked_) which requires exclusive access
773 // 2. We're decrementing to_remove_ which is also modified by other threads
774 // (though all modifications are already under lock)
775 // 3. Other threads read items_ when searching for items to cancel in cancel_item_locked_()
776 // 4. We need a consistent view of items_ and to_remove_ throughout the operation
777 // Without the lock, we could access items_ while another thread is reading it,
778 // leading to race conditions
779 LockGuard guard{this->lock_};
780 while (!this->items_.empty()) {
781 SchedulerItem *item = this->items_[0];
782 if (!this->is_item_removed_locked_(item))
783 break;
784 this->to_remove_decrement_locked_();
785 this->recycle_item_main_loop_(this->pop_raw_locked_());
786 }
787 return !this->items_.empty();
788}
789Scheduler::SchedulerItem *HOT Scheduler::pop_raw_locked_() {
790 std::pop_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
791
792 SchedulerItem *item = this->items_.back();
793 this->items_.pop_back();
794 return item;
795}
796
797// Helper to execute a scheduler item
798uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) {
799 // Resolve the component and (for SELF_POINTER/deferred items) the source name from the shared
800 // union slot with a single name-type check. Self-keyed items have no owning component; their slot
801 // holds the source name (e.g. the owning script), published so deferred work chained inside the
802 // callback re-captures it and the blocking warning can name the script instead of "<null>".
803 Component *component;
804 const LogString *source;
805 if (item->get_name_type() == NameType::SELF_POINTER) {
806 component = nullptr;
807 source = item->source_name;
808 } else {
809 component = item->component;
810 source = nullptr;
811 }
812 // Guard publishes the item's identity + dispatch time, then times the callback.
813 LoopBlockingGuard guard{component, source, now};
814 item->callback();
815 uint32_t end = guard.finish();
816 // Feed the watchdog after each scheduled item (both main heap and defer
817 // queue paths go through here). A run of back-to-back callbacks cannot
818 // starve the wdt. The inline fast path is a load + sub + branch — nearly
819 // free when the 3 ms rate limit hasn't elapsed.
821 return end;
822}
823
824// Common implementation for cancel operations - handles locking
825bool HOT Scheduler::cancel_item_(Component *component, NameType name_type, const char *static_name, uint32_t hash_or_id,
826 SchedulerItem::Type type, bool match_retry) {
827 LockGuard guard{this->lock_};
828 // Public cancel path uses default find_first=false to cancel ALL matches because
829 // DelayAction parallel mode (skip_cancel=true) can create multiple items with the same key.
830 return this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type, match_retry);
831}
832
833// Helper to cancel matching items - must be called with lock held.
834// When find_first=true, stops after the first match and exits across containers
835// (used by set_timer_common_ where cancel-before-add guarantees at most one match).
836// When find_first=false, cancels ALL matches across all containers (needed for
837// public cancel path where DelayAction parallel mode can create duplicates).
838// name_type determines matching: STATIC_STRING uses static_name, others use hash_or_id
839size_t Scheduler::mark_matching_items_removed_slow_locked_(std::vector<SchedulerItem *> &container,
840 Component *component, NameType name_type,
841 const char *static_name, uint32_t hash_or_id,
842 SchedulerItem::Type type, bool match_retry,
843 bool find_first) {
844 size_t count = 0;
845 for (auto *item : container) {
846 if (this->matches_item_locked_(item, component, name_type, static_name, hash_or_id, type, match_retry)) {
847 this->set_item_removed_(item, true);
848 if (find_first)
849 return 1;
850 count++;
851 }
852 }
853 return count;
854}
855
856bool HOT Scheduler::cancel_item_locked_(Component *component, NameType name_type, const char *static_name,
857 uint32_t hash_or_id, SchedulerItem::Type type, bool match_retry,
858 bool find_first) {
859 // Early return if static string name is invalid
860 if (name_type == NameType::STATIC_STRING && static_name == nullptr) {
861 return false;
862 }
863
864 size_t total_cancelled = 0;
865
866#ifndef ESPHOME_THREAD_SINGLE
867 // Mark items in defer queue as cancelled (they'll be skipped when processed)
868 if (type == SchedulerItem::TIMEOUT) {
869 total_cancelled += this->mark_matching_items_removed_locked_(this->defer_queue_, component, name_type, static_name,
870 hash_or_id, type, match_retry, find_first);
871 if (find_first && total_cancelled > 0)
872 return true;
873 }
874#endif /* not ESPHOME_THREAD_SINGLE */
875
876 // Cancel items in the main heap
877 // We only mark items for removal here - never recycle directly.
878 // The main loop may be executing an item's callback right now, and recycling
879 // would destroy the callback while it's running (use-after-free).
880 // Only the main loop in call() should recycle items after execution completes.
881 {
882 size_t heap_cancelled = this->mark_matching_items_removed_locked_(this->items_, component, name_type, static_name,
883 hash_or_id, type, match_retry, find_first);
884 total_cancelled += heap_cancelled;
885 this->to_remove_add_locked_(heap_cancelled);
886 if (find_first && total_cancelled > 0)
887 return true;
888 }
889
890 // Cancel items in to_add_
891 total_cancelled += this->mark_matching_items_removed_locked_(this->to_add_, component, name_type, static_name,
892 hash_or_id, type, match_retry, find_first);
893
894 return total_cancelled > 0;
895}
896
897bool HOT Scheduler::SchedulerItem::cmp(SchedulerItem *a, SchedulerItem *b) {
898 // High bits are almost always equal (change only on 32-bit rollover ~49 days)
899 // Optimize for common case: check low bits first when high bits are equal
900 return (a->next_execution_high_ == b->next_execution_high_) ? (a->next_execution_low_ > b->next_execution_low_)
901 : (a->next_execution_high_ > b->next_execution_high_);
902}
903
904// Recycle a SchedulerItem back to the freelist for reuse.
905// IMPORTANT: Caller must hold the scheduler lock.
906void Scheduler::recycle_item_main_loop_(SchedulerItem *item) {
907 if (item == nullptr)
908 return;
909
910 item->callback = nullptr; // release captured resources
911 item->next_free = this->scheduler_item_pool_head_;
912 this->scheduler_item_pool_head_ = item;
913 this->scheduler_item_pool_size_++;
914#ifdef ESPHOME_DEBUG_SCHEDULER
915 ESP_LOGD(TAG, "Recycled item to pool (pool size now: %zu)", this->scheduler_item_pool_size_);
916#endif
917}
918
919// Shrink a SchedulerItem* vector's capacity to its current size.
920// std::vector::shrink_to_fit() is non-binding and our toolchain ignores it; the classic
921// swap-with-copy idiom (std::vector<T>(other).swap(other)) instantiates the iterator-range
922// constructor which pulls in std::__throw_bad_array_new_length and ~120 B of related
923// stdlib RTTI/typeinfo. Build into a temp via reserve + push_back instead, then move-assign:
924// reserve uses operator new (throws bad_alloc, already linked) and push_back without growth
925// is the noexcept tail path. Move-assign just swaps pointers.
926// Out-of-line + noinline so the callers in trim_freelist() share one body.
927void __attribute__((noinline)) Scheduler::shrink_scheduler_vector_(std::vector<SchedulerItem *> *v) {
928 if (v->capacity() == v->size())
929 return; // already exact, common after a quiet period
930 std::vector<SchedulerItem *> tmp;
931 tmp.reserve(v->size());
932 for (SchedulerItem *p : *v)
933 tmp.push_back(p);
934 *v = std::move(tmp);
935}
936
937void Scheduler::trim_freelist() {
938 LockGuard guard{this->lock_};
939 SchedulerItem *item = this->scheduler_item_pool_head_;
940 size_t freed = 0;
941 while (item != nullptr) {
942 SchedulerItem *next = item->next_free;
943 delete item;
944#ifdef ESPHOME_DEBUG_SCHEDULER
945 this->debug_live_items_--;
946#endif
947 item = next;
948 freed++;
949 }
950 this->scheduler_item_pool_head_ = nullptr;
951 this->scheduler_item_pool_size_ = 0;
952
953 // items_/to_add_/defer_queue_ retain their boot-peak vector capacity (vector grows
954 // by doubling and otherwise keeps the peak). Reclaim that slack as well.
955 shrink_scheduler_vector_(&this->items_);
956 shrink_scheduler_vector_(&this->to_add_);
957#ifndef ESPHOME_THREAD_SINGLE
958 shrink_scheduler_vector_(&this->defer_queue_);
959#endif
960
961#ifdef ESPHOME_DEBUG_SCHEDULER
962 ESP_LOGD(TAG, "Freelist trimmed (%zu items freed)", freed);
963#else
964 (void) freed;
965#endif
966}
967
968#ifdef ESPHOME_DEBUG_SCHEDULER
969void Scheduler::debug_log_timer_(const SchedulerItem *item, NameType name_type, const char *static_name,
970 uint32_t hash_or_id, SchedulerItem::Type type, uint32_t delay, uint64_t now) {
971 // Validate static strings in debug mode
972 if (name_type == NameType::STATIC_STRING && static_name != nullptr) {
973 validate_static_string(static_name);
974 }
975
976 // Debug logging
977 SchedulerNameLog name_log;
978 const char *type_str = (type == SchedulerItem::TIMEOUT) ? "timeout" : "interval";
979 if (type == SchedulerItem::TIMEOUT) {
980 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
981 name_log.format(name_type, static_name, hash_or_id), type_str, delay);
982 } else {
983 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ", offset=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
984 name_log.format(name_type, static_name, hash_or_id), type_str, delay,
985 static_cast<uint32_t>(item->get_next_execution() - now));
986 }
987}
988#endif /* ESPHOME_DEBUG_SCHEDULER */
989
990// Pop from freelist or allocate. IMPORTANT: caller must hold the lock and must overwrite
991// `item->component` before releasing it -- the popped slot still holds the freelist link.
992Scheduler::SchedulerItem *Scheduler::get_item_from_pool_locked_() {
993 if (this->scheduler_item_pool_head_ != nullptr) {
994 SchedulerItem *item = this->scheduler_item_pool_head_;
995 this->scheduler_item_pool_head_ = item->next_free;
996 this->scheduler_item_pool_size_--;
997#ifdef ESPHOME_DEBUG_SCHEDULER
998 ESP_LOGD(TAG, "Reused item from pool (pool size now: %zu)", this->scheduler_item_pool_size_);
999#endif
1000 return item;
1001 }
1002#ifdef ESPHOME_DEBUG_SCHEDULER
1003 ESP_LOGD(TAG, "Allocated new item (pool empty)");
1004#endif
1005 auto *item = new SchedulerItem();
1006#ifdef ESPHOME_DEBUG_SCHEDULER
1007 this->debug_live_items_++;
1008#endif
1009 return item;
1010}
1011
1012#ifdef ESPHOME_DEBUG_SCHEDULER
1013bool Scheduler::debug_verify_no_leak_() const {
1014 // Invariant: every live SchedulerItem must be in exactly one container.
1015 // debug_live_items_ tracks allocations minus deletions.
1016 size_t accounted = this->items_.size() + this->to_add_.size() + this->scheduler_item_pool_size_;
1017#ifndef ESPHOME_THREAD_SINGLE
1018 accounted += this->defer_queue_.size();
1019#endif
1020 if (accounted != this->debug_live_items_) {
1021 ESP_LOGE(TAG,
1022 "SCHEDULER LEAK DETECTED: live=%" PRIu32 " but accounted=%" PRIu32 " (items=%" PRIu32 " to_add=%" PRIu32
1023 " pool=%" PRIu32
1024#ifndef ESPHOME_THREAD_SINGLE
1025 " defer=%" PRIu32
1026#endif
1027 ")",
1028 static_cast<uint32_t>(this->debug_live_items_), static_cast<uint32_t>(accounted),
1029 static_cast<uint32_t>(this->items_.size()), static_cast<uint32_t>(this->to_add_.size()),
1030 static_cast<uint32_t>(this->scheduler_item_pool_size_)
1031#ifndef ESPHOME_THREAD_SINGLE
1032 ,
1033 static_cast<uint32_t>(this->defer_queue_.size())
1034#endif
1035 );
1036 assert(false);
1037 return false;
1038 }
1039 return true;
1040}
1041#endif
1042
1043} // namespace esphome
void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time)
Feed the task watchdog, hot entry.
const LogString * get_component_log_str() const ESPHOME_ALWAYS_INLINE
Get the integration where this component was declared as a LogString for logging.
Definition component.h:325
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
Definition component.h:437
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:32
const Component * component
Definition component.cpp:34
uint16_t type
static float float b
const char *const name
Definition lsm6ds.cpp:11
const char *const TAG
Definition spi.cpp:7
const char int const __FlashStringHelper * format
Definition log.h:74
void retry_handler(const std::shared_ptr< RetryArgs > &args)
const char int const __FlashStringHelper va_list args
Definition log.h:74
uint64_t millis_64()
Definition hal.cpp:29
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:12
void HOT delay(uint32_t ms)
Definition hal.cpp:85
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.
Definition helpers.h:834
constexpr uint32_t SCHEDULER_DONT_RUN
Definition component.h:61
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17