libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::WritePort Class Reference

WritePort class for handling write operations. More...

#include <libxr_rw.hpp>

Inheritance diagram for LibXR::WritePort:
[legend]
Collaboration diagram for LibXR::WritePort:
[legend]

Data Structures

class  Stream
 WritePort 的流式写入操作器,支持链式 << 操作和批量提交。 More...
 

Public Types

enum class  LockState : uint32_t { LOCKED = 0 , UNLOCKED = UINT32_MAX }
 

Public Member Functions

 WritePort (size_t queue_size=3, size_t buffer_size=128)
 构造一个新的 WritePort 对象。 Constructs a new WritePort object.
 
virtual size_t EmptySize ()
 获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.
 
virtual size_t Size ()
 获取当前数据队列的已使用大小。 Gets the used size of the current data queue.
 
bool Writable ()
 判断端口是否可写。 Checks whether the port is writable.
 
WritePortoperator= (WriteFun fun)
 赋值运算符重载,用于设置写入函数。 Overloaded assignment operator to set the write function.
 
void Finish (bool in_isr, ErrorCode ans, WriteInfoBlock &info, uint32_t size)
 更新写入操作的状态。 Updates the status of the write operation.
 
void MarkAsRunning (WriteOperation &op)
 标记写入操作为运行中。 Marks the write operation as running.
 
ErrorCode operator() (ConstRawData data, WriteOperation &op)
 执行写入操作。 Performs a write operation.
 
virtual void Reset ()
 Resets the WritePort.
 

Data Fields

WriteFun write_fun_ = nullptr
 
LockFreeQueue< WriteInfoBlock > * queue_info_
 
LockFreeQueue< uint8_t > * queue_data_
 
std::atomic< LockState > lock_ {LockState::UNLOCKED}
 
size_t write_size_ = 0
 

Private Member Functions

ErrorCode CommitWrite (ConstRawData data, WriteOperation &op, bool pushed=false)
 

Detailed Description

WritePort class for handling write operations.

处理写入操作的WritePort类。

Definition at line 402 of file libxr_rw.hpp.

Member Enumeration Documentation

◆ LockState

enum class LibXR::WritePort::LockState : uint32_t
strong

Definition at line 405 of file libxr_rw.hpp.

406 {
407 LOCKED = 0,
408 UNLOCKED = UINT32_MAX
409 };

Constructor & Destructor Documentation

◆ WritePort()

WritePort::WritePort ( size_t queue_size = 3,
size_t buffer_size = 128 )

构造一个新的 WritePort 对象。 Constructs a new WritePort object.

该构造函数初始化无锁操作队列 queue_info_ 和数据块队列 queue_data_。 This constructor initializes the lock-free operation queue queue_info_ and the data block queue queue_data_.

Parameters
queue_size队列的大小,默认为 3。 The size of the queue, default is 3.
block_size缓存的数据的最大大小,默认为 128。 The maximum size of cached data, default is 128.

Definition at line 178 of file libxr_rw.cpp.

