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

带固定 payload 类型的有界 MPMC 队列。 Bounded MPMC queue with a fixed payload type. More...

#include <mpmc_queue.hpp>

Inheritance diagram for LibXR::MPMCQueue< Payload >:
[legend]
Collaboration diagram for LibXR::MPMCQueue< Payload >:
[legend]

Public Types

using ValueType = Payload
 仅接受平凡可拷贝 payload / Accepts only trivially copyable payloads.
 
- Public Types inherited from LibXR::QueueTypedBase< MPMCQueue< Payload >, Payload >
using ValueType
 队列元素类型 / Queue element type.
 
- Public Types inherited from LibXR::MPMCQueueBase
using SequenceType
 单调递增的逻辑序号类型 / Monotonic logical sequence type.
 
using SequenceDiffType
 

Public Member Functions

 MPMCQueue (size_t capacity)
 构造一个 MPMC 队列 / Construct one MPMC queue.
 
- Public Member Functions inherited from LibXR::QueueTypedBase< MPMCQueue< Payload >, Payload >
ErrorCode Push (const Payload &item)
 推入一个强类型元素 / Push one typed element.
 
ErrorCode Pop (Payload &item)
 弹出一个强类型元素 / Pop one typed element.
 
ErrorCode Pop ()
 丢弃一个队头元素 / Discard one front element.
 
- Public Member Functions inherited from LibXR::MPMCQueueBase
 MPMCQueueBase (size_t element_size, size_t capacity)
 构造一个字节队列内核 / Construct one byte-queue core
 
 ~MPMCQueueBase ()
 析构字节队列内核 / Destroy the 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
 
size_t MaxSize () const
 获取队列最大容量 / Get the maximum queue capacity
 
size_t Size () const
 获取并发快照下的当前元素数 / Get the current approximate element count
 
size_t EmptySize () const
 获取剩余空槽数 / Get the current free-slot count
 
size_t ElementSize () const
 获取单个 payload 的字节数 / Get the byte size of one payload
 

Detailed Description

template<typename Payload>
class LibXR::MPMCQueue< Payload >

带固定 payload 类型的有界 MPMC 队列。 Bounded MPMC queue with a fixed payload type.

队列内部只把 payload 当成原始字节块搬运,不会在内部形成 Payload* 指针, 因此 Payload 的对齐要求不会额外约束队列内部字节缓冲区的语义边界。

The queue moves payloads only as raw byte blocks and never forms internal Payload* pointers, so Payload alignment does not add an extra semantic constraint on the internal byte-buffer contract.

适用场合:MPMC 保留为公共类型,适合任务或线程之间、调用方能等待或稍后 重试的场合,以及大容量、多核高争用的场合。单核上若某个操作在"占槽"与 "发布"之间被抢占,其他上下文会看到瞬时的 FULL/EMPTY(假的满/空),因此: 被抢占时拿到的 FULL/EMPTY 可能是瞬时的,调用方要能等待或稍后重试;不要 在中断里循环重试,也不要断言入队一定成功;RTOS 里高优先级任务拿到 FULL/EMPTY 时要让出 CPU 再试,不能原地自旋。中断会读写、拿到 FULL 只能 丢数据的场合,改用 SlotPool。

Intended use: MPMC stays a public type for task/thread producers and consumers that can wait or retry later, and for large capacities with high multi-core contention. On a single core, when one context is preempted between claiming a cell and publishing it, other contexts observe transient FULL/EMPTY (spurious full/empty). Therefore: a FULL/EMPTY observed while someone is preempted may be transient and callers must be able to wait or retry later; never retry in a loop inside an interrupt and never assert that an enqueue must succeed; in an RTOS, a higher-priority task that observes FULL/EMPTY must yield the CPU before retrying instead of spinning; and when interrupts both read and write and a FULL can only drop data, use SlotPool instead.

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

Definition at line 46 of file mpmc_queue.hpp.

Member Typedef Documentation

◆ ValueType

template<typename Payload >
using LibXR::MPMCQueue< Payload >::ValueType = Payload

仅接受平凡可拷贝 payload / Accepts only trivially copyable payloads.

仅接受平凡可析构 payload / Accepts only trivially destructible payloads. 队列元素类型 / Queue element type.

Definition at line 57 of file mpmc_queue.hpp.

Constructor & Destructor Documentation

◆ MPMCQueue()

template<typename Payload >
LibXR::MPMCQueue< Payload >::MPMCQueue ( size_t capacity)
inlineexplicit

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

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

Definition at line 69 of file mpmc_queue.hpp.

69: MPMCQueueBase(sizeof(Payload), capacity) {}
MPMCQueueBase(size_t element_size, size_t capacity)
构造一个字节队列内核 / Construct one byte-queue core

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