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>

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

Public Types

enum class  BusyState : uint32_t { IDLE = 0 , PENDING = 1 , EVENT = UINT32_MAX }
 

Public Member Functions

 ReadPort (size_t buffer_size=128)
 Constructs a ReadPort with queue sizes.
 
virtual size_t EmptySize ()
 获取队列的剩余可用空间。 Gets the remaining available space in the queue.
 
virtual 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 Finish (bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
 更新读取操作的状态。 Updates the status of the read operation.
 
void MarkAsRunning (ReadInfoBlock &info)
 标记读取操作为运行中。 Marks the read operation as running.
 
ErrorCode operator() (RawData data, ReadOperation &op)
 读取操作符重载,用于执行读取操作。 Overloaded function call operator to perform a read operation.
 
virtual void ProcessPendingReads (bool in_isr)
 Processes pending reads.
 
virtual void Reset ()
 Resets the ReadPort.
 

Data Fields

ReadFun read_fun_ = nullptr
 
LockFreeQueue< uint8_t > * queue_data_ = nullptr
 
size_t read_size_ = 0
 
ReadInfoBlock info_
 
std::atomic< BusyState > busy_ {BusyState::IDLE}
 

Detailed Description

ReadPort class for handling read operations.

处理读取操作的ReadPort类。

Definition at line 267 of file libxr_rw.hpp.

Member Enumeration Documentation

◆ BusyState

enum class LibXR::ReadPort::BusyState : uint32_t
strong

Definition at line 270 of file libxr_rw.hpp.

271 {
272 IDLE = 0,
273 PENDING = 1,
274 EVENT = UINT32_MAX
275 };

Constructor & Destructor Documentation

◆ ReadPort()

ReadPort::ReadPort ( size_t buffer_size = 128)

Constructs a ReadPort with queue sizes.

以指定队列大小构造ReadPort。

Parameters
queue_sizeNumber of queued operations.
buffer_sizeSize of each buffer.

Definition at line 10 of file libxr_rw.cpp.

11 : queue_data_(buffer_size > 0 ? new(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
12 LockFreeQueue<uint8_t>(buffer_size)
13 : nullptr)
14{
15}
无锁队列实现 / Lock-free queue implementation

Member Function Documentation

◆ EmptySize()

size_t ReadPort::EmptySize ( )
virtual

获取队列的剩余可用空间。 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).

Reimplemented in LibXR::ESP32UARTReadPort, and LibXR::TinyUSBUARTReadPort.

Definition at line 17 of file libxr_rw.cpp.

18{
19 ASSERT(queue_data_ != nullptr);
20 return queue_data_->EmptySize();
21}
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue

◆ Finish()

void ReadPort::Finish ( bool in_isr,
ErrorCode ans,
ReadInfoBlock & info,
uint32_t size )

更新读取操作的状态。 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 37 of file libxr_rw.cpp.

38{
39 read_size_ = size;
40 busy_.store(BusyState::IDLE, std::memory_order_release);
41 info.op.UpdateStatus(in_isr, std::forward<ErrorCode>(ans));
42}
void UpdateStatus(bool in_isr, Status &&...status)
Updates operation status based on type.
Definition libxr_rw.hpp:171
ReadOperation op
Read operation instance. 读取操作实例。
Definition libxr_rw.hpp:254

◆ MarkAsRunning()

void ReadPort::MarkAsRunning ( ReadInfoBlock & info)

标记读取操作为运行中。 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 44 of file libxr_rw.cpp.

44{ info.op.MarkAsRunning(); }
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
Definition libxr_rw.hpp:202

◆ operator()()

ErrorCode ReadPort::operator() ( RawData data,
ReadOperation & op )

读取操作符重载,用于执行读取操作。 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 46 of file libxr_rw.cpp.

47{
48 if (Readable())
49 {
50 BusyState is_busy = busy_.load(std::memory_order_relaxed);
51
52 if (is_busy == BusyState::PENDING)
53 {
54 return ErrorCode::BUSY;
55 }
56
57 while (true)
58 {
59 busy_.store(BusyState::IDLE, std::memory_order_release);
60
61 if (queue_data_ != nullptr)
62 {
63 auto readable_size = queue_data_->Size();
64
65 if (readable_size >= data.size_ && readable_size != 0)
66 {
67 auto ans =
68 queue_data_->PopBatch(reinterpret_cast<uint8_t*>(data.addr_), data.size_);
69 UNUSED(ans);
70 read_size_ = data.size_;
71 ASSERT(ans == ErrorCode::OK);
72 if (op.type != ReadOperation::OperationType::BLOCK)
73 {
74 op.UpdateStatus(false, ErrorCode::OK);
75 }
76 return ErrorCode::OK;
77 }
78 }
79
80 info_ = ReadInfoBlock{data, op};
81
82 op.MarkAsRunning();
83
84 auto ans = read_fun_(*this);
85
86 if (ans != ErrorCode::OK)
87 {
88 BusyState expected = BusyState::IDLE;
89 if (busy_.compare_exchange_weak(expected, BusyState::PENDING,
90 std::memory_order_acq_rel,
91 std::memory_order_acquire))
92 {
93 break;
94 }
95 else
96 {
97 expected = BusyState::PENDING;
98 continue;
99 }
100 }
101 else
102 {
103 read_size_ = data.size_;
104 if (op.type != ReadOperation::OperationType::BLOCK)
105 {
106 op.UpdateStatus(false, ErrorCode::OK);
107 }
108 return ErrorCode::OK;
109 }
110 }
111
112 if (op.type == ReadOperation::OperationType::BLOCK)
113 {
114 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
115 }
116 else
117 {
118 return ErrorCode::OK;
119 }
120 }
121 else
122 {
123 return ErrorCode::NOT_SUPPORT;
124 }
125}
size_t Size() const
获取当前队列中的元素数量 / Returns the number of elements currently in the queue
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
union LibXR::Operation::@4 data
OperationType type
Definition libxr_rw.hpp:225
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
bool Readable()
Checks if read operations are supported.
Definition libxr_rw.cpp:29
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25
Read information block structure.
Definition libxr_rw.hpp:252

◆ operator=()

ReadPort & ReadPort::operator= ( ReadFun fun)

赋值运算符重载,用于设置读取函数。 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 31 of file libxr_rw.cpp.

32{
33 read_fun_ = fun;
34 return *this;
35}

◆ ProcessPendingReads()

void ReadPort::ProcessPendingReads ( bool in_isr)
virtual

Processes pending reads.

处理挂起的读取请求。

Parameters
in_isr指示是否在中断上下文中执行。 Indicates whether the operation is executed in an interrupt context.

Reimplemented in LibXR::ESP32UARTReadPort, and LibXR::TinyUSBUARTReadPort.

Definition at line 127 of file libxr_rw.cpp.

128{
129 ASSERT(queue_data_ != nullptr);
130
131 if (in_isr)
132 {
133 auto is_busy = busy_.load(std::memory_order_relaxed);
134
135 if (is_busy == BusyState::PENDING)
136 {
137 if (queue_data_->Size() >= info_.data.size_)
138 {
139 if (info_.data.size_ > 0)
140 {
141 auto ans = queue_data_->PopBatch(reinterpret_cast<uint8_t*>(info_.data.addr_),
142 info_.data.size_);
143 UNUSED(ans);
144 ASSERT(ans == ErrorCode::OK);
145 }
146 Finish(in_isr, ErrorCode::OK, info_, info_.data.size_);
147 }
148 }
149 else if (is_busy == BusyState::IDLE)
150 {
151 busy_.store(BusyState::EVENT, std::memory_order_release);
152 }
153 }
154 else
155 {
156 if (busy_.load(std::memory_order_relaxed) == BusyState::PENDING)
157 {
158 if (queue_data_->Size() >= info_.data.size_)
159 {
160 if (info_.data.size_ > 0)
161 {
162 auto ans = queue_data_->PopBatch(reinterpret_cast<uint8_t*>(info_.data.addr_),
163 info_.data.size_);
164 UNUSED(ans);
165 ASSERT(ans == ErrorCode::OK);
166 }
167 Finish(in_isr, ErrorCode::OK, info_, info_.data.size_);
168 }
169 }
170 }
171}
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
更新读取操作的状态。 Updates the status of the read operation.
Definition libxr_rw.cpp:37
RawData data
Data buffer. 数据缓冲区。
Definition libxr_rw.hpp:253

◆ Readable()

bool ReadPort::Readable ( )

Checks if read operations are supported.

检查是否支持读取操作。

Definition at line 29 of file libxr_rw.cpp.

29{ return read_fun_ != nullptr; }

◆ Reset()

void ReadPort::Reset ( )
virtual

Resets the ReadPort.

重置ReadPort。

Reimplemented in LibXR::ESP32UARTReadPort, and LibXR::TinyUSBUARTReadPort.

Definition at line 173 of file libxr_rw.cpp.

174{
175 ASSERT(queue_data_ != nullptr);
176 queue_data_->Reset();
177}
void Reset()
重置队列 / Resets the queue

◆ Size()

size_t ReadPort::Size ( )
virtual

获取当前队列的已使用大小。 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).

Reimplemented in LibXR::ESP32UARTReadPort, and LibXR::TinyUSBUARTReadPort.

Definition at line 23 of file libxr_rw.cpp.

24{
25 ASSERT(queue_data_ != nullptr);
26 return queue_data_->Size();
27}

Field Documentation

◆ busy_

std::atomic<BusyState> LibXR::ReadPort::busy_ {BusyState::IDLE}

Definition at line 281 of file libxr_rw.hpp.

281{BusyState::IDLE};

◆ info_

ReadInfoBlock LibXR::ReadPort::info_

Definition at line 280 of file libxr_rw.hpp.

◆ queue_data_

LockFreeQueue<uint8_t>* LibXR::ReadPort::queue_data_ = nullptr

Definition at line 278 of file libxr_rw.hpp.

◆ read_fun_

ReadFun LibXR::ReadPort::read_fun_ = nullptr

Definition at line 277 of file libxr_rw.hpp.

◆ read_size_

size_t LibXR::ReadPort::read_size_ = 0

Definition at line 279 of file libxr_rw.hpp.


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