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

单生产者单消费者无锁队列 / Single-producer single-consumer lock-free queue. More...

#include <spsc_queue.hpp>

Inheritance diagram for LibXR::SPSCQueue< Data >:
[legend]
Collaboration diagram for LibXR::SPSCQueue< Data >:
[legend]

Public Types

using ValueType = Data
 
- Public Types inherited from LibXR::QueueTypedBase< SPSCQueue< Data >, Data >
using ValueType
 队列元素类型 / Queue element type.
 
- Public Types inherited from LibXR::SPSCQueueBase
using IndexType = size_t
 环形缓冲区索引类型 / Ring-buffer index type.
 

Public Member Functions

 SPSCQueue (size_t length)
 构造一个 SPSC 队列 / Construct one SPSC queue.
 
 ~SPSCQueue ()=default
 析构一个 SPSC 队列 / Destroy one SPSC queue.
 
ErrorCode Peek (Data &item)
 查看一个队头 payload 但不出队 / Peek one front payload without dequeuing it.
 
ErrorCode PushBatch (const Data *data, size_t size)
 批量推入多个 payload / Push multiple payloads into the queue.
 
template<typename Writer >
ErrorCode PushWithWriter (Writer &&writer)
 通过写入器回调推入一个 payload / Push one payload via a writer callback.
 
template<typename Writer >
ErrorCode PushWithWriter (size_t size, Writer &&writer)
 通过写入器回调批量推入 payload / Push multiple payloads via a writer callback.
 
template<typename Writer >
size_t ProduceWithWriter (size_t limit, Writer &&writer)
 通过一次双 span 回调生产 payload 前缀 / Produce a payload prefix through one two-span writer callback.
 
template<typename Reader >
ErrorCode PopWithReader (Reader &&reader)
 通过读取器回调弹出一个 payload / Pop one payload via a reader callback.
 
template<typename Reader >
ErrorCode PopWithReader (size_t size, Reader &&reader)
 通过读取器回调批量弹出 payload / Pop multiple payloads via a reader callback.
 
template<typename Reader >
size_t ConsumeWithReader (size_t limit, Reader &&reader)
 通过一次双 span 回调消费 payload 前缀 / Consume a payload prefix through one two-span reader callback.
 
ErrorCode PopBatch (Data *data, size_t size)
 批量弹出多个 payload / Pop multiple payloads from the queue.
 
ErrorCode PeekBatch (Data *data, size_t size)
 批量查看多个 payload 但不出队 / Peek multiple payloads without dequeuing them.
 
void Reset ()
 从 consumer 侧丢弃当前可见的全部 payload / Discard currently available payloads from the consumer side
 
- Public Member Functions inherited from LibXR::QueueTypedBase< SPSCQueue< Data >, Data >
ErrorCode Push (const Data &item)
 推入一个强类型元素 / Push one typed element.
 
ErrorCode Pop (Data &item)
 弹出一个强类型元素 / Pop one typed element.
 
ErrorCode Pop ()
 丢弃一个队头元素 / Discard one front element.
 
- Public Member Functions inherited from LibXR::SPSCQueueBase
 SPSCQueueBase (size_t element_size, size_t capacity)
 构造 SPSC 字节队列内核 / Construct the SPSC byte-queue core
 
 SPSCQueueBase (size_t element_size, size_t element_align, size_t capacity)
 构造 SPSC 字节队列内核并指定元素对齐 / Construct the SPSC byte-queue core with explicit element alignment
 
 ~SPSCQueueBase ()
 析构 SPSC 字节队列内核 / Destroy the SPSC byte-queue core
 
ErrorCode PushBytes (const void *value)
 按字节入队一个 payload / Enqueue one payload by bytes
 
ErrorCode PopBytes (void *value=nullptr)
 按字节出队一个 payload;传空指针时仅丢弃队头元素 / Dequeue one payload by bytes; pass null to discard the front item only
 
ErrorCode PeekBytes (void *value)
 按字节查看一个队头 payload 但不出队 / Peek one front payload by bytes without dequeuing it
 
ErrorCode PushBatchBytes (const void *data, size_t count)
 按字节批量入队多个 payload / Enqueue multiple payloads by bytes
 