179 : queue_info_(new(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
181 queue_data_(buffer_size > 0 ? new(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
182 LockFreeQueue<uint8_t>(buffer_size)
183 : nullptr)
184{
185}
无锁队列实现 / Lock-free queue implementation

Member Function Documentation

◆ CommitWrite()

ErrorCode WritePort::CommitWrite ( ConstRawData data,
WriteOperation & op,
bool pushed = false )
private

Definition at line 243 of file libxr_rw.cpp.

244{
245 if (!meta_pushed && queue_info_->EmptySize() < 1)
246 {
247 lock_.store(LockState::UNLOCKED, std::memory_order_release);
248 return ErrorCode::FULL;
249 }
250
251 if (queue_data_)
252 {
253 ErrorCode ans = ErrorCode::OK;
254 if (!meta_pushed)
255 {
256 if (queue_data_->EmptySize() < data.size_)
257 {
258 lock_.store(LockState::UNLOCKED, std::memory_order_release);
259 return ErrorCode::FULL;
260 }
261
262 ans = queue_data_->PushBatch(reinterpret_cast<const uint8_t*>(data.addr_),
263 data.size_);
264 UNUSED(ans);
265 ASSERT(ans == ErrorCode::OK);
266
267 WriteInfoBlock info{data, op};
268 ans = queue_info_->Push(info);
269
270 ASSERT(ans == ErrorCode::OK);
271 }
272
273 op.MarkAsRunning();
274
275 ans = write_fun_(*this);
276
277 if (!meta_pushed)
278 {
279 lock_.store(LockState::UNLOCKED, std::memory_order_release);
280 }
281
282 if (ans == ErrorCode::OK)
283 {
284 write_size_ = data.size_;
285 if (op.type != WriteOperation::OperationType::BLOCK)
286 {
287 op.UpdateStatus(false, ErrorCode::OK);
288 }
289 return ErrorCode::OK;
290 }
291
292 if (op.type == WriteOperation::OperationType::BLOCK)
293 {
294 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
295 }
296
297 return ErrorCode::OK;
298 }
299 else
300 {
301 WriteInfoBlock info{data, op};
302 auto ans = queue_info_->Push(info);
303
304 ASSERT(ans == ErrorCode::OK);
305
306 op.MarkAsRunning();
307
308 ans = write_fun_(*this);
309
310 lock_.store(LockState::UNLOCKED, std::memory_order_release);
311
312 if (ans == ErrorCode::OK)
313 {
314 write_size_ = data.size_;
315 if (op.type != WriteOperation::OperationType::BLOCK)
316 {
317 op.UpdateStatus(false, ErrorCode::OK);
318 }
319 return ErrorCode::OK;
320 }
321
322 if (op.type == WriteOperation::OperationType::BLOCK)
323 {
324 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
325 }
326 else
327 {
328 return ErrorCode::OK;
329 }
330 }
331}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
Definition libxr_rw.hpp:202
void UpdateStatus(bool in_isr, Status &&...status)
Updates operation status based on type.
Definition libxr_rw.hpp:171
union LibXR::Operation::@4 data
OperationType type
Definition libxr_rw.hpp:226
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25

◆ EmptySize()

size_t WritePort::EmptySize ( )
virtual

获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.

Returns
返回数据队列的空闲大小。 Returns the empty size of the data queue.

Reimplemented in LibXR::ESP32UARTWritePort, and LibXR::TinyUSBUARTWritePort.

Definition at line 187 of file libxr_rw.cpp.

188{
189 ASSERT(queue_data_ != nullptr);
190 return queue_data_->EmptySize();
191}

◆ Finish()

void WritePort::Finish ( bool in_isr,
ErrorCode ans,
WriteInfoBlock & info,
uint32_t size )

更新写入操作的状态。 Updates the status of the write operation.

该函数在写入操作过程中更新 write_size_ 并调用 UpdateStatus 方法更新 op 的状态。 This function updates write_size_ and calls UpdateStatus on op during a write operation.

Parameters
in_isr指示是否在中断上下文中执行。 Indicates whether the operation is executed in an interrupt context.
ans错误码,用于指示操作的结果。 Error code indicating the result of the operation.
op需要更新状态的 WriteOperation 引用。 Reference to the WriteOperation whose status needs to be updated.
size写入的数据大小。 The size of the written data.

Definition at line 207 of file libxr_rw.cpp.

208{
209 write_size_ = size;
210 info.op.UpdateStatus(in_isr, std::forward<ErrorCode>(ans));
211}
WriteOperation op
Write operation instance. 写入操作实例。
Definition libxr_rw.hpp:261

◆ MarkAsRunning()

void WritePort::MarkAsRunning ( WriteOperation & op)

标记写入操作为运行中。 Marks the write operation as running.

该函数用于将 op 标记为运行状态,以指示当前正在进行写入操作。 This function marks op as running to indicate an ongoing write operation.

Parameters
op需要更新状态的 WriteOperation 引用。 Reference to the WriteOperation whose status needs to be updated.

Definition at line 213 of file libxr_rw.cpp.

213{ op.MarkAsRunning(); }

◆ operator()()

ErrorCode WritePort::operator() ( ConstRawData data,
WriteOperation & op )

执行写入操作。 Performs a write operation.

该函数检查端口是否可写,并根据 data.size_op 的类型执行不同的操作。 This function checks if the port is writable and performs different actions based on data.size_ and the type of op.

Parameters
data需要写入的原始数据。 Raw data to be written.
op写入操作对象,包含操作类型和同步机制。 Write operation object containing the operation type and synchronization mechanism.
Returns
返回操作的 ErrorCode,指示操作结果。 Returns an ErrorCode indicating the result of the operation.

Definition at line 215 of file libxr_rw.cpp.

216{
217 if (Writable())
218 {
219 if (data.size_ == 0)
220 {
221 write_size_ = 0;
222 if (op.type != WriteOperation::OperationType::BLOCK)
223 {
224 op.UpdateStatus(false, ErrorCode::OK);
225 }
226 return ErrorCode::OK;
227 }
228
229 LockState expected = LockState::UNLOCKED;
230 if (!lock_.compare_exchange_strong(expected, LockState::LOCKED))
231 {
232 return ErrorCode::BUSY;
233 }
234
235 return CommitWrite(data, op);
236 }
237 else
238 {
239 return ErrorCode::NOT_SUPPORT;
240 }
241}
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
Definition libxr_rw.cpp:199

◆ operator=()

WritePort & WritePort::operator= ( WriteFun fun)

赋值运算符重载,用于设置写入函数。 Overloaded assignment operator to set the write function.

该函数允许使用 WriteFun 类型的函数对象赋值给 WritePort,从而设置 write_fun_。 This function allows assigning a WriteFun function object to WritePort, setting write_fun_.

Parameters
fun要分配的写入函数。 The write function to be assigned.
Returns
返回自身的引用,以支持链式调用。 Returns a reference to itself for chaining.

Definition at line 201 of file libxr_rw.cpp.

202{
203 write_fun_ = fun;
204 return *this;
205}

◆ Reset()

void WritePort::Reset ( )
virtual

Resets the WritePort.

重置WritePort。

Reimplemented in LibXR::ESP32UARTWritePort, and LibXR::TinyUSBUARTWritePort.

Definition at line 333 of file libxr_rw.cpp.

334{
335 ASSERT(queue_data_ != nullptr);
336 queue_data_->Reset();
337 queue_info_->Reset();
338}
void Reset()
重置队列 / Resets the queue

◆ Size()

size_t WritePort::Size ( )
virtual

获取当前数据队列的已使用大小。 Gets the used size of the current data queue.

Returns
返回数据队列的已使用大小。 Returns the size of the data queue.

Reimplemented in LibXR::ESP32UARTWritePort, and LibXR::TinyUSBUARTWritePort.

Definition at line 193 of file libxr_rw.cpp.

194{
195 ASSERT(queue_data_ != nullptr);
196 return queue_data_->Size();
197}
size_t Size() const
获取当前队列中的元素数量 / Returns the number of elements currently in the queue

◆ Writable()

bool WritePort::Writable ( )

判断端口是否可写。 Checks whether the port is writable.

Returns
如果 write_fun_ 不为空,则返回 true,否则返回 false。 Returns true if write_fun_ is not null, otherwise returns false.

Definition at line 199 of file libxr_rw.cpp.

199{ return write_fun_ != nullptr; }

Field Documentation

◆ lock_

std::atomic<LockState> LibXR::WritePort::lock_ {LockState::UNLOCKED}

Definition at line 414 of file libxr_rw.hpp.

414{LockState::UNLOCKED};

◆ queue_data_

LockFreeQueue<uint8_t>* LibXR::WritePort::queue_data_

Definition at line 413 of file libxr_rw.hpp.

◆ queue_info_

LockFreeQueue<WriteInfoBlock>* LibXR::WritePort::queue_info_

Definition at line 412 of file libxr_rw.hpp.

◆ write_fun_

WriteFun LibXR::WritePort::write_fun_ = nullptr

Definition at line 411 of file libxr_rw.hpp.

◆ write_size_

size_t LibXR::WritePort::write_size_ = 0

Definition at line 415 of file libxr_rw.hpp.


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