|
libxr
Embedded C++ framework for bare-metal, RTOS and Linux
|
槽池的无类型内核:槽状态机、发布序号与扫描逻辑。 Typeless core of the slot pool: slot state machine, publish sequence and scan logic. More...
#include <slot_pool.hpp>
Public Member Functions | |
| SlotPoolBase (size_t slot_size, size_t slot_align, size_t slot_count) | |
| 构造槽池内核 / Construct the slot-pool core. | |
| ~SlotPoolBase () | |
| 析构槽池内核 / Destroy the slot-pool core. | |
| ErrorCode | Acquire (size_t &index) |
| 认领一个空闲槽(空闲 → 写入中) / Claim one free slot (free -> writing). | |
| void | Publish (size_t index) |
| 发布已写好的槽(写入中 → 就绪),并取得发布序号 / Publish a fully written slot (writing -> ready) and take a publish sequence number. | |
| ErrorCode | Take (size_t &index) |
| 认领序号最小的就绪槽(就绪 → 读取中) / Claim the oldest ready slot (ready -> reading). | |
| void | Release (size_t index) |
| 释放自己持有的槽(写入中或读取中 → 空闲) / Release an owned slot (writing or reading -> free). | |
| size_t | Size () const |
| 获取槽总数 / Get the total number of slots. | |
Protected Types | |
| enum | SlotState : uint32_t { SLOT_FREE = 0 , SLOT_WRITING = 1 , SLOT_READY = 2 , SLOT_READING = 3 } |
| 槽状态 / Slot state. More... | |
Protected Member Functions | |
| void * | SlotPtr (size_t index) |
| 获取指定槽位的存储地址 / Get the storage address of one slot. | |
| const void * | SlotPtr (size_t index) const |
| 获取指定槽位的存储地址(只读)/ Get the storage address of one slot (const). | |
Protected Attributes | |
| const size_t | slot_size_ |
| 单个槽的字节数 / Byte size of one slot. | |
| const size_t | slot_count_ |
| 槽总数 / Total number of slots. | |
| const size_t | slot_stride_ |
| 相邻槽位之间的步长 / Byte stride between slots. | |
| const size_t | alloc_align_ |
| std::atomic< uint32_t > * | states_ |
| 每槽状态字 / Per-slot atomic state word. | |
| std::atomic< uint32_t > * | seqs_ |
| 每槽发布序号 / Per-slot publish sequence. | |
| std::byte * | slots_ |
| 槽存储缓冲区 / Byte buffer storing slots. | |
| std::atomic< uint32_t > | publish_seq_ |
| std::atomic< uint32_t > | release_count_ |
Static Protected Attributes | |
| static constexpr size_t | INVALID_INDEX = static_cast<size_t>(-1) |
| 无效槽位下标 / Sentinel for "no slot chosen". | |
槽池的无类型内核:槽状态机、发布序号与扫描逻辑。 Typeless core of the slot pool: slot state machine, publish sequence and scan logic.
内核只管理每个槽的原子状态与发布序号,以及按槽位下标访问的原始存储; payload 类型语义由上层的 SlotPool<T> 负责。所有状态转换都用 CAS 完成, 同一时刻一个槽只属于一个执行者。
The core only manages the per-slot atomic state, the publish sequence and the raw per-index storage; payload type semantics live in SlotPool<T> above it. Every state transition is a CAS, so at any moment a slot belongs to exactly one execution context.
Definition at line 35 of file slot_pool.hpp.
|
protected |
槽状态 / Slot state.
Definition at line 125 of file slot_pool.hpp.
| LibXR::SlotPoolBase::SlotPoolBase | ( | size_t | slot_size, |
| size_t | slot_align, | ||
| size_t | slot_count ) |
构造槽池内核 / Construct the slot-pool core.
| slot_size | 单个槽的字节数 / Byte size of one slot |
| slot_align | 槽存储的对齐要求 / Alignment requirement of the slot storage |
| slot_count | 槽总数 / Total number of slots |
Definition at line 8 of file slot_pool.cpp.
| LibXR::SlotPoolBase::~SlotPoolBase | ( | ) |
析构槽池内核 / Destroy the slot-pool core.
Definition at line 40 of file slot_pool.cpp.
| ErrorCode LibXR::SlotPoolBase::Acquire | ( | size_t & | index | ) |
认领一个空闲槽(空闲 → 写入中) / Claim one free slot (free -> writing).
| index | 成功时写入认领的槽位下标 / Receives the claimed slot index on success |
ErrorCode::OK;没有空闲槽返回 ErrorCode::FULL Returns ErrorCode::OK on success; ErrorCode::FULL when no slot is free扫描全部槽位,跳过被其他上下文占用的槽(写入中、就绪、读取中)。 一个被打断的操作只占住它自己的槽,不会挡住其他上下文认领剩余的空闲槽。 The full slot array is scanned and slots owned by other contexts (writing, ready, reading) are skipped. An interrupted operation only occupies its own slot and never blocks other contexts from claiming the remaining free slots.
Definition at line 49 of file slot_pool.cpp.
| void LibXR::SlotPoolBase::Publish | ( | size_t | index | ) |
发布已写好的槽(写入中 → 就绪),并取得发布序号 / Publish a fully written slot (writing -> ready) and take a publish sequence number.
| index | 已通过 Acquire 认领的槽位下标 / Slot index claimed via Acquire |
发布序号在 Publish 开始时由全局计数器 fetch_add 取得;Take 在已就绪 的槽中按序号先后出队。调用方必须仍持有该槽(写入中),重复发布或发布未 认领的槽是调用方错误。 The sequence number comes from a global fetch_add counter when Publish begins; Take dequeues ready slots in sequence order. The caller must still own the slot (writing); double publishing or publishing an unclaimed slot is a caller error.
Definition at line 88 of file slot_pool.cpp.
| void LibXR::SlotPoolBase::Release | ( | size_t | index | ) |
释放自己持有的槽(写入中或读取中 → 空闲) / Release an owned slot (writing or reading -> free).
| index | 自己持有的槽位下标 / Slot index owned by the caller |
释放不会失败;放弃一个只 Acquire 未发布的槽同样是合法用法 (分配器模式)。释放其他执行者持有的槽是调用方错误。 Releasing cannot fail; abandoning a slot that was only acquired (allocator usage) is also valid. Releasing a slot owned by another context is a caller error.
Definition at line 184 of file slot_pool.cpp.
|
inlinenodiscard |
获取槽总数 / Get the total number of slots.
Definition at line 113 of file slot_pool.hpp.
|
nodiscardprotected |
获取指定槽位的存储地址 / Get the storage address of one slot.
Definition at line 202 of file slot_pool.cpp.
|
nodiscardprotected |
获取指定槽位的存储地址(只读)/ Get the storage address of one slot (const).
Definition at line 208 of file slot_pool.cpp.
| ErrorCode LibXR::SlotPoolBase::Take | ( | size_t & | index | ) |
认领序号最小的就绪槽(就绪 → 读取中) / Claim the oldest ready slot (ready -> reading).
| index | 成功时写入认领的槽位下标 / Receives the claimed slot index on success |
ErrorCode::OK;没有就绪槽返回 ErrorCode::EMPTY Returns ErrorCode::OK on success; ErrorCode::EMPTY when no slot is ready槽之间按发布序号挑选(序号比较允许回绕):同一发布者内部严格保序;一个 还没发布完的槽不会挡住其他就绪槽,因此不同发布者之间不保证先进先出。 Slots are selected by publish sequence (wraparound-safe comparison): each publisher keeps its own order, and a slot that is not published yet never hides other ready slots, so there is no FIFO guarantee across publishers.
Definition at line 104 of file slot_pool.cpp.
|
protected |
槽存储的分配对齐 / Allocation alignment of the slot storage.
Definition at line 136 of file slot_pool.hpp.
|
staticconstexprprotected |
无效槽位下标 / Sentinel for "no slot chosen".
Definition at line 122 of file slot_pool.hpp.
|
protected |
发布序号计数器,兼作 Take 重扫的发布计数 / Publish counter, also the change detector for the Take rescan.
Definition at line 144 of file slot_pool.hpp.
|
protected |
释放计数,Acquire 重扫的变化检测 / Release counter, the change detector for the Acquire rescan.
Definition at line 147 of file slot_pool.hpp.
|
protected |
每槽发布序号 / Per-slot publish sequence.
Definition at line 139 of file slot_pool.hpp.
|
protected |
槽总数 / Total number of slots.
Definition at line 134 of file slot_pool.hpp.
|
protected |
单个槽的字节数 / Byte size of one slot.
Definition at line 133 of file slot_pool.hpp.
|
protected |
相邻槽位之间的步长 / Byte stride between slots.
Definition at line 135 of file slot_pool.hpp.
|
protected |
槽存储缓冲区 / Byte buffer storing slots.
Definition at line 140 of file slot_pool.hpp.
|
protected |
每槽状态字 / Per-slot atomic state word.
Definition at line 138 of file slot_pool.hpp.