template<typename Writer >
ErrorCode PushBytesWithWriter (size_t count, Writer &&writer)
 通过写入器回调批量入队 payload / Enqueue payloads through a writer callback
 
template<typename Writer >
size_t ProduceWithWriter (size_t limit, Writer &&writer)
 通过一次双 span 回调生产 payload 前缀 / Produce a payload prefix through one two-span writer callback.
 
ErrorCode PopBatchBytes (void *data, size_t count)
 按字节批量出队多个 payload / Dequeue multiple payloads by bytes
 
template<typename Reader >
ErrorCode PopBytesWithReader (size_t count, Reader &&reader)
 通过读取器回调批量出队 payload / Dequeue payloads through a reader callback
 
template<typename Reader >
size_t ConsumeWithReader (size_t limit, Reader &&reader)
 通过一次双 span 回调消费 payload 前缀 / Consume a payload prefix through one two-span reader callback.
 
ErrorCode PeekBatchBytes (void *data, size_t count)
 按字节批量查看多个 payload 但不出队 / Peek multiple payloads by bytes without dequeuing them
 
void Reset ()
 从 consumer 侧丢弃当前可见的全部 payload / Discard all currently available payloads from the consumer side
 
size_t Size () const
 获取当前已用元素数 / Get the current element count
 
size_t EmptySize () const
 获取剩余空槽数 / Get the current free-slot count
 
size_t MaxSize () const
 获取队列最大容量 / Get the maximum queue capacity
 

Detailed Description

template<typename Data>
class LibXR::SPSCQueue< Data >

单生产者单消费者无锁队列 / Single-producer single-consumer lock-free queue.

模板壳只把 Data 映射为固定大小的字节 payload 并复用 SPSCQueueBase, 不在队列内部管理 Data 对象生命周期。调用方必须保证 payload 可以按该 项目的队列契约进行字节搬运。

This template wrapper maps Data to a fixed-size byte payload and reuses SPSCQueueBase. It does not manage Data object lifetime inside the queue. The caller must ensure that the payload is valid for this project's byte-moving queue contract.

Template Parameters
Data队列存储的数据类型 / Queue element type.

Definition at line 27 of file spsc_queue.hpp.

Member Typedef Documentation

◆ ValueType

template<typename Data >
using LibXR::SPSCQueue< Data >::ValueType = Data

队列元素类型 / Queue element type.

Definition at line 33 of file spsc_queue.hpp.

Constructor & Destructor Documentation

◆ SPSCQueue()

template<typename Data >
LibXR::SPSCQueue< Data >::SPSCQueue ( size_t length)
inlineexplicit

构造一个 SPSC 队列 / Construct one SPSC queue.

Parameters
length队列容量 / Queue capacity.
Note
包含动态内存分配 / Contains dynamic memory allocation.

Definition at line 45 of file spsc_queue.hpp.

45 : SPSCQueueBase(sizeof(Data), alignof(Data), length)
46 {
47 }
SPSCQueueBase(size_t element_size, size_t capacity)
构造 SPSC 字节队列内核 / Construct the SPSC byte-queue core

Member Function Documentation

◆ ConsumeWithReader()

template<typename Data >
template<typename Reader >
size_t LibXR::SPSCQueue< Data >::ConsumeWithReader ( size_t limit,
Reader && reader )
inline

通过一次双 span 回调消费 payload 前缀 / Consume a payload prefix through one two-span reader callback.

Template Parameters
Reader读取器类型 / Reader callback type.
Parameters
limit最多提供的 payload 个数 / Maximum number of offered payloads.
reader签名为 size_t(const Data*, size_t, const Data*, size_t),返回已接受的 前缀个数 / Callback with signature size_t(const Data*, size_t, const Data*, size_t); returns the accepted prefix count.
Returns
实际从队列移除的 payload 个数 / Number of payloads removed from the queue.
Precondition
队列只有一个 consumer;回调期间不得重入同侧操作或 Reset / The queue has one consumer; no same-side operation or Reset during the callback.
Note
没有可用数据或 limit == 0 时不调用回调;回调指针只在调用期间有效 / The callback is skipped when no data is available or limit == 0; pointers are valid only during the callback.

