libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::LockFreeQueue< Data > Class Template Reference

无锁队列实现 / Lock-free queue implementation More...

#include <lockfree_queue.hpp>

Public Member Functions

 LockFreeQueue (size_t length)
 构造函数 / Constructor
 
 ~LockFreeQueue ()
 析构函数 / Destructor
 
Data * operator[] (uint32_t index)
 获取指定索引的数据指针 / Retrieves the data pointer at a specified index
 
template<typename ElementData = Data>
ErrorCode Push (ElementData &&item)
 向队列中推入数据 / Pushes data into the queue
 
template<typename ElementData = Data>
ErrorCode Pop (ElementData &item)
 从队列中弹出数据 / Pops data from the queue
 
ErrorCode Pop (Data &item)
 从队列中移除头部元素,并获取该元素的数据 (Remove the front element from the queue and retrieve its data).
 
ErrorCode Pop ()
 从队列中弹出数据(不返回数据) / Pops data from the queue (without returning data)
 
ErrorCode Peek (Data &item)
 获取队列头部数据但不弹出 / Retrieves the front data of the queue without popping
 
ErrorCode PushBatch (const Data *data, size_t size)
 批量推入数据 / Pushes multiple elements into the queue
 
ErrorCode PopBatch (Data *data, size_t size)
 批量弹出数据 / Pops multiple elements from the queue
 
ErrorCode PeekBatch (Data *data, size_t size)
 批量查看队列中的数据(不移除) / Peeks multiple elements from the queue without removing them
 
void Reset ()
 重置队列 / Resets the queue
 
size_t Size () const
 获取当前队列中的元素数量 / Returns the number of elements currently in the queue
 
size_t EmptySize ()
 计算队列剩余可用空间 / Calculates the remaining available space in the queue
 

Private Member Functions

constexpr size_t AlignUp (size_t size, size_t align)
 
uint32_t Increment (uint32_t index) const
 

Private Attributes

std::atomic< uint32_t > head_
 
std::atomic< uint32_t > tail_
 
const size_t LENGTH
 
Data * queue_handle_
 

Detailed Description

template<typename Data>
class LibXR::LockFreeQueue< Data >

无锁队列实现 / Lock-free queue implementation

该类实现了单生产者多消费者无锁队列(SPMC Lock-Free Queue),支持多线程环境下的高效入队和出队操作, 适用于需要高并发性能的场景,如实时系统和多线程数据处理。 This class implements a single-producer, multiple-consumer lock-free queue (SPMC Lock-Free Queue) that supports high-efficiency enqueue and dequeue operations in a multi-threaded environment. It is suitable for scenarios requiring high concurrency, such as real-time systems and multi-threaded data processing.

Template Parameters
Data队列存储的数据类型 / The type of data stored in the queue.

Definition at line 25 of file lockfree_queue.hpp.

Constructor & Destructor Documentation

◆ LockFreeQueue()

template<typename Data >
LibXR::LockFreeQueue< Data >::LockFreeQueue ( size_t length)
inline

构造函数 / Constructor

Parameters
length队列的最大容量 / Maximum capacity of the queue

创建一个指定大小的无锁队列,并初始化相关变量。 Creates a lock-free queue with the specified size and initializes relevant variables.

Note
包含动态内存分配。 Contains dynamic memory allocation.

Definition at line 43 of file lockfree_queue.hpp.

44 : head_(0),
45 tail_(0),
46 LENGTH(AlignUp(length, LIBXR_ALIGN_SIZE) - 1),
47 queue_handle_(new Data[LENGTH + 1])
48 {
49 }

◆ ~LockFreeQueue()

template<typename Data >
LibXR::LockFreeQueue< Data >::~LockFreeQueue ( )
inline

析构函数 / Destructor

释放队列所占用的内存。 Releases the memory occupied by the queue.

Definition at line 57 of file lockfree_queue.hpp.

57{ delete[] queue_handle_; }

Member Function Documentation

◆ AlignUp()

template<typename Data >
size_t LibXR::LockFreeQueue< Data >::AlignUp ( size_t size,
size_t align )
inlineconstexprprivate

Definition at line 27 of file lockfree_queue.hpp.

28 {
29 return ((size + align - 1) / align) * align;
30 }

◆ EmptySize()

template<typename Data >
size_t LibXR::LockFreeQueue< Data >::EmptySize ( )
inline

计算队列剩余可用空间 / Calculates the remaining available space in the queue

Definition at line 386 of file lockfree_queue.hpp.

