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:
32 size_t;
34 std::make_signed_t<SequenceType>;
36
42 MPMCQueueBase(size_t element_size, size_t capacity);
47
56 ErrorCode PushBytes(const void* value);
66 ErrorCode PopBytes(void* value = nullptr);
67
72 [[nodiscard]] size_t MaxSize() const { return capacity_; }
86 [[nodiscard]] size_t Size() const;
91 [[nodiscard]] size_t EmptySize() const { return capacity_ - Size(); }
96 [[nodiscard]] size_t ElementSize() const { return element_size_; }
97
98 private:
100 struct alignas(LibXR::CONCURRENCY_ALIGNMENT) SequenceCell
101 {
102 std::atomic<SequenceType>
104 };
105
107 [[nodiscard]] void* PayloadPtr(size_t index);
110 [[nodiscard]] const void* PayloadPtr(size_t index) const;
112 [[nodiscard]] static size_t AlignUpChecked(size_t value, size_t align);
114 [[nodiscard]] static size_t MultiplyChecked(size_t lhs, size_t rhs);
119 static constexpr size_t PAYLOAD_ALLOC_ALIGN =
120 std::max(alignof(size_t), alignof(std::max_align_t));
121
130
131 const size_t element_size_;
132 const size_t capacity_;
133 const size_t payload_stride_;
136 std::byte* payloads_;
137
138 alignas(LibXR::CONCURRENCY_ALIGNMENT) std::atomic<
140 alignas(LibXR::CONCURRENCY_ALIGNMENT) std::atomic<
142};
143} // namespace LibXR
有界 MPMC 字节队列内核 / Bounded MPMC byte-queue core
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
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 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.
std::atomic< SequenceType > tail_
下一个待入队的逻辑位置。 Next logical enqueue position.
size_t ElementSize() const
获取单个 payload 的字节数 / Get the byte size of one payload
std::atomic< SequenceType > head_
下一个待出队的逻辑位置。 Next logical dequeue position.
void * PayloadPtr(size_t index)
获取指定槽位 payload 起始地址。 Get the payload base address of one slot.
~MPMCQueueBase()
析构字节队列内核 / Destroy the byte-queue core
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.
size_t SequenceType
单调递增的逻辑序号类型 / Monotonic logical sequence type.
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:62
每个逻辑槽对应的序号单元。 Sequence cell for one logical slot.
std::atomic< SequenceType > value
当前槽的逻辑序号。 Current logical sequence of the slot.