ESPHome 2026.3.0-dev
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// Memory pool configuration constants
18// Pool size of 5 matches typical usage patterns (2-4 active timers)
19// - Minimal memory overhead (~250 bytes on ESP32)
20// - Sufficient for most configs with a couple sensors/components
21// - Still prevents heap fragmentation and allocation stalls
22// - Complex setups with many timers will just allocate beyond the pool
23// See https://github.com/esphome/backlog/issues/52
24static constexpr size_t MAX_POOL_SIZE = 5;
25
26// Maximum number of logically deleted (cancelled) items before forcing cleanup.
27// Set to 5 to match the pool size - when we have as many cancelled items as our
28// pool can hold, it's time to clean up and recycle them.
29static constexpr uint32_t MAX_LOGICALLY_DELETED_ITEMS = 5;
30// max delay to start an interval sequence
31static constexpr uint32_t MAX_INTERVAL_DELAY = 5000;
32
33// Prevent inlining of SchedulerItem deletion. On BK7231N (Thumb-1), GCC inlines
34// ~unique_ptr<SchedulerItem> (~30 bytes each) at every destruction site. Defining
35// the deleter in the .cpp file ensures a single copy of the destructor + operator delete.
36void Scheduler::SchedulerItemDeleter::operator()(SchedulerItem *ptr) const noexcept { delete ptr; }
37
38#if defined(ESPHOME_LOG_HAS_VERBOSE) || defined(ESPHOME_DEBUG_SCHEDULER)
39// Helper struct for formatting scheduler item names consistently in logs
40// Uses a stack buffer to avoid heap allocation
41// Uses ESPHOME_snprintf_P/ESPHOME_PSTR for ESP8266 to keep format strings in flash
42struct SchedulerNameLog {
43 char buffer[20]; // Enough for "id:4294967295" or "hash:0xFFFFFFFF" or "(null)"
44
45 // Format a scheduler item name for logging
46 // Returns pointer to formatted string (either static_name or internal buffer)
47 const char *format(Scheduler::NameType name_type, const char *static_name, uint32_t hash_or_id) {
48 using NameType = Scheduler::NameType;
49 if (name_type == NameType::STATIC_STRING) {
50 if (static_name)
51 return static_name;
52 // Copy "(null)" to buffer to keep it in flash on ESP8266
53 ESPHOME_strncpy_P(buffer, ESPHOME_PSTR("(null)"), sizeof(buffer));
54 return buffer;
55 } else if (name_type == NameType::HASHED_STRING) {
56 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("hash:0x%08" PRIX32), hash_or_id);
57 return buffer;
58 } else if (name_type == NameType::NUMERIC_ID) {
59 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("id:%" PRIu32), hash_or_id);
60 return buffer;
61 } else { // NUMERIC_ID_INTERNAL
62 ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("iid:%" PRIu32), hash_or_id);
63 return buffer;
64 }
65 }
66};
67#endif
68
69// Uncomment to debug scheduler
70// #define ESPHOME_DEBUG_SCHEDULER
71
72#ifdef ESPHOME_DEBUG_SCHEDULER
73// Helper to validate that a pointer looks like it's in static memory
74static void validate_static_string(const char *name) {
75 if (name == nullptr)
76 return;
77
78 // This is a heuristic check - stack and heap pointers are typically
79 // much higher in memory than static data
80 uintptr_t addr = reinterpret_cast<uintptr_t>(name);
81
82 // Create a stack variable to compare against
83 int stack_var;
84 uintptr_t stack_addr = reinterpret_cast<uintptr_t>(&stack_var);
85
86 // If the string pointer is near our stack variable, it's likely on the stack
87 // Using 8KB range as ESP32 main task stack is typically 8192 bytes
88 if (addr > (stack_addr - 0x2000) && addr < (stack_addr + 0x2000)) {
89 ESP_LOGW(TAG,
90 "WARNING: Scheduler name '%s' at %p appears to be on the stack - this is unsafe!\n"
91 " Stack reference at %p",
92 name, name, &stack_var);
93 }
94
95 // Also check if it might be on the heap by seeing if it's in a very different range
96 // This is platform-specific but generally heap is allocated far from static memory
97 static const char *static_str = "test";
98 uintptr_t static_addr = reinterpret_cast<uintptr_t>(static_str);
99
100 // If the address is very far from known static memory, it might be heap
101 if (addr > static_addr + 0x100000 || (static_addr > 0x100000 && addr < static_addr - 0x100000)) {
102 ESP_LOGW(TAG, "WARNING: Scheduler name '%s' at %p might be on heap (static ref at %p)", name, name, static_str);
103 }
104}
105#endif /* ESPHOME_DEBUG_SCHEDULER */
106
107// A note on locking: the `lock_` lock protects the `items_` and `to_add_` containers. It must be taken when writing to
108// them (i.e. when adding/removing items, but not when changing items). As items are only deleted from the loop task,
109// iterating over them from the loop task is fine; but iterating from any other context requires the lock to be held to
110// avoid the main thread modifying the list while it is being accessed.
111
112// Calculate random offset for interval timers
113// Extracted from set_timer_common_ to reduce code size - float math + random_float()
114// only needed for intervals, not timeouts
115uint32_t Scheduler::calculate_interval_offset_(uint32_t delay) {
116 return static_cast<uint32_t>(std::min(delay / 2, MAX_INTERVAL_DELAY) * random_float());
117}
118
119// Check if a retry was already cancelled in items_ or to_add_
120// Extracted from set_timer_common_ to reduce code size - retry path is cold and deprecated
121// Remove before 2026.8.0 along with all retry code
122bool Scheduler::is_retry_cancelled_locked_(Component *component, NameType name_type, const char *static_name,
123 uint32_t hash_or_id) {
124 for (auto *container : {&this->items_, &this->to_add_}) {
125 for (auto &item : *container) {
126 if (item && this->is_item_removed_locked_(item.get()) &&
127 this->matches_item_locked_(item, component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
128 /* match_retry= */ true, /* skip_removed= */ false)) {
129 return true;
130 }
131 }
132 }
133 return false;
134}
135
136// Common implementation for both timeout and interval
137// name_type determines storage type: STATIC_STRING uses static_name, others use hash_or_id
138void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type type, NameType name_type,
139 const char *static_name, uint32_t hash_or_id, uint32_t delay,
140 std::function<void()> &&func, bool is_retry, bool skip_cancel) {
141 if (delay == SCHEDULER_DONT_RUN) {
142 // Still need to cancel existing timer if we have a name/id
143 if (!skip_cancel) {
144 LockGuard guard{this->lock_};
145 this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type);
146 }
147 return;
148 }
149
150 // Take lock early to protect scheduler_item_pool_ access
151 LockGuard guard{this->lock_};
152
153 // Create and populate the scheduler item
154 auto item = this->get_item_from_pool_locked_();
155 item->component = component;
156 item->set_name(name_type, static_name, hash_or_id);
157 item->type = type;
158 item->callback = std::move(func);
159 // Reset remove flag - recycled items may have been cancelled (remove=true) in previous use
160 this->set_item_removed_(item.get(), false);
161 item->is_retry = is_retry;
162
163 // Determine target container: defer_queue_ for deferred items, to_add_ for everything else.
164 // Using a pointer lets both paths share the cancel + push_back epilogue.
165 auto *target = &this->to_add_;
166
167#ifndef ESPHOME_THREAD_SINGLE
168 // Special handling for defer() (delay = 0, type = TIMEOUT)
169 // Single-core platforms don't need thread-safe defer handling
170 if (delay == 0 && type == SchedulerItem::TIMEOUT) {
171 // Put in defer queue for guaranteed FIFO execution
172 target = &this->defer_queue_;
173 } else
174#endif /* not ESPHOME_THREAD_SINGLE */
175 {
176 // Only non-defer items need a timestamp for scheduling
177 const uint64_t now_64 = millis_64();
178
179 // Type-specific setup
180 if (type == SchedulerItem::INTERVAL) {
181 item->interval = delay;
182 // first execution happens immediately after a random smallish offset
183 uint32_t offset = this->calculate_interval_offset_(delay);
184 item->set_next_execution(now_64 + offset);
185#ifdef ESPHOME_LOG_HAS_VERBOSE
186 SchedulerNameLog name_log;
187 ESP_LOGV(TAG, "Scheduler interval for %s is %" PRIu32 "ms, offset %" PRIu32 "ms",
188 name_log.format(name_type, static_name, hash_or_id), delay, offset);
189#endif
190 } else {
191 item->interval = 0;
192 item->set_next_execution(now_64 + delay);
193 }
194
195#ifdef ESPHOME_DEBUG_SCHEDULER
196 this->debug_log_timer_(item.get(), name_type, static_name, hash_or_id, type, delay, now_64);
197#endif /* ESPHOME_DEBUG_SCHEDULER */
198
199 // For retries, check if there's a cancelled timeout first
200 // Skip check for anonymous retries (STATIC_STRING with nullptr) - they can't be cancelled by name
201 if (is_retry && (name_type != NameType::STATIC_STRING || static_name != nullptr) &&
202 type == SchedulerItem::TIMEOUT &&
203 this->is_retry_cancelled_locked_(component, name_type, static_name, hash_or_id)) {
204 // Skip scheduling - the retry was cancelled
205#ifdef ESPHOME_DEBUG_SCHEDULER
206 SchedulerNameLog skip_name_log;
207 ESP_LOGD(TAG, "Skipping retry '%s' - found cancelled item",
208 skip_name_log.format(name_type, static_name, hash_or_id));
209#endif
210 return;
211 }
212 }
213
214 // Common epilogue: atomic cancel-and-add (unless skip_cancel is true)
215 if (!skip_cancel) {
216 this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type);
217 }
218 target->push_back(std::move(item));
219}
220
221void HOT Scheduler::set_timeout(Component *component, const char *name, uint32_t timeout,
222 std::function<void()> &&func) {
223 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::STATIC_STRING, name, 0, timeout,
224 std::move(func));
225}
226
227void HOT Scheduler::set_timeout(Component *component, const std::string &name, uint32_t timeout,
228 std::function<void()> &&func) {
229 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
230 timeout, std::move(func));
231}
232void HOT Scheduler::set_timeout(Component *component, uint32_t id, uint32_t timeout, std::function<void()> &&func) {
233 this->set_timer_common_(component, SchedulerItem::TIMEOUT, NameType::NUMERIC_ID, nullptr, id, timeout,
234 std::move(func));
235}
236bool HOT Scheduler::cancel_timeout(Component *component, const std::string &name) {
237 return this->cancel_item_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), SchedulerItem::TIMEOUT);
238}
239bool HOT Scheduler::cancel_timeout(Component *component, const char *name) {
240 return this->cancel_item_(component, NameType::STATIC_STRING, name, 0, SchedulerItem::TIMEOUT);
241}
242bool HOT Scheduler::cancel_timeout(Component *component, uint32_t id) {
243 return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::TIMEOUT);
244}
245void HOT Scheduler::set_interval(Component *component, const std::string &name, uint32_t interval,
246 std::function<void()> &&func) {
247 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::HASHED_STRING, nullptr, fnv1a_hash(name),
248 interval, std::move(func));
249}
250
251void HOT Scheduler::set_interval(Component *component, const char *name, uint32_t interval,
252 std::function<void()> &&func) {
253 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::STATIC_STRING, name, 0, interval,
254 std::move(func));
255}
256void HOT Scheduler::set_interval(Component *component, uint32_t id, uint32_t interval, std::function<void()> &&func) {
257 this->set_timer_common_(component, SchedulerItem::INTERVAL, NameType::NUMERIC_ID, nullptr, id, interval,
258 std::move(func));
259}
260bool HOT Scheduler::cancel_interval(Component *component, const std::string &name) {
261 return this->cancel_item_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), SchedulerItem::INTERVAL);
262}
263bool HOT Scheduler::cancel_interval(Component *component, const char *name) {
264 return this->cancel_item_(component, NameType::STATIC_STRING, name, 0, SchedulerItem::INTERVAL);
265}
266bool HOT Scheduler::cancel_interval(Component *component, uint32_t id) {
267 return this->cancel_item_(component, NameType::NUMERIC_ID, nullptr, id, SchedulerItem::INTERVAL);
268}
269
270// Suppress deprecation warnings for RetryResult usage in the still-present (but deprecated) retry implementation.
271// Remove before 2026.8.0 along with all retry code.
272#pragma GCC diagnostic push
273#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
274
275struct RetryArgs {
276 // Ordered to minimize padding on 32-bit systems
277 std::function<RetryResult(uint8_t)> func;
278 Component *component;
279 Scheduler *scheduler;
280 // Union for name storage - only one is used based on name_type
281 union {
282 const char *static_name; // For STATIC_STRING
283 uint32_t hash_or_id; // For HASHED_STRING or NUMERIC_ID
284 } name_;
285 uint32_t current_interval;
286 float backoff_increase_factor;
287 Scheduler::NameType name_type; // Discriminator for name_ union
288 uint8_t retry_countdown;
289};
290
291void retry_handler(const std::shared_ptr<RetryArgs> &args) {
292 RetryResult const retry_result = args->func(--args->retry_countdown);
293 if (retry_result == RetryResult::DONE || args->retry_countdown <= 0)
294 return;
295 // second execution of `func` happens after `initial_wait_time`
296 // args->name_ is owned by the shared_ptr<RetryArgs>
297 // which is captured in the lambda and outlives the SchedulerItem
298 const char *static_name = (args->name_type == Scheduler::NameType::STATIC_STRING) ? args->name_.static_name : nullptr;
299 uint32_t hash_or_id = (args->name_type != Scheduler::NameType::STATIC_STRING) ? args->name_.hash_or_id : 0;
300 args->scheduler->set_timer_common_(
301 args->component, Scheduler::SchedulerItem::TIMEOUT, args->name_type, static_name, hash_or_id,
302 args->current_interval, [args]() { retry_handler(args); },
303 /* is_retry= */ true);
304 // backoff_increase_factor applied to third & later executions
305 args->current_interval *= args->backoff_increase_factor;
306}
307
308void HOT Scheduler::set_retry_common_(Component *component, NameType name_type, const char *static_name,
309 uint32_t hash_or_id, uint32_t initial_wait_time, uint8_t max_attempts,
310 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
311 this->cancel_retry_(component, name_type, static_name, hash_or_id);
312
313 if (initial_wait_time == SCHEDULER_DONT_RUN)
314 return;
315
316#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
317 {
318 SchedulerNameLog name_log;
319 ESP_LOGVV(TAG, "set_retry(name='%s', initial_wait_time=%" PRIu32 ", max_attempts=%u, backoff_factor=%0.1f)",
320 name_log.format(name_type, static_name, hash_or_id), initial_wait_time, max_attempts,
321 backoff_increase_factor);
322 }
323#endif
324
325 if (backoff_increase_factor < 0.0001) {
326 ESP_LOGE(TAG, "set_retry: backoff_factor %0.1f too small, using 1.0: %s", backoff_increase_factor,
327 (name_type == NameType::STATIC_STRING && static_name) ? static_name : "");
328 backoff_increase_factor = 1;
329 }
330
331 auto args = std::make_shared<RetryArgs>();
332 args->func = std::move(func);
333 args->component = component;
334 args->scheduler = this;
335 args->name_type = name_type;
336 if (name_type == NameType::STATIC_STRING) {
337 args->name_.static_name = static_name;
338 } else {
339 args->name_.hash_or_id = hash_or_id;
340 }
341 args->current_interval = initial_wait_time;
342 args->backoff_increase_factor = backoff_increase_factor;
343 args->retry_countdown = max_attempts;
344
345 // First execution of `func` immediately - use set_timer_common_ with is_retry=true
346 this->set_timer_common_(
347 component, SchedulerItem::TIMEOUT, name_type, static_name, hash_or_id, 0, [args]() { retry_handler(args); },
348 /* is_retry= */ true);
349}
350
351void HOT Scheduler::set_retry(Component *component, const char *name, uint32_t initial_wait_time, uint8_t max_attempts,
352 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
353 this->set_retry_common_(component, NameType::STATIC_STRING, name, 0, initial_wait_time, max_attempts, std::move(func),
354 backoff_increase_factor);
355}
356
357bool HOT Scheduler::cancel_retry_(Component *component, NameType name_type, const char *static_name,
358 uint32_t hash_or_id) {
359 return this->cancel_item_(component, name_type, static_name, hash_or_id, SchedulerItem::TIMEOUT,
360 /* match_retry= */ true);
361}
362bool HOT Scheduler::cancel_retry(Component *component, const char *name) {
363 return this->cancel_retry_(component, NameType::STATIC_STRING, name, 0);
364}
365
366void HOT Scheduler::set_retry(Component *component, const std::string &name, uint32_t initial_wait_time,
367 uint8_t max_attempts, std::function<RetryResult(uint8_t)> func,
368 float backoff_increase_factor) {
369 this->set_retry_common_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name), initial_wait_time,
370 max_attempts, std::move(func), backoff_increase_factor);
371}
372
373bool HOT Scheduler::cancel_retry(Component *component, const std::string &name) {
374 return this->cancel_retry_(component, NameType::HASHED_STRING, nullptr, fnv1a_hash(name));
375}
376
377void HOT Scheduler::set_retry(Component *component, uint32_t id, uint32_t initial_wait_time, uint8_t max_attempts,
378 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
379 this->set_retry_common_(component, NameType::NUMERIC_ID, nullptr, id, initial_wait_time, max_attempts,
380 std::move(func), backoff_increase_factor);
381}
382
383bool HOT Scheduler::cancel_retry(Component *component, uint32_t id) {
384 return this->cancel_retry_(component, NameType::NUMERIC_ID, nullptr, id);
385}
386
387#pragma GCC diagnostic pop // End suppression of deprecated RetryResult warnings
388
389optional<uint32_t> HOT Scheduler::next_schedule_in(uint32_t now) {
390 // IMPORTANT: This method should only be called from the main thread (loop task).
391 // It performs cleanup and accesses items_[0] without holding a lock, which is only
392 // safe when called from the main thread. Other threads must not call this method.
393
394 // If no items, return empty optional
395 if (this->cleanup_() == 0)
396 return {};
397
398 auto &item = this->items_[0];
399 const auto now_64 = this->millis_64_from_(now);
400 const uint64_t next_exec = item->get_next_execution();
401 if (next_exec < now_64)
402 return 0;
403 return next_exec - now_64;
404}
405
406void Scheduler::full_cleanup_removed_items_() {
407 // We hold the lock for the entire cleanup operation because:
408 // 1. We're rebuilding the entire items_ list, so we need exclusive access throughout
409 // 2. Other threads must see either the old state or the new state, not intermediate states
410 // 3. The operation is already expensive (O(n)), so lock overhead is negligible
411 // 4. No operations inside can block or take other locks, so no deadlock risk
412 LockGuard guard{this->lock_};
413
414 // Compact in-place: move valid items forward, recycle removed ones
415 size_t write = 0;
416 for (size_t read = 0; read < this->items_.size(); ++read) {
417 if (!is_item_removed_locked_(this->items_[read].get())) {
418 if (write != read) {
419 this->items_[write] = std::move(this->items_[read]);
420 }
421 ++write;
422 } else {
423 this->recycle_item_main_loop_(std::move(this->items_[read]));
424 }
425 }
426 this->items_.erase(this->items_.begin() + write, this->items_.end());
427 // Rebuild the heap structure since items are no longer in heap order
428 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
429 this->to_remove_ = 0;
430}
431
432#ifndef ESPHOME_THREAD_SINGLE
433void Scheduler::compact_defer_queue_locked_() {
434 // Rare case: new items were added during processing - compact the vector
435 // This only happens when:
436 // 1. A deferred callback calls defer() again, or
437 // 2. Another thread calls defer() while we're processing
438 //
439 // Move unprocessed items (added during this loop) to the front for next iteration
440 //
441 // SAFETY: Compacted items may include cancelled items (marked for removal via
442 // cancel_item_locked_() during execution). This is safe because should_skip_item_()
443 // checks is_item_removed_() before executing, so cancelled items will be skipped
444 // and recycled on the next loop iteration.
445 size_t remaining = this->defer_queue_.size() - this->defer_queue_front_;
446 for (size_t i = 0; i < remaining; i++) {
447 this->defer_queue_[i] = std::move(this->defer_queue_[this->defer_queue_front_ + i]);
448 }
449 // Use erase() instead of resize() to avoid instantiating _M_default_append
450 // (saves ~156 bytes flash). Erasing from the end is O(1) - no shifting needed.
451 this->defer_queue_.erase(this->defer_queue_.begin() + remaining, this->defer_queue_.end());
452}
453#endif /* not ESPHOME_THREAD_SINGLE */
454
455void HOT Scheduler::call(uint32_t now) {
456#ifndef ESPHOME_THREAD_SINGLE
457 this->process_defer_queue_(now);
458#endif /* not ESPHOME_THREAD_SINGLE */
459
460 // Extend the caller's 32-bit timestamp to 64-bit for scheduler operations
461 const auto now_64 = this->millis_64_from_(now);
462 this->process_to_add();
463
464 // Track if any items were added to to_add_ during this call (intervals or from callbacks)
465 bool has_added_items = false;
466
467#ifdef ESPHOME_DEBUG_SCHEDULER
468 static uint64_t last_print = 0;
469
470 if (now_64 - last_print > 2000) {
471 last_print = now_64;
472 std::vector<SchedulerItemPtr> old_items;
473 ESP_LOGD(TAG, "Items: count=%zu, pool=%zu, now=%" PRIu64, this->items_.size(), this->scheduler_item_pool_.size(),
474 now_64);
475 // Cleanup before debug output
476 this->cleanup_();
477 while (!this->items_.empty()) {
478 SchedulerItemPtr item;
479 {
480 LockGuard guard{this->lock_};
481 item = this->pop_raw_locked_();
482 }
483
484 SchedulerNameLog name_log;
485 bool is_cancelled = is_item_removed_(item.get());
486 ESP_LOGD(TAG, " %s '%s/%s' interval=%" PRIu32 " next_execution in %" PRIu64 "ms at %" PRIu64 "%s",
487 item->get_type_str(), LOG_STR_ARG(item->get_source()),
488 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
489 item->get_next_execution() - now_64, item->get_next_execution(), is_cancelled ? " [CANCELLED]" : "");
490
491 old_items.push_back(std::move(item));
492 }
493 ESP_LOGD(TAG, "\n");
494
495 {
496 LockGuard guard{this->lock_};
497 this->items_ = std::move(old_items);
498 // Rebuild heap after moving items back
499 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
500 }
501 }
502#endif /* ESPHOME_DEBUG_SCHEDULER */
503
504 // Cleanup removed items before processing
505 // First try to clean items from the top of the heap (fast path)
506 this->cleanup_();
507
508 // If we still have too many cancelled items, do a full cleanup
509 // This only happens if cancelled items are stuck in the middle/bottom of the heap
510 if (this->to_remove_ >= MAX_LOGICALLY_DELETED_ITEMS) {
511 this->full_cleanup_removed_items_();
512 }
513 while (!this->items_.empty()) {
514 // Don't copy-by value yet
515 auto &item = this->items_[0];
516 if (item->get_next_execution() > now_64) {
517 // Not reached timeout yet, done for this call
518 break;
519 }
520 // Don't run on failed components
521 if (item->component != nullptr && item->component->is_failed()) {
522 LockGuard guard{this->lock_};
523 this->recycle_item_main_loop_(this->pop_raw_locked_());
524 continue;
525 }
526
527 // Check if item is marked for removal
528 // This handles two cases:
529 // 1. Item was marked for removal after cleanup_() but before we got here
530 // 2. Item is marked for removal but wasn't at the front of the heap during cleanup_()
531#ifdef ESPHOME_THREAD_MULTI_NO_ATOMICS
532 // Multi-threaded platforms without atomics: must take lock to safely read remove flag
533 {
534 LockGuard guard{this->lock_};
535 if (is_item_removed_locked_(item.get())) {
536 this->recycle_item_main_loop_(this->pop_raw_locked_());
537 this->to_remove_--;
538 continue;
539 }
540 }
541#else
542 // Single-threaded or multi-threaded with atomics: can check without lock
543 if (is_item_removed_(item.get())) {
544 LockGuard guard{this->lock_};
545 this->recycle_item_main_loop_(this->pop_raw_locked_());
546 this->to_remove_--;
547 continue;
548 }
549#endif
550
551#ifdef ESPHOME_DEBUG_SCHEDULER
552 {
553 SchedulerNameLog name_log;
554 ESP_LOGV(TAG, "Running %s '%s/%s' with interval=%" PRIu32 " next_execution=%" PRIu64 " (now=%" PRIu64 ")",
555 item->get_type_str(), LOG_STR_ARG(item->get_source()),
556 name_log.format(item->get_name_type(), item->get_name(), item->get_name_hash_or_id()), item->interval,
557 item->get_next_execution(), now_64);
558 }
559#endif /* ESPHOME_DEBUG_SCHEDULER */
560
561 // Warning: During callback(), a lot of stuff can happen, including:
562 // - timeouts/intervals get added, potentially invalidating vector pointers
563 // - timeouts/intervals get cancelled
564 now = this->execute_item_(item.get(), now);
565
566 LockGuard guard{this->lock_};
567
568 // Only pop after function call, this ensures we were reachable
569 // during the function call and know if we were cancelled.
570 auto executed_item = this->pop_raw_locked_();
571
572 if (this->is_item_removed_locked_(executed_item.get())) {
573 // We were removed/cancelled in the function call, recycle and continue
574 this->to_remove_--;
575 this->recycle_item_main_loop_(std::move(executed_item));
576 continue;
577 }
578
579 if (executed_item->type == SchedulerItem::INTERVAL) {
580 executed_item->set_next_execution(now_64 + executed_item->interval);
581 // Add new item directly to to_add_
582 // since we have the lock held
583 this->to_add_.push_back(std::move(executed_item));
584 } else {
585 // Timeout completed - recycle it
586 this->recycle_item_main_loop_(std::move(executed_item));
587 }
588
589 has_added_items |= !this->to_add_.empty();
590 }
591
592 if (has_added_items) {
593 this->process_to_add();
594 }
595}
596void HOT Scheduler::process_to_add() {
597 LockGuard guard{this->lock_};
598 for (auto &it : this->to_add_) {
599 if (is_item_removed_locked_(it.get())) {
600 // Recycle cancelled items
601 this->recycle_item_main_loop_(std::move(it));
602 continue;
603 }
604
605 this->items_.push_back(std::move(it));
606 std::push_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
607 }
608 this->to_add_.clear();
609}
610size_t HOT Scheduler::cleanup_() {
611 // Fast path: if nothing to remove, just return the current size
612 // Reading to_remove_ without lock is safe because:
613 // 1. We only call this from the main thread during call()
614 // 2. If it's 0, there's definitely nothing to cleanup
615 // 3. If it becomes non-zero after we check, cleanup will happen on the next loop iteration
616 // 4. Not all platforms support atomics, so we accept this race in favor of performance
617 // 5. The worst case is a one-loop-iteration delay in cleanup, which is harmless
618 if (this->to_remove_ == 0)
619 return this->items_.size();
620
621 // We must hold the lock for the entire cleanup operation because:
622 // 1. We're modifying items_ (via pop_raw_locked_) which requires exclusive access
623 // 2. We're decrementing to_remove_ which is also modified by other threads
624 // (though all modifications are already under lock)
625 // 3. Other threads read items_ when searching for items to cancel in cancel_item_locked_()
626 // 4. We need a consistent view of items_ and to_remove_ throughout the operation
627 // Without the lock, we could access items_ while another thread is reading it,
628 // leading to race conditions
629 LockGuard guard{this->lock_};
630 while (!this->items_.empty()) {
631 auto &item = this->items_[0];
632 if (!this->is_item_removed_locked_(item.get()))
633 break;
634 this->to_remove_--;
635 this->recycle_item_main_loop_(this->pop_raw_locked_());
636 }
637 return this->items_.size();
638}
639Scheduler::SchedulerItemPtr HOT Scheduler::pop_raw_locked_() {
640 std::pop_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
641
642 // Move the item out before popping - this is the item that was at the front of the heap
643 auto item = std::move(this->items_.back());
644
645 this->items_.pop_back();
646 return item;
647}
648
649// Helper to execute a scheduler item
650uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) {
651 App.set_current_component(item->component);
652 WarnIfComponentBlockingGuard guard{item->component, now};
653 item->callback();
654 return guard.finish();
655}
656
657// Common implementation for cancel operations - handles locking
658bool HOT Scheduler::cancel_item_(Component *component, NameType name_type, const char *static_name, uint32_t hash_or_id,
659 SchedulerItem::Type type, bool match_retry) {
660 LockGuard guard{this->lock_};
661 return this->cancel_item_locked_(component, name_type, static_name, hash_or_id, type, match_retry);
662}
663
664// Helper to cancel items - must be called with lock held
665// name_type determines matching: STATIC_STRING uses static_name, others use hash_or_id
666bool HOT Scheduler::cancel_item_locked_(Component *component, NameType name_type, const char *static_name,
667 uint32_t hash_or_id, SchedulerItem::Type type, bool match_retry) {
668 // Early return if static string name is invalid
669 if (name_type == NameType::STATIC_STRING && static_name == nullptr) {
670 return false;
671 }
672
673 size_t total_cancelled = 0;
674
675#ifndef ESPHOME_THREAD_SINGLE
676 // Mark items in defer queue as cancelled (they'll be skipped when processed)
677 if (type == SchedulerItem::TIMEOUT) {
678 total_cancelled += this->mark_matching_items_removed_locked_(this->defer_queue_, component, name_type, static_name,
679 hash_or_id, type, match_retry);
680 }
681#endif /* not ESPHOME_THREAD_SINGLE */
682
683 // Cancel items in the main heap
684 // We only mark items for removal here - never recycle directly.
685 // The main loop may be executing an item's callback right now, and recycling
686 // would destroy the callback while it's running (use-after-free).
687 // Only the main loop in call() should recycle items after execution completes.
688 if (!this->items_.empty()) {
689 size_t heap_cancelled = this->mark_matching_items_removed_locked_(this->items_, component, name_type, static_name,
690 hash_or_id, type, match_retry);
691 total_cancelled += heap_cancelled;
692 this->to_remove_ += heap_cancelled;
693 }
694
695 // Cancel items in to_add_
696 total_cancelled += this->mark_matching_items_removed_locked_(this->to_add_, component, name_type, static_name,
697 hash_or_id, type, match_retry);
698
699 return total_cancelled > 0;
700}
701
702bool HOT Scheduler::SchedulerItem::cmp(const SchedulerItemPtr &a, const SchedulerItemPtr &b) {
703 // High bits are almost always equal (change only on 32-bit rollover ~49 days)
704 // Optimize for common case: check low bits first when high bits are equal
705 return (a->next_execution_high_ == b->next_execution_high_) ? (a->next_execution_low_ > b->next_execution_low_)
706 : (a->next_execution_high_ > b->next_execution_high_);
707}
708
709// Recycle a SchedulerItem back to the pool for reuse.
710// IMPORTANT: Caller must hold the scheduler lock before calling this function.
711// This protects scheduler_item_pool_ from concurrent access by other threads
712// that may be acquiring items from the pool in set_timer_common_().
713void Scheduler::recycle_item_main_loop_(SchedulerItemPtr item) {
714 if (!item)
715 return;
716
717 if (this->scheduler_item_pool_.size() < MAX_POOL_SIZE) {
718 // Clear callback to release captured resources
719 item->callback = nullptr;
720 this->scheduler_item_pool_.push_back(std::move(item));
721#ifdef ESPHOME_DEBUG_SCHEDULER
722 ESP_LOGD(TAG, "Recycled item to pool (pool size now: %zu)", this->scheduler_item_pool_.size());
723#endif
724 } else {
725#ifdef ESPHOME_DEBUG_SCHEDULER
726 ESP_LOGD(TAG, "Pool full (size: %zu), deleting item", this->scheduler_item_pool_.size());
727#endif
728 }
729 // else: unique_ptr will delete the item when it goes out of scope
730}
731
732#ifdef ESPHOME_DEBUG_SCHEDULER
733void Scheduler::debug_log_timer_(const SchedulerItem *item, NameType name_type, const char *static_name,
734 uint32_t hash_or_id, SchedulerItem::Type type, uint32_t delay, uint64_t now) {
735 // Validate static strings in debug mode
736 if (name_type == NameType::STATIC_STRING && static_name != nullptr) {
737 validate_static_string(static_name);
738 }
739
740 // Debug logging
741 SchedulerNameLog name_log;
742 const char *type_str = (type == SchedulerItem::TIMEOUT) ? "timeout" : "interval";
743 if (type == SchedulerItem::TIMEOUT) {
744 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
745 name_log.format(name_type, static_name, hash_or_id), type_str, delay);
746 } else {
747 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ", offset=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
748 name_log.format(name_type, static_name, hash_or_id), type_str, delay,
749 static_cast<uint32_t>(item->get_next_execution() - now));
750 }
751}
752#endif /* ESPHOME_DEBUG_SCHEDULER */
753
754// Helper to get or create a scheduler item from the pool
755// IMPORTANT: Caller must hold the scheduler lock before calling this function.
756Scheduler::SchedulerItemPtr Scheduler::get_item_from_pool_locked_() {
757 SchedulerItemPtr item;
758 if (!this->scheduler_item_pool_.empty()) {
759 item = std::move(this->scheduler_item_pool_.back());
760 this->scheduler_item_pool_.pop_back();
761#ifdef ESPHOME_DEBUG_SCHEDULER
762 ESP_LOGD(TAG, "Reused item from pool (pool size now: %zu)", this->scheduler_item_pool_.size());
763#endif
764 } else {
765 item = SchedulerItemPtr(new SchedulerItem());
766#ifdef ESPHOME_DEBUG_SCHEDULER
767 ESP_LOGD(TAG, "Allocated new item (pool empty)");
768#endif
769 }
770 return item;
771}
772
773} // namespace esphome
void set_current_component(Component *component)
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:387
const Component * component
Definition component.cpp:37
uint16_t type
static float float b
const char *const TAG
Definition spi.cpp:7
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
float random_float()
Return a random float between 0 and 1.
Definition helpers.cpp:159
void retry_handler(const std::shared_ptr< RetryArgs > &args)
uint64_t HOT millis_64()
Definition core.cpp:26
void HOT delay(uint32_t ms)
Definition core.cpp:27
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:599
constexpr uint32_t SCHEDULER_DONT_RUN
Definition component.h:49