386{ return LENGTH - Size(); }
size_t Size() const
获取当前队列中的元素数量 / Returns the number of elements currently in the queue

◆ Increment()

template<typename Data >
uint32_t LibXR::LockFreeQueue< Data >::Increment ( uint32_t index) const
inlineprivate

Definition at line 394 of file lockfree_queue.hpp.

394{ return (index + 1) % (LENGTH + 1); }

◆ operator[]()

template<typename Data >
Data * LibXR::LockFreeQueue< Data >::operator[] ( uint32_t index)
inline

获取指定索引的数据指针 / Retrieves the data pointer at a specified index

Parameters
index数据索引 / Data index
Returns
指向该索引数据的指针 / Pointer to the data at the given index

Definition at line 64 of file lockfree_queue.hpp.

64{ return &queue_handle_[static_cast<size_t>(index)]; }

◆ Peek()

template<typename Data >
ErrorCode LibXR::LockFreeQueue< Data >::Peek ( Data & item)
inline

获取队列头部数据但不弹出 / Retrieves the front data of the queue without popping

Parameters
item用于存储获取的数据 / Variable to store the retrieved data
Returns
操作结果,成功返回 ErrorCode::OK,队列为空返回 ErrorCode::EMPTY / Operation result: returns ErrorCode::OK on success, ErrorCode::EMPTY if the queue is empty

Definition at line 194 of file lockfree_queue.hpp.

195 {
196 while (true)
197 {
198 auto current_head = head_.load(std::memory_order_relaxed);
199 if (current_head == tail_.load(std::memory_order_acquire))
200 {
201 return ErrorCode::EMPTY;
202 }
203
204 item = queue_handle_[current_head];
205
206 if (head_.load(std::memory_order_acquire) == current_head)
207 {
208 return ErrorCode::OK;
209 }
210 }
211 }

◆ PeekBatch()

template<typename Data >
ErrorCode LibXR::LockFreeQueue< Data >::PeekBatch ( Data * data,
size_t size )
inline

批量查看队列中的数据(不移除) / Peeks multiple elements from the queue without removing them

Parameters
data数据存储数组指针 / Pointer to the array to store peeked data
size要查看的元素个数 / Number of elements to peek
Returns
操作结果,成功返回 ErrorCode::OK,队列中数据不足返回 ErrorCode::EMPTY / Operation result: returns ErrorCode::OK on success, ErrorCode::EMPTY if not enough data is available

Definition at line 313 of file lockfree_queue.hpp.

314 {
315 if (size == 0)
316 {
317 return ErrorCode::OK;
318 }
319
320 const size_t CAPACITY = LENGTH + 1;
321
322 while (true)
323 {
324 auto current_head = head_.load(std::memory_order_relaxed);
325 auto current_tail = tail_.load(std::memory_order_acquire);
326
327 size_t available = (current_tail >= current_head)
328 ? (current_tail - current_head)
329 : (CAPACITY - current_head + current_tail);
330
331 if (available < size)
332 {
333 return ErrorCode::EMPTY;
334 }
335
336 if (data != nullptr)
337 {
338 size_t first_chunk = LibXR::min(size, CAPACITY - current_head);
340 reinterpret_cast<void *>(data),
341 reinterpret_cast<const void *>(queue_handle_ + current_head),
342 first_chunk * sizeof(Data));
343
344 if (size > first_chunk)
345 {
346 LibXR::Memory::FastCopy(reinterpret_cast<void *>(data + first_chunk),
347 reinterpret_cast<const void *>(queue_handle_),
348 (size - first_chunk) * sizeof(Data));
349 }
350 }
351
352 if (head_.load(std::memory_order_acquire) == current_head)
353 {
354 return ErrorCode::OK;
355 }
356 }
357 }
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
Definition libxr_mem.cpp:3
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ Pop() [1/3]

template<typename Data >
ErrorCode LibXR::LockFreeQueue< Data >::Pop ( )
inline

从队列中弹出数据(不返回数据) / Pops data from the queue (without returning data)

Returns
操作结果,成功返回 ErrorCode::OK,队列为空返回 ErrorCode::EMPTY / Operation result: returns ErrorCode::OK on success, ErrorCode::EMPTY if the queue is empty

Definition at line 166 of file lockfree_queue.hpp.

167 {
168 auto current_head = head_.load(std::memory_order_relaxed);
169
170 while (true)
171 {
172 if (current_head == tail_.load(std::memory_order_acquire))
173 {
174 return ErrorCode::EMPTY;
175 }
176
177 if (head_.compare_exchange_weak(current_head, Increment(current_head),
178 std::memory_order_acq_rel,
179 std::memory_order_relaxed))
180 {
181 return ErrorCode::OK;
182 }
183 }
184 }

