libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
lockfree_list.cpp
1#include "lockfree_list.hpp"
2
3using namespace LibXR;
4
5LockFreeList::BaseNode::BaseNode(size_t size) : size_(size) {}
6
7LockFreeList::BaseNode::~BaseNode() { ASSERT(next_ == nullptr); }
8
10{
11 head_.next_.store(&head_, std::memory_order_relaxed);
12}
13
15{
16 for (auto pos = head_.next_.load(); pos != &head_;)
17 {
18 auto tmp = pos->next_.load();
19 pos->next_.store(nullptr);
20 pos = tmp;
21 }
22
23 head_.next_.store(nullptr);
24}
25
27{
28 BaseNode* current_head = nullptr;
29 do
30 {
31 current_head = head_.next_.load(std::memory_order_acquire);
32 data.next_.store(current_head, std::memory_order_relaxed);
33 } while (!head_.next_.compare_exchange_weak(
34 current_head, &data, std::memory_order_release, std::memory_order_acquire));
35}
36
37uint32_t LockFreeList::Size() noexcept
38{
39 uint32_t size = 0;
40 for (auto pos = head_.next_.load(std::memory_order_acquire); pos != &head_;
41 pos = pos->next_.load(std::memory_order_relaxed))
42 {
43 ++size;
44 }
45 return size;
46}
链表基础节点,所有节点都继承自该类。 Base node for the linked list, serving as a parent for all nodes.
BaseNode(size_t size)
构造 BaseNode 并设置节点大小。 Constructs a BaseNode and sets its size.
~BaseNode()
析构函数,确保节点不会在列表中残留。 Destructor ensuring the node does not remain in the list.
std::atomic< BaseNode * > next_
指向下一个节点的原子指针。 Atomic pointer to the next node.
LockFreeList() noexcept
默认构造函数,初始化链表头节点。 Default constructor initializing the linked list head node.
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
BaseNode head_
链表头节点。 The head node of the list.
~LockFreeList()
析构函数,释放所有节点。 Destructor releasing all nodes.
uint32_t Size() noexcept
获取链表中的节点数量。 Gets the number of nodes in the linked list.
LibXR 命名空间
Definition ch32_gpio.hpp:9