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
 
uint32_t SlotCount () const
 获取槽总数 / Get the total number of slots in the pool
 

Protected Member Functions

Slotoperator[] (uint32_t index)
 

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
Note
包含动态内存分配。 Contains dynamic memory allocation.

Definition at line 66 of file lockfree_pool.hpp.

67 : SLOT_COUNT(slot_count),
68 slots_(new (std::align_val_t(LIBXR_CACHE_LINE_SIZE)) Slot[slot_count])
69 {
70 for (uint32_t index = 0; index < SLOT_COUNT; index++)
71 {
72 slots_[index].slot.state.store(SlotState::FREE, std::memory_order_relaxed);
73 }
74 }
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 79 of file lockfree_pool.hpp.

79{ 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 275 of file lockfree_pool.hpp.

276 {
277 uint32_t size = 0;
278 for (uint32_t index = 0; index < SLOT_COUNT; index++)
279 {
280 auto state = slots_[index].slot.state.load(std::memory_order_relaxed);
281 if (state == SlotState::FREE || state == SlotState::RECYCLE)
282 {
283 size++;
284 }
285 }
286 return size;
287 }

◆ 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:

Definition at line 160 of file lockfree_pool.hpp.

161 {
162 uint32_t start_index = 0;
163
164 return Get(data, start_index);
165 }
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:

Definition at line 177 of file lockfree_pool.hpp.

178 {
179 for (uint32_t index = start_index; index < SLOT_COUNT; index++)
180 {
181 auto expected = slots_[index].slot.state.load(std::memory_order_acquire);
182 if (expected == SlotState::READY)
183 {
184 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::BUSY,
185 std::memory_order_acquire,
186 std::memory_order_relaxed))
187 {
188 data = slots_[index].slot.data;
189 slots_[index].slot.state.store(SlotState::RECYCLE, std::memory_order_release);
190 start_index = index;
191 return ErrorCode::OK;
192 }
193 }
194 if (expected == SlotState::FREE)
195 {
196 start_index = 0;
197 return ErrorCode::EMPTY;
198 }
199 }
200
201 start_index = 0;
202 return ErrorCode::EMPTY;
203 }
@ EMPTY
为空 | Empty
@ OK
操作成功 | Operation successful
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:

Definition at line 214 of file lockfree_pool.hpp.

215 {
216 auto expected = slots_[index].slot.state.load(std::memory_order_acquire);
217 if (expected == SlotState::READY)
218 {
219 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::BUSY,
220 std::memory_order_acquire,
221 std::memory_order_relaxed))
222 {
223 data = slots_[index].slot.data;
224 slots_[index].slot.state.store(SlotState::RECYCLE, std::memory_order_release);
225 return ErrorCode::OK;
226 }
227 }
228 return ErrorCode::EMPTY;
229 }

◆ operator[]()

template<typename Data >
Slot & LibXR::LockFreePool< Data >::operator[] ( uint32_t index)
inlineprotected

Definition at line 297 of file lockfree_pool.hpp.

298 {
299 if (index >= SLOT_COUNT)
300 {
301 ASSERT(false);
302 }
303 return slots_[index];
304 }

◆ 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:

Definition at line 89 of file lockfree_pool.hpp.

90 {
91 uint32_t start_index = 0;
92 return Put(data, start_index);
93 }
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:

Definition at line 105 of file lockfree_pool.hpp.

106 {
107 for (uint32_t index = start_index; index < SLOT_COUNT; index++)
108 {
109 auto expected = slots_[index].slot.state.load(std::memory_order_relaxed);
110 if (expected == SlotState::FREE || expected == SlotState::RECYCLE)
111 {
112 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::BUSY,
113 std::memory_order_release,
114 std::memory_order_relaxed))
115 {
116 slots_[index].slot.data = data;
117 slots_[index].slot.state.store(SlotState::READY, std::memory_order_release);
118 start_index = index;
119 return ErrorCode::OK;
120 }
121 }
122 }
123 return ErrorCode::FULL;
124 }
@ FULL
已满 | Full

◆ 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:

Definition at line 135 of file lockfree_pool.hpp.

136 {
137 auto expected = slots_[index].slot.state.load(std::memory_order_relaxed);
138 if (expected == SlotState::FREE || expected == SlotState::RECYCLE)
139 {
140 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::BUSY,
141 std::memory_order_release,
142 std::memory_order_relaxed))
143 {
144 slots_[index].slot.data = data;
145 slots_[index].slot.state.store(SlotState::READY, std::memory_order_release);
146 return ErrorCode::OK;
147 }
148 }
149 return ErrorCode::FULL;
150 }

◆ RecycleSlot()

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

回收指定槽位 / Recycle a slot

Parameters
index槽索引 / Slot index
Returns
ErrorCode 操作结果 / Operation result:

Definition at line 239 of file lockfree_pool.hpp.

240 {
241 auto expected = slots_[index].slot.state.load(std::memory_order_relaxed);
242 if (expected == SlotState::READY)
243 {
244 if (slots_[index].slot.state.compare_exchange_strong(expected, SlotState::RECYCLE,
245 std::memory_order_release,
246 std::memory_order_relaxed))
247 {
248 return ErrorCode::OK;
249 }
250 }
251 return ErrorCode::EMPTY;
252 }

◆ 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 258 of file lockfree_pool.hpp.

259 {
260 uint32_t size = 0;
261 for (uint32_t index = 0; index < SLOT_COUNT; index++)
262 {
263 if (slots_[index].slot.state.load(std::memory_order_relaxed) == SlotState::READY)
264 {
265 size++;
266 }
267 }
268 return size;
269 }

◆ SlotCount()

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

获取槽总数 / Get the total number of slots in the pool

Returns
uint32_t 槽总数

Definition at line 294 of file lockfree_pool.hpp.

294{ return SLOT_COUNT; }

Field Documentation

◆ SLOT_COUNT

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

槽总数 / Number of slots

Definition at line 307 of file lockfree_pool.hpp.

◆ slots_

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

槽数组指针 / Array of slots

Definition at line 308 of file lockfree_pool.hpp.


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