libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
mpmc_queue_base.hpp
1#pragma once
2
3#include <algorithm>
4#include <atomic>
5#include <cstddef>
6#include <cstdint>
7#include <limits>
8#include <new>
9#include <type_traits>
10
11#include "libxr_def.hpp"
12
13namespace LibXR
14{
29{
30 public:
31 using SequenceType = size_t;
33 std::make_signed_t<SequenceType>;
34
40 MPMCQueueBase(size_t element_size, size_t capacity);
45
54 ErrorCode PushBytes(const void* value);
64 ErrorCode PopBytes(void* value = nullptr);
65
70 [[nodiscard]] size_t MaxSize() const { return capacity_; }
84 [[nodiscard]] size_t Size() const;
89 [[nodiscard]] size_t EmptySize() const { return capacity_ - Size(); }
94 [[nodiscard]] size_t ElementSize() const { return element_size_; }
95
96 private:
98 struct alignas(LibXR::CONCURRENCY_ALIGNMENT) SequenceCell
99 {
100 std::atomic<SequenceType> value;
101 };
102
104 [[nodiscard]] void* PayloadPtr(size_t index);
106 [[nodiscard]] const void* PayloadPtr(size_t index) const;
108 [[nodiscard]] static size_t AlignUpChecked(size_t value, size_t align);
110 [[nodiscard]] static size_t MultiplyChecked(size_t lhs, size_t rhs);
114 static constexpr size_t PAYLOAD_ALLOC_ALIGN =
115 std::max(alignof(size_t), alignof(std::max_align_t));
116
125
126 const size_t element_size_;
127 const size_t capacity_;
128 const size_t payload_stride_;
130 std::byte* payloads_;
131
132 alignas(LibXR::CONCURRENCY_ALIGNMENT) std::atomic<SequenceType>
134 alignas(LibXR::CONCURRENCY_ALIGNMENT) std::atomic<SequenceType>
136};
137} // namespace LibXR
有界 MPMC 字节队列内核 / Bounded MPMC byte-queue core
size_t SequenceType
单调递增的逻辑序号类型 / Monotonic logical sequence type.
MPMCQueueBase(size_t element_size, size_t capacity)
构造一个字节队列内核 / Construct one byte-queue core
size_t EmptySize() const
获取剩余空槽数 / Get the current free-slot count
static size_t MultiplyChecked(size_t lhs, size_t rhs)
安全地计算乘积。 Safely multiply two size values.
std::make_signed_t< SequenceType > SequenceDiffType
序号差值判定类型 / Signed type used for sequence-delta checks.
static size_t AlignUpChecked(size_t value, size_t align)
安全地向上对齐字节数。 Safely align one byte count upward.
size_t Size() const
获取并发快照下的当前元素数 / Get the current approximate element count
std::byte * payloads_
payload 字节缓冲区。 Byte buffer storing payloads.
const size_t payload_stride_
相邻 payload 槽位之间的步长。 Byte stride between adjacent payload slots.
std::atomic< SequenceType > head_
下一个待出队的逻辑位置。 Next logical dequeue position.
const size_t capacity_
队列容量。 Queue capacity.
ErrorCode PopBytes(void *value=nullptr)
按字节出队一个 payload;传空指针时只丢弃队头元素 / Dequeue one payload by bytes; pass null to discard the front item only
static constexpr size_t PAYLOAD_ALLOC_ALIGN
payload 缓冲区整体分配对齐 / Allocation alignment used for the whole payload buffer
MPMCQueueBase(MPMCQueueBase &&)
禁止移动构造。 Non-movable.
size_t ElementSize() const
获取单个 payload 的字节数 / Get the byte size of one payload
void * PayloadPtr(size_t index)
获取指定槽位 payload 起始地址。 Get the payload base address of one slot.
~MPMCQueueBase()
析构字节队列内核 / Destroy the byte-queue core
std::atomic< SequenceType > tail_
下一个待入队的逻辑位置。 Next logical enqueue position.
MPMCQueueBase(const MPMCQueueBase &)
禁止拷贝构造。 Non-copyable.
MPMCQueueBase & operator=(MPMCQueueBase &&)
禁止移动赋值。 Non-move-assignable.
size_t MaxSize() const
获取队列最大容量 / Get the maximum queue capacity
SequenceCell * sequences_
槽序号数组。 Array of per-slot sequence cells.
MPMCQueueBase & operator=(const MPMCQueueBase &)
禁止拷贝赋值。 Non-copy-assignable.
ErrorCode PushBytes(const void *value)
按字节入队一个 payload / Enqueue one payload by bytes
const size_t element_size_
单个 payload 的字节数。 Byte size of one payload.
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
constexpr size_t CONCURRENCY_ALIGNMENT
并发结构对齐粒度(用于降低多核伪共享) / Alignment policy used by concurrency-oriented structures
Definition libxr_def.hpp:60
每个逻辑槽对应的序号单元。 Sequence cell for one logical slot.
std::atomic< SequenceType > value
当前槽的逻辑序号。 Current logical sequence of the slot.