ESPHome 2025.10.1
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"
8#include <algorithm>
9#include <cinttypes>
10#include <cstring>
11#include <limits>
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// Half the 32-bit range - used to detect rollovers vs normal time progression
31static constexpr uint32_t HALF_MAX_UINT32 = std::numeric_limits<uint32_t>::max() / 2;
32// max delay to start an interval sequence
33static constexpr uint32_t MAX_INTERVAL_DELAY = 5000;
34
35// Uncomment to debug scheduler
36// #define ESPHOME_DEBUG_SCHEDULER
37
38#ifdef ESPHOME_DEBUG_SCHEDULER
39// Helper to validate that a pointer looks like it's in static memory
40static void validate_static_string(const char *name) {
41 if (name == nullptr)
42 return;
43
44 // This is a heuristic check - stack and heap pointers are typically
45 // much higher in memory than static data
46 uintptr_t addr = reinterpret_cast<uintptr_t>(name);
47
48 // Create a stack variable to compare against
49 int stack_var;
50 uintptr_t stack_addr = reinterpret_cast<uintptr_t>(&stack_var);
51
52 // If the string pointer is near our stack variable, it's likely on the stack
53 // Using 8KB range as ESP32 main task stack is typically 8192 bytes
54 if (addr > (stack_addr - 0x2000) && addr < (stack_addr + 0x2000)) {
55 ESP_LOGW(TAG,
56 "WARNING: Scheduler name '%s' at %p appears to be on the stack - this is unsafe!\n"
57 " Stack reference at %p",
58 name, name, &stack_var);
59 }
60
61 // Also check if it might be on the heap by seeing if it's in a very different range
62 // This is platform-specific but generally heap is allocated far from static memory
63 static const char *static_str = "test";
64 uintptr_t static_addr = reinterpret_cast<uintptr_t>(static_str);
65
66 // If the address is very far from known static memory, it might be heap
67 if (addr > static_addr + 0x100000 || (static_addr > 0x100000 && addr < static_addr - 0x100000)) {
68 ESP_LOGW(TAG, "WARNING: Scheduler name '%s' at %p might be on heap (static ref at %p)", name, name, static_str);
69 }
70}
71#endif /* ESPHOME_DEBUG_SCHEDULER */
72
73// A note on locking: the `lock_` lock protects the `items_` and `to_add_` containers. It must be taken when writing to
74// them (i.e. when adding/removing items, but not when changing items). As items are only deleted from the loop task,
75// iterating over them from the loop task is fine; but iterating from any other context requires the lock to be held to
76// avoid the main thread modifying the list while it is being accessed.
77
78// Common implementation for both timeout and interval
79void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type type, bool is_static_string,
80 const void *name_ptr, uint32_t delay, std::function<void()> func, bool is_retry,
81 bool skip_cancel) {
82 // Get the name as const char*
83 const char *name_cstr = this->get_name_cstr_(is_static_string, name_ptr);
84
85 if (delay == SCHEDULER_DONT_RUN) {
86 // Still need to cancel existing timer if name is not empty
87 if (!skip_cancel) {
88 LockGuard guard{this->lock_};
89 this->cancel_item_locked_(component, name_cstr, type);
90 }
91 return;
92 }
93
94 // Get fresh timestamp BEFORE taking lock - millis_64_ may need to acquire lock itself
95 const uint64_t now = this->millis_64_(millis());
96
97 // Take lock early to protect scheduler_item_pool_ access
98 LockGuard guard{this->lock_};
99
100 // Create and populate the scheduler item
101 std::unique_ptr<SchedulerItem> item;
102 if (!this->scheduler_item_pool_.empty()) {
103 // Reuse from pool
104 item = std::move(this->scheduler_item_pool_.back());
105 this->scheduler_item_pool_.pop_back();
106#ifdef ESPHOME_DEBUG_SCHEDULER
107 ESP_LOGD(TAG, "Reused item from pool (pool size now: %zu)", this->scheduler_item_pool_.size());
108#endif
109 } else {
110 // Allocate new if pool is empty
111 item = make_unique<SchedulerItem>();
112#ifdef ESPHOME_DEBUG_SCHEDULER
113 ESP_LOGD(TAG, "Allocated new item (pool empty)");
114#endif
115 }
116 item->component = component;
117 item->set_name(name_cstr, !is_static_string);
118 item->type = type;
119 item->callback = std::move(func);
120 // Initialize remove to false (though it should already be from constructor)
121#ifdef ESPHOME_THREAD_MULTI_ATOMICS
122 item->remove.store(false, std::memory_order_relaxed);
123#else
124 item->remove = false;
125#endif
126 item->is_retry = is_retry;
127
128#ifndef ESPHOME_THREAD_SINGLE
129 // Special handling for defer() (delay = 0, type = TIMEOUT)
130 // Single-core platforms don't need thread-safe defer handling
131 if (delay == 0 && type == SchedulerItem::TIMEOUT) {
132 // Put in defer queue for guaranteed FIFO execution
133 if (!skip_cancel) {
134 this->cancel_item_locked_(component, name_cstr, type);
135 }
136 this->defer_queue_.push_back(std::move(item));
137 return;
138 }
139#endif /* not ESPHOME_THREAD_SINGLE */
140
141 // Type-specific setup
142 if (type == SchedulerItem::INTERVAL) {
143 item->interval = delay;
144 // first execution happens immediately after a random smallish offset
145 // Calculate random offset (0 to min(interval/2, 5s))
146 uint32_t offset = (uint32_t) (std::min(delay / 2, MAX_INTERVAL_DELAY) * random_float());
147 item->set_next_execution(now + offset);
148 ESP_LOGV(TAG, "Scheduler interval for %s is %" PRIu32 "ms, offset %" PRIu32 "ms", name_cstr ? name_cstr : "", delay,
149 offset);
150 } else {
151 item->interval = 0;
152 item->set_next_execution(now + delay);
153 }
154
155#ifdef ESPHOME_DEBUG_SCHEDULER
156 // Validate static strings in debug mode
157 if (is_static_string && name_cstr != nullptr) {
158 validate_static_string(name_cstr);
159 }
160
161 // Debug logging
162 const char *type_str = (type == SchedulerItem::TIMEOUT) ? "timeout" : "interval";
163 if (type == SchedulerItem::TIMEOUT) {
164 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
165 name_cstr ? name_cstr : "(null)", type_str, delay);
166 } else {
167 ESP_LOGD(TAG, "set_%s(name='%s/%s', %s=%" PRIu32 ", offset=%" PRIu32 ")", type_str, LOG_STR_ARG(item->get_source()),
168 name_cstr ? name_cstr : "(null)", type_str, delay,
169 static_cast<uint32_t>(item->get_next_execution() - now));
170 }
171#endif /* ESPHOME_DEBUG_SCHEDULER */
172
173 // For retries, check if there's a cancelled timeout first
174 if (is_retry && name_cstr != nullptr && type == SchedulerItem::TIMEOUT &&
175 (has_cancelled_timeout_in_container_(this->items_, component, name_cstr, /* match_retry= */ true) ||
176 has_cancelled_timeout_in_container_(this->to_add_, component, name_cstr, /* match_retry= */ true))) {
177 // Skip scheduling - the retry was cancelled
178#ifdef ESPHOME_DEBUG_SCHEDULER
179 ESP_LOGD(TAG, "Skipping retry '%s' - found cancelled item", name_cstr);
180#endif
181 return;
182 }
183
184 // If name is provided, do atomic cancel-and-add (unless skip_cancel is true)
185 // Cancel existing items
186 if (!skip_cancel) {
187 this->cancel_item_locked_(component, name_cstr, type);
188 }
189 // Add new item directly to to_add_
190 // since we have the lock held
191 this->to_add_.push_back(std::move(item));
192}
193
194void HOT Scheduler::set_timeout(Component *component, const char *name, uint32_t timeout, std::function<void()> func) {
195 this->set_timer_common_(component, SchedulerItem::TIMEOUT, true, name, timeout, std::move(func));
196}
197
198void HOT Scheduler::set_timeout(Component *component, const std::string &name, uint32_t timeout,
199 std::function<void()> func) {
200 this->set_timer_common_(component, SchedulerItem::TIMEOUT, false, &name, timeout, std::move(func));
201}
202bool HOT Scheduler::cancel_timeout(Component *component, const std::string &name) {
203 return this->cancel_item_(component, false, &name, SchedulerItem::TIMEOUT);
204}
205bool HOT Scheduler::cancel_timeout(Component *component, const char *name) {
206 return this->cancel_item_(component, true, name, SchedulerItem::TIMEOUT);
207}
208void HOT Scheduler::set_interval(Component *component, const std::string &name, uint32_t interval,
209 std::function<void()> func) {
210 this->set_timer_common_(component, SchedulerItem::INTERVAL, false, &name, interval, std::move(func));
211}
212
213void HOT Scheduler::set_interval(Component *component, const char *name, uint32_t interval,
214 std::function<void()> func) {
215 this->set_timer_common_(component, SchedulerItem::INTERVAL, true, name, interval, std::move(func));
216}
217bool HOT Scheduler::cancel_interval(Component *component, const std::string &name) {
218 return this->cancel_item_(component, false, &name, SchedulerItem::INTERVAL);
219}
220bool HOT Scheduler::cancel_interval(Component *component, const char *name) {
221 return this->cancel_item_(component, true, name, SchedulerItem::INTERVAL);
222}
223
224struct RetryArgs {
225 std::function<RetryResult(uint8_t)> func;
226 uint8_t retry_countdown;
227 uint32_t current_interval;
228 Component *component;
229 std::string name; // Keep as std::string since retry uses it dynamically
230 float backoff_increase_factor;
231 Scheduler *scheduler;
232};
233
234void retry_handler(const std::shared_ptr<RetryArgs> &args) {
235 RetryResult const retry_result = args->func(--args->retry_countdown);
236 if (retry_result == RetryResult::DONE || args->retry_countdown <= 0)
237 return;
238 // second execution of `func` happens after `initial_wait_time`
239 args->scheduler->set_timer_common_(
240 args->component, Scheduler::SchedulerItem::TIMEOUT, false, &args->name, args->current_interval,
241 [args]() { retry_handler(args); }, /* is_retry= */ true);
242 // backoff_increase_factor applied to third & later executions
243 args->current_interval *= args->backoff_increase_factor;
244}
245
246void HOT Scheduler::set_retry_common_(Component *component, bool is_static_string, const void *name_ptr,
247 uint32_t initial_wait_time, uint8_t max_attempts,
248 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
249 const char *name_cstr = this->get_name_cstr_(is_static_string, name_ptr);
250
251 if (name_cstr != nullptr)
252 this->cancel_retry(component, name_cstr);
253
254 if (initial_wait_time == SCHEDULER_DONT_RUN)
255 return;
256
257 ESP_LOGVV(TAG, "set_retry(name='%s', initial_wait_time=%" PRIu32 ", max_attempts=%u, backoff_factor=%0.1f)",
258 name_cstr ? name_cstr : "", initial_wait_time, max_attempts, backoff_increase_factor);
259
260 if (backoff_increase_factor < 0.0001) {
261 ESP_LOGE(TAG, "backoff_factor %0.1f too small, using 1.0: %s", backoff_increase_factor, name_cstr ? name_cstr : "");
262 backoff_increase_factor = 1;
263 }
264
265 auto args = std::make_shared<RetryArgs>();
266 args->func = std::move(func);
267 args->retry_countdown = max_attempts;
268 args->current_interval = initial_wait_time;
269 args->component = component;
270 args->name = name_cstr ? name_cstr : ""; // Convert to std::string for RetryArgs
271 args->backoff_increase_factor = backoff_increase_factor;
272 args->scheduler = this;
273
274 // First execution of `func` immediately - use set_timer_common_ with is_retry=true
275 this->set_timer_common_(
276 component, SchedulerItem::TIMEOUT, false, &args->name, 0, [args]() { retry_handler(args); },
277 /* is_retry= */ true);
278}
279
280void HOT Scheduler::set_retry(Component *component, const std::string &name, uint32_t initial_wait_time,
281 uint8_t max_attempts, std::function<RetryResult(uint8_t)> func,
282 float backoff_increase_factor) {
283 this->set_retry_common_(component, false, &name, initial_wait_time, max_attempts, std::move(func),
284 backoff_increase_factor);
285}
286
287void HOT Scheduler::set_retry(Component *component, const char *name, uint32_t initial_wait_time, uint8_t max_attempts,
288 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor) {
289 this->set_retry_common_(component, true, name, initial_wait_time, max_attempts, std::move(func),
290 backoff_increase_factor);
291}
292bool HOT Scheduler::cancel_retry(Component *component, const std::string &name) {
293 return this->cancel_retry(component, name.c_str());
294}
295
296bool HOT Scheduler::cancel_retry(Component *component, const char *name) {
297 // Cancel timeouts that have is_retry flag set
298 LockGuard guard{this->lock_};
299 return this->cancel_item_locked_(component, name, SchedulerItem::TIMEOUT, /* match_retry= */ true);
300}
301
302optional<uint32_t> HOT Scheduler::next_schedule_in(uint32_t now) {
303 // IMPORTANT: This method should only be called from the main thread (loop task).
304 // It performs cleanup and accesses items_[0] without holding a lock, which is only
305 // safe when called from the main thread. Other threads must not call this method.
306
307 // If no items, return empty optional
308 if (this->cleanup_() == 0)
309 return {};
310
311 auto &item = this->items_[0];
312 // Convert the fresh timestamp from caller (usually Application::loop()) to 64-bit
313 const auto now_64 = this->millis_64_(now); // 'now' from parameter - fresh from caller
314 const uint64_t next_exec = item->get_next_execution();
315 if (next_exec < now_64)
316 return 0;
317 return next_exec - now_64;
318}
319void HOT Scheduler::call(uint32_t now) {
320#ifndef ESPHOME_THREAD_SINGLE
321 // Process defer queue first to guarantee FIFO execution order for deferred items.
322 // Previously, defer() used the heap which gave undefined order for equal timestamps,
323 // causing race conditions on multi-core systems (ESP32, BK7200).
324 // With the defer queue:
325 // - Deferred items (delay=0) go directly to defer_queue_ in set_timer_common_
326 // - Items execute in exact order they were deferred (FIFO guarantee)
327 // - No deferred items exist in to_add_, so processing order doesn't affect correctness
328 // Single-core platforms don't use this queue and fall back to the heap-based approach.
329 //
330 // Note: Items cancelled via cancel_item_locked_() are marked with remove=true but still
331 // processed here. They are removed from the queue normally via pop_front() but skipped
332 // during execution by should_skip_item_(). This is intentional - no memory leak occurs.
333 while (!this->defer_queue_.empty()) {
334 // The outer check is done without a lock for performance. If the queue
335 // appears non-empty, we lock and process an item. We don't need to check
336 // empty() again inside the lock because only this thread can remove items.
337 std::unique_ptr<SchedulerItem> item;
338 {
339 LockGuard lock(this->lock_);
340 item = std::move(this->defer_queue_.front());
341 this->defer_queue_.pop_front();
342 }
343
344 // Execute callback without holding lock to prevent deadlocks
345 // if the callback tries to call defer() again
346 if (!this->should_skip_item_(item.get())) {
347 now = this->execute_item_(item.get(), now);
348 }
349 // Recycle the defer item after execution
350 this->recycle_item_(std::move(item));
351 }
352#endif /* not ESPHOME_THREAD_SINGLE */
353
354 // Convert the fresh timestamp from main loop to 64-bit for scheduler operations
355 const auto now_64 = this->millis_64_(now); // 'now' from parameter - fresh from Application::loop()
356 this->process_to_add();
357
358 // Track if any items were added to to_add_ during this call (intervals or from callbacks)
359 bool has_added_items = false;
360
361#ifdef ESPHOME_DEBUG_SCHEDULER
362 static uint64_t last_print = 0;
363
364 if (now_64 - last_print > 2000) {
365 last_print = now_64;
366 std::vector<std::unique_ptr<SchedulerItem>> old_items;
367#ifdef ESPHOME_THREAD_MULTI_ATOMICS
368 const auto last_dbg = this->last_millis_.load(std::memory_order_relaxed);
369 const auto major_dbg = this->millis_major_.load(std::memory_order_relaxed);
370 ESP_LOGD(TAG, "Items: count=%zu, pool=%zu, now=%" PRIu64 " (%" PRIu16 ", %" PRIu32 ")", this->items_.size(),
371 this->scheduler_item_pool_.size(), now_64, major_dbg, last_dbg);
372#else /* not ESPHOME_THREAD_MULTI_ATOMICS */
373 ESP_LOGD(TAG, "Items: count=%zu, pool=%zu, now=%" PRIu64 " (%" PRIu16 ", %" PRIu32 ")", this->items_.size(),
374 this->scheduler_item_pool_.size(), now_64, this->millis_major_, this->last_millis_);
375#endif /* else ESPHOME_THREAD_MULTI_ATOMICS */
376 // Cleanup before debug output
377 this->cleanup_();
378 while (!this->items_.empty()) {
379 std::unique_ptr<SchedulerItem> item;
380 {
381 LockGuard guard{this->lock_};
382 item = std::move(this->items_[0]);
383 this->pop_raw_();
384 }
385
386 const char *name = item->get_name();
387 bool is_cancelled = is_item_removed_(item.get());
388 ESP_LOGD(TAG, " %s '%s/%s' interval=%" PRIu32 " next_execution in %" PRIu64 "ms at %" PRIu64 "%s",
389 item->get_type_str(), LOG_STR_ARG(item->get_source()), name ? name : "(null)", item->interval,
390 item->get_next_execution() - now_64, item->get_next_execution(), is_cancelled ? " [CANCELLED]" : "");
391
392 old_items.push_back(std::move(item));
393 }
394 ESP_LOGD(TAG, "\n");
395
396 {
397 LockGuard guard{this->lock_};
398 this->items_ = std::move(old_items);
399 // Rebuild heap after moving items back
400 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
401 }
402 }
403#endif /* ESPHOME_DEBUG_SCHEDULER */
404
405 // Cleanup removed items before processing
406 // First try to clean items from the top of the heap (fast path)
407 this->cleanup_();
408
409 // If we still have too many cancelled items, do a full cleanup
410 // This only happens if cancelled items are stuck in the middle/bottom of the heap
411 if (this->to_remove_ >= MAX_LOGICALLY_DELETED_ITEMS) {
412 // We hold the lock for the entire cleanup operation because:
413 // 1. We're rebuilding the entire items_ list, so we need exclusive access throughout
414 // 2. Other threads must see either the old state or the new state, not intermediate states
415 // 3. The operation is already expensive (O(n)), so lock overhead is negligible
416 // 4. No operations inside can block or take other locks, so no deadlock risk
417 LockGuard guard{this->lock_};
418
419 std::vector<std::unique_ptr<SchedulerItem>> valid_items;
420
421 // Move all non-removed items to valid_items, recycle removed ones
422 for (auto &item : this->items_) {
423 if (!is_item_removed_(item.get())) {
424 valid_items.push_back(std::move(item));
425 } else {
426 // Recycle removed items
427 this->recycle_item_(std::move(item));
428 }
429 }
430
431 // Replace items_ with the filtered list
432 this->items_ = std::move(valid_items);
433 // Rebuild the heap structure since items are no longer in heap order
434 std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
435 this->to_remove_ = 0;
436 }
437 while (!this->items_.empty()) {
438 // Don't copy-by value yet
439 auto &item = this->items_[0];
440 if (item->get_next_execution() > now_64) {
441 // Not reached timeout yet, done for this call
442 break;
443 }
444 // Don't run on failed components
445 if (item->component != nullptr && item->component->is_failed()) {
446 LockGuard guard{this->lock_};
447 this->pop_raw_();
448 continue;
449 }
450
451 // Check if item is marked for removal
452 // This handles two cases:
453 // 1. Item was marked for removal after cleanup_() but before we got here
454 // 2. Item is marked for removal but wasn't at the front of the heap during cleanup_()
455#ifdef ESPHOME_THREAD_MULTI_NO_ATOMICS
456 // Multi-threaded platforms without atomics: must take lock to safely read remove flag
457 {
458 LockGuard guard{this->lock_};
459 if (is_item_removed_(item.get())) {
460 this->pop_raw_();
461 this->to_remove_--;
462 continue;
463 }
464 }
465#else
466 // Single-threaded or multi-threaded with atomics: can check without lock
467 if (is_item_removed_(item.get())) {
468 LockGuard guard{this->lock_};
469 this->pop_raw_();
470 this->to_remove_--;
471 continue;
472 }
473#endif
474
475#ifdef ESPHOME_DEBUG_SCHEDULER
476 const char *item_name = item->get_name();
477 ESP_LOGV(TAG, "Running %s '%s/%s' with interval=%" PRIu32 " next_execution=%" PRIu64 " (now=%" PRIu64 ")",
478 item->get_type_str(), LOG_STR_ARG(item->get_source()), item_name ? item_name : "(null)", item->interval,
479 item->get_next_execution(), now_64);
480#endif /* ESPHOME_DEBUG_SCHEDULER */
481
482 // Warning: During callback(), a lot of stuff can happen, including:
483 // - timeouts/intervals get added, potentially invalidating vector pointers
484 // - timeouts/intervals get cancelled
485 now = this->execute_item_(item.get(), now);
486
487 LockGuard guard{this->lock_};
488
489 auto executed_item = std::move(this->items_[0]);
490 // Only pop after function call, this ensures we were reachable
491 // during the function call and know if we were cancelled.
492 this->pop_raw_();
493
494 if (executed_item->remove) {
495 // We were removed/cancelled in the function call, stop
496 this->to_remove_--;
497 continue;
498 }
499
500 if (executed_item->type == SchedulerItem::INTERVAL) {
501 executed_item->set_next_execution(now_64 + executed_item->interval);
502 // Add new item directly to to_add_
503 // since we have the lock held
504 this->to_add_.push_back(std::move(executed_item));
505 } else {
506 // Timeout completed - recycle it
507 this->recycle_item_(std::move(executed_item));
508 }
509
510 has_added_items |= !this->to_add_.empty();
511 }
512
513 if (has_added_items) {
514 this->process_to_add();
515 }
516}
517void HOT Scheduler::process_to_add() {
518 LockGuard guard{this->lock_};
519 for (auto &it : this->to_add_) {
520 if (is_item_removed_(it.get())) {
521 // Recycle cancelled items
522 this->recycle_item_(std::move(it));
523 continue;
524 }
525
526 this->items_.push_back(std::move(it));
527 std::push_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
528 }
529 this->to_add_.clear();
530}
531size_t HOT Scheduler::cleanup_() {
532 // Fast path: if nothing to remove, just return the current size
533 // Reading to_remove_ without lock is safe because:
534 // 1. We only call this from the main thread during call()
535 // 2. If it's 0, there's definitely nothing to cleanup
536 // 3. If it becomes non-zero after we check, cleanup will happen on the next loop iteration
537 // 4. Not all platforms support atomics, so we accept this race in favor of performance
538 // 5. The worst case is a one-loop-iteration delay in cleanup, which is harmless
539 if (this->to_remove_ == 0)
540 return this->items_.size();
541
542 // We must hold the lock for the entire cleanup operation because:
543 // 1. We're modifying items_ (via pop_raw_) which requires exclusive access
544 // 2. We're decrementing to_remove_ which is also modified by other threads
545 // (though all modifications are already under lock)
546 // 3. Other threads read items_ when searching for items to cancel in cancel_item_locked_()
547 // 4. We need a consistent view of items_ and to_remove_ throughout the operation
548 // Without the lock, we could access items_ while another thread is reading it,
549 // leading to race conditions
550 LockGuard guard{this->lock_};
551 while (!this->items_.empty()) {
552 auto &item = this->items_[0];
553 if (!item->remove)
554 break;
555 this->to_remove_--;
556 this->pop_raw_();
557 }
558 return this->items_.size();
559}
560void HOT Scheduler::pop_raw_() {
561 std::pop_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
562
563 // Instead of destroying, recycle the item
564 this->recycle_item_(std::move(this->items_.back()));
565
566 this->items_.pop_back();
567}
568
569// Helper to execute a scheduler item
570uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) {
571 App.set_current_component(item->component);
572 WarnIfComponentBlockingGuard guard{item->component, now};
573 item->callback();
574 return guard.finish();
575}
576
577// Common implementation for cancel operations
578bool HOT Scheduler::cancel_item_(Component *component, bool is_static_string, const void *name_ptr,
579 SchedulerItem::Type type) {
580 // Get the name as const char*
581 const char *name_cstr = this->get_name_cstr_(is_static_string, name_ptr);
582
583 // obtain lock because this function iterates and can be called from non-loop task context
584 LockGuard guard{this->lock_};
585 return this->cancel_item_locked_(component, name_cstr, type);
586}
587
588// Helper to cancel items by name - must be called with lock held
589bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type,
590 bool match_retry) {
591 // Early return if name is invalid - no items to cancel
592 if (name_cstr == nullptr) {
593 return false;
594 }
595
596 size_t total_cancelled = 0;
597
598 // Check all containers for matching items
599#ifndef ESPHOME_THREAD_SINGLE
600 // Mark items in defer queue as cancelled (they'll be skipped when processed)
601 if (type == SchedulerItem::TIMEOUT) {
602 total_cancelled += this->mark_matching_items_removed_(this->defer_queue_, component, name_cstr, type, match_retry);
603 }
604#endif /* not ESPHOME_THREAD_SINGLE */
605
606 // Cancel items in the main heap
607 // Special case: if the last item in the heap matches, we can remove it immediately
608 // (removing the last element doesn't break heap structure)
609 if (!this->items_.empty()) {
610 auto &last_item = this->items_.back();
611 if (this->matches_item_(last_item, component, name_cstr, type, match_retry)) {
612 this->recycle_item_(std::move(this->items_.back()));
613 this->items_.pop_back();
614 total_cancelled++;
615 }
616 // For other items in heap, we can only mark for removal (can't remove from middle of heap)
617 size_t heap_cancelled = this->mark_matching_items_removed_(this->items_, component, name_cstr, type, match_retry);
618 total_cancelled += heap_cancelled;
619 this->to_remove_ += heap_cancelled; // Track removals for heap items
620 }
621
622 // Cancel items in to_add_
623 total_cancelled += this->mark_matching_items_removed_(this->to_add_, component, name_cstr, type, match_retry);
624
625 return total_cancelled > 0;
626}
627
628uint64_t Scheduler::millis_64_(uint32_t now) {
629 // THREAD SAFETY NOTE:
630 // This function has three implementations, based on the precompiler flags
631 // - ESPHOME_THREAD_SINGLE - Runs on single-threaded platforms (ESP8266, RP2040, etc.)
632 // - ESPHOME_THREAD_MULTI_NO_ATOMICS - Runs on multi-threaded platforms without atomics (LibreTiny)
633 // - ESPHOME_THREAD_MULTI_ATOMICS - Runs on multi-threaded platforms with atomics (ESP32, HOST, etc.)
634 //
635 // Make sure all changes are synchronized if you edit this function.
636 //
637 // IMPORTANT: Always pass fresh millis() values to this function. The implementation
638 // handles out-of-order timestamps between threads, but minimizing time differences
639 // helps maintain accuracy.
640 //
641
642#ifdef ESPHOME_THREAD_SINGLE
643 // This is the single core implementation.
644 //
645 // Single-core platforms have no concurrency, so this is a simple implementation
646 // that just tracks 32-bit rollover (every 49.7 days) without any locking or atomics.
647
648 uint16_t major = this->millis_major_;
649 uint32_t last = this->last_millis_;
650
651 // Check for rollover
652 if (now < last && (last - now) > HALF_MAX_UINT32) {
653 this->millis_major_++;
654 major++;
655#ifdef ESPHOME_DEBUG_SCHEDULER
656 ESP_LOGD(TAG, "Detected true 32-bit rollover at %" PRIu32 "ms (was %" PRIu32 ")", now, last);
657#endif /* ESPHOME_DEBUG_SCHEDULER */
658 }
659
660 // Only update if time moved forward
661 if (now > last) {
662 this->last_millis_ = now;
663 }
664
665 // Combine major (high 32 bits) and now (low 32 bits) into 64-bit time
666 return now + (static_cast<uint64_t>(major) << 32);
667
668#elif defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
669 // This is the multi core no atomics implementation.
670 //
671 // Without atomics, this implementation uses locks more aggressively:
672 // 1. Always locks when near the rollover boundary (within 10 seconds)
673 // 2. Always locks when detecting a large backwards jump
674 // 3. Updates without lock in normal forward progression (accepting minor races)
675 // This is less efficient but necessary without atomic operations.
676 uint16_t major = this->millis_major_;
677 uint32_t last = this->last_millis_;
678
679 // Define a safe window around the rollover point (10 seconds)
680 // This covers any reasonable scheduler delays or thread preemption
681 static const uint32_t ROLLOVER_WINDOW = 10000; // 10 seconds in milliseconds
682
683 // Check if we're near the rollover boundary (close to std::numeric_limits<uint32_t>::max() or just past 0)
684 bool near_rollover = (last > (std::numeric_limits<uint32_t>::max() - ROLLOVER_WINDOW)) || (now < ROLLOVER_WINDOW);
685
686 if (near_rollover || (now < last && (last - now) > HALF_MAX_UINT32)) {
687 // Near rollover or detected a rollover - need lock for safety
688 LockGuard guard{this->lock_};
689 // Re-read with lock held
690 last = this->last_millis_;
691
692 if (now < last && (last - now) > HALF_MAX_UINT32) {
693 // True rollover detected (happens every ~49.7 days)
694 this->millis_major_++;
695 major++;
696#ifdef ESPHOME_DEBUG_SCHEDULER
697 ESP_LOGD(TAG, "Detected true 32-bit rollover at %" PRIu32 "ms (was %" PRIu32 ")", now, last);
698#endif /* ESPHOME_DEBUG_SCHEDULER */
699 }
700 // Update last_millis_ while holding lock
701 this->last_millis_ = now;
702 } else if (now > last) {
703 // Normal case: Not near rollover and time moved forward
704 // Update without lock. While this may cause minor races (microseconds of
705 // backwards time movement), they're acceptable because:
706 // 1. The scheduler operates at millisecond resolution, not microsecond
707 // 2. We've already prevented the critical rollover race condition
708 // 3. Any backwards movement is orders of magnitude smaller than scheduler delays
709 this->last_millis_ = now;
710 }
711 // If now <= last and we're not near rollover, don't update
712 // This minimizes backwards time movement
713
714 // Combine major (high 32 bits) and now (low 32 bits) into 64-bit time
715 return now + (static_cast<uint64_t>(major) << 32);
716
717#elif defined(ESPHOME_THREAD_MULTI_ATOMICS)
718 // This is the multi core with atomics implementation.
719 //
720 // Uses atomic operations with acquire/release semantics to ensure coherent
721 // reads of millis_major_ and last_millis_ across cores. Features:
722 // 1. Epoch-coherency retry loop to handle concurrent updates
723 // 2. Lock only taken for actual rollover detection and update
724 // 3. Lock-free CAS updates for normal forward time progression
725 // 4. Memory ordering ensures cores see consistent time values
726
727 for (;;) {
728 uint16_t major = this->millis_major_.load(std::memory_order_acquire);
729
730 /*
731 * Acquire so that if we later decide **not** to take the lock we still
732 * observe a `millis_major_` value coherent with the loaded `last_millis_`.
733 * The acquire load ensures any later read of `millis_major_` sees its
734 * corresponding increment.
735 */
736 uint32_t last = this->last_millis_.load(std::memory_order_acquire);
737
738 // If we might be near a rollover (large backwards jump), take the lock for the entire operation
739 // This ensures rollover detection and last_millis_ update are atomic together
740 if (now < last && (last - now) > HALF_MAX_UINT32) {
741 // Potential rollover - need lock for atomic rollover detection + update
742 LockGuard guard{this->lock_};
743 // Re-read with lock held; mutex already provides ordering
744 last = this->last_millis_.load(std::memory_order_relaxed);
745
746 if (now < last && (last - now) > HALF_MAX_UINT32) {
747 // True rollover detected (happens every ~49.7 days)
748 this->millis_major_.fetch_add(1, std::memory_order_relaxed);
749 major++;
750#ifdef ESPHOME_DEBUG_SCHEDULER
751 ESP_LOGD(TAG, "Detected true 32-bit rollover at %" PRIu32 "ms (was %" PRIu32 ")", now, last);
752#endif /* ESPHOME_DEBUG_SCHEDULER */
753 }
754 /*
755 * Update last_millis_ while holding the lock to prevent races
756 * Publish the new low-word *after* bumping `millis_major_` (done above)
757 * so readers never see a mismatched pair.
758 */
759 this->last_millis_.store(now, std::memory_order_release);
760 } else {
761 // Normal case: Try lock-free update, but only allow forward movement within same epoch
762 // This prevents accidentally moving backwards across a rollover boundary
763 while (now > last && (now - last) < HALF_MAX_UINT32) {
764 if (this->last_millis_.compare_exchange_weak(last, now,
765 std::memory_order_release, // success
766 std::memory_order_relaxed)) { // failure
767 break;
768 }
769 // CAS failure means no data was published; relaxed is fine
770 // last is automatically updated by compare_exchange_weak if it fails
771 }
772 }
773 uint16_t major_end = this->millis_major_.load(std::memory_order_relaxed);
774 if (major_end == major)
775 return now + (static_cast<uint64_t>(major) << 32);
776 }
777 // Unreachable - the loop always returns when major_end == major
778 __builtin_unreachable();
779
780#else
781#error \
782 "No platform threading model defined. One of ESPHOME_THREAD_SINGLE, ESPHOME_THREAD_MULTI_NO_ATOMICS, or ESPHOME_THREAD_MULTI_ATOMICS must be defined."
783#endif
784}
785
786bool HOT Scheduler::SchedulerItem::cmp(const std::unique_ptr<SchedulerItem> &a,
787 const std::unique_ptr<SchedulerItem> &b) {
788 // High bits are almost always equal (change only on 32-bit rollover ~49 days)
789 // Optimize for common case: check low bits first when high bits are equal
790 return (a->next_execution_high_ == b->next_execution_high_) ? (a->next_execution_low_ > b->next_execution_low_)
791 : (a->next_execution_high_ > b->next_execution_high_);
792}
793
794void Scheduler::recycle_item_(std::unique_ptr<SchedulerItem> item) {
795 if (!item)
796 return;
797
798 if (this->scheduler_item_pool_.size() < MAX_POOL_SIZE) {
799 // Clear callback to release captured resources
800 item->callback = nullptr;
801 // Clear dynamic name if any
802 item->clear_dynamic_name();
803 this->scheduler_item_pool_.push_back(std::move(item));
804#ifdef ESPHOME_DEBUG_SCHEDULER
805 ESP_LOGD(TAG, "Recycled item to pool (pool size now: %zu)", this->scheduler_item_pool_.size());
806#endif
807 } else {
808#ifdef ESPHOME_DEBUG_SCHEDULER
809 ESP_LOGD(TAG, "Pool full (size: %zu), deleting item", this->scheduler_item_pool_.size());
810#endif
811 }
812 // else: unique_ptr will delete the item when it goes out of scope
813}
814
815} // namespace esphome
void set_current_component(Component *component)
const Component * component
Definition component.cpp:37
uint16_t type
const char *const TAG
Definition spi.cpp:8
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:157
void retry_handler(const std::shared_ptr< RetryArgs > &args)
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.