◆ Pop() [2/3]

template<typename Data >
ErrorCode LibXR::LockFreeQueue< Data >::Pop ( Data & item)
inline

从队列中移除头部元素,并获取该元素的数据 (Remove the front element from the queue and retrieve its data).

This function atomically updates the head_ index using compare_exchange_weak to ensure thread safety. If the queue is empty, it returns ErrorCode::EMPTY. Otherwise, it updates the head pointer, retrieves the element, and returns ErrorCode::OK. 该函数使用 compare_exchange_weak 原子地更新 head_ 索引,以确保线程安全。 如果队列为空,则返回 ErrorCode::EMPTY,否则更新头指针,获取元素数据,并返回 ErrorCode::OK

Parameters
item用于存储弹出元素的引用 (Reference to store the popped element).
Returns
操作结果 (Operation result):
  • ErrorCode::OK 表示成功移除并获取元素 (ErrorCode::OK if an element was successfully removed and retrieved).
  • ErrorCode::EMPTY 表示队列为空 (ErrorCode::EMPTY if the queue is empty).

Definition at line 137 of file lockfree_queue.hpp.

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 item = queue_handle_[current_head];
149
150 if (head_.compare_exchange_weak(current_head, Increment(current_head),
151 std::memory_order_acq_rel,
152 std::memory_order_relaxed))
153 {
154 return ErrorCode::OK;
155 }
156 }
157 }

◆ Pop() [3/3]

template<typename Data >
template<typename ElementData = Data>
ErrorCode LibXR::LockFreeQueue< Data >::Pop ( ElementData & item)
inline

从队列中弹出数据 / Pops data from the queue

Parameters
item用于存储弹出数据的变量 / Variable to store the popped data
Returns
操作结果,成功返回 ErrorCode::OK,队列为空返回 ErrorCode::EMPTY / Operation result: returns ErrorCode::OK on success, ErrorCode::EMPTY if the queue is empty

Definition at line 97 of file lockfree_queue.hpp.

98 {
99 auto current_head = head_.load(std::memory_order_relaxed);
100
101 while (true)
102 {
103 if (current_head == tail_.load(std::memory_order_acquire))
104 {
105 return ErrorCode::EMPTY;
106 }
107
108 item = queue_handle_[current_head];
109
110 if (head_.compare_exchange_weak(current_head, Increment(current_head),
111 std::memory_order_acq_rel,
112 std::memory_order_relaxed))
113 {
114 return ErrorCode::OK;
115 }
116 }
117 }

◆ PopBatch()

template<typename Data >
ErrorCode LibXR::LockFreeQueue< Data >::PopBatch ( Data * data,
size_t size )
inline

批量弹出数据 / Pops multiple elements from the queue

Parameters
data数据存储数组指针 / Pointer to the array to store popped data
size需要弹出的数据个数 / Number of elements to pop
Returns
操作结果,成功返回 ErrorCode::OK,队列为空返回 ErrorCode::EMPTY / Operation result: returns ErrorCode::OK on success, ErrorCode::EMPTY if the queue is empty

Definition at line 260 of file lockfree_queue.hpp.

261 {
262 size_t capacity = LENGTH + 1;
263
264 while (true)
265 {
266 auto current_head = head_.load(std::memory_order_relaxed);
267 auto current_tail = tail_.load(std::memory_order_acquire);
268
269 size_t available = (current_tail >= current_head)
270 ? (current_tail - current_head)
271 : (capacity - current_head + current_tail);
272
273 if (available < size)
274 {
275 return ErrorCode::EMPTY;
276 }
277
278 if (data != nullptr)
279 {
280 size_t first_chunk = LibXR::min(size, capacity - current_head);
282 reinterpret_cast<void *>(data),
283 reinterpret_cast<const void *>(queue_handle_ + current_head),
284 first_chunk * sizeof(Data));
285
286 if (size > first_chunk)
287 {
288 LibXR::Memory::FastCopy(reinterpret_cast<void *>(data + first_chunk),
289 reinterpret_cast<const void *>(queue_handle_),
290 (size - first_chunk) * sizeof(Data));
291 }
292 }
293
294 size_t new_head = (current_head + size) % capacity;
295
296 if (head_.compare_exchange_weak(current_head, new_head, std::memory_order_acq_rel,
297 std::memory_order_relaxed))
298 {
299 return ErrorCode::OK;
300 }
301 }
302 }

◆ Push()

