libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::LatestSnapshot< T > Class Template Reference

只保留最新一份完整值的单生产者、单消费者邮箱 / Single-producer, single-consumer mailbox that keeps only the latest complete value More...

#include <latest_snapshot.hpp>

Public Member Functions

 LatestSnapshot (const T &initial) noexcept(std::is_nothrow_copy_constructible_v< T >)
 用同一个初始值构造三个槽 / Construct all three slots with the same initial value
 
void Store (const T &value) noexcept(std::is_nothrow_copy_assignable_v< T >)
 发布一个完整的值 / Publish one complete value
 
bool LoadLatest (T &output) noexcept(std::is_nothrow_copy_assignable_v< T >)
 把最新的完整值拷入 output / Copy the latest complete value into output
 
 LatestSnapshot (const LatestSnapshot &)=delete
 禁止拷贝构造 / Non-copyable
 
LatestSnapshot & operator= (const LatestSnapshot &)=delete
 禁止拷贝赋值 / Non-copy-assignable
 
 LatestSnapshot (LatestSnapshot &&)=delete
 禁止移动构造 / Non-movable
 
LatestSnapshot & operator= (LatestSnapshot &&)=delete
 禁止移动赋值 / Non-move-assignable
 

Detailed Description

template<typename T>
class LibXR::LatestSnapshot< T >

只保留最新一份完整值的单生产者、单消费者邮箱 / Single-producer, single-consumer mailbox that keeps only the latest complete value

内部有三个槽:生产者独占后槽,消费者独占前槽,中间槽由一个原子状态 交给对方,承载最新一次完成的发布。连续的 Store() 可以覆盖消费者尚未 取走的中间值,但不会覆盖消费者正在拷贝的值。适用于只关心最新数据的 场合,例如在接收中断里保存最新的反馈帧,由控制线程读取。

The three slots are a back slot owned by the producer, a front slot owned by the consumer, and a middle slot handed over through one atomic state, which carries the latest completed publication. Repeated Store() calls may overwrite a middle value the consumer has not taken, but never the value the consumer is copying. It suits data of which only the latest value matters, such as the latest feedback frame stored in a receive interrupt and read by a control thread.

Template Parameters
T可拷贝构造、可拷贝赋值的值类型 / Copy-constructible, copy-assignable value type
Warning
只能有一个生产者调用 Store(),一个串行的消费者调用 LoadLatest();生产者与消费者的调用可以在不同核、线程或中断中 重叠。 Exactly one producer may call Store(), and exactly one serialized consumer may call LoadLatest(); producer and consumer calls may overlap on different cores or in thread and interrupt contexts.

Definition at line 38 of file latest_snapshot.hpp.

Constructor & Destructor Documentation

◆ LatestSnapshot()

template<typename T >
LibXR::LatestSnapshot< T >::LatestSnapshot ( const T & initial)
inlineexplicitnoexcept

用同一个初始值构造三个槽 / Construct all three slots with the same initial value

Parameters
initial初始值,LoadLatest() 在第一次 Store() 之前输出它 / Initial value, output by LoadLatest() before the first Store()

Definition at line 53 of file latest_snapshot.hpp.

55 : slots_{initial, initial, initial}
56 {
57 }

Member Function Documentation

◆ LoadLatest()

template<typename T >
bool LibXR::LatestSnapshot< T >::LoadLatest ( T & output)
inlinenoexcept

把最新的完整值拷入 output / Copy the latest complete value into output

有新的发布时先取得它;没有时 output 得到消费者上一次取得的值。只能 由唯一的消费者串行调用。

A newer publication is acquired first; without one, output receives the value the consumer acquired last. Only the single consumer may call it, one call at a time.

Parameters
output接收值的对象 / Object receiving the value
Returns
本次取得了新的发布时为 true,否则为 false / true when this call acquired a newer publication, otherwise false

Definition at line 92 of file latest_snapshot.hpp.

93 {
94 bool updated = false;
95 uint32_t observed = state_.load(std::memory_order_acquire);
96
97 while (HasNew(observed))
98 {
99 const uint32_t desired = Pack(front_, false);
100 if (state_.compare_exchange_weak(observed, desired, std::memory_order_acq_rel,
101 std::memory_order_acquire))
102 {
103 front_ = Index(observed);
104 updated = true;
105 break;
106 }
107 }
108
109 output = slots_[front_];
110 return updated;
111 }

◆ Store()

template<typename T >
void LibXR::LatestSnapshot< T >::Store ( const T & value)
inlinenoexcept

发布一个完整的值 / Publish one complete value

先把值拷入生产者独占的后槽,再把该槽作为最新的中间槽交给消费者。 只能由唯一的生产者调用。

The value is copied into the producer-owned back slot before that slot is released to the consumer as the newest middle slot. Only the single producer may call it.

Parameters
value要发布的值 / Value to publish

Definition at line 70 of file latest_snapshot.hpp.

71 {
72 slots_[back_] = value;
73
74 const uint32_t previous =
75 state_.exchange(Pack(back_, true), std::memory_order_acq_rel);
76 back_ = Index(previous);
77 }

The documentation for this class was generated from the following file: