libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::WritePort::Stream Class Reference
Collaboration diagram for LibXR::WritePort::Stream:
[legend]

Public Member Functions

 Stream (LibXR::WritePort *port, LibXR::WriteOperation op)
 构造流写入对象,并尝试锁定端口。
 
 ~Stream ()
 析构时自动提交已累积的数据并释放锁。
 
ErrorCode Write (ConstRawData data)
 追加一个原始数据片段到当前流批次。
 
ErrorCode Write (std::string_view text)
 追加一个文本片段到当前流批次。
 
Streamoperator<< (const ConstRawData &data)
 追加写入数据的语法糖,忽略返回状态并支持链式调用。
 
ErrorCode Commit ()
 手动提交已写入的数据到队列,并释放当前锁。
 
ErrorCode Acquire ()
 为当前流批次获取一次可写入的端口所有权。
 
size_t EmptySize () const
 获取当前批次还可追加的剩余字节数。
 

Private Member Functions

ErrorCode SubmitBuffered ()
 将当前已缓存批次提交给 WritePort。
 
void Release ()
 将当前批次的端口所有权归还给 WritePort。
 

Private Attributes

LibXR::WritePortport_
 写端口指针 Pointer to the WritePort
 
LibXR::WriteOperation op_
 写操作对象 Write operation object
 
size_t batch_capacity_ = 0
 当前批次可用的总容量 Total capacity reserved for the current batch
 
size_t buffered_size_ = 0
 当前批次已追加到共享 data queue、但尚未发布对应元数据的字节数 Bytes already appended into the shared data queue for the current batch, but whose metadata has not yet been published
 
bool owns_port_ = false
 当前 Stream 是否持有该批次的端口所有权 Whether this Stream currently owns the batch
 

Detailed Description

Definition at line 73 of file write_port.hpp.

Constructor & Destructor Documentation

◆ Stream()

WritePort::Stream::Stream ( LibXR::WritePort * port,
LibXR::WriteOperation op )

构造流写入对象,并尝试锁定端口。

Constructs a Stream object and tries to acquire WritePort lock.

Parameters
port指向 WritePort 的指针 Pointer to WritePort.
op写操作对象(可重用)Write operation object (can be reused).

Definition at line 5 of file write_stream.cpp.

6 : port_(port), op_(op)
7{
8 UNUSED(Acquire());
9}
ErrorCode Acquire()
为当前流批次获取一次可写入的端口所有权。
LibXR::WritePort * port_
写端口指针 Pointer to the WritePort
LibXR::WriteOperation op_
写操作对象 Write operation object

◆ ~Stream()

WritePort::Stream::~Stream ( )

析构时自动提交已累积的数据并释放锁。

Destructor: automatically commits any accumulated data and releases the lock.

Definition at line 13 of file write_stream.cpp.

14{
15 if (owns_port_ && buffered_size_ > 0)
16 {
17 UNUSED(SubmitBuffered());
18 }
19
20 if (owns_port_)
21 {
22 Release();
23 }
24}
ErrorCode SubmitBuffered()
将当前已缓存批次提交给 WritePort。
void Release()
将当前批次的端口所有权归还给 WritePort。
bool owns_port_
当前 Stream 是否持有该批次的端口所有权 Whether this Stream currently owns the batch
size_t buffered_size_
当前批次已追加到共享 data queue、但尚未发布对应元数据的字节数 Bytes already appended into the shared data queue for the curren...

Member Function Documentation

◆ Acquire()

ErrorCode WritePort::Stream::Acquire ( )
nodiscard

为当前流批次获取一次可写入的端口所有权。

Acquires append ownership for the current stream batch.

Returns
返回获取结果。Returns the acquisition result.

Definition at line 26 of file write_stream.cpp.

