guard esp8266

This commit is contained in:
J. Nick Koston 2025-07-04 10:59:58 -05:00
parent 0fc3f0e162
commit bdb7e19fd0
No known key found for this signature in database
2 changed files with 9 additions and 0 deletions

View File

@ -83,6 +83,7 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
#ifndef USE_ESP8266
// Special handling for defer() (delay = 0, type = TIMEOUT)
// ESP8266 is excluded because it doesn't need thread-safe defer handling
if (delay == 0 && type == SchedulerItem::TIMEOUT) {
// Put in defer queue for guaranteed FIFO execution
LockGuard guard{this->lock_};
@ -227,6 +228,8 @@ void HOT Scheduler::call() {
// - Deferred items (delay=0) go directly to defer_queue_ in set_timer_common_
// - Items execute in exact order they were deferred (FIFO guarantee)
// - No deferred items exist in to_add_, so processing order doesn't affect correctness
// ESP8266 doesn't use this queue - it falls back to the heap-based approach since
// it's single-core and doesn't have thread safety concerns.
while (!this->defer_queue_.empty()) {
// IMPORTANT: The double-check pattern is REQUIRED for thread safety:
// 1. First check: !defer_queue_.empty() without lock (may become stale)
@ -437,6 +440,7 @@ bool HOT Scheduler::cancel_item_common_(Component *component, bool is_static_str
// Check all containers for matching items
#ifndef USE_ESP8266
// Only check defer_queue_ on platforms that have it
for (auto &item : this->defer_queue_) {
if (this->matches_item_(item, component, name_cstr, type)) {
item->remove = true;

View File

@ -167,6 +167,11 @@ class Scheduler {
std::vector<std::unique_ptr<SchedulerItem>> items_;
std::vector<std::unique_ptr<SchedulerItem>> to_add_;
#ifndef USE_ESP8266
// ESP8266 doesn't need the defer queue because:
// 1. It's single-core with no preemptive multitasking
// 2. All code runs in a single thread context
// 3. defer() calls can't have race conditions without true concurrency
// 4. Saves 40 bytes of RAM on memory-constrained ESP8266 devices
std::deque<std::unique_ptr<SchedulerItem>> defer_queue_; // FIFO queue for defer() calls
#endif
uint32_t last_millis_{0};