libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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>
103 value;
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
125 MPMCQueueBase& operator=(const MPMCQueueBase&);
129 MPMCQueueBase& operator=(MPMCQueueBase&&);
130
131 const size_t element_size_;
132 const size_t capacity_;
133 const size_t payload_stride_;
135 SequenceCell* sequences_;
136 std::byte* payloads_;
137
138 alignas(LibXR::CONCURRENCY_ALIGNMENT) std::atomic<
139 SequenceType> head_;
140 alignas(LibXR::CONCURRENCY_ALIGNMENT) std::atomic<
141 SequenceType> tail_;
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
std::make_signed_t< SequenceType > SequenceDiffType
size_t Size() const
获取并发快照下的当前元素数 / Get the current approximate element count
ErrorCode PopBytes(void *value=nullptr)
按字节出队一个 payload;传空指针时只丢弃队头元素 / Dequeue one payload by bytes; pass null to discard the front item only
size_t ElementSize() const
获取单个 payload 的字节数 / Get the byte size of one payload
~MPMCQueueBase()
析构字节队列内核 / Destroy the byte-queue core
size_t MaxSize() const
获取队列最大容量 / Get the maximum queue capacity
size_t SequenceType
单调递增的逻辑序号类型 / Monotonic logical sequence type.
ErrorCode PushBytes(const void *value)
按字节入队一个 payload / Enqueue one payload by bytes
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
constexpr size_t CONCURRENCY_ALIGNMENT
并发结构对齐粒度(用于降低多核伪共享) / Alignment policy used by concurrency-oriented structures
Definition libxr_def.hpp:61