mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 20:27:45 +08:00
[scheduler] Use std::atomic<uint8_t> instead of std::atomic<bool> for remove flag (#14626)
This commit is contained in:
@@ -178,9 +178,11 @@ class Scheduler {
|
|||||||
uint16_t next_execution_high_; // Upper 16 bits (millis_major counter)
|
uint16_t next_execution_high_; // Upper 16 bits (millis_major counter)
|
||||||
|
|
||||||
#ifdef ESPHOME_THREAD_MULTI_ATOMICS
|
#ifdef ESPHOME_THREAD_MULTI_ATOMICS
|
||||||
// Multi-threaded with atomics: use atomic for lock-free access
|
// Multi-threaded with atomics: use atomic uint8_t for lock-free access.
|
||||||
// Place atomic<bool> separately since it can't be packed with bit fields
|
// std::atomic<bool> is not used because GCC on Xtensa generates an indirect
|
||||||
std::atomic<bool> remove{false};
|
// function call for std::atomic<bool>::load() instead of inlining it.
|
||||||
|
// std::atomic<uint8_t> inlines correctly on all platforms.
|
||||||
|
std::atomic<uint8_t> remove{0};
|
||||||
|
|
||||||
// Bit-packed fields (4 bits used, 4 bits padding in 1 byte)
|
// Bit-packed fields (4 bits used, 4 bits padding in 1 byte)
|
||||||
enum Type : uint8_t { TIMEOUT, INTERVAL } type : 1;
|
enum Type : uint8_t { TIMEOUT, INTERVAL } type : 1;
|
||||||
@@ -204,7 +206,7 @@ class Scheduler {
|
|||||||
next_execution_low_(0),
|
next_execution_low_(0),
|
||||||
next_execution_high_(0),
|
next_execution_high_(0),
|
||||||
#ifdef ESPHOME_THREAD_MULTI_ATOMICS
|
#ifdef ESPHOME_THREAD_MULTI_ATOMICS
|
||||||
// remove is initialized in the member declaration as std::atomic<bool>{false}
|
// remove is initialized in the member declaration
|
||||||
type(TIMEOUT),
|
type(TIMEOUT),
|
||||||
name_type_(NameType::STATIC_STRING),
|
name_type_(NameType::STATIC_STRING),
|
||||||
is_retry(false) {
|
is_retry(false) {
|
||||||
@@ -508,7 +510,7 @@ class Scheduler {
|
|||||||
// Multi-threaded with atomics: use atomic store with appropriate ordering
|
// Multi-threaded with atomics: use atomic store with appropriate ordering
|
||||||
// Release ordering when setting to true ensures cancellation is visible to other threads
|
// Release ordering when setting to true ensures cancellation is visible to other threads
|
||||||
// Relaxed ordering when setting to false is sufficient for initialization
|
// Relaxed ordering when setting to false is sufficient for initialization
|
||||||
item->remove.store(removed, removed ? std::memory_order_release : std::memory_order_relaxed);
|
item->remove.store(removed ? 1 : 0, removed ? std::memory_order_release : std::memory_order_relaxed);
|
||||||
#else
|
#else
|
||||||
// Single-threaded (ESPHOME_THREAD_SINGLE) or
|
// Single-threaded (ESPHOME_THREAD_SINGLE) or
|
||||||
// multi-threaded without atomics (ESPHOME_THREAD_MULTI_NO_ATOMICS): direct write
|
// multi-threaded without atomics (ESPHOME_THREAD_MULTI_NO_ATOMICS): direct write
|
||||||
|
|||||||
Reference in New Issue
Block a user