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

链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes. More...

#include <lockfree_list.hpp>

Collaboration diagram for LibXR::LockFreeList:
[legend]

Data Structures

class  BaseNode
 链表基础节点,所有节点都继承自该类。 Base node for the linked list, serving as a parent for all nodes. More...
 
class  Node
 数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data types. More...
 

Public Member Functions

 LockFreeList () noexcept
 默认构造函数,初始化链表头节点。 Default constructor initializing the linked list head node.
 
 ~LockFreeList ()
 析构函数,释放所有节点。 Destructor releasing all nodes.
 
void Add (BaseNode &data)
 向链表添加一个节点。 Adds a node to the linked list.
 
uint32_t Size () noexcept
 获取链表中的节点数量。 Gets the number of nodes in the linked list.
 
template<typename Data , typename Func , SizeLimitMode LimitMode = SizeLimitMode::MORE>
ErrorCode Foreach (Func func)
 遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
 

Private Attributes

BaseNode head_
 链表头节点。 The head node of the list.
 

Detailed Description

链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.

该类提供了基本的链表操作,包括添加、删除节点,以及遍历链表的功能, 具有线程安全的特性。 This class provides fundamental linked list operations, including adding, deleting nodes, and traversing the list, with thread-safety features.

Definition at line 22 of file lockfree_list.hpp.

Constructor & Destructor Documentation

◆ LockFreeList()

LibXR::LockFreeList::LockFreeList ( )
inlinenoexcept

默认构造函数,初始化链表头节点。 Default constructor initializing the linked list head node.

Definition at line 120 of file lockfree_list.hpp.

120 : head_(0)
121 {
122 head_.next_.store(&head_, std::memory_order_relaxed);
123 }
std::atomic< BaseNode * > next_
指向下一个节点的原子指针。 Atomic pointer to the next node.
BaseNode head_
链表头节点。 The head node of the list.

◆ ~LockFreeList()

LibXR::LockFreeList::~LockFreeList ( )
inline

析构函数,释放所有节点。 Destructor releasing all nodes.

Definition at line 129 of file lockfree_list.hpp.

130 {
131 for (auto pos = head_.next_.load(); pos != &head_;)
132 {
133 auto tmp = pos->next_.load();
134 pos->next_.store(nullptr);
135 pos = tmp;
136 }
137
138 head_.next_.store(nullptr);
139 }

Member Function Documentation

◆ Add()

void LibXR::LockFreeList::Add ( BaseNode & data)
inline

向链表添加一个节点。 Adds a node to the linked list.

Parameters
data要添加的 BaseNode 节点。 The BaseNode node to be added.

Definition at line 148 of file lockfree_list.hpp.

149 {
150 BaseNode* current_head = nullptr;
151 do
152 {
153 current_head = head_.next_.load(std::memory_order_acquire);
154 data.next_.store(current_head, std::memory_order_relaxed);
155 } while (!head_.next_.compare_exchange_weak(
156 current_head, &data, std::memory_order_release, std::memory_order_acquire));
157 }

◆ Foreach()

template<typename Data , typename Func , SizeLimitMode LimitMode = SizeLimitMode::MORE>
ErrorCode LibXR::LockFreeList::Foreach ( Func func)
inline

遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.

Template Parameters
Data存储的数据类型。 The type of stored data.
Func回调函数类型。 The callback function type.
LimitMode大小限制模式,默认为 MORE。 Size limit mode, default is MORE.
Parameters
func需要应用于每个节点数据的回调函数。 The callback function to be applied to each node's data.
Returns
返回 ErrorCode,指示操作是否成功。 Returns ErrorCode, indicating whether the operation was successful.

Definition at line 193 of file lockfree_list.hpp.

194 {
195 for (auto pos = head_.next_.load(std::memory_order_acquire); pos != &head_;
196 pos = pos->next_.load(std::memory_order_relaxed))
197 {
198 Assert::SizeLimitCheck<LimitMode>(sizeof(Data), pos->size_);
199 if (auto res = func(static_cast<Node<Data>*>(pos)->data_); res != ErrorCode::OK)
200 {
201 return res;
202 }
203 }
204 return ErrorCode::OK;
205 }
static void SizeLimitCheck(size_t limit, size_t size)
在非调试模式下的占位大小检查函数(无实际作用)。 Dummy size limit check for non-debug builds.

◆ Size()

uint32_t LibXR::LockFreeList::Size ( )
inlinenoexcept

获取链表中的节点数量。 Gets the number of nodes in the linked list.

Returns
返回链表中节点的数量。 Returns the number of nodes in the list.

Definition at line 166 of file lockfree_list.hpp.

167 {
168 uint32_t size = 0;
169 for (auto pos = head_.next_.load(std::memory_order_acquire); pos != &head_;
170 pos = pos->next_.load(std::memory_order_relaxed))
171 {
172 ++size;
173 }
174 return size;
175 }

Field Documentation

◆ head_

BaseNode LibXR::LockFreeList::head_
private

链表头节点。 The head node of the list.

Definition at line 208 of file lockfree_list.hpp.


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