libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
driver.hpp File Reference

终端线程与轮询入口 / Thread and polling entry points for Terminal. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

static void ThreadFun (Terminal *term)
 在线程中读取输入并驱动终端 / Read input and drive the terminal in a thread.
 
static void TaskFun (Terminal *term)
 检查读取结果并推进终端输入 / Check read completion and advance terminal input.
 

Detailed Description

终端线程与轮询入口 / Thread and polling entry points for Terminal.

线程与轮询入口实现 / Thread and polling entry point implementations.

终端线程与轮询入口 / Thread and polling entry points for Terminal.

Definition in file driver.hpp.

Function Documentation

◆ TaskFun()

static void TaskFun ( Terminal * term)
static

检查读取结果并推进终端输入 / Check read completion and advance terminal input.

Parameters
term终端实例 / Terminal instance.
Note
首次调用发起读取;RUNNING 时返回,DONE 时解析并提交输出,再发起下次读取。 ERROR 时重新读取。解析和输出提交受 write_mutex_ 保护。 Starts a read on first use; returns for RUNNING. On DONE, parses and commits output, then starts the next read. On ERROR, restarts reading. Parsing and output submission hold write_mutex_.

Definition at line 45 of file driver.hpp.

46{
47 ReadOperation op(term->read_status_);
48
49 auto start_read = [&]()
50 {
51 term->request_read_size_ =
52 LibXR::min(LibXR::max(1u, term->read_port_->Size()), READ_BUFF_SIZE);
53 auto buffer = RawData(term->read_buff_, term->request_read_size_);
54 (*term->read_port_)(buffer, op);
55 };
56
57 while (true)
58 {
59 const auto status = term->read_status_.load(std::memory_order_acquire);
60 switch (status)
61 {
62 case ReadOperation::OperationPollingStatus::READY:
63 {
64 term->request_read_size_ =
65 LibXR::min(LibXR::max(1u, term->read_port_->Size()), READ_BUFF_SIZE);
66 auto buffer = RawData(term->read_buff_, term->request_read_size_);
67 (*term->read_port_)(buffer, op);
68 continue;
69 }
70 case ReadOperation::OperationPollingStatus::RUNNING:
71 return;
72 case ReadOperation::OperationPollingStatus::DONE:
73 {
74 term->write_mutex_->Lock();
75 auto buffer = RawData(term->read_buff_, term->request_read_size_);
76 term->Parse(buffer);
77 (void)term->write_stream_.Commit();
78 term->write_mutex_->Unlock();
79 start_read();
80 return;
81 }
82 case ReadOperation::OperationPollingStatus::ERROR:
83 {
84 start_read();
85 return;
86 }
87 }
88 }
89}
Operation< ErrorCode > ReadOperation
读操作的完成通知描述符 / Read completion descriptor.
constexpr auto min(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最小值 / Computes the minimum of two numbers
constexpr auto max(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最大值 / Computes the maximum of two numbers

◆ ThreadFun()

static void ThreadFun ( Terminal * term)
static

在线程中读取输入并驱动终端 / Read input and drive the terminal in a thread.

Parameters
term终端实例 / Terminal instance.
Note
无数据时通过零长度 BLOCK 读等待队列非空;取得输入后,在 write_mutex_ 保护下 解析并提交输出。 With no data, a zero-length BLOCK read waits for a nonempty queue. Received input is parsed and output committed under write_mutex_.

Definition at line 14 of file driver.hpp.

15{
16 Semaphore read_sem, write_sem;
17 ReadOperation op(read_sem);
18
19 term->write_op_ = WriteOperation(write_sem, 10);
20
21 while (true)
22 {
23 term->request_read_size_ = LibXR::min(term->read_port_->Size(), READ_BUFF_SIZE);
24 auto buffer = RawData(term->read_buff_, term->request_read_size_);
25
26 if ((*term->read_port_)(buffer, op) == ErrorCode::OK && term->request_read_size_ > 0)
27 {
28 term->write_mutex_->Lock();
29 term->Parse(buffer);
30 (void)term->write_stream_.Commit();
31 term->write_mutex_->Unlock();
32 }
33 }
34}
Operation< ErrorCode > WriteOperation
写操作的完成通知描述符 / Write completion descriptor.