5#include "libxr_def.hpp"
7static constexpr size_t LIBXR_CACHE_LINE_SIZE =
8 (
sizeof(std::atomic<size_t>) * 8 > 32) ? 64 : 32;
25template <
typename Data>
63 template <
typename ElementData = Data>
66 const auto CURRENT_TAIL = tail_.load(std::memory_order_relaxed);
69 if (
NEXT_TAIL == head_.load(std::memory_order_acquire))
71 return ErrorCode::FULL;
75 tail_.store(
NEXT_TAIL, std::memory_order_release);
86 template <
typename ElementData = Data>
89 auto current_head = head_.load(std::memory_order_relaxed);
93 if (
current_head == tail_.load(std::memory_order_acquire))
95 return ErrorCode::EMPTY;
99 std::memory_order_acquire,
100 std::memory_order_relaxed))
103 return ErrorCode::OK;
132 auto current_head = head_.load(std::memory_order_relaxed);
136 if (
current_head == tail_.load(std::memory_order_acquire))
138 return ErrorCode::EMPTY;
142 std::memory_order_acquire,
143 std::memory_order_relaxed))
145 std::atomic_thread_fence(std::memory_order_acquire);
147 return ErrorCode::OK;
162 auto current_head = head_.load(std::memory_order_relaxed);
166 if (
current_head == tail_.load(std::memory_order_acquire))
168 return ErrorCode::EMPTY;
172 std::memory_order_acquire,
173 std::memory_order_relaxed))
175 return ErrorCode::OK;
191 const auto CURRENT_HEAD = head_.load(std::memory_order_acquire);
192 if (
CURRENT_HEAD == tail_.load(std::memory_order_acquire))
194 return ErrorCode::EMPTY;
198 return ErrorCode::OK;
211 auto current_tail = tail_.load(std::memory_order_relaxed);
212 auto current_head = head_.load(std::memory_order_acquire);
221 return ErrorCode::FULL;
224 for (
size_t i = 0; i < size; ++i)
230 return ErrorCode::OK;
247 auto current_head = head_.load(std::memory_order_relaxed);
248 auto current_tail = tail_.load(std::memory_order_acquire);
256 return ErrorCode::EMPTY;
267 std::memory_order_relaxed))
269 return ErrorCode::OK;
282 head_.store(0, std::memory_order_relaxed);
283 tail_.store(0, std::memory_order_relaxed);
292 const auto CURRENT_HEAD = head_.load(std::memory_order_acquire);
293 const auto CURRENT_TAIL = tail_.load(std::memory_order_acquire);
304 alignas(LIBXR_CACHE_LINE_SIZE) std::atomic<unsigned int> head_;
305 alignas(LIBXR_CACHE_LINE_SIZE) std::atomic<unsigned int> tail_;
309 unsigned int Increment(
unsigned int index)
const {
return (
index + 1) % (length_ + 1); }
无锁队列实现 / Lock-free queue implementation
void Reset()
重置队列 / Resets the queue
ErrorCode Pop(ElementData &item)
从队列中弹出数据 / Pops data from the queue
ErrorCode Pop()
从队列中弹出数据(不返回数据) / Pops data from the queue (without returning data)
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
ErrorCode Push(ElementData &&item)
向队列中推入数据 / Pushes data into the queue
ErrorCode PopBatch(Data *data, size_t batch_size)
批量弹出数据 / Pops multiple elements from the queue
ErrorCode Pop(Data &item)
从队列中移除头部元素,并获取该元素的数据 (Remove the front element from the queue and retrieve its data).
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue
LockFreeQueue(size_t length)
构造函数 / Constructor
Data * operator[](uint32_t index)
获取指定索引的数据指针 / Retrieves the data pointer at a specified index
size_t Size() const
获取当前队列中的元素数量 / Returns the number of elements currently in the queue
~LockFreeQueue()
析构函数 / Destructor
ErrorCode Peek(Data &item)
获取队列头部数据但不弹出 / Retrieves the front data of the queue without popping
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值