30#ifndef HF_UTILS_RTOS_WRAP_FLAGSSAVER_H_
31#define HF_UTILS_RTOS_WRAP_FLAGSSAVER_H_
49template <std::
size_t kWordCount>
51 uint64_t
bits[kWordCount > 0 ? kWordCount : 1]{};
59template <
typename FlagId, std::
size_t kCount>
63 (kCount + 63U) / 64U > 0U ? (kCount + 63U) / 64U : 1U;
69 [[nodiscard]] virtual
bool IsSet(FlagId
id) const noexcept = 0;
76 [[nodiscard]] virtual uint32_t
Seq() const noexcept = 0;
95template <typename FlagId, std::
size_t kCount>
106 virtual
bool Set(FlagId
id, uint32_t now_ms = 0) noexcept = 0;
112 virtual
bool Clear(FlagId
id, uint32_t now_ms = 0) noexcept = 0;
115 virtual
bool ClearAll(uint32_t now_ms = 0) noexcept = 0;
125template <typename FlagId, std::
size_t kCount>
139 if (event_group_created_) {
141 event_group_created_ =
false;
150 bool Set(FlagId
id, uint32_t now_ms = 0) noexcept
override
152 return WriteSlot_(
id,
true, now_ms);
155 bool Clear(FlagId
id, uint32_t now_ms = 0) noexcept
override
157 return WriteSlot_(
id,
false, now_ms);
160 bool ClearAll(uint32_t now_ms = 0) noexcept
override
162 bool changed =
false;
163 for (
auto& w : words_) {
164 const uint64_t prev = w.exchange(0, std::memory_order_acq_rel);
165 if (prev != 0U) changed =
true;
167 if (!changed)
return false;
168 if (now_ms != 0U) last_change_ms_.store(now_ms, std::memory_order_release);
175 [[nodiscard]]
bool IsSet(FlagId
id)
const noexcept override
177 const std::size_t idx =
static_cast<std::size_t
>(id);
178 if (idx >= kCount)
return false;
179 const std::size_t word = idx / 64U;
180 const std::size_t bit = idx % 64U;
181 return (words_[word].load(std::memory_order_acquire)
182 & (uint64_t{1} << bit)) != 0U;
187 for (std::size_t i = 0; i <
kWordCount; ++i) {
188 out.bits[i] = words_[i].load(std::memory_order_acquire);
190 out.seq = seq_.load(std::memory_order_acquire);
191 out.last_change_ms = last_change_ms_.load(std::memory_order_acquire);
194 [[nodiscard]] uint32_t
Seq() const noexcept
override
196 return seq_.load(std::memory_order_acquire);
201 return last_change_ms_.load(std::memory_order_acquire);
206 if (!EnsureEventGroup_())
return false;
208 const OS_Ulong wait = (timeout_ms == UINT32_MAX)
216 return (rc ==
OS_SUCCESS) && ((actual & kEventBit_) != 0U);
221 if (!EnsureEventGroup_())
return false;
226 bool WriteSlot_(FlagId
id,
bool set, uint32_t now_ms)
noexcept
228 const std::size_t idx =
static_cast<std::size_t
>(id);
229 if (idx >= kCount)
return false;
231 const std::size_t word = idx / 64U;
232 const uint64_t mask = uint64_t{1} << (idx % 64U);
233 auto& w = words_[word];
235 const uint64_t prev = set
236 ? w.fetch_or(mask, std::memory_order_acq_rel)
237 : w.fetch_and(~mask, std::memory_order_acq_rel);
239 const bool was_set = (prev & mask) != 0U;
240 if (was_set == set)
return false;
242 if (now_ms != 0U) last_change_ms_.store(now_ms, std::memory_order_release);
249 seq_.fetch_add(1, std::memory_order_acq_rel);
250 if (EnsureEventGroup_()) {
257 if (event_group_created_)
return true;
259 event_group_created_ =
true;
261 return event_group_created_;
267 std::atomic<uint32_t> seq_{0};
268 std::atomic<uint32_t> last_change_ms_{0};
270 bool event_group_created_{
false};
Generic RTOS abstraction — public header.
C-facing helper functions for delays, cycle counters, and creation of RTOS handles (queues,...
constexpr uint32_t os_convert_msec_to_delay_ticks(uint32_t milliseconds)
Converts milliseconds to delay ticks, rounding down but never to zero.
Definition OsUtility.h:74
Read-side ABC for FlagsSaver.
Definition FlagsSaver.h:60
virtual uint32_t LastChangeMs() const noexcept=0
Most recent now_ms value passed to Set / Clear (0 if never).
virtual void Snapshot_(Snapshot &out) const noexcept=0
Copy current bit state + sequence + last-change timestamp into out.
virtual bool IsSet(FlagId id) const noexcept=0
True if id is currently set.
virtual bool ClearWaitEvent() noexcept=0
Discard any pending change-signal without waiting.
virtual bool WaitForChange(uint32_t timeout_ms) noexcept=0
Block up to timeout_ms waiting for any change since the last call to WaitForChange or ClearWaitEvent.
static constexpr std::size_t kWordCount
Definition FlagsSaver.h:62
virtual uint32_t Seq() const noexcept=0
virtual ~FlagsReader() noexcept=default
Concrete two-state flag bitset.
Definition FlagsSaver.h:129
FlagsSaver() noexcept=default
uint32_t Seq() const noexcept override
Definition FlagsSaver.h:194
void SignalChange_() noexcept
Definition FlagsSaver.h:247
typename Base::Snapshot Snapshot
Definition FlagsSaver.h:132
FlagsSaver & operator=(const FlagsSaver &)=delete
uint32_t LastChangeMs() const noexcept override
Most recent now_ms value passed to Set / Clear (0 if never).
Definition FlagsSaver.h:199
bool WriteSlot_(FlagId id, bool set, uint32_t now_ms) noexcept
Definition FlagsSaver.h:226
bool ClearAll(uint32_t now_ms=0) noexcept override
Reset every slot to Cleared and signal a change.
Definition FlagsSaver.h:160
bool ClearWaitEvent() noexcept override
Discard any pending change-signal without waiting.
Definition FlagsSaver.h:219
bool EnsureEventGroup_() noexcept
Definition FlagsSaver.h:255
FlagsSaver(const FlagsSaver &)=delete
bool Clear(FlagId id, uint32_t now_ms=0) noexcept override
Mark id cleared. Idempotent: returns false if state was already Cleared (no event signalled).
Definition FlagsSaver.h:155
void Snapshot_(Snapshot &out) const noexcept override
Copy current bit state + sequence + last-change timestamp into out.
Definition FlagsSaver.h:185
bool WaitForChange(uint32_t timeout_ms) noexcept override
Block up to timeout_ms waiting for any change since the last call to WaitForChange or ClearWaitEvent.
Definition FlagsSaver.h:204
bool Set(FlagId id, uint32_t now_ms=0) noexcept override
Mark id set. Idempotent: returns false if state was already Set (no event signalled).
Definition FlagsSaver.h:150
bool IsSet(FlagId id) const noexcept override
True if id is currently set.
Definition FlagsSaver.h:175
Write-side ABC for FlagsSaver.
Definition FlagsSaver.h:96
virtual ~FlagsWriter() noexcept=default
Definition ErrorHistory.h:34
@ OS_SUCCESS
Definition os_cmsis.h:50
@ OS_EVENT_CLEAR
Definition os_cmsis.h:53
@ OS_OR
Definition os_cmsis.h:52
@ OS_WAIT_FOREVER
Definition os_cmsis.h:48
static OS_Uint os_event_group_set(OS_EventGroup *g, OS_Ulong flags)
Definition os_cmsis.h:320
static OS_Uint os_event_group_get(OS_EventGroup *g, OS_Ulong flags, OS_Uint option, OS_Ulong *actual, OS_Ulong wait)
Definition os_cmsis.h:330
static OS_Uint os_event_group_delete(OS_EventGroup *g)
Definition os_cmsis.h:316
static OS_Uint os_event_group_clear(OS_EventGroup *g, OS_Ulong flags)
Definition os_cmsis.h:325
osEventFlagsId_t OS_EventGroup
Definition os_cmsis.h:31
unsigned int OS_Uint
Definition os_cmsis.h:36
unsigned long OS_Ulong
Definition os_cmsis.h:35
static OS_Uint os_event_group_create(OS_EventGroup *g, const char *name)
Definition os_cmsis.h:309
Snapshot of a FlagsSaver at a point in time.
Definition FlagsSaver.h:50
uint32_t last_change_ms
Definition FlagsSaver.h:53
uint32_t seq
Definition FlagsSaver.h:52
uint64_t bits[kWordCount > 0 ? kWordCount :1]
Definition FlagsSaver.h:51