libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::SPSCQueueBase Class Reference

单生产者单消费者字节队列内核 / Single-producer single-consumer byte-queue core More...

#include <spsc_queue_base.hpp>

Inheritance diagram for LibXR::SPSCQueueBase:
[legend]

Public Types

using IndexType = size_t
 环形缓冲区索引类型 / Ring-buffer index type.
 

Public Member Functions

 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
 
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
 
ErrorCode PeekBatchBytes (void *data, size_t count)
 按字节批量查看多个 payload 但不出队 / Peek multiple payloads by bytes without dequeuing them
 
void Reset ()
 重置队列状态 / Reset the queue state
 
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
 

Private Member Functions

void InitStorage ()
 
std::byte * PayloadPtr (IndexType index)
 获取指定槽位 payload 起始地址 / Get the payload base address of one slot
 
const std::byte * PayloadPtr (IndexType index) const
 获取指定槽位 payload 起始地址(只读) / Get the payload base address of one slot (const)
 
size_t RingCapacity () const
 获取环形缓冲区的物理槽位总数 / Get the physical ring-slot count
 
IndexType Increment (IndexType index) const
 沿环形缓冲区推进一个槽位 / Advance one slot along the ring
 
 SPSCQueueBase (const SPSCQueueBase &)
 禁止拷贝构造。 Non-copyable.
 
SPSCQueueBaseoperator= (const SPSCQueueBase &)
 禁止拷贝赋值。 Non-copy-assignable.
 
 SPSCQueueBase (SPSCQueueBase &&)
 禁止移动构造。 Non-movable.
 
SPSCQueueBaseoperator= (SPSCQueueBase &&)
 禁止移动赋值。 Non-move-assignable.
 

Static Private Member Functions

static size_t AlignUpChecked (size_t size, size_t align)
 安全地向上对齐字节数 / Safely align one byte count upward
 
static size_t ComputeStride (size_t element_size, size_t element_align)
 
static size_t MultiplyChecked (size_t lhs, size_t rhs)
 安全地计算两个字节数的乘积 / Safely multiply two byte counts
 

Private Attributes

const size_t element_size_
 单个 payload 的字节数。 Byte size of one payload.
 
const size_t capacity_
 队列容量。 Queue capacity.
 
const size_t payload_alloc_align_
 整体分配对齐。 Allocation alignment for the payload buffer.
 
const size_t payload_stride_
 相邻 payload 槽位之间的步长。 Byte stride between adjacent payload slots.
 
std::byte * payloads_
 payload 字节缓冲区。 Byte buffer storing payloads.
 
std::atomic< IndexTypehead_
 下一个待出队的环形下标。 Next ring index to dequeue.
 
std::atomic< IndexTypetail_
 下一个待入队的环形下标。 Next ring index to enqueue.
 

Detailed Description

单生产者单消费者字节队列内核 / Single-producer single-consumer byte-queue core

这个内核只负责 SPSC ring 的索引推进、容量判断与原始字节存储,不包含多消费者 抢占逻辑,也不暴露任何 CAS 路径。上层 SPSCQueue<Data> 只负责把具体类型 Data 映射到这些字节槽位。

This core contains only the SPSC ring progression, capacity checks, and raw byte storage. It does not include any multi-consumer claiming logic or expose any CAS-based path. The upper SPSCQueue<Data> wrapper is responsible only for mapping the concrete Data type onto these byte slots.

Definition at line 27 of file spsc_queue_base.hpp.

Member Typedef Documentation

◆ IndexType

环形缓冲区索引类型 / Ring-buffer index type.

Definition at line 30 of file spsc_queue_base.hpp.

Constructor & Destructor Documentation

◆ SPSCQueueBase() [1/2]

LibXR::SPSCQueueBase::SPSCQueueBase ( size_t element_size,
size_t capacity )
inline

构造 SPSC 字节队列内核 / Construct the SPSC byte-queue core

Parameters
element_size单个 payload 的字节数 / Byte size of one payload
capacity队列容量 / Queue capacity

Definition at line 37 of file spsc_queue_base.hpp.