27{
28 if (owns_port_)
29 {
30 return ErrorCode::OK;
31 }
32
33 if (port_ == nullptr)
34 {
36 }
37
38 DEV_ASSERT(port_->queue_info_ != nullptr);
39 DEV_ASSERT(port_->queue_data_ != nullptr);
40
41 if (!port_->Writable())
42 {
44 }
45
46 BusyState expected = BusyState::IDLE;
47 if (!port_->busy_.compare_exchange_strong(expected, BusyState::LOCKED,
48 std::memory_order_acq_rel,
49 std::memory_order_acquire))
50 {
51 return ErrorCode::BUSY;
52 }
53
54 if (port_->queue_info_->EmptySize() < 1)
55 {
56 port_->busy_.store(BusyState::IDLE, std::memory_order_release);
57 return ErrorCode::FULL;
58 }
59
60 owns_port_ = true;
62 return ErrorCode::OK;
63}
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue
size_t batch_capacity_
当前批次可用的总容量 Total capacity reserved for the current batch
std::atomic< BusyState > busy_
Shared submit/wait handoff state. 共享的提交/等待交接状态。
@ LOCKED
Submission path owns queue mutation. 提交路径占有写队列/元数据修改权。
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
LockFreeQueue< uint8_t > * queue_data_
Payload queue for pending write bytes. 挂起写入字节的数据队列。
LockFreeQueue< WriteInfoBlock > * queue_info_
Metadata queue for pending write batches. 挂起写批次的元数据队列。
@ BUSY
忙碌 | Busy
@ PTR_NULL
空指针 | Null pointer
@ NOT_SUPPORT
不支持 | Not supported
@ FULL
已满 | Full
@ OK
操作成功 | Operation successful

◆ Commit()

ErrorCode WritePort::Stream::Commit ( )

手动提交已写入的数据到队列,并释放当前锁。

Manually commit accumulated data to the queue, then release the current lock.

调用后会发布当前批次对应的元数据、使这批已追加到共享 data queue 的字节正式成为 一个可消费的写操作,并将 size 计数归零。适合周期性手动 flush。 After calling, the metadata that describes the current batch is published so the bytes already appended into the shared data queue become one consumable write operation, and the size counter is reset. Suitable for periodic manual flush.

Returns
返回操作的 ErrorCode,指示操作结果。 Returns an ErrorCode indicating the result of the operation.

Definition at line 147 of file write_stream.cpp.

148{
149 auto ans = ErrorCode::OK;
150
151 if (owns_port_ && buffered_size_ > 0)
152 {
153 ans = SubmitBuffered();
154 if (op_.type == WriteOperation::OperationType::BLOCK)
155 {
156 return ans;
157 }
158 }
159
160 if (owns_port_)
161 {
162 Release();
163 }
164
165 return ans;
166}
OperationType type

◆ EmptySize()

size_t LibXR::WritePort::Stream::EmptySize ( ) const
inlinenodiscard

获取当前批次还可追加的剩余字节数。

Returns the remaining appendable bytes in the current batch.

返回值只在 Acquire 成功后有意义;若当前尚未持有流批次所有权,则返回 0。 The return value is meaningful only after Acquire succeeds; it returns zero while this stream does not currently own the batch.

Definition at line 159 of file write_port.hpp.

160 {
162 }

◆ operator<<()

WritePort::Stream & WritePort::Stream::operator<< ( const ConstRawData & data)

追加写入数据的语法糖,忽略返回状态并支持链式调用。

Syntax sugar for appending data; ignores the status and supports chaining.

Parameters
data要写入的数据 Data to write.
Returns
返回自身引用 Enables chainable call.

Definition at line 131 of file write_stream.cpp.

132{
133 if (Acquire() != ErrorCode::OK)
134 {
135 return *this;
136 }
137
138 if (EmptySize() < data.size_)
139 {
140 return *this;
141 }
142
143 UNUSED(Write(data));
144 return *this;
145}
size_t size_
数据字节数 / Data size in bytes
size_t EmptySize() const
获取当前批次还可追加的剩余字节数。
ErrorCode Write(ConstRawData data)
追加一个原始数据片段到当前流批次。

◆ Release()

void WritePort::Stream::Release ( )
private

将当前批次的端口所有权归还给 WritePort。

Releases the current batch ownership back to WritePort.

Definition at line 122 of file write_stream.cpp.

123{
124 if (owns_port_)
125 {
126 owns_port_ = false;
127 port_->busy_.store(BusyState::IDLE, std::memory_order_release);
128 }
129}

◆ SubmitBuffered()

ErrorCode WritePort::Stream::SubmitBuffered ( )
nodiscardprivate

将当前已缓存批次提交给 WritePort。

Submits the currently buffered batch to WritePort.

非 BLOCK 路径下,提交后当前 Stream 仍负责释放端口所有权;BLOCK 路径下, 所有权会交给 WritePort 的等待状态机继续管理。 On non-BLOCK paths, this Stream still releases the port ownership after submission; on BLOCK paths, ownership is handed off to WritePort's wait-state machine.

