libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
write_stream.cpp
1#include "write_port.hpp"
2
3using namespace LibXR;
4
5WritePort::Stream::Stream(WritePort* port, WriteOperation op) : port_(port), op_(op)
6{
7 UNUSED(Acquire());
8}
9
11{
12 if (!owns_port_)
13 {
14 return;
15 }
16
17 if (buffered_size_ != 0U)
18 {
19 UNUSED(SubmitBuffered());
20 return;
21 }
22
23 Release();
24 CompleteEmpty();
25}
26
28{
29 if (owns_port_)
30 {
31 return ErrorCode::OK;
32 }
33 if (port_ == nullptr)
34 {
36 }
37 if (!port_->Writable())
38 {
40 }
41 if (!port_->TryClaimProducer())
42 {
43 return ErrorCode::BUSY;
44 }
45
46 if (!port_->IsAdmissionMode() && port_->queue_requests_->EmptySize() < 1U)
47 {
48 port_->ReleaseProducer(WritePort::Phase::IDLE, false);
49 return ErrorCode::FULL;
50 }
51
52 owns_port_ = true;
53 return ErrorCode::OK;
54}
55
57{
58 if (port_ == nullptr || !port_->Writable())
59 {
61 }
62
63 if (data.size_ == 0U)
64 {
65 return ErrorCode::OK;
66 }
67
68 const ErrorCode acquire_result = Acquire();
69 if (acquire_result != ErrorCode::OK)
70 {
71 return acquire_result;
72 }
73
74 ASSERT(data.addr_ != nullptr);
75
76 if (port_->queue_data_ == nullptr || port_->queue_data_->EmptySize() < data.size_)
77 {
78 return ErrorCode::FULL;
79 }
80
81 [[maybe_unused]] const ErrorCode result = port_->queue_data_->PushBatch(
82 reinterpret_cast<const uint8_t*>(data.addr_), data.size_);
83 DEV_ASSERT(result == ErrorCode::OK);
84 buffered_size_ += data.size_;
85 return ErrorCode::OK;
86}
87
88ErrorCode WritePort::Stream::SubmitBuffered()
89{
90 DEV_ASSERT(owns_port_);
91 DEV_ASSERT(buffered_size_ != 0U);
92
93 const size_t size = buffered_size_;
94 buffered_size_ = 0U;
95 owns_port_ = false;
96 return port_->IsAdmissionMode() ? port_->CommitAdmission(size, op_, false)
97 : port_->CommitQueued(size, op_, false);
98}
99
100void WritePort::Stream::Release()
101{
102 if (!owns_port_)
103 {
104 return;
105 }
106 DEV_ASSERT(buffered_size_ == 0U);
107 owns_port_ = false;
108 port_->ReleaseProducer(WritePort::Phase::IDLE, false);
109}
110
111void WritePort::Stream::CompleteEmpty()
112{
113 if (op_.type != WriteOperation::OperationType::BLOCK)
114 {
115 op_.UpdateStatus(false, ErrorCode::OK);
116 }
117}
118
120{
121 if (Acquire() == ErrorCode::OK && EmptySize() >= data.size_)
122 {
123 UNUSED(Write(data));
124 }
125 return *this;
126}
127
129{
130 if (!owns_port_)
131 {
132 return ErrorCode::OK;
133 }
134 if (buffered_size_ != 0U)
135 {
136 return SubmitBuffered();
137 }
138
139 Release();
140 CompleteEmpty();
141 return ErrorCode::OK;
142}
只读原始数据视图 / Immutable raw data view
size_t size_
数据字节数 / Data size in bytes
const void * addr_
数据起始地址 / Data start address
合并多次写入的流接口 / Stream interface for batched writes.
ErrorCode Acquire()
尝试取得端口写入权 / Try to acquire the port's producer access.
ErrorCode Commit()
提交批次并释放写入权 / Submit the batch and release producer access.
Stream(WritePort *port, WriteOperation op)
构造流并尝试取得写入权 / Construct a stream and try to acquire the port.
~Stream()
提交剩余批次并结束流 / Submit the remaining batch and destroy the stream.
ErrorCode Write(ConstRawData data)
向当前批次追加数据 / Append data to the current batch.
Stream & operator<<(const ConstRawData &data)
尝试追加数据并返回流引用 / Try to append data and return the stream.
基于 SPSC 字节队列的写端口 / Write endpoint backed by an SPSC byte queue.
size_t EmptySize() const
获取字节队列当前空闲空间 / Get current byte queue free space.
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ BUSY
忙碌 / Busy
@ PTR_NULL
空指针 / Null pointer
@ NOT_SUPPORT
不支持 / Not supported
@ FULL
已满 / Full
@ OK
操作成功 / Operation successful