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

无锁无序槽池 / Lock-free, unordered slot pool More...

#include <lockfree_pool.hpp>

Collaboration diagram for LibXR::LockFreePool< Data >:
[legend]

Data Structures

union  Slot
 单个槽结构体 / Individual slot structure (cache line aligned) More...
 

Public Types

enum class  SlotState : uint32_t { FREE = 0 , BUSY = 1 , READY = 2 , RECYCLE = UINT32_MAX }
 槽状态 / Slot state More...
 

Public Member Functions

 LockFreePool (uint32_t slot_count)
 构造对象池 / Constructor for the pool
 
 ~LockFreePool ()
 析构,释放槽池内存 / Destructor, releasing pool memory
 
ErrorCode Put (const Data &data)
 向池中放入一个元素 / Put an element into the pool
 
ErrorCode Put (const Data &data, uint32_t &start_index)
 向池中放入一个元素,返回起始槽索引 / Put an element into the pool and return the starting slot index
 
ErrorCode PutToSlot (const Data &data, uint32_t index)
 向指定槽放入一个元素 / Put an element into a specific slot
 
ErrorCode Get (Data &data)
 从池中取出一个元素 / Retrieve an element from the pool
 
ErrorCode Get (Data &data, uint32_t &start_index)
 从指定槽位开始,取出一个元素 / Retrieve an element from the pool
 
ErrorCode GetFromSlot (Data &data, uint32_t index)
 从指定槽位开始,取出一个元素 / Retrieve an element from the pool
 
ErrorCode RecycleSlot (uint32_t index)
 回收指定槽位 / Recycle a slot
 
size_t Size () const
 查询池中可取元素数量 / Query the number of available elements in the pool
 
size_t EmptySize ()
 查询当前池可用槽数量 / Query the number of writable slots in the pool
 

Private Attributes

const uint32_t SLOT_COUNT
 槽总数 / Number of slots
 
Slotslots_
 槽数组指针 / Array of slots
 

Detailed Description

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

无锁无序槽池 / Lock-free, unordered slot pool

该类实现了一个固定容量的无锁无序对象池,允许多个线程安全地并发存入(Put)和取出(Get)元素。每个槽独立管理,无严格顺序,适用于高性能、多线程或中断场景的数据缓存、对象重用等需求。

This class implements a lock-free, unordered slot/object pool with a fixed size. Multiple threads can safely Put (store) and Get (retrieve) elements concurrently. Each slot is managed independently, making it ideal for high-performance, multi-thread, or interrupt-safe buffering and object reuse.

Template Parameters
Data槽中存储的数据类型 / Type of data stored in each slot.

Definition at line 25 of file lockfree_pool.hpp.

Member Enumeration Documentation

◆ SlotState

template<typename Data >
enum class LibXR::LockFreePool::SlotState : uint32_t
strong

槽状态 / Slot state

Enumerator
FREE 

空闲,可写 / Slot is free and can be written.

BUSY 

正在写入 / Slot is being written.

READY 

写入完成,可读 / Slot contains valid data, ready to read.

RECYCLE 

已读待回收 / Slot was read, waiting for next write.

Definition at line 30 of file lockfree_pool.hpp.

31 {
32 FREE = 0,
33 BUSY = 1,
34 READY = 2,
35 RECYCLE = UINT32_MAX
36
37 };
@ RECYCLE
已读待回收 / Slot was read, waiting for next write.
@ READY
写入完成,可读 / Slot contains valid data, ready to read.
@ BUSY
正在写入 / Slot is being written.
@ FREE
空闲,可写 / Slot is free and can be written.

Constructor & Destructor Documentation

◆ LockFreePool()

template<typename Data >
LibXR::LockFreePool< Data >::LockFreePool ( uint32_t slot_count)
inline

构造对象池 / Constructor for the pool

Parameters
slot_count槽数量 / Number of slots in the pool

Definition at line 56 of file lockfree_pool.hpp.

57 : SLOT_COUNT(slot_count),
58 slots_(new(std::align_val_t(LIBXR_CACHE_LINE_SIZE)) Slot[slot_count])
59 {
60 for (uint32_t index = 0; index < SLOT_COUNT; index++)
61 {
62 slots_[index].slot.state.store(SlotState::FREE, std::memory_order_relaxed);
63 }
64 }
Slot * slots_
槽数组指针 / Array of slots
const uint32_t SLOT_COUNT
槽总数 / Number of slots
std::atomic< SlotState > state
当前槽状态 / Current state

◆ ~LockFreePool()

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

