libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
lockfree_queue.hpp
1#pragma once
2
3#include <atomic>
4
5#include "libxr_def.hpp"
6
7namespace LibXR
8{
9
24template <typename Data>
25class alignas(LIBXR_CACHE_LINE_SIZE) LockFreeQueue
26{
27 inline constexpr size_t AlignUp(size_t size, size_t align)
28 {
29 return ((size + align - 1) / align) * align;
30 }
31
32 public:
40 LockFreeQueue(size_t length)
41 : head_(0),
42 tail_(0),
43 LENGTH(AlignUp(length, LIBXR_ALIGN_SIZE) - 1),
44 queue_handle_(new Data[LENGTH + 1])
45 {
46 }
47
54 ~LockFreeQueue() { delete[] queue_handle_; }
55
61 Data *operator[](uint32_t index) { return &queue_handle_[static_cast<size_t>(index)]; }
62
70 template <typename ElementData = Data>
71 ErrorCode Push(ElementData &&item)
72 {
73 const auto CURRENT_TAIL = tail_.load(std::memory_order_relaxed);
74 const auto NEXT_TAIL = Increment(CURRENT_TAIL);
75
76 if (NEXT_TAIL == head_.load(std::memory_order_acquire))
77 {
78 return ErrorCode::FULL;
79 }
80
81 queue_handle_[CURRENT_TAIL] = std::forward<ElementData>(item);
82 tail_.store(NEXT_TAIL, std::memory_order_release);
83 return ErrorCode::OK;
84 }
85
93 template <typename ElementData = Data>
94 ErrorCode Pop(ElementData &item)
95 {
96 auto current_head = head_.load(std::memory_order_relaxed);
97
98 while (true)
99 {
100 if (current_head == tail_.load(std::memory_order_acquire))
101 {
102 return ErrorCode::EMPTY;
103 }
104
105 if (head_.compare_exchange_weak(current_head, Increment(current_head),
106 std::memory_order_acquire,
107 std::memory_order_relaxed))
108 {
109 item = queue_handle_[current_head];
110 return ErrorCode::OK;
111 }
112 }
113 }
114
137 ErrorCode Pop(Data &item)
138 {
139 auto current_head = head_.load(std::memory_order_relaxed);
140
141 while (true)
142 {
143 if (current_head == tail_.load(std::memory_order_acquire))
144 {
145 return ErrorCode::EMPTY;
146 }
147
148 if (head_.compare_exchange_weak(current_head, Increment(current_head),
149 std::memory_order_acquire,
150 std::memory_order_relaxed))
151 {
152 std::atomic_thread_fence(std::memory_order_acquire);
153 item = queue_handle_[current_head];
154 return ErrorCode::OK;
155 }
156 current_head = head_.load(std::memory_order_relaxed);
157 }
158 }
159
167 ErrorCode Pop()
168 {
169 auto current_head = head_.load(std::memory_order_relaxed);
170
171 while (true)
172 {
173 if (current_head == tail_.load(std::memory_order_acquire))
174 {
175 return ErrorCode::EMPTY;
176 }
177
178 if (head_.compare_exchange_weak(current_head, Increment(current_head),
179 std::memory_order_acquire,
180 std::memory_order_relaxed))
181 {
182 return ErrorCode::OK;
183 }
184 current_head = head_.load(std::memory_order_relaxed);
185 }
186 }
187
196 ErrorCode Peek(Data &item)
197 {
198 const auto CURRENT_HEAD = head_.load(std::memory_order_acquire);
199 if (CURRENT_HEAD == tail_.load(std::memory_order_acquire))
200 {
201 return ErrorCode::EMPTY;
202 }
203
204 item = queue_handle_[CURRENT_HEAD];
205 return ErrorCode::OK;
206 }
207
216 ErrorCode PushBatch(const Data *data, size_t size)
217 {
218 auto current_tail = tail_.load(std::memory_order_relaxed);
219 auto current_head = head_.load(std::memory_order_acquire);
220
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);
225
226 if (free_space < size)
227 {
228 return ErrorCode::FULL;
229 }
230
231 size_t first_chunk = LibXR::min(size, capacity - current_tail);
232 LibXR::Memory::FastCopy(reinterpret_cast<void *>(queue_handle_ + current_tail),
233 reinterpret_cast<const void *>(data),
234 first_chunk * sizeof(Data));
235
236 if (size > first_chunk)
237 {
238 LibXR::Memory::FastCopy(reinterpret_cast<void *>(queue_handle_),
239 reinterpret_cast<const void *>(data + first_chunk),
240 (size - first_chunk) * sizeof(Data));
241 }
242
243 tail_.store((current_tail + size) % capacity, std::memory_order_release);
244 return ErrorCode::OK;
245 }
246
255 ErrorCode PopBatch(Data *data, size_t size)
256 {
257 size_t capacity = LENGTH + 1;
258
259 while (true)
260 {
261 auto current_head = head_.load(std::memory_order_relaxed);
262 auto current_tail = tail_.load(std::memory_order_acquire);
263
264 size_t available = (current_tail >= current_head)
265 ? (current_tail - current_head)
266 : (capacity - current_head + current_tail);
267
268 if (available < size)
269 {
270 return ErrorCode::EMPTY;
271 }
272
273 if (data != nullptr)
274 {
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));
280
281 if (size > first_chunk)
282 {
283 LibXR::Memory::FastCopy(reinterpret_cast<void *>(data + first_chunk),
284 reinterpret_cast<const void *>(queue_handle_),
285 (size - first_chunk) * sizeof(Data));
286 }
287 }
288
289 size_t new_head = (current_head + size) % capacity;
290
291 if (head_.compare_exchange_weak(current_head, new_head, std::memory_order_acquire,
292 std::memory_order_relaxed))
293 {
294 return ErrorCode::OK;
295 }
296 }
297 }
298
308 ErrorCode PeekBatch(Data *data, size_t size)
309 {
310 size_t capacity = LENGTH + 1;
311
312 while (true)
313 {
314 auto current_head = head_.load(std::memory_order_relaxed);
315 auto current_tail = tail_.load(std::memory_order_acquire);
316
317 size_t available = (current_tail >= current_head)
318 ? (current_tail - current_head)
319 : (capacity - current_head + current_tail);
320
321 if (available < size)
322 {
323 return ErrorCode::EMPTY;
324 }
325
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));
331
332 if (size > first_chunk)
333 {
334 LibXR::Memory::FastCopy(reinterpret_cast<void *>(data + first_chunk),
335 reinterpret_cast<const void *>(queue_handle_),
336 (size - first_chunk) * sizeof(Data));
337 }
338
339 if (head_.load(std::memory_order_acquire) == current_head)
340 {
341 return ErrorCode::OK;
342 }
343 }
344 }
345
352 void Reset()
353 {
354 head_.store(0, std::memory_order_relaxed);
355 tail_.store(0, std::memory_order_relaxed);
356 }
357
362 size_t Size() const
363 {
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);
368 }
369
373 size_t EmptySize() { return LENGTH - Size(); }
374
375 private:
376 alignas(LIBXR_CACHE_LINE_SIZE) std::atomic<uint32_t> head_;
377 alignas(LIBXR_CACHE_LINE_SIZE) std::atomic<uint32_t> tail_;
378 const size_t LENGTH;
379 Data *queue_handle_;
380
381 uint32_t Increment(uint32_t index) const { return (index + 1) % (LENGTH + 1); }
382};
383
384} // namespace LibXR
无锁队列实现 / 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
Definition libxr_mem.cpp:17
LibXR 命名空间
Definition ch32_gpio.hpp:9
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值