38 : element_size_(element_size),
39 capacity_(capacity),
40 payload_alloc_align_(alignof(std::max_align_t)),
41 payload_stride_(ComputeStride(element_size, alignof(std::byte))),
42 payloads_(nullptr),
43 head_(0),
44 tail_(0)
45 {
46 InitStorage();
47 }
std::atomic< IndexType > head_
下一个待出队的环形下标。 Next ring index to dequeue.
std::atomic< IndexType > tail_
下一个待入队的环形下标。 Next ring index to enqueue.
const size_t payload_alloc_align_
整体分配对齐。 Allocation alignment for the payload buffer.
std::byte * payloads_
payload 字节缓冲区。 Byte buffer storing payloads.
const size_t payload_stride_
相邻 payload 槽位之间的步长。 Byte stride between adjacent payload slots.
const size_t capacity_
队列容量。 Queue capacity.
const size_t element_size_
单个 payload 的字节数。 Byte size of one payload.

◆ SPSCQueueBase() [2/2]

LibXR::SPSCQueueBase::SPSCQueueBase ( size_t element_size,
size_t element_align,
size_t capacity )
inline

构造 SPSC 字节队列内核并指定元素对齐 / Construct the SPSC byte-queue core with explicit element alignment

Parameters
element_size单个 payload 的字节数 / Byte size of one payload
element_align单个 payload 的对齐要求 / Alignment requirement of one payload
capacity队列容量 / Queue capacity

Definition at line 56 of file spsc_queue_base.hpp.

57 : element_size_(element_size),
58 capacity_(capacity),
59 payload_alloc_align_(alignof(std::max_align_t)),
60 payload_stride_(ComputeStride(element_size, element_align)),
61 payloads_(nullptr),
62 head_(0),
63 tail_(0)
64 {
65 InitStorage();
66 }

◆ ~SPSCQueueBase()

LibXR::SPSCQueueBase::~SPSCQueueBase ( )
inline

析构 SPSC 字节队列内核 / Destroy the SPSC byte-queue core

Definition at line 87 of file spsc_queue_base.hpp.

88 {
89 ::operator delete[](payloads_, std::align_val_t(payload_alloc_align_));
90 }

Member Function Documentation

◆ AlignUpChecked()

static size_t LibXR::SPSCQueueBase::AlignUpChecked ( size_t size,
size_t align )
inlinestaticprivate

安全地向上对齐字节数 / Safely align one byte count upward

Parameters
size待对齐字节数 / Byte count to align
align目标对齐粒度 / Target alignment granularity
Returns
对齐后的字节数 / Aligned byte count

Definition at line 497 of file spsc_queue_base.hpp.

498 {
499 ASSERT(align > 0);
500 ASSERT((align & (align - 1)) == 0);
501 ASSERT(size <= std::numeric_limits<size_t>::max() - (align - 1));
502 return ((size + align - 1) / align) * align;
503 }

◆ ComputeStride()

static size_t LibXR::SPSCQueueBase::ComputeStride ( size_t element_size,
size_t element_align )
inlinestaticprivate

Definition at line 505 of file spsc_queue_base.hpp.

506 {
507 ASSERT(element_size > 0);
508 ASSERT(element_align > 0);
509 ASSERT((element_align & (element_align - 1)) == 0);
510 return AlignUpChecked(element_size, element_align);
511 }
static size_t AlignUpChecked(size_t size, size_t align)
安全地向上对齐字节数 / Safely align one byte count upward

◆ EmptySize()

size_t LibXR::SPSCQueueBase::EmptySize ( ) const
inline

获取剩余空槽数 / Get the current free-slot count

Returns
当前空槽个数 / Current number of free slots

Definition at line 434 of file spsc_queue_base.hpp.

434{ return capacity_ - Size(); }
size_t Size() const
获取当前已用元素数 / Get the current element count

◆ Increment()

IndexType LibXR::SPSCQueueBase::Increment ( IndexType index) const
inlineprivate

沿环形缓冲区推进一个槽位 / Advance one slot along the ring

Parameters
index当前槽位下标 / Current slot index
Returns
推进后的槽位下标 / Advanced slot index

Definition at line 477 of file spsc_queue_base.hpp.

478 {
479 return (index + 1) % RingCapacity();
480 }
size_t RingCapacity() const
获取环形缓冲区的物理槽位总数 / Get the physical ring-slot count

◆ InitStorage()

void LibXR::SPSCQueueBase::InitStorage ( )
inlineprivate

Definition at line 69 of file spsc_queue_base.hpp.

70 {
71 ASSERT(element_size_ > 0);
72 ASSERT(payload_alloc_align_ > 0);
73 ASSERT(capacity_ > 0);
74 ASSERT(capacity_ <= std::numeric_limits<size_t>::max() - 1);
75 ASSERT((payload_alloc_align_ & (payload_alloc_align_ - 1)) == 0);
76
77 const size_t payload_bytes = MultiplyChecked(payload_stride_, RingCapacity());
78 payloads_ = static_cast<std::byte*>(::operator new[](
79 payload_bytes, std::align_val_t(payload_alloc_align_)));
80 }
static size_t MultiplyChecked(size_t lhs, size_t rhs)
安全地计算两个字节数的乘积 / Safely multiply two byte counts