析构,释放槽池内存 / Destructor, releasing pool memory

Definition at line 69 of file lockfree_pool.hpp.

69{ delete[] slots_; }

Member Function Documentation

◆ EmptySize()

template<typename Data >
size_t LibXR::LockFreePool< Data >::EmptySize ( )
inlinenodiscard

查询当前池可用槽数量 / Query the number of writable slots in the pool

Returns
可写入的空槽数 / Number of writable slots in pool

Definition at line 265 of file lockfree_pool.hpp.

266 {
267 uint32_t size = 0;
268 for (uint32_t index = 0; index < SLOT_COUNT; index++)
269 {
270 auto state = slots_[index].slot.state.load(std::memory_order_relaxed);
271 if (state == SlotState::FREE || state == SlotState::RECYCLE)
272 {
273 size++;
274 }
275 }
276 return size;
277 }

◆ Get() [1/2]

template<typename Data >
ErrorCode LibXR::LockFreePool< Data >::Get ( Data & data)
inline

从池中取出一个元素 / Retrieve an element from the pool

Parameters
[out]data获取到的数据 / Variable to store the retrieved data
Returns
操作结果 / Operation result:
  • ErrorCode::OK:成功取出 / Successfully retrieved
  • ErrorCode::EMPTY:池空,无可取元素 / Pool empty, no available element

Definition at line 150 of file lockfree_pool.hpp.

151 {
152 uint32_t start_index = 0;
153
154 return Get(data, start_index);
155 }
ErrorCode Get(Data &data)
从池中取出一个元素 / Retrieve an element from the pool

◆ Get() [2/2]

template<typename Data >
ErrorCode LibXR::LockFreePool< Data >::Get ( Data & data,
uint32_t & start_index )
inline

从指定槽位开始,取出一个元素 / Retrieve an element from the pool

Parameters
[out]data获取到的数据 / Variable to store the retrieved data
[in,out]start_index本次起始槽,返回本次实际取出槽位置 / Starting slot index, returned with the actual slot index
Returns
操作结果 / Operation result:
  • ErrorCode::OK:成功取出 / Successfully retrieved
  • ErrorCode::EMPTY:池空,无可取元素 / Pool empty, no available element

Definition at line 167 of file lockfree_pool.hpp.

168 {
169 for (uint32_t index = start_index; index < SLOT_COUNT; index++)
170 {
171 auto expected = slots_[index].slot.state.load(std::memory_order_acquire);
172 if (expected == SlotState::READY)
173 {
174 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::BUSY,
175 std::memory_order_acquire,
176 std::memory_order_relaxed))
177 {
178 data = slots_[index].slot.data;
179 slots_[index].slot.state.store(SlotState::RECYCLE, std::memory_order_release);
180 start_index = index;
181 return ErrorCode::OK;
182 }
183 }
184 if (expected == SlotState::FREE)
185 {
186 start_index = 0;
187 return ErrorCode::EMPTY;
188 }
189 }
190
191 start_index = 0;
192 return ErrorCode::EMPTY;
193 }
Data data
槽内数据 / Stored data

◆ GetFromSlot()

template<typename Data >
ErrorCode LibXR::LockFreePool< Data >::GetFromSlot ( Data & data,
uint32_t index )
inline

从指定槽位开始,取出一个元素 / Retrieve an element from the pool

Parameters
data获取到的数据 / Variable to store the retrieved data
index槽索引 / Slot index
Returns
ErrorCode 操作结果 / Operation result:
  • ErrorCode::OK:成功取出 / Successfully retrieved
  • ErrorCode::EMPTY:池空,无可取元素 / Pool empty, no available element

Definition at line 204 of file lockfree_pool.hpp.

205 {
206 auto expected = slots_[index].slot.state.load(std::memory_order_acquire);
207 if (expected == SlotState::READY)
208 {
209 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::BUSY,
210 std::memory_order_acquire,
211 std::memory_order_relaxed))
212 {
213 data = slots_[index].slot.data;
214 slots_[index].slot.state.store(SlotState::RECYCLE, std::memory_order_release);
215 return ErrorCode::OK;
216 }
217 }
218 return ErrorCode::EMPTY;
219 }

◆ Put() [1/2]

template<typename Data >
ErrorCode LibXR::LockFreePool< Data >::Put ( const Data & data)
inline

向池中放入一个元素 / Put an element into the pool

Parameters
data要存储的数据 / Data to store
Returns
操作结果 / Operation result:
  • ErrorCode::OK:成功放入 / Successfully put
  • ErrorCode::FULL:池满,无法放入 / Pool full, cannot put

