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