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 = 2 }
 

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
 
Mutex mutex_
 
ReadInfoBlock info_
 
std::atomic< BusyState > busy_ {BusyState::Idle}
 

Detailed Description

ReadPort class for handling read operations.

处理读取操作的ReadPort类。

Definition at line 269 of file libxr_rw.hpp.

Member Enumeration Documentation

◆ BusyState

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

Definition at line 272 of file libxr_rw.hpp.

273 {
274 Idle = 0,
275 Pending = 1,
276 Event = 2
277 };

Constructor & Destructor Documentation

◆ ReadPort()

LibXR::ReadPort::ReadPort ( 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 292 of file libxr_rw.hpp.

293 : queue_data_(buffer_size > 0 ? new(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
294 LockFreeQueue<uint8_t>(buffer_size)
295 : nullptr)
296 {
297 }

Member Function Documentation

◆ EmptySize()

virtual size_t LibXR::ReadPort::EmptySize ( )
inlinevirtual

获取队列的剩余可用空间。 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 309 of file libxr_rw.hpp.

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

◆ Finish()

void LibXR::ReadPort::Finish ( 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 371 of file libxr_rw.hpp.

372 {
373 read_size_ = size;
374 busy_.store(BusyState::Idle, std::memory_order_release);
375 info.op.UpdateStatus(in_isr, std::forward<ErrorCode>(ans));
376 }

◆ MarkAsRunning()

void LibXR::ReadPort::MarkAsRunning ( 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 388 of file libxr_rw.hpp.

388{ info.op.MarkAsRunning(); }

◆ 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 406 of file libxr_rw.hpp.

407 {
408 if (Readable())
409 {
410 mutex_.Lock();
411
412 BusyState is_busy = busy_.load(std::memory_order_relaxed);
413
414 if (is_busy == BusyState::Pending)
415 {
416 mutex_.Unlock();
417 return ErrorCode::BUSY;
418 }
419
420 while (true)
421 {
422 busy_.store(BusyState::Idle, std::memory_order_release);
423
424 if (queue_data_ != nullptr)
425 {
426 auto readable_size = queue_data_->Size();
427
428 if (readable_size >= data.size_ && readable_size != 0)
429 {
430 auto ans = queue_data_->PopBatch(reinterpret_cast<uint8_t *>(data.addr_),
431 data.size_);
432 UNUSED(ans);
433 read_size_ = data.size_;
434 ASSERT(ans == ErrorCode::OK);
435 if (op.type != ReadOperation::OperationType::BLOCK)
436 {
437 op.UpdateStatus(false, ErrorCode::OK);
438 }
439 mutex_.Unlock();
440 return ErrorCode::OK;
441 }
442 }
443
444 info_ = ReadInfoBlock{data, op};
445
446 op.MarkAsRunning();
447
448 auto ans = read_fun_(*this);
449
450 if (ans != ErrorCode::OK)
451 {
452 BusyState expected = BusyState::Idle;
453 if (busy_.compare_exchange_strong(expected, BusyState::Pending,
454 std::memory_order_acq_rel,
455 std::memory_order_acquire))
456 {
457 break;
458 }
459 else
460 {
461 expected = BusyState::Pending;
462 continue;
463 }
464 }
465 else
466 {
467 read_size_ = data.size_;
468 if (op.type != ReadOperation::OperationType::BLOCK)
469 {
470 op.UpdateStatus(false, ErrorCode::OK);
471 }
472 mutex_.Unlock();
473 return ErrorCode::OK;
474 }
475 }
476
477 mutex_.Unlock();
478
479 if (op.type == ReadOperation::OperationType::BLOCK)
480 {
481 return op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
482 }
483 else
484 {
485 return ErrorCode::OK;
486 }
487 }
488 else
489 {
490 return ErrorCode::NOT_SUPPORT;
491 }
492 }
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
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:14
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:28
bool Readable()
Checks if read operations are supported.
Definition libxr_rw.hpp:333

◆ 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 348 of file libxr_rw.hpp.

349 {
350 read_fun_ = fun;
351 return *this;
352 }

◆ ProcessPendingReads()

virtual void LibXR::ReadPort::ProcessPendingReads ( bool in_isr)
inlinevirtual

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 501 of file libxr_rw.hpp.

502 {
503 ASSERT(queue_data_ != nullptr);
504
505 if (in_isr)
506 {
507 auto is_busy = busy_.load(std::memory_order_relaxed);
508
509 if (is_busy == BusyState::Pending)
510 {
511 if (queue_data_->Size() >= info_.data.size_)
512 {
513 if (info_.data.size_ > 0)
514 {
515 auto ans = queue_data_->PopBatch(
516 reinterpret_cast<uint8_t *>(info_.data.addr_), info_.data.size_);
517 UNUSED(ans);
518 ASSERT(ans == ErrorCode::OK);
519 }
520 Finish(in_isr, ErrorCode::OK, info_, info_.data.size_);
521 }
522 }
523 else if (is_busy == BusyState::Idle)
524 {
525 busy_.store(BusyState::Event, std::memory_order_release);
526 }
527 }
528 else
529 {
530 LibXR::Mutex::LockGuard lock_guard(mutex_);
531 if (busy_.load(std::memory_order_relaxed) == BusyState::Pending)
532 {
533 if (queue_data_->Size() >= info_.data.size_)
534 {
535 if (info_.data.size_ > 0)
536 {
537 auto ans = queue_data_->PopBatch(
538 reinterpret_cast<uint8_t *>(info_.data.addr_), info_.data.size_);
539 UNUSED(ans);
540 ASSERT(ans == ErrorCode::OK);
541 }
542 Finish(in_isr, ErrorCode::OK, info_, info_.data.size_);
543 }
544 }
545 }
546 }
互斥锁的 RAII 机制封装 (RAII-style mechanism for automatic mutex management).
Definition mutex.hpp:65
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
更新读取操作的状态。 Updates the status of the read operation.
Definition libxr_rw.hpp:371
RawData data
Data buffer. 数据缓冲区。
Definition libxr_rw.hpp:255

◆ Readable()

bool LibXR::ReadPort::Readable ( )
inline

Checks if read operations are supported.

检查是否支持读取操作。

Definition at line 333 of file libxr_rw.hpp.

333{ return read_fun_ != nullptr; }

◆ Reset()

virtual void LibXR::ReadPort::Reset ( )
inlinevirtual

Resets the ReadPort.

重置ReadPort。

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

Definition at line 550 of file libxr_rw.hpp.

551 {
552 ASSERT(queue_data_ != nullptr);
553 Mutex::LockGuard lock_guard(mutex_);
554 queue_data_->Reset();
555 read_size_ = 0;
556 }
void Reset()
重置队列 / Resets the queue

◆ Size()

virtual size_t LibXR::ReadPort::Size ( )
inlinevirtual

获取当前队列的已使用大小。 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 325 of file libxr_rw.hpp.

326 {
327 ASSERT(queue_data_ != nullptr);
328 return queue_data_->Size();
329 }

Field Documentation

◆ busy_

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

Definition at line 284 of file libxr_rw.hpp.

284{BusyState::Idle};

◆ info_

ReadInfoBlock LibXR::ReadPort::info_

Definition at line 283 of file libxr_rw.hpp.

◆ mutex_

Mutex LibXR::ReadPort::mutex_

Definition at line 282 of file libxr_rw.hpp.

◆ queue_data_

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

Definition at line 280 of file libxr_rw.hpp.

◆ read_fun_

ReadFun LibXR::ReadPort::read_fun_ = nullptr

Definition at line 279 of file libxr_rw.hpp.

◆ read_size_

size_t LibXR::ReadPort::read_size_ = 0

Definition at line 281 of file libxr_rw.hpp.


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