mirror of
https://github.com/esphome/esphome.git
synced 2025-08-02 08:27:47 +00:00
Fix scheduler with libretiny
This commit is contained in:
parent
7cdb48b820
commit
6740561bd7
@ -509,7 +509,11 @@ uint64_t Scheduler::millis_64_(uint32_t now) {
|
|||||||
|
|
||||||
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
||||||
// Multi-threaded platforms: Need to handle rollover carefully
|
// Multi-threaded platforms: Need to handle rollover carefully
|
||||||
|
#ifdef USE_LIBRETINY
|
||||||
|
uint32_t last = this->last_millis_;
|
||||||
|
#else
|
||||||
uint32_t last = this->last_millis_.load(std::memory_order_relaxed);
|
uint32_t last = this->last_millis_.load(std::memory_order_relaxed);
|
||||||
|
#endif // USE_LIBRETINY
|
||||||
|
|
||||||
// If we might be near a rollover (large backwards jump), take the lock for the entire operation
|
// If we might be near a rollover (large backwards jump), take the lock for the entire operation
|
||||||
// This ensures rollover detection and last_millis_ update are atomic together
|
// This ensures rollover detection and last_millis_ update are atomic together
|
||||||
@ -517,7 +521,11 @@ uint64_t Scheduler::millis_64_(uint32_t now) {
|
|||||||
// Potential rollover - need lock for atomic rollover detection + update
|
// Potential rollover - need lock for atomic rollover detection + update
|
||||||
LockGuard guard{this->lock_};
|
LockGuard guard{this->lock_};
|
||||||
// Re-read with lock held
|
// Re-read with lock held
|
||||||
|
#ifdef USE_LIBRETINY
|
||||||
|
last = this->last_millis_;
|
||||||
|
#else
|
||||||
last = this->last_millis_.load(std::memory_order_relaxed);
|
last = this->last_millis_.load(std::memory_order_relaxed);
|
||||||
|
#endif
|
||||||
|
|
||||||
if (now < last && (last - now) > HALF_MAX_UINT32) {
|
if (now < last && (last - now) > HALF_MAX_UINT32) {
|
||||||
// True rollover detected (happens every ~49.7 days)
|
// True rollover detected (happens every ~49.7 days)
|
||||||
@ -527,8 +535,21 @@ uint64_t Scheduler::millis_64_(uint32_t now) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
// Update last_millis_ while holding lock to prevent races
|
// Update last_millis_ while holding lock to prevent races
|
||||||
|
#ifdef USE_LIBRETINY
|
||||||
|
this->last_millis_ = now;
|
||||||
|
#else
|
||||||
this->last_millis_.store(now, std::memory_order_relaxed);
|
this->last_millis_.store(now, std::memory_order_relaxed);
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
|
#ifdef USE_LIBRETINY
|
||||||
|
// LibreTiny does not support atomics, so we use a simple lock-free update
|
||||||
|
// This is not completely safe, but without atomics we don't have a choice
|
||||||
|
// and in practice we don't have a lot of task on libretiny so it should be fine.
|
||||||
|
if (now > last && (now - last) < HALF_MAX_UINT32) {
|
||||||
|
// Normal case: Update last_millis_ if time moved forward
|
||||||
|
this->last_millis_ = now;
|
||||||
|
}
|
||||||
|
#else
|
||||||
// Normal case: Try lock-free update, but only allow forward movement within same epoch
|
// Normal case: Try lock-free update, but only allow forward movement within same epoch
|
||||||
// This prevents accidentally moving backwards across a rollover boundary
|
// This prevents accidentally moving backwards across a rollover boundary
|
||||||
while (now > last && (now - last) < HALF_MAX_UINT32) {
|
while (now > last && (now - last) < HALF_MAX_UINT32) {
|
||||||
@ -537,6 +558,7 @@ uint64_t Scheduler::millis_64_(uint32_t now) {
|
|||||||
}
|
}
|
||||||
// last is automatically updated by compare_exchange_weak if it fails
|
// last is automatically updated by compare_exchange_weak if it fails
|
||||||
}
|
}
|
||||||
|
#endif // USE_LIBRETINY
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user