◆ MaxSize()

size_t LibXR::SPSCQueueBase::MaxSize ( ) const
inline

获取队列最大容量 / Get the maximum queue capacity

Returns
队列容量 / Queue capacity

Definition at line 440 of file spsc_queue_base.hpp.

440{ return capacity_; }

◆ MultiplyChecked()

static size_t LibXR::SPSCQueueBase::MultiplyChecked ( size_t lhs,
size_t rhs )
inlinestaticprivate

安全地计算两个字节数的乘积 / Safely multiply two byte counts

Parameters
lhs左操作数 / Left operand
rhs右操作数 / Right operand
Returns
乘积结果 / Product result

Definition at line 519 of file spsc_queue_base.hpp.

520 {
521 if (lhs == 0 || rhs == 0)
522 {
523 return 0;
524 }
525
526 ASSERT(lhs <= std::numeric_limits<size_t>::max() / rhs);
527 return lhs * rhs;
528 }

◆ PayloadPtr() [1/2]

std::byte * LibXR::SPSCQueueBase::PayloadPtr ( IndexType index)
inlineprivate

获取指定槽位 payload 起始地址 / Get the payload base address of one slot

Parameters
index目标槽位下标 / Target slot index
Returns
指向目标槽位 payload 起始地址的指针 / Pointer to the slot payload base address

Definition at line 448 of file spsc_queue_base.hpp.

449 {
450 return payloads_ + index * payload_stride_;
451 }

◆ PayloadPtr() [2/2]

const std::byte * LibXR::SPSCQueueBase::PayloadPtr ( IndexType index) const
inlineprivate

获取指定槽位 payload 起始地址(只读) / Get the payload base address of one slot (const)

Parameters
index目标槽位下标 / Target slot index
Returns
指向目标槽位 payload 起始地址的只读指针 / Read-only pointer to the slot payload base address

Definition at line 460 of file spsc_queue_base.hpp.

461 {
462 return payloads_ + index * payload_stride_;
463 }

◆ PeekBatchBytes()

ErrorCode LibXR::SPSCQueueBase::PeekBatchBytes ( void * data,
size_t count )
inline

按字节批量查看多个 payload 但不出队 / Peek multiple payloads by bytes without dequeuing them

Parameters
data用于接收 payload 的字节缓冲区 / Byte buffer receiving payloads
countpayload 个数 / Number of payloads
Returns
成功返回 ErrorCode::OK;元素不足返回 ErrorCode::EMPTY Returns ErrorCode::OK on success; returns ErrorCode::EMPTY when there are not enough payloads available

Definition at line 376 of file spsc_queue_base.hpp.

377 {
378 if (count == 0U)
379 {
380 return ErrorCode::OK;
381 }
382 if (data == nullptr)
383 {
384 return ErrorCode::PTR_NULL;
385 }
386
387 const auto current_head = head_.load(std::memory_order_relaxed);
388 const auto current_tail = tail_.load(std::memory_order_acquire);
389 const size_t capacity = RingCapacity();
390 const size_t available = (current_tail >= current_head)
391 ? (current_tail - current_head)
392 : (capacity - current_head + current_tail);
393
394 if (available < count)
395 {
396 return ErrorCode::EMPTY;
397 }
398
399 auto* dst = static_cast<std::byte*>(data);
400 for (size_t index = 0; index < count; ++index)
401 {
403 PayloadPtr((current_head + index) % capacity),
405 }
406 return ErrorCode::OK;
407 }
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
std::byte * PayloadPtr(IndexType index)
获取指定槽位 payload 起始地址 / Get the payload base address of one slot
@ PTR_NULL
空指针 | Null pointer
@ EMPTY
为空 | Empty
@ OK
操作成功 | Operation successful

◆ PeekBytes()

ErrorCode LibXR::SPSCQueueBase::PeekBytes ( void * value)
inline

按字节查看一个队头 payload 但不出队 / Peek one front payload by bytes without dequeuing it

Parameters
value用于接收 payload 的缓冲区 / Buffer that receives the payload
Returns
成功返回 ErrorCode::OK;队列空返回 ErrorCode::EMPTY;空指针返回 ErrorCode::PTR_NULL Returns ErrorCode::OK on success; returns ErrorCode::EMPTY when the queue is empty; returns ErrorCode::PTR_NULL when value is null

