FastLED 3.10.8
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:
50 // constexpr so namespace-scope atomics are constant-initialized instead
51 // of retaining a startup constructor (#4788).
52 constexpr AtomicFake() FL_NO_EXCEPT : mValue{} {}
53 constexpr explicit AtomicFake(T value) FL_NO_EXCEPT : mValue(value) {}
54
55 // Non-copyable and non-movable
59 AtomicFake& operator=(AtomicFake&&) FL_NO_EXCEPT = delete;
60
61 // Basic atomic operations - fake implementation (not actually atomic)
62 // Memory order parameters are accepted but ignored (no-op in single-threaded mode)
66
70
72 T old = mValue;
73 mValue = value;
74 return old;
75 }
76
78 if (mValue == expected) {
79 mValue = desired;
80 return true;
81 } else {
83 return false;
84 }
85 }
86
90
91 // Assignment operator
93 store(value);
94 return value;
95 }
96
97 // Conversion operator
98 operator T() const FL_NO_EXCEPT {
99 return load();
100 }
101
102 // Arithmetic operators (for integral and floating point types)
103 // Avoid deprecated volatile increment/decrement (C++20)
105 T temp = mValue + 1;
106 mValue = temp;
107 return temp;
108 }
109
111 T temp = mValue;
112 mValue = temp + 1;
113 return temp;
114 }
115
117 T temp = mValue - 1;
118 mValue = temp;
119 return temp;
120 }
121
123 T temp = mValue;
124 mValue = temp - 1;
125 return temp;
126 }
127
129 T temp = mValue + value;
130 mValue = temp;
131 return temp;
132 }
133
135 T temp = mValue - value;
136 mValue = temp;
137 return temp;
138 }
139
141 T temp = mValue & value;
142 mValue = temp;
143 return temp;
144 }
145
147 T temp = mValue | value;
148 mValue = temp;
149 return temp;
150 }
151
153 T temp = mValue ^ value;
154 mValue = temp;
155 return temp;
156 }
157
158 // Fetch operations
160 T old = mValue;
161 T temp = old + value;
162 mValue = temp;
163 return old;
164 }
165
167 T old = mValue;
168 T temp = old - value;
169 mValue = temp;
170 return old;
171 }
172
174 T old = mValue;
175 T temp = old & value;
176 mValue = temp;
177 return old;
178 }
179
181 T old = mValue;
182 T temp = old | value;
183 mValue = temp;
184 return old;
185 }
186
188 T old = mValue;
189 T temp = old ^ value;
190 mValue = temp;
191 return old;
192 }
193
194 private:
195 T volatile mValue;
196};
197
198} // namespace fl
bool compare_exchange_weak(T &expected, T desired, memory_order=memory_order_seq_cst) FL_NO_EXCEPT
Definition atomic.h:77
T operator^=(T value) FL_NO_EXCEPT
Definition atomic.h:152
bool volatile mValue
Definition atomic.h:195
AtomicFake(AtomicFake &&) FL_NO_EXCEPT=delete
AtomicFake(const AtomicFake &) FL_NO_EXCEPT=delete
T fetch_xor(T value) FL_NO_EXCEPT
Definition atomic.h:187
T operator--(int) FL_NO_EXCEPT
Definition atomic.h:122
void store(T value, memory_order=memory_order_seq_cst) FL_NO_EXCEPT
Definition atomic.h:67
T operator--() FL_NO_EXCEPT
Definition atomic.h:116
T operator++(int) FL_NO_EXCEPT
Definition atomic.h:110
T operator|=(T value) FL_NO_EXCEPT
Definition atomic.h:146
constexpr AtomicFake() FL_NO_EXCEPT
Definition atomic.h:52
T exchange(T value, memory_order=memory_order_seq_cst) FL_NO_EXCEPT
Definition atomic.h:71
T operator-=(T value) FL_NO_EXCEPT
Definition atomic.h:134
T fetch_or(T value) FL_NO_EXCEPT
Definition atomic.h:180
T operator&=(T value) FL_NO_EXCEPT
Definition atomic.h:140
T operator++() FL_NO_EXCEPT
Definition atomic.h:104
constexpr AtomicFake(T value) FL_NO_EXCEPT
Definition atomic.h:53
bool load(memory_order=memory_order_seq_cst) const FL_NO_EXCEPT
Definition atomic.h:63
T fetch_sub(T value) FL_NO_EXCEPT
Definition atomic.h:166
bool compare_exchange_strong(T &expected, T desired, memory_order=memory_order_seq_cst) FL_NO_EXCEPT
Definition atomic.h:87
T operator+=(T value) FL_NO_EXCEPT
Definition atomic.h:128
AtomicFake & operator=(const AtomicFake &) FL_NO_EXCEPT=delete
T operator=(T value) FL_NO_EXCEPT
Definition atomic.h:92
T fetch_and(T value) FL_NO_EXCEPT
Definition atomic.h:173
T fetch_add(T value) FL_NO_EXCEPT
Definition atomic.h:159
expected type for operations that can fail (C++23-style)
Definition expected.h:79
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
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.
Platform detection for FASTLED_MULTITHREADED macro.