5#include "libxr_def.hpp"
24template <
typename Data>
27 inline constexpr size_t AlignUp(
size_t size,
size_t align)
29 return ((size + align - 1) / align) * align;
43 LENGTH(AlignUp(length, LIBXR_ALIGN_SIZE) - 1),
44 queue_handle_(new Data[LENGTH + 1])
61 Data *
operator[](uint32_t index) {
return &queue_handle_[
static_cast<size_t>(index)]; }
70 template <
typename ElementData = Data>
71 ErrorCode
Push(ElementData &&item)
73 const auto CURRENT_TAIL = tail_.load(std::memory_order_relaxed);
74 const auto NEXT_TAIL = Increment(CURRENT_TAIL);
76 if (NEXT_TAIL == head_.load(std::memory_order_acquire))
78 return ErrorCode::FULL;
81 queue_handle_[CURRENT_TAIL] = std::forward<ElementData>(item);
82 tail_.store(NEXT_TAIL, std::memory_order_release);
93 template <
typename ElementData = Data>
94 ErrorCode
Pop(ElementData &item)
96 auto current_head = head_.load(std::memory_order_relaxed);
100 if (current_head == tail_.load(std::memory_order_acquire))
102 return ErrorCode::EMPTY;
105 if (head_.compare_exchange_weak(current_head, Increment(current_head),
106 std::memory_order_acquire,
107 std::memory_order_relaxed))
109 item = queue_handle_[current_head];
110 return ErrorCode::OK;
139 auto current_head = head_.load(std::memory_order_relaxed);
143 if (current_head == tail_.load(std::memory_order_acquire))
145 return ErrorCode::EMPTY;
148 if (head_.compare_exchange_weak(current_head, Increment(current_head),
149 std::memory_order_acquire,
150 std::memory_order_relaxed))
152 std::atomic_thread_fence(std::memory_order_acquire);
153 item = queue_handle_[current_head];
154 return ErrorCode::OK;
156 current_head = head_.load(std::memory_order_relaxed);
169 auto current_head = head_.load(std::memory_order_relaxed);
173 if (current_head == tail_.load(std::memory_order_acquire))
175 return ErrorCode::EMPTY;
178 if (head_.compare_exchange_weak(current_head, Increment(current_head),
179 std::memory_order_acquire,
180 std::memory_order_relaxed))
182 return ErrorCode::OK;
184 current_head = head_.load(std::memory_order_relaxed);
198 const auto CURRENT_HEAD = head_.load(std::memory_order_acquire);
199 if (CURRENT_HEAD == tail_.load(std::memory_order_acquire))
201 return ErrorCode::EMPTY;
204 item = queue_handle_[CURRENT_HEAD];
205 return ErrorCode::OK;
218 auto current_tail = tail_.load(std::memory_order_relaxed);
219 auto current_head = head_.load(std::memory_order_acquire);
221 size_t capacity = LENGTH + 1;
222 size_t free_space = (current_tail >= current_head)
223 ? (capacity - (current_tail - current_head) - 1)
224 : (current_head - current_tail - 1);
226 if (free_space < size)
228 return ErrorCode::FULL;
231 size_t first_chunk =
LibXR::min(size, capacity - current_tail);
233 reinterpret_cast<const void *
>(data),
234 first_chunk *
sizeof(Data));
236 if (size > first_chunk)
239 reinterpret_cast<const void *
>(data + first_chunk),
240 (size - first_chunk) *
sizeof(Data));
243 tail_.store((current_tail + size) % capacity, std::memory_order_release);
244 return ErrorCode::OK;
257 size_t capacity = LENGTH + 1;
261 auto current_head = head_.load(std::memory_order_relaxed);
262 auto current_tail = tail_.load(std::memory_order_acquire);
264 size_t available = (current_tail >= current_head)
265 ? (current_tail - current_head)
266 : (capacity - current_head + current_tail);
268 if (available < size)
270 return ErrorCode::EMPTY;
275 size_t first_chunk =
LibXR::min(size, capacity - current_head);
277 reinterpret_cast<void *
>(data),
278 reinterpret_cast<const void *
>(queue_handle_ + current_head),
279 first_chunk *
sizeof(Data));
281 if (size > first_chunk)
284 reinterpret_cast<const void *
>(queue_handle_),
285 (size - first_chunk) *
sizeof(Data));
289 size_t new_head = (current_head + size) % capacity;
291 if (head_.compare_exchange_weak(current_head, new_head, std::memory_order_acquire,
292 std::memory_order_relaxed))
294 return ErrorCode::OK;
310 size_t capacity = LENGTH + 1;
314 auto current_head = head_.load(std::memory_order_relaxed);
315 auto current_tail = tail_.load(std::memory_order_acquire);
317 size_t available = (current_tail >= current_head)
318 ? (current_tail - current_head)
319 : (capacity - current_head + current_tail);
321 if (available < size)
323 return ErrorCode::EMPTY;
326 size_t first_chunk =
LibXR::min(size, capacity - current_head);
328 reinterpret_cast<void *
>(data),
329 reinterpret_cast<const void *
>(queue_handle_ + current_head),
330 first_chunk *
sizeof(Data));
332 if (size > first_chunk)
335 reinterpret_cast<const void *
>(queue_handle_),
336 (size - first_chunk) *
sizeof(Data));
339 if (head_.load(std::memory_order_acquire) == current_head)
341 return ErrorCode::OK;
354 head_.store(0, std::memory_order_relaxed);
355 tail_.store(0, std::memory_order_relaxed);
364 const auto CURRENT_HEAD = head_.load(std::memory_order_acquire);
365 const auto CURRENT_TAIL = tail_.load(std::memory_order_acquire);
366 return (CURRENT_TAIL >= CURRENT_HEAD) ? (CURRENT_TAIL - CURRENT_HEAD)
367 : ((LENGTH + 1) - CURRENT_HEAD + CURRENT_TAIL);
376 alignas(LIBXR_CACHE_LINE_SIZE) std::atomic<uint32_t> head_;
377 alignas(LIBXR_CACHE_LINE_SIZE) std::atomic<uint32_t> tail_;
381 uint32_t Increment(uint32_t 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 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
ErrorCode PeekBatch(Data *data, size_t size)
批量查看队列中的数据(不移除) / Peeks multiple elements from the queue without removing them
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 PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
ErrorCode Peek(Data &item)
获取队列头部数据但不弹出 / Retrieves the front data of the queue without popping
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值