30#ifndef HF_UTILS_RTOS_WRAP_SEQLOCKSNAPSHOT_H_
31#define HF_UTILS_RTOS_WRAP_SEQLOCKSNAPSHOT_H_
54 virtual uint32_t
Read(T& out) const noexcept = 0;
57 [[nodiscard]] virtual uint32_t
Seq() const noexcept = 0;
79 virtual
void Publish(const T& value) noexcept = 0;
87 static_assert(std::is_trivially_copyable_v<T>,
88 "SeqlockSnapshot<T> requires a trivially copyable T");
95 if (event_group_created_) {
97 event_group_created_ =
false;
106 void Publish(
const T& value)
noexcept override
108 const uint32_t s0 = seq_.load(std::memory_order_relaxed);
109 seq_.store(s0 + 1U, std::memory_order_release);
110 std::atomic_thread_fence(std::memory_order_release);
111 std::memcpy(&payload_, &value,
sizeof(T));
112 std::atomic_thread_fence(std::memory_order_release);
113 seq_.store(s0 + 2U, std::memory_order_release);
119 uint32_t
Read(T& out)
const noexcept override
122 const uint32_t s1 = seq_.load(std::memory_order_acquire);
123 if ((s1 & 1U) != 0U) {
126 std::atomic_thread_fence(std::memory_order_acquire);
127 std::memcpy(&out, &payload_,
sizeof(T));
128 std::atomic_thread_fence(std::memory_order_acquire);
129 const uint32_t s2 = seq_.load(std::memory_order_acquire);
137 [[nodiscard]] uint32_t
Seq() const noexcept
override
139 return seq_.load(std::memory_order_acquire);
144 if (!EnsureEventGroup_())
return false;
146 const OS_Ulong wait = (timeout_ms == UINT32_MAX)
154 return (rc ==
OS_SUCCESS) && ((actual & kEventBit_) != 0U);
159 if (!EnsureEventGroup_())
return false;
166 if (EnsureEventGroup_()) {
173 if (event_group_created_)
return true;
175 event_group_created_ =
true;
177 return event_group_created_;
182 mutable std::atomic<uint32_t> seq_{0};
185 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
Definition SeqlockSnapshot.h:86
void SignalChange_() noexcept
Definition SeqlockSnapshot.h:164
SeqlockSnapshot() noexcept=default
SeqlockSnapshot & operator=(const SeqlockSnapshot &)=delete
bool EnsureEventGroup_() noexcept
Definition SeqlockSnapshot.h:171
uint32_t Seq() const noexcept override
Monotonic change counter (each successful Publish adds 2).
Definition SeqlockSnapshot.h:137
uint32_t Read(T &out) const noexcept override
Read the latest coherent snapshot.
Definition SeqlockSnapshot.h:119
SeqlockSnapshot(const SeqlockSnapshot &)=delete
bool WaitForChange(uint32_t timeout_ms) noexcept override
Block up to timeout_ms waiting for any change since the last call to WaitForChange / ClearWaitEvent.
Definition SeqlockSnapshot.h:142
void Publish(const T &value) noexcept override
Atomically replace the published snapshot with value.
Definition SeqlockSnapshot.h:106
bool ClearWaitEvent() noexcept override
Drop any pending change-signal without waiting.
Definition SeqlockSnapshot.h:157
Definition SeqlockSnapshot.h:46
virtual uint32_t Seq() const noexcept=0
Monotonic change counter (each successful Publish adds 2).
virtual bool ClearWaitEvent() noexcept=0
Drop any pending change-signal without waiting.
virtual uint32_t Read(T &out) const noexcept=0
Read the latest coherent snapshot.
virtual bool WaitForChange(uint32_t timeout_ms) noexcept=0
Block up to timeout_ms waiting for any change since the last call to WaitForChange / ClearWaitEvent.
virtual ~SnapshotReader() noexcept=default
Definition SeqlockSnapshot.h:70
virtual ~SnapshotWriter() 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