template<typename Data >
template<typename ElementData = Data>
ErrorCode LibXR::LockFreeQueue< Data >::Push ( ElementData && item)
inline

向队列中推入数据 / Pushes data into the queue

Parameters
item要插入的元素 / Element to be inserted
Returns
操作结果,成功返回 ErrorCode::OK,队列满返回 ErrorCode::FULL / Operation result: returns ErrorCode::OK on success, ErrorCode::FULL if the queue is full

Definition at line 74 of file lockfree_queue.hpp.

75 {
76 const auto CURRENT_TAIL = tail_.load(std::memory_order_relaxed);
77 const auto NEXT_TAIL = Increment(CURRENT_TAIL);
78
79 if (NEXT_TAIL == head_.load(std::memory_order_acquire))
80 {
81 return ErrorCode::FULL;
82 }
83
84 queue_handle_[CURRENT_TAIL] = std::forward<ElementData>(item);
85 tail_.store(NEXT_TAIL, std::memory_order_release);
86 return ErrorCode::OK;
87 }

◆ PushBatch()

template<typename Data >
ErrorCode LibXR::LockFreeQueue< Data >::PushBatch ( const Data * data,
size_t size )
inline

批量推入数据 / Pushes multiple elements into the queue

Parameters
data数据数组指针 / Pointer to the data array
size数据个数 / Number of elements
Returns
操作结果,成功返回 ErrorCode::OK,队列满返回 ErrorCode::FULL / Operation result: returns ErrorCode::OK on success, ErrorCode::FULL if the queue is full

Definition at line 221 of file lockfree_queue.hpp.

222 {
223 auto current_tail = tail_.load(std::memory_order_relaxed);
224 auto current_head = head_.load(std::memory_order_acquire);
225
226 size_t capacity = LENGTH + 1;
227 size_t free_space = (current_tail >= current_head)
228 ? (capacity - (current_tail - current_head) - 1)
229 : (current_head - current_tail - 1);
230
231 if (free_space < size)
232 {
233 return ErrorCode::FULL;
234 }
235
236 size_t first_chunk = LibXR::min(size, capacity - current_tail);
237 LibXR::Memory::FastCopy(reinterpret_cast<void *>(queue_handle_ + current_tail),
238 reinterpret_cast<const void *>(data),
239 first_chunk * sizeof(Data));
240
241 if (size > first_chunk)
242 {
243 LibXR::Memory::FastCopy(reinterpret_cast<void *>(queue_handle_),
244 reinterpret_cast<const void *>(data + first_chunk),
245 (size - first_chunk) * sizeof(Data));
246 }
247
248 tail_.store((current_tail + size) % capacity, std::memory_order_release);
249 return ErrorCode::OK;
250 }

◆ Reset()

template<typename Data >
void LibXR::LockFreeQueue< Data >::Reset ( )
inline

重置队列 / Resets the queue

该方法清空队列并将头尾指针重置为 0。 This method clears the queue and resets the head and tail pointers to 0.

Definition at line 365 of file lockfree_queue.hpp.

366 {
367 head_.store(0, std::memory_order_relaxed);
368 tail_.store(0, std::memory_order_relaxed);
369 }

◆ Size()

template<typename Data >
size_t LibXR::LockFreeQueue< Data >::Size ( ) const
inline

获取当前队列中的元素数量 / Returns the number of elements currently in the queue

Definition at line 375 of file lockfree_queue.hpp.

376 {
377 const auto CURRENT_HEAD = head_.load(std::memory_order_acquire);
378 const auto CURRENT_TAIL = tail_.load(std::memory_order_acquire);
379 return (CURRENT_TAIL >= CURRENT_HEAD) ? (CURRENT_TAIL - CURRENT_HEAD)
380 : ((LENGTH + 1) - CURRENT_HEAD + CURRENT_TAIL);
381 }

Field Documentation

◆ head_

template<typename Data >
std::atomic<uint32_t> LibXR::LockFreeQueue< Data >::head_
private

Definition at line 389 of file lockfree_queue.hpp.

◆ LENGTH

template<typename Data >
const size_t LibXR::LockFreeQueue< Data >::LENGTH
private

Definition at line 391 of file lockfree_queue.hpp.

◆ queue_handle_

template<typename Data >
Data* LibXR::LockFreeQueue< Data >::queue_handle_
private

Definition at line 392 of file lockfree_queue.hpp.

◆ tail_

template<typename Data >
std::atomic<uint32_t> LibXR::LockFreeQueue< Data >::tail_
private

Definition at line 390 of file lockfree_queue.hpp.


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