6#include "libxr_def.hpp"
24template <
typename Data>
41 union alignas(LibXR::CACHE_LINE_SIZE)
Slot
70 for (uint32_t index = 0; index <
SLOT_COUNT; index++)
91 uint32_t start_index = 0;
92 return Put(data, start_index);
107 for (uint32_t index = start_index; index <
SLOT_COUNT; index++)
109 auto expected =
slots_[index].slot.
state.load(std::memory_order_relaxed);
113 std::memory_order_release,
114 std::memory_order_relaxed))
137 auto expected =
slots_[index].slot.
state.load(std::memory_order_relaxed);
141 std::memory_order_release,
142 std::memory_order_relaxed))
162 uint32_t start_index = 0;
164 return Get(data, start_index);
179 for (uint32_t index = start_index; index <
SLOT_COUNT; index++)
181 auto expected =
slots_[index].slot.
state.load(std::memory_order_acquire);
185 std::memory_order_acquire,
186 std::memory_order_relaxed))
216 auto expected =
slots_[index].slot.
state.load(std::memory_order_acquire);
220 std::memory_order_acquire,
221 std::memory_order_relaxed))
241 auto expected =
slots_[index].slot.
state.load(std::memory_order_relaxed);
245 std::memory_order_release,
246 std::memory_order_relaxed))
258 [[nodiscard]]
size_t Size()
const
261 for (uint32_t index = 0; index <
SLOT_COUNT; index++)
278 for (uint32_t index = 0; index <
SLOT_COUNT; index++)
280 auto state =
slots_[index].slot.
state.load(std::memory_order_relaxed);
297 Slot& operator[](uint32_t index)
无锁无序槽池 / Lock-free, unordered slot pool
size_t EmptySize()
查询当前池可用槽数量 / Query the number of writable slots in the pool
ErrorCode GetFromSlot(Data &data, uint32_t index)
从指定槽位开始,取出一个元素 / Retrieve an element from the pool
Slot * slots_
槽数组指针 / Array of slots
~LockFreePool()
析构,释放槽池内存 / Destructor, releasing pool memory
ErrorCode PutToSlot(const Data &data, uint32_t index)
向指定槽放入一个元素 / Put an element into a specific slot
ErrorCode Get(Data &data, uint32_t &start_index)
从指定槽位开始,取出一个元素 / Retrieve an element from the pool
SlotState
槽状态 / Slot state
@ 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.
LockFreePool(uint32_t slot_count)
构造对象池 / Constructor for the pool
ErrorCode RecycleSlot(uint32_t index)
回收指定槽位 / Recycle a slot
ErrorCode Put(const Data &data)
向池中放入一个元素 / Put an element into the pool
const uint32_t SLOT_COUNT
槽总数 / Number of slots
ErrorCode Get(Data &data)
从池中取出一个元素 / Retrieve an element from the pool
ErrorCode Put(const Data &data, uint32_t &start_index)
向池中放入一个元素,返回起始槽索引 / Put an element into the pool and return the starting slot index
size_t Size() const
查询池中可取元素数量 / Query the number of available elements in the pool
uint32_t SlotCount() const
获取槽总数 / Get the total number of slots in the pool
@ OK
操作成功 | Operation successful
constexpr size_t CACHE_LINE_SIZE
缓存行大小 / Cache line size
单个槽结构体 / Individual slot structure (cache line aligned)
Data data
槽内数据 / Stored data
std::atomic< SlotState > state
当前槽状态 / Current state
uint8_t pad[LibXR::CACHE_LINE_SIZE]
缓存行填充 / Cache line padding