Definition at line 155 of file spsc_queue_base.hpp.

156 {
157 if (value == nullptr)
158 {
159 return ErrorCode::PTR_NULL;
160 }
161
162 const auto current_head = head_.load(std::memory_order_relaxed);
163 if (current_head == tail_.load(std::memory_order_acquire))
164 {
165 return ErrorCode::EMPTY;
166 }
167
168 LibXR::Memory::FastCopy(value, PayloadPtr(current_head), element_size_);
169 return ErrorCode::OK;
170 }

◆ PopBatchBytes()

ErrorCode LibXR::SPSCQueueBase::PopBatchBytes ( void * data,
size_t count )
inline

按字节批量出队多个 payload / Dequeue multiple payloads by bytes

Parameters
data用于接收 payload 的字节缓冲区;传 nullptr 时仅丢弃 / Byte buffer receiving payloads; pass nullptr to discard only
countpayload 个数 / Number of payloads
Returns
成功返回 ErrorCode::OK;元素不足返回 ErrorCode::EMPTY Returns ErrorCode::OK on success; returns ErrorCode::EMPTY when there are not enough payloads available

Definition at line 278 of file spsc_queue_base.hpp.

279 {
280 if (count == 0U)
281 {
282 return ErrorCode::OK;
283 }
284
285 const auto current_head = head_.load(std::memory_order_relaxed);
286 const auto current_tail = tail_.load(std::memory_order_acquire);
287 const size_t capacity = RingCapacity();
288 const size_t available = (current_tail >= current_head)
289 ? (current_tail - current_head)
290 : (capacity - current_head + current_tail);
291
292 if (available < count)
293 {
294 return ErrorCode::EMPTY;
295 }
296
297 auto* dst = static_cast<std::byte*>(data);
298 if (dst != nullptr)
299 {
300 for (size_t index = 0; index < count; ++index)
301 {
303 PayloadPtr((current_head + index) % capacity),
305 }
306 }
307
308 head_.store((current_head + count) % capacity, std::memory_order_release);
309 return ErrorCode::OK;
310 }

◆ PopBytes()

ErrorCode LibXR::SPSCQueueBase::PopBytes ( void * value = nullptr)
inline

按字节出队一个 payload;传空指针时仅丢弃队头元素 / Dequeue one payload by bytes; pass null to discard the front item only

Parameters
value用于接收 payload 的缓冲区;传 nullptr 时仅丢弃 / Buffer that receives the payload; pass nullptr to discard only
Returns
成功返回 ErrorCode::OK;队列空返回 ErrorCode::EMPTY Returns ErrorCode::OK on success; returns ErrorCode::EMPTY when the queue is empty

Definition at line 129 of file spsc_queue_base.hpp.

130 {
131 const auto current_head = head_.load(std::memory_order_relaxed);
132
133 if (current_head == tail_.load(std::memory_order_acquire))
134 {
135 return ErrorCode::EMPTY;
136 }
137
138 if (value != nullptr)
139 {
140 LibXR::Memory::FastCopy(value, PayloadPtr(current_head), element_size_);
141 }
142
143 head_.store(Increment(current_head), std::memory_order_release);
144 return ErrorCode::OK;
145 }
IndexType Increment(IndexType index) const
沿环形缓冲区推进一个槽位 / Advance one slot along the ring

◆ PopBytesWithReader()

template<typename Reader >
ErrorCode LibXR::SPSCQueueBase::PopBytesWithReader ( size_t count,
Reader && reader )
inline

通过读取器回调批量出队 payload / Dequeue payloads through a reader callback

Template Parameters
Reader读取器类型 / Reader callback type
Parameters
countpayload 个数 / Number of payloads
reader读取器,签名为 ErrorCode(const void* buffer, size_t chunk_count) / Reader with signature ErrorCode(const void* buffer, size_t chunk_count)
Returns
成功返回 ErrorCode::OK;元素不足返回 ErrorCode::EMPTY;否则返回读取器错误码 / Returns ErrorCode::OK on success, ErrorCode::EMPTY when there are not enough payloads, otherwise returns the reader error code
Note
读取器每次收到一段连续 payload 存储区,字节长度为 chunk_count * element_size / The reader receives one contiguous payload storage chunk each time, with byte length chunk_count * element_size

Definition at line 327 of file spsc_queue_base.hpp.

