FastLED 3.9.15
Loading...
Searching...
No Matches
atomic.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/thread_config.h" // For FASTLED_MULTITHREADED (no circular deps)
4#include "fl/stl/int.h"
5
6// Determine if we need real atomics:
7// 1. Multi-threaded mode (pthread support)
8// 2. ESP32 platforms (ISRs require atomic operations, and ESP32 has fast hardware atomics)
9// Note: ESP8266 excluded - lacks native atomic support, would require linking libatomic
10#if FASTLED_MULTITHREADED || defined(FL_IS_ESP32)
11#define FASTLED_USE_REAL_ATOMICS 1
12#include "platforms/atomic.h" // IWYU pragma: keep
13#include "fl/stl/noexcept.h"
14#else
15#define FASTLED_USE_REAL_ATOMICS 0
16#endif
17
18namespace fl {
19
20#if FASTLED_USE_REAL_ATOMICS
21template <typename T>
22using atomic = AtomicReal<T>;
23#else
24template <typename T> class AtomicFake;
25template <typename T>
27#endif
28
34
36
37// Forward declare memory_order enum (defined in platforms/shared/atomic.h for real atomics)
38#if !FASTLED_USE_REAL_ATOMICS
46#endif
47
48template <typename T> class AtomicFake {
49 public:
52
53 // Non-copyable and non-movable
57 AtomicFake& operator=(AtomicFake&&) FL_NOEXCEPT = delete;
58
59 // Basic atomic operations - fake implementation (not actually atomic)
60 // Memory order parameters are accepted but ignored (no-op in single-threaded mode)
64
68
70 T old = mValue;
71 mValue = value;
72 return old;
73 }
74
76 if (mValue == expected) {
77 mValue = desired;
78 return true;
79 } else {
81 return false;
82 }
83 }
84
88
89 // Assignment operator
91 store(value);
92 return value;
93 }
94
95 // Conversion operator
96 operator T() const FL_NOEXCEPT {
97 return load();
98 }
99
100 // Arithmetic operators (for integral and floating point types)
101 // Avoid deprecated volatile increment/decrement (C++20)
103 T temp = mValue + 1;
104 mValue = temp;
105 return temp;
106 }
107
109 T temp = mValue;
110 mValue = temp + 1;
111 return temp;
112 }
113
115 T temp = mValue - 1;
116 mValue = temp;
117 return temp;
118 }
119
121 T temp = mValue;
122 mValue = temp - 1;
123 return temp;
124 }
125
127 T temp = mValue + value;
128 mValue = temp;
129 return temp;
130 }
131
133 T temp = mValue - value;
134 mValue = temp;
135 return temp;
136 }
137
139 T temp = mValue & value;
140 mValue = temp;
141 return temp;
142 }
143
145 T temp = mValue | value;
146 mValue = temp;
147 return temp;
148 }
149
151 T temp = mValue ^ value;
152 mValue = temp;
153 return temp;
154 }
155
156 // Fetch operations
158 T old = mValue;
159 T temp = old + value;
160 mValue = temp;
161 return old;
162 }
163
165 T old = mValue;
166 T temp = old - value;
167 mValue = temp;
168 return old;
169 }
170
172 T old = mValue;
173 T temp = old & value;
174 mValue = temp;
175 return old;
176 }
177
179 T old = mValue;
180 T temp = old | value;
181 mValue = temp;
182 return old;
183 }
184
186 T old = mValue;
187 T temp = old ^ value;
188 mValue = temp;
189 return old;
190 }
191
192 private:
193 T volatile mValue;
194};
195
196} // namespace fl
void store(T value, memory_order=memory_order_seq_cst) FL_NOEXCEPT
Definition atomic.h:65
T exchange(T value, memory_order=memory_order_seq_cst) FL_NOEXCEPT
Definition atomic.h:69
T operator^=(T value) FL_NOEXCEPT
Definition atomic.h:150
bool volatile mValue
Definition atomic.h:193
T operator-=(T value) FL_NOEXCEPT
Definition atomic.h:132
T operator--() FL_NOEXCEPT
Definition atomic.h:114
T operator=(T value) FL_NOEXCEPT
Definition atomic.h:90
T fetch_xor(T value) FL_NOEXCEPT
Definition atomic.h:185
T operator++(int) FL_NOEXCEPT
Definition atomic.h:108
AtomicFake(T value) FL_NOEXCEPT
Definition atomic.h:51
T fetch_and(T value) FL_NOEXCEPT
Definition atomic.h:171
AtomicFake(AtomicFake &&) FL_NOEXCEPT=delete
T operator--(int) FL_NOEXCEPT
Definition atomic.h:120
T fetch_or(T value) FL_NOEXCEPT
Definition atomic.h:178
T operator+=(T value) FL_NOEXCEPT
Definition atomic.h:126
bool load(memory_order=memory_order_seq_cst) const FL_NOEXCEPT
Definition atomic.h:61
T operator&=(T value) FL_NOEXCEPT
Definition atomic.h:138
T operator|=(T value) FL_NOEXCEPT
Definition atomic.h:144
T fetch_sub(T value) FL_NOEXCEPT
Definition atomic.h:164
T operator++() FL_NOEXCEPT
Definition atomic.h:102
AtomicFake & operator=(const AtomicFake &) FL_NOEXCEPT=delete
AtomicFake(const AtomicFake &) FL_NOEXCEPT=delete
AtomicFake() FL_NOEXCEPT
Definition atomic.h:50
T fetch_add(T value) FL_NOEXCEPT
Definition atomic.h:157
bool compare_exchange_strong(T &expected, T desired, memory_order=memory_order_seq_cst) FL_NOEXCEPT
Definition atomic.h:85
bool compare_exchange_weak(T &expected, T desired, memory_order=memory_order_seq_cst) FL_NOEXCEPT
Definition atomic.h:75
expected type for operations that can fail (C++23-style)
Definition expected.h:79
constexpr int type_rank< T >::value
AtomicFake< T > atomic
Definition atomic.h:26
memory_order
Definition atomic.h:39
@ memory_order_relaxed
Definition atomic.h:40
@ memory_order_acquire
Definition atomic.h:41
@ memory_order_seq_cst
Definition atomic.h:44
@ memory_order_release
Definition atomic.h:42
@ memory_order_acq_rel
Definition atomic.h:43
atomic< fl::i32 > atomic_i32
Definition atomic.h:33
atomic< fl::u32 > atomic_u32
Definition atomic.h:32
atomic< unsigned int > atomic_uint
Definition atomic.h:31
atomic< int > atomic_int
Definition atomic.h:30
atomic< bool > atomic_bool
Definition atomic.h:29
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Platform detection for FASTLED_MULTITHREADED macro.