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

ReadPort class for handling read operations. More...

#include <libxr_rw.hpp>

Collaboration diagram for LibXR::ReadPort:

Public Member Functions

 ReadPort (size_t queue_size=3, size_t buffer_size=128)
 Constructs a ReadPort with queue sizes.
 
size_t EmptySize ()
 获取队列的剩余可用空间。 Gets the remaining available space in the queue.
 
size_t Size ()
 获取当前队列的已使用大小。 Gets the currently used size of the queue.
 
bool Readable ()
 Checks if read operations are supported.
 
ReadPortoperator= (ReadFun fun)
 赋值运算符重载,用于设置读取函数。 Overloaded assignment operator to set the read function.
 
void UpdateStatus (bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
 更新读取操作的状态。 Updates the status of the read operation.
 
void UpdateStatus (ReadInfoBlock &info)
 标记读取操作为运行中。 Marks the read operation as running.
 
ErrorCode operator() (RawData data, ReadOperation &op)
 读取操作符重载,用于执行读取操作。 Overloaded function call operator to perform a read operation.
 
void ProcessPendingReads ()
 Processes pending reads.
 
void Reset ()
 Resets the ReadPort.
 

Data Fields

ReadFun read_fun_ = nullptr
 
LockFreeQueue< ReadInfoBlock > * queue_block_ = nullptr
 
BaseQueuequeue_data_ = nullptr
 
size_t read_size_ = 0
 
Mutex mutex_
 

Detailed Description

ReadPort class for handling read operations.

处理读取操作的ReadPort类。

Definition at line 308 of file libxr_rw.hpp.

Constructor & Destructor Documentation

◆ ReadPort()

LibXR::ReadPort::ReadPort ( size_t  queue_size = 3,
size_t  buffer_size = 128 
)
inline

Constructs a ReadPort with queue sizes.

以指定队列大小构造ReadPort。

Parameters
queue_sizeNumber of queued operations.
buffer_sizeSize of each buffer.

Definition at line 323 of file libxr_rw.hpp.

324 : queue_block_(new LockFreeQueue<ReadInfoBlock>(queue_size)),
325 queue_data_(new BaseQueue(1, buffer_size))
326 {
327 }
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ EmptySize()

size_t LibXR::ReadPort::EmptySize ( )
inline

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

该函数返回 queue_block_ 中当前可用的空闲空间大小。 This function returns the size of the available empty space in queue_block_.

Returns
返回队列的空闲大小(单位:字节)。 Returns the empty size of the queue (in bytes).

Definition at line 339 of file libxr_rw.hpp.

339{ return queue_data_->EmptySize(); }
size_t EmptySize()
获取队列的空闲空间 (Get the available space in the queue).
Definition queue.hpp:325

◆ operator()()

ErrorCode LibXR::ReadPort::operator() ( RawData  data,
ReadOperation op 
)
inline

读取操作符重载,用于执行读取操作。 Overloaded function call operator to perform a read operation.

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

Parameters
data包含要读取的数据。 Contains the data to be read.
op读取操作对象,包含操作类型和同步机制。 Read operation object containing the operation type and synchronization mechanism.
Returns
返回操作的 ErrorCode,指示操作结果。 Returns an ErrorCode indicating the result of the operation.

Definition at line 427 of file libxr_rw.hpp.

428 {
429 if (Readable())
430 {
431 if (data.size_ == 0)
432 {
433 op.UpdateStatus(false, ErrorCode::OK);
434 if (op.type == ReadOperation::OperationType::BLOCK)
435 {
436 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
437 }
438 return ErrorCode::OK;
439 }
440
441 Mutex::LockGuard lock_guard(mutex_);
442
443 ReadInfoBlock block = {data, std::move(op)};
444 if (queue_block_->Push(block) != ErrorCode::OK)
445 {
446 return ErrorCode::FULL;
447 }
448
449 auto ans = read_fun_(*this);
450 if (op.type == ReadOperation::OperationType::BLOCK)
451 {
452 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
453 }
454 else
455 {
456 return ans;
457 }
458 }
459 else
460 {
461 return ErrorCode::NOT_SUPPORT;
462 }
463 }
bool Readable()
Checks if read operations are supported.
Definition libxr_rw.hpp:355

◆ operator=()

ReadPort & LibXR::ReadPort::operator= ( ReadFun  fun)
inline

赋值运算符重载,用于设置读取函数。 Overloaded assignment operator to set the read function.

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

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

Definition at line 370 of file libxr_rw.hpp.

371 {
372 read_fun_ = fun;
373 return *this;
374 }

◆ ProcessPendingReads()

void LibXR::ReadPort::ProcessPendingReads ( )
inline

Processes pending reads.

处理挂起的读取请求。

Definition at line 469 of file libxr_rw.hpp.

470 {
471 ReadInfoBlock block;
472 while (queue_block_->Peek(block) == ErrorCode::OK)
473 {
474 if (queue_data_->Size() >= block.data_.size_)
475 {
476 queue_data_->PopBatch(block.data_.addr_, block.data_.size_);
477 read_size_ = block.data_.size_;
478 block.op_.UpdateStatus(true, ErrorCode::OK);
479 queue_block_->Pop();
480 }
481 else
482 {
483 break;
484 }
485 }
486 }
size_t Size()
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.hpp:305
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
Definition queue.hpp:208

◆ Readable()

bool LibXR::ReadPort::Readable ( )
inline

Checks if read operations are supported.

检查是否支持读取操作。

Definition at line 355 of file libxr_rw.hpp.

355{ return read_fun_ != nullptr; }

◆ Reset()

void LibXR::ReadPort::Reset ( )
inline

Resets the ReadPort.

重置ReadPort。

Definition at line 490 of file libxr_rw.hpp.

491 {
492 queue_block_->Reset();
493 queue_data_->Reset();
494 read_size_ = 0;
495 }
void Reset()
重置队列,清空所有数据 (Reset the queue and clear all data).
Definition queue.hpp:295

◆ Size()

size_t LibXR::ReadPort::Size ( )
inline

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

该函数返回 queue_block_ 当前已占用的空间大小。 This function returns the size of the space currently used in queue_block_.

Returns
返回队列的已使用大小(单位:字节)。 Returns the used size of the queue (in bytes).

Definition at line 351 of file libxr_rw.hpp.

351{ return queue_data_->Size(); }

◆ UpdateStatus() [1/2]

void LibXR::ReadPort::UpdateStatus ( bool  in_isr,
ErrorCode  ans,
ReadInfoBlock info,
uint32_t  size 
)
inline

更新读取操作的状态。 Updates the status of the read operation.

该函数用于在读取操作过程中更新 read_size_ 并调用 UpdateStatus 方法更新 info.op_ 的状态。 This function updates read_size_ and calls UpdateStatus on info.op_ during a read operation.

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

Definition at line 393 of file libxr_rw.hpp.

394 {
395 read_size_ = size;
396 info.op_.UpdateStatus(in_isr, std::forward<ErrorCode>(ans));
397 }

◆ UpdateStatus() [2/2]

void LibXR::ReadPort::UpdateStatus ( ReadInfoBlock info)
inline

标记读取操作为运行中。 Marks the read operation as running.

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

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

Definition at line 409 of file libxr_rw.hpp.

409{ info.op_.MarkAsRunning(); }

Field Documentation

◆ mutex_

Mutex LibXR::ReadPort::mutex_

Definition at line 315 of file libxr_rw.hpp.

◆ queue_block_

LockFreeQueue<ReadInfoBlock>* LibXR::ReadPort::queue_block_ = nullptr

Definition at line 312 of file libxr_rw.hpp.

◆ queue_data_

BaseQueue* LibXR::ReadPort::queue_data_ = nullptr

Definition at line 313 of file libxr_rw.hpp.

◆ read_fun_

ReadFun LibXR::ReadPort::read_fun_ = nullptr

Definition at line 311 of file libxr_rw.hpp.

◆ read_size_

size_t LibXR::ReadPort::read_size_ = 0

Definition at line 314 of file libxr_rw.hpp.


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