328 {
329 if (count == 0U)
330 {
331 return ErrorCode::OK;
332 }
333
334 const auto current_head = head_.load(std::memory_order_relaxed);
335 const auto current_tail = tail_.load(std::memory_order_acquire);
336 const size_t capacity = RingCapacity();
337 const size_t available = (current_tail >= current_head)
338 ? (current_tail - current_head)
339 : (capacity - current_head + current_tail);
340
341 if (available < count)
342 {
343 return ErrorCode::EMPTY;
344 }
345
346 const size_t first_chunk = std::min(count, capacity - current_head);
347 Reader& reader_ref = reader;
348 const ErrorCode first_ec = reader_ref(PayloadPtr(current_head), first_chunk);
349 if (first_ec != ErrorCode::OK)
350 {
351 return first_ec;
352 }
353
354 if (count > first_chunk)
355 {
356 const ErrorCode second_ec = reader_ref(PayloadPtr(0), count - first_chunk);
357 if (second_ec != ErrorCode::OK)
358 {
359 return second_ec;
360 }
361 }
362
363 head_.store((current_head + count) % capacity, std::memory_order_release);
364 return ErrorCode::OK;
365 }
ErrorCode
定义错误码枚举

◆ PushBatchBytes()

ErrorCode LibXR::SPSCQueueBase::PushBatchBytes ( const void * data,
size_t count )
inline

按字节批量入队多个 payload / Enqueue multiple payloads by bytes

Parameters
data指向 payload 数组的字节指针 / Byte pointer to the payload array
countpayload 个数 / Number of payloads
Returns
成功返回 ErrorCode::OK;队列满返回 ErrorCode::FULL Returns ErrorCode::OK on success; returns ErrorCode::FULL when the queue is full

Definition at line 180 of file spsc_queue_base.hpp.

181 {
182 if (count == 0U)
183 {
184 return ErrorCode::OK;
185 }
186 if (data == nullptr)
187 {
188 return ErrorCode::PTR_NULL;
189 }
190
191 const auto current_tail = tail_.load(std::memory_order_relaxed);
192 const auto current_head = head_.load(std::memory_order_acquire);
193 const size_t capacity = RingCapacity();
194 const size_t free_space =
195 (current_tail >= current_head) ? (capacity - (current_tail - current_head) - 1)
196 : (current_head - current_tail - 1);
197
198 if (free_space < count)
199 {
200 return ErrorCode::FULL;
201 }
202
203 const auto* src = static_cast<const std::byte*>(data);
204 for (size_t index = 0; index < count; ++index)
205 {
206 LibXR::Memory::FastCopy(PayloadPtr((current_tail + index) % capacity),
207 src + index * element_size_, element_size_);
208 }
209
210 tail_.store((current_tail + count) % capacity, std::memory_order_release);
211 return ErrorCode::OK;
212 }
@ FULL
已满 | Full

◆ PushBytes()

ErrorCode LibXR::SPSCQueueBase::PushBytes ( const void * value)
inline

按字节入队一个 payload / Enqueue one payload by bytes

Parameters
value指向待入队 payload 的指针 / Pointer to the payload to enqueue
Returns
成功返回 ErrorCode::OK;队列满返回 ErrorCode::FULL;空指针返回 ErrorCode::PTR_NULL Returns ErrorCode::OK on success; returns ErrorCode::FULL when the queue is full; returns ErrorCode::PTR_NULL when value is null

Definition at line 100 of file spsc_queue_base.hpp.

101 {
102 if (value == nullptr)
103 {
104 return ErrorCode::PTR_NULL;
105 }
106
107 const auto current_tail = tail_.load(std::memory_order_relaxed);
108 const auto next_tail = Increment(current_tail);
109
110 if (next_tail == head_.load(std::memory_order_acquire))
111 {
112 return ErrorCode::FULL;
113 }
114
115 LibXR::Memory::FastCopy(PayloadPtr(current_tail), value, element_size_);
116 tail_.store(next_tail, std::memory_order_release);
117 return ErrorCode::OK;
118 }

◆ PushBytesWithWriter()

template<typename Writer >
ErrorCode LibXR::SPSCQueueBase::PushBytesWithWriter ( size_t count,
Writer && writer )
inline

通过写入器回调批量入队 payload / Enqueue payloads through a writer callback