Definition at line 79 of file lockfree_pool.hpp.

80 {
81 uint32_t start_index = 0;
82 return Put(data, start_index);
83 }
ErrorCode Put(const Data &data)
向池中放入一个元素 / Put an element into the pool

◆ Put() [2/2]

template<typename Data >
ErrorCode LibXR::LockFreePool< Data >::Put ( const Data & data,
uint32_t & start_index )
inline

向池中放入一个元素,返回起始槽索引 / Put an element into the pool and return the starting slot index

Parameters
data要存储的数据 / Data to store
start_index起始槽索引 / Starting slot index
Returns
ErrorCode 操作结果 / Operation result:
  • ErrorCode::OK:成功放入 / Successfully put
  • ErrorCode::FULL:池满,无法放入 / Pool full, cannot put

Definition at line 95 of file lockfree_pool.hpp.

96 {
97 for (uint32_t index = start_index; index < SLOT_COUNT; index++)
98 {
99 auto expected = slots_[index].slot.state.load(std::memory_order_relaxed);
100 if (expected == SlotState::FREE || expected == SlotState::RECYCLE)
101 {
102 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::BUSY,
103 std::memory_order_release,
104 std::memory_order_relaxed))
105 {
106 slots_[index].slot.data = data;
107 slots_[index].slot.state.store(SlotState::READY, std::memory_order_release);
108 start_index = index;
109 return ErrorCode::OK;
110 }
111 }
112 }
113 return ErrorCode::FULL;
114 }

◆ PutToSlot()

template<typename Data >
ErrorCode LibXR::LockFreePool< Data >::PutToSlot ( const Data & data,
uint32_t index )
inline

向指定槽放入一个元素 / Put an element into a specific slot

Parameters
data要存储的数据 / Data to store
index槽索引 / Slot index
Returns
ErrorCode 操作结果 / Operation result:
  • ErrorCode::OK:成功放入 / Successfully put
  • ErrorCode::FULL:池满,无法放入 / Pool full, cannot put

Definition at line 125 of file lockfree_pool.hpp.

126 {
127 auto expected = slots_[index].slot.state.load(std::memory_order_relaxed);
128 if (expected == SlotState::FREE || expected == SlotState::RECYCLE)
129 {
130 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::BUSY,
131 std::memory_order_release,
132 std::memory_order_relaxed))
133 {
134 slots_[index].slot.data = data;
135 slots_[index].slot.state.store(SlotState::READY, std::memory_order_release);
136 return ErrorCode::OK;
137 }
138 }
139 return ErrorCode::FULL;
140 }

◆ RecycleSlot()

template<typename Data >
ErrorCode LibXR::LockFreePool< Data >::RecycleSlot ( uint32_t index)
inline

回收指定槽位 / Recycle a slot

Parameters
index槽索引 / Slot index
Returns
ErrorCode 操作结果 / Operation result:
  • ErrorCode::OK:成功回收 / Successfully recycled
  • ErrorCode::EMPTY:池空,无可回收元素 / Pool empty, no available element

Definition at line 229 of file lockfree_pool.hpp.

230 {
231 auto expected = slots_[index].slot.state.load(std::memory_order_relaxed);
232 if (expected == SlotState::READY)
233 {
234 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::RECYCLE,
235 std::memory_order_release,
236 std::memory_order_relaxed))
237 {
238 return ErrorCode::OK;
239 }
240 }
241 return ErrorCode::EMPTY;
242 }

◆ Size()

template<typename Data >
size_t LibXR::LockFreePool< Data >::Size ( ) const
inlinenodiscard

查询池中可取元素数量 / Query the number of available elements in the pool

Returns
当前池中READY元素个数 / Number of ready elements in pool

Definition at line 248 of file lockfree_pool.hpp.

249 {
250 uint32_t size = 0;
251 for (uint32_t index = 0; index < SLOT_COUNT; index++)
252 {
253 if (slots_[index].slot.state.load(std::memory_order_relaxed) == SlotState::READY)
254 {
255 size++;
256 }
257 }
258 return size;
259 }

Field Documentation

◆ SLOT_COUNT

template<typename Data >
const uint32_t LibXR::LockFreePool< Data >::SLOT_COUNT
private

槽总数 / Number of slots

Definition at line 280 of file lockfree_pool.hpp.

◆ slots_

template<typename Data >
Slot* LibXR::LockFreePool< Data >::slots_
private

槽数组指针 / Array of slots

Definition at line 281 of file lockfree_pool.hpp.


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