Definition at line 231 of file spsc_queue.hpp.

232 {
233 static_assert(std::is_trivially_copyable_v<Data>);
234 static_assert(std::is_trivially_destructible_v<Data>);
236 limit,
237 [&](const void* first, size_t first_size, const void* second,
238 size_t second_size) -> size_t
239 {
240 return reader(static_cast<const Data*>(first), first_size,
241 static_cast<const Data*>(second), second_size);
242 });
243 }
size_t ConsumeWithReader(size_t limit, Reader &&reader)
通过一次双 span 回调消费 payload 前缀 / Consume a payload prefix through one two-span reader callback.

◆ Peek()

template<typename Data >
ErrorCode LibXR::SPSCQueue< Data >::Peek ( Data & item)
inline

查看一个队头 payload 但不出队 / Peek one front payload without dequeuing it.

Parameters
item用于接收 payload / Receives the peeked payload.
Returns
成功返回 ErrorCode::OK;队列空返回 ErrorCode::EMPTY Returns ErrorCode::OK on success; returns ErrorCode::EMPTY when the queue is empty

Definition at line 61 of file spsc_queue.hpp.

61{ return SPSCQueueBase::PeekBytes(&item); }
ErrorCode PeekBytes(void *value)
按字节查看一个队头 payload 但不出队 / Peek one front payload by bytes without dequeuing it

◆ PeekBatch()

template<typename Data >
ErrorCode LibXR::SPSCQueue< Data >::PeekBatch ( Data * data,
size_t size )
inline

批量查看多个 payload 但不出队 / Peek multiple payloads without dequeuing them.

Parameters
data用于接收 payload 的数组 / Array receiving peeked payloads.
sizepayload 个数 / Number of payloads.
Returns
成功返回 ErrorCode::OK;队列空返回 ErrorCode::EMPTY Returns ErrorCode::OK on success; returns ErrorCode::EMPTY when the queue does not contain enough payloads

Definition at line 266 of file spsc_queue.hpp.

267 {
268 return SPSCQueueBase::PeekBatchBytes(data, size);
269 }
ErrorCode PeekBatchBytes(void *data, size_t count)
按字节批量查看多个 payload 但不出队 / Peek multiple payloads by bytes without dequeuing them

◆ PopBatch()

template<typename Data >
ErrorCode LibXR::SPSCQueue< Data >::PopBatch ( Data * data,
size_t size )
inline

批量弹出多个 payload / Pop multiple payloads from the queue.

Parameters
data用于接收 payload 的数组 / Array receiving dequeued payloads.
sizepayload 个数 / Number of payloads.
Returns
成功返回 ErrorCode::OK;队列空返回 ErrorCode::EMPTY Returns ErrorCode::OK on success; returns ErrorCode::EMPTY when the queue does not contain enough payloads

Definition at line 253 of file spsc_queue.hpp.

254 {
255 return SPSCQueueBase::PopBatchBytes(data, size);
256 }
ErrorCode PopBatchBytes(void *data, size_t count)
按字节批量出队多个 payload / Dequeue multiple payloads by bytes

◆ PopWithReader() [1/2]

template<typename Data >
template<typename Reader >
ErrorCode LibXR::SPSCQueue< Data >::PopWithReader ( Reader && reader)
inline

通过读取器回调弹出一个 payload / Pop one payload via a reader callback.

Template Parameters
Reader读取器类型 / Reader callback type.
Parameters
reader读取器回调,签名为 ErrorCode(const Data* buffer, size_t count) Reader callback with signature ErrorCode(const Data* buffer, size_t count).
Returns
成功返回 ErrorCode::OK;队列空返回 ErrorCode::EMPTY;否则返回读取器错误码 Returns ErrorCode::OK on success; returns ErrorCode::EMPTY when the queue is empty; otherwise returns the reader error code
Note
回调中的 count 固定为 1,读取器成功后才提交出队。 The callback always receives count == 1; the pop is committed only after the reader succeeds.

Definition at line 174 of file spsc_queue.hpp.