Definition at line 92 of file write_stream.cpp.

93{
94 ASSERT(owns_port_);
95 ASSERT(buffered_size_ > 0);
96
97 if (op_.type == WriteOperation::OperationType::BLOCK)
98 {
99 // Publish the wait state before the queued metadata can be consumed.
100 // 元数据可能被消费前,先发布等待状态。
102 port_->busy_.store(BusyState::BLOCK_PUBLISHING, std::memory_order_release);
103 }
104
105 auto ans = port_->queue_info_->Push(
107 ASSERT(ans == ErrorCode::OK);
108
109 ans = port_->CommitWrite({nullptr, buffered_size_}, op_, true);
110 buffered_size_ = 0;
111
112 if (op_.type == WriteOperation::OperationType::BLOCK)
113 {
114 // WritePort now owns the BLOCK wait/finish state machine.
115 // BLOCK 等待/完成状态机此后由 WritePort 接管。
116 owns_port_ = false;
117 }
118
119 return ans;
120}
只读原始数据视图 / Immutable raw data view
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
ErrorCode CommitWrite(ConstRawData data, WriteOperation &op, bool pushed=false, bool in_isr=false)
提交写入操作。 Commits a write operation.

◆ Write() [1/2]

ErrorCode WritePort::Stream::Write ( ConstRawData data)
nodiscard

追加一个原始数据片段到当前流批次。

Appends one raw-data chunk to the current stream batch.

该接口是 Stream 的底层语义写入入口。它负责拿到当前批次的写入所有权,并尝试将 整个片段原子地追加到当前批次对应的共享 data queue 尾部;Commit() 负责随后发布 这批字节对应的元数据。若空间不足则返回 FULL,并保持该片段完全未写入。 This is the low-level semantic write entrypoint of Stream. It acquires the current batch ownership and then attempts to append the whole chunk atomically into the shared data-queue tail; Commit() later publishes the metadata that describes this batch. If space is insufficient it returns FULL and leaves that chunk entirely unwritten.

Parameters
data要写入的数据片段 Raw-data chunk to append.
Returns
返回操作结果。Returns the write result.

Definition at line 65 of file write_stream.cpp.

66{
67 if (data.size_ == 0)
68 {
69 return ErrorCode::OK;
70 }
71
72 if (data.addr_ == nullptr)
73 {
75 }
76
77 auto lock_result = Acquire();
78 if (lock_result != ErrorCode::OK)
79 {
80 return lock_result;
81 }
82
83 auto ans = port_->queue_data_->PushBatch(reinterpret_cast<const uint8_t*>(data.addr_),
84 data.size_);
85 if (ans == ErrorCode::OK)
86 {
87 buffered_size_ += data.size_;
88 }
89 return ans;
90}
const void * addr_
数据起始地址 / Data start address
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue

◆ Write() [2/2]

ErrorCode LibXR::WritePort::Stream::Write ( std::string_view text)
inlinenodiscard

追加一个文本片段到当前流批次。

Appends one text chunk to the current stream batch.

Parameters
text要写入的文本片段 Text chunk to append.
Returns
返回操作结果。Returns the write result.

Definition at line 115 of file write_port.hpp.

116 {
117 return Write(ConstRawData{text.data(), text.size()});
118 }

Field Documentation

◆ batch_capacity_

size_t LibXR::WritePort::Stream::batch_capacity_ = 0
private

当前批次可用的总容量 Total capacity reserved for the current batch

Definition at line 184 of file write_port.hpp.

◆ buffered_size_

size_t LibXR::WritePort::Stream::buffered_size_ = 0
private

当前批次已追加到共享 data queue、但尚未发布对应元数据的字节数 Bytes already appended into the shared data queue for the current batch, but whose metadata has not yet been published

Definition at line 185 of file write_port.hpp.

◆ op_

LibXR::WriteOperation LibXR::WritePort::Stream::op_
private

写操作对象 Write operation object

Definition at line 183 of file write_port.hpp.

◆ owns_port_

bool LibXR::WritePort::Stream::owns_port_ = false
private

当前 Stream 是否持有该批次的端口所有权 Whether this Stream currently owns the batch

Definition at line 186 of file write_port.hpp.

◆ port_

LibXR::WritePort* LibXR::WritePort::Stream::port_
private

写端口指针 Pointer to the WritePort

Definition at line 182 of file write_port.hpp.


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