Template Parameters
Writer写入器类型 / Writer callback type
Parameters
countpayload 个数 / Number of payloads
writer写入器,签名为 ErrorCode(void* buffer, size_t chunk_count) / Writer with signature ErrorCode(void* buffer, size_t chunk_count)
Returns
成功返回 ErrorCode::OK;空间不足返回 ErrorCode::FULL;否则返回写入器错误码 / Returns ErrorCode::OK on success, ErrorCode::FULL when free space is insufficient, otherwise returns the writer error code
Note
写入器每次收到一段连续 payload 存储区,字节长度为 chunk_count * element_size / The writer receives one contiguous payload storage chunk each time, with byte length chunk_count * element_size

Definition at line 229 of file spsc_queue_base.hpp.

230 {
231 if (count == 0U)
232 {
233 return ErrorCode::OK;
234 }
235
236 const auto current_tail = tail_.load(std::memory_order_relaxed);
237 const auto current_head = head_.load(std::memory_order_acquire);
238 const size_t capacity = RingCapacity();
239 const size_t free_space =
240 (current_tail >= current_head) ? (capacity - (current_tail - current_head) - 1)
241 : (current_head - current_tail - 1);
242
243 if (free_space < count)
244 {
245 return ErrorCode::FULL;
246 }
247
248 const size_t first_chunk = std::min(count, capacity - current_tail);
249 Writer& writer_ref = writer;
250 const ErrorCode first_ec = writer_ref(PayloadPtr(current_tail), first_chunk);
251 if (first_ec != ErrorCode::OK)
252 {
253 return first_ec;
254 }
255
256 if (count > first_chunk)
257 {
258 const ErrorCode second_ec = writer_ref(PayloadPtr(0), count - first_chunk);
259 if (second_ec != ErrorCode::OK)
260 {
261 return second_ec;
262 }
263 }
264
265 tail_.store((current_tail + count) % capacity, std::memory_order_release);
266 return ErrorCode::OK;
267 }

◆ Reset()

void LibXR::SPSCQueueBase::Reset ( )
inline

重置队列状态 / Reset the queue state

Definition at line 412 of file spsc_queue_base.hpp.

413 {
414 head_.store(0, std::memory_order_relaxed);
415 tail_.store(0, std::memory_order_relaxed);
416 }

◆ RingCapacity()

size_t LibXR::SPSCQueueBase::RingCapacity ( ) const
inlineprivate

获取环形缓冲区的物理槽位总数 / Get the physical ring-slot count

Returns
物理环形槽位总数(含一个保留空槽) / Physical ring-slot count including one reserved empty slot

Definition at line 470 of file spsc_queue_base.hpp.

470{ return capacity_ + 1; }

◆ Size()

size_t LibXR::SPSCQueueBase::Size ( ) const
inline

获取当前已用元素数 / Get the current element count

Returns
当前元素个数 / Current number of stored payloads

Definition at line 422 of file spsc_queue_base.hpp.

423 {
424 const auto current_head = head_.load(std::memory_order_acquire);
425 const auto current_tail = tail_.load(std::memory_order_acquire);
426 return (current_tail >= current_head) ? (current_tail - current_head)
427 : (RingCapacity() - current_head + current_tail);
428 }

Field Documentation

◆ capacity_

const size_t LibXR::SPSCQueueBase::capacity_
private

队列容量。 Queue capacity.

Definition at line 531 of file spsc_queue_base.hpp.

◆ element_size_

const size_t LibXR::SPSCQueueBase::element_size_
private

单个 payload 的字节数。 Byte size of one payload.

Definition at line 530 of file spsc_queue_base.hpp.

◆ head_

std::atomic<IndexType> LibXR::SPSCQueueBase::head_
private

下一个待出队的环形下标。 Next ring index to dequeue.

Definition at line 537 of file spsc_queue_base.hpp.

◆ payload_alloc_align_

const size_t LibXR::SPSCQueueBase::payload_alloc_align_
private

整体分配对齐。 Allocation alignment for the payload buffer.

Definition at line 532 of file spsc_queue_base.hpp.

◆ payload_stride_

const size_t LibXR::SPSCQueueBase::payload_stride_
private

相邻 payload 槽位之间的步长。 Byte stride between adjacent payload slots.

Definition at line 533 of file spsc_queue_base.hpp.

◆ payloads_

std::byte* LibXR::SPSCQueueBase::payloads_
private

payload 字节缓冲区。 Byte buffer storing payloads.

Definition at line 534 of file spsc_queue_base.hpp.

◆ tail_

std::atomic<IndexType> LibXR::SPSCQueueBase::tail_
private

下一个待入队的环形下标。 Next ring index to enqueue.

Definition at line 539 of file spsc_queue_base.hpp.


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