|
libxr
1.0
Want to be the best embedded framework
|
无锁无序槽池 / Lock-free, unordered slot pool More...
#include <lockfree_pool.hpp>
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 | |
| Slot & | operator[] (uint32_t index) |
Private Attributes | |
| const uint32_t | SLOT_COUNT |
| 槽总数 / Number of slots | |
| Slot * | slots_ |
| 槽数组指针 / Array of slots | |
无锁无序槽池 / 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.
| Data | 槽中存储的数据类型 / Type of data stored in each slot. |
Definition at line 25 of file lockfree_pool.hpp.
|
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.
|
inline |
构造对象池 / Constructor for the pool
| slot_count | 槽数量 / Number of slots in the pool |
Definition at line 56 of file lockfree_pool.hpp.
|
inline |
|
inlinenodiscard |
查询当前池可用槽数量 / Query the number of writable slots in the pool
Definition at line 265 of file lockfree_pool.hpp.
|
inline |
从池中取出一个元素 / Retrieve an element from the pool
| [out] | data | 获取到的数据 / Variable to store the retrieved data |
ErrorCode::OK:成功取出 / Successfully retrievedErrorCode::EMPTY:池空,无可取元素 / Pool empty, no available element Definition at line 150 of file lockfree_pool.hpp.
|
inline |
从指定槽位开始,取出一个元素 / Retrieve an element from the pool
| [out] | data | 获取到的数据 / Variable to store the retrieved data |
| [in,out] | start_index | 本次起始槽,返回本次实际取出槽位置 / Starting slot index, returned with the actual slot index |
ErrorCode::OK:成功取出 / Successfully retrievedErrorCode::EMPTY:池空,无可取元素 / Pool empty, no available element Definition at line 167 of file lockfree_pool.hpp.
|
inline |
从指定槽位开始,取出一个元素 / Retrieve an element from the pool
| data | 获取到的数据 / Variable to store the retrieved data |
| index | 槽索引 / Slot index |
ErrorCode::OK:成功取出 / Successfully retrievedErrorCode::EMPTY:池空,无可取元素 / Pool empty, no available element Definition at line 204 of file lockfree_pool.hpp.
|
inlineprotected |
Definition at line 287 of file lockfree_pool.hpp.
|
inline |
向池中放入一个元素 / Put an element into the pool
| data | 要存储的数据 / Data to store |
ErrorCode::OK:成功放入 / Successfully putErrorCode::FULL:池满,无法放入 / Pool full, cannot put Definition at line 79 of file lockfree_pool.hpp.
|
inline |
向池中放入一个元素,返回起始槽索引 / Put an element into the pool and return the starting slot index
| data | 要存储的数据 / Data to store |
| start_index | 起始槽索引 / Starting slot index |
ErrorCode::OK:成功放入 / Successfully putErrorCode::FULL:池满,无法放入 / Pool full, cannot put Definition at line 95 of file lockfree_pool.hpp.
|
inline |
向指定槽放入一个元素 / Put an element into a specific slot
| data | 要存储的数据 / Data to store |
| index | 槽索引 / Slot index |
ErrorCode::OK:成功放入 / Successfully putErrorCode::FULL:池满,无法放入 / Pool full, cannot put Definition at line 125 of file lockfree_pool.hpp.
|
inline |
回收指定槽位 / Recycle a slot
| index | 槽索引 / Slot index |
ErrorCode::OK:成功回收 / Successfully recycledErrorCode::EMPTY:池空,无可回收元素 / Pool empty, no available element Definition at line 229 of file lockfree_pool.hpp.
|
inlinenodiscard |
查询池中可取元素数量 / Query the number of available elements in the pool
Definition at line 248 of file lockfree_pool.hpp.
|
inline |
获取槽总数 / Get the total number of slots in the pool
Definition at line 284 of file lockfree_pool.hpp.
|
private |
槽总数 / Number of slots
Definition at line 297 of file lockfree_pool.hpp.
|
private |
槽数组指针 / Array of slots
Definition at line 298 of file lockfree_pool.hpp.