175 {
176 return PopWithReader(1, std::forward<Reader>(reader));
177 }
ErrorCode PopWithReader(Reader &&reader)
通过读取器回调弹出一个 payload / Pop one payload via a reader callback.

◆ PopWithReader() [2/2]

template<typename Data >
template<typename Reader >
ErrorCode LibXR::SPSCQueue< Data >::PopWithReader ( size_t size,
Reader && reader )
inline

通过读取器回调批量弹出 payload / Pop multiple payloads via a reader callback.

Template Parameters
Reader读取器类型 / Reader callback type.
Parameters
sizepayload 个数 / Number of payloads.
reader读取器回调,签名为 ErrorCode(const Data* buffer, size_t count)。 Reader callback with signature ErrorCode(const Data* buffer, size_t count).
Returns
成功返回 ErrorCode::OK;队列空返回 ErrorCode::EMPTY;否则返回读取器错误码。 Returns ErrorCode::OK on success; returns ErrorCode::EMPTY when the queue is empty; otherwise returns the reader error code.
Note
仅当读取器处理完整批次后才提交出队。 The pop is committed only after the reader accepts the full batch.

Definition at line 194 of file spsc_queue.hpp.

195 {
196 static_assert(
197 std::is_trivially_copyable_v<Data>,
198 "batched SPSCQueue::PopWithReader requires trivially copyable payloads");
199 static_assert(
200 std::is_trivially_destructible_v<Data>,
201 "batched SPSCQueue::PopWithReader requires trivially destructible payloads");
202 static_assert(std::is_invocable_v<Reader&, const Data*, size_t>,
203 "PopWithReader reader must be callable as "
204 "ErrorCode(const Data* buffer, size_t count)");
205 using ReaderRet = std::invoke_result_t<Reader&, const Data*, size_t>;
206 static_assert(std::is_convertible_v<ReaderRet, ErrorCode>,
207 "PopWithReader reader return type must be convertible to ErrorCode");
208
209 Reader& reader_ref = reader;
211 size, [&](const void* buffer, size_t count) -> ErrorCode
212 { return reader_ref(static_cast<const Data*>(buffer), count); });
213 }
ErrorCode PopBytesWithReader(size_t count, Reader &&reader)
通过读取器回调批量出队 payload / Dequeue payloads through a reader callback
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes

◆ ProduceWithWriter()

template<typename Data >
template<typename Writer >
size_t LibXR::SPSCQueue< Data >::ProduceWithWriter ( size_t limit,
Writer && writer )
inline

通过一次双 span 回调生产 payload 前缀 / Produce a payload prefix through one two-span writer callback.

Template Parameters
Writer写入器类型 / Writer callback type.
Parameters
limit最多提供的 payload 个数 / Maximum number of offered payloads.
writer签名为 size_t(Data*, size_t, Data*, size_t),返回已写入的前缀个数 / Callback with signature size_t(Data*, size_t, Data*, size_t); returns the written prefix count.
Returns
实际发布到队列的 payload 个数 / Number of payloads published to the queue.
Precondition
队列只有一个 producer;回调期间不得重入同侧操作或 Reset / The queue has one producer; no same-side operation or Reset during the callback.
Note
没有可用空间或 limit == 0 时不调用回调;回调指针只在调用期间有效 / The callback is skipped when no space is available or limit == 0; pointers are valid only during the callback.

Definition at line 147 of file spsc_queue.hpp.

148 {
149 static_assert(std::is_trivially_copyable_v<Data>);
150 static_assert(std::is_trivially_destructible_v<Data>);
152 limit,
153 [&](void* first, size_t first_size, void* second, size_t second_size) -> size_t
154 {
155 return writer(static_cast<Data*>(first), first_size, static_cast<Data*>(second),
156 second_size);
157 });
158 }
size_t ProduceWithWriter(size_t limit, Writer &&writer)
通过一次双 span 回调生产 payload 前缀 / Produce a payload prefix through one two-span writer callback.

◆ PushBatch()

template<typename Data >
ErrorCode LibXR::SPSCQueue< Data >::PushBatch ( const Data * data,
size_t size )
inline

批量推入多个 payload / Push multiple payloads into the queue.

Parameters
datapayload 数组指针 / Pointer to the payload array.
sizepayload 个数 / Number of payloads.
Returns
成功返回 ErrorCode::OK;队列满返回 ErrorCode::FULL Returns ErrorCode::OK on success; returns ErrorCode::FULL when the queue is full

Definition at line 71 of file spsc_queue.hpp.

72 {
73 return SPSCQueueBase::PushBatchBytes(data, size);
74 }
ErrorCode PushBatchBytes(const void *data, size_t count)
按字节批量入队多个 payload / Enqueue multiple payloads by bytes

◆ PushWithWriter() [1/2]

template<typename Data >
template<typename Writer >
ErrorCode LibXR::SPSCQueue< Data >::PushWithWriter ( size_t size,
Writer && writer )
inline

通过写入器回调批量推入 payload / Push multiple payloads via a writer callback.

Template Parameters
Writer写入器类型 / Writer callback type.
Parameters
sizepayload 个数 / Number of payloads.
writer写入器回调,签名为 ErrorCode(Data* buffer, size_t count)。 Writer callback with signature ErrorCode(Data* buffer, size_t count).
Returns
成功返回 ErrorCode::OK;队列满返回 ErrorCode::FULL;否则返回写入器错误码。 Returns ErrorCode::OK on success; returns ErrorCode::FULL when the queue is full; otherwise returns the writer error code.
Note
仅当写入器处理完整批次后才提交入队。 The enqueue is committed only after the writer accepts the full batch.

Definition at line 110 of file spsc_queue.hpp.

111 {
112 static_assert(
113 std::is_trivially_copyable_v<Data>,
114 "batched SPSCQueue::PushWithWriter requires trivially copyable payloads");
115 static_assert(
116 std::is_trivially_destructible_v<Data>,
117 "batched SPSCQueue::PushWithWriter requires trivially destructible payloads");
118 static_assert(std::is_invocable_v<Writer&, Data*, size_t>,
119 "PushWithWriter writer must be callable as "
120 "ErrorCode(Data* buffer, size_t count)");
121 using WriterRet = std::invoke_result_t<Writer&, Data*, size_t>;
122 static_assert(std::is_convertible_v<WriterRet, ErrorCode>,
123 "PushWithWriter writer return type must be convertible to ErrorCode");
124
125 Writer& writer_ref = writer;
127 size, [&](void* buffer, size_t count) -> ErrorCode
128 { return writer_ref(static_cast<Data*>(buffer), count); });
129 }
ErrorCode PushBytesWithWriter(size_t count, Writer &&writer)
通过写入器回调批量入队 payload / Enqueue payloads through a writer callback

◆ PushWithWriter() [2/2]

template<typename Data >
template<typename Writer >
ErrorCode LibXR::SPSCQueue< Data >::PushWithWriter ( Writer && writer)
inline

通过写入器回调推入一个 payload / Push one payload via a writer callback.

Template Parameters
Writer写入器类型 / Writer callback type.
Parameters
writer写入器回调,签名为 ErrorCode(Data* buffer, size_t count) Writer callback with signature ErrorCode(Data* buffer, size_t count).
Returns
成功返回 ErrorCode::OK;队列满返回 ErrorCode::FULL;否则返回写入器错误码 Returns ErrorCode::OK on success; returns ErrorCode::FULL when the queue is full; otherwise returns the writer error code
Note
回调中的 count 固定为 1,成功写入后才提交到队列。 The callback always receives count == 1; the payload is committed only after the writer succeeds.

Definition at line 90 of file spsc_queue.hpp.

91 {
92 return PushWithWriter(1, std::forward<Writer>(writer));
93 }
ErrorCode PushWithWriter(Writer &&writer)
通过写入器回调推入一个 payload / Push one payload via a writer callback.

◆ Reset()

template<typename Data >
void LibXR::SPSCQueue< Data >::Reset ( )
inline

从 consumer 侧丢弃当前可见的全部 payload / Discard currently available payloads from the consumer side

Definition at line 275 of file spsc_queue.hpp.

void Reset()
从 consumer 侧丢弃当前可见的全部 payload / Discard all currently available payloads from the consumer side

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