libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
read_port.hpp
1#pragma once
2
3#include <atomic>
4#include <cstddef>
5#include <cstdint>
6
7#include "lockfree_queue.hpp"
8#include "operation.hpp"
9
10namespace LibXR
11{
12
18{
19 public:
20 // Exposed low-level state and helpers for the read-path core. Some members stay public
21 // because low-level libxr tests and driver glue inspect them directly.
22 // 读路径核心的低层状态与辅助类型。部分成员保持 public,
23 // 是因为 libxr 的底层测试与驱动胶水层会直接检查它们。
24
25 // Read BLOCK states:
26 // PENDING = waiting for queue-fed completion after read_fun_ was notified
27 // BLOCK_CLAIMED = wakeup now belongs to the waiter
28 // BLOCK_DETACHED = timeout detached the waiter
29 // The same semaphore may be reused only after the previous BLOCK call
30 // returns and the port goes back to IDLE.
31 // 读 BLOCK 状态:
32 // PENDING = 已通知 read_fun_,等待队列侧完成
33 // BLOCK_CLAIMED = 唤醒已经归当前 waiter 所有
34 // BLOCK_DETACHED = timeout 已把 waiter 分离
35 // 同一个信号量只能在上一次 BLOCK 调用返回、端口回到 IDLE 后复用。
36 enum class BusyState : uint32_t
37 {
38 IDLE = 0,
39 PENDING = 1,
41 BLOCK_CLAIMED = 2,
43 BLOCK_DETACHED = 3,
45 EVENT = UINT32_MAX
47 };
48
49 ReadFun read_fun_ = nullptr;
52 std::atomic<BusyState> busy_{BusyState::IDLE};
54
64 ReadPort(size_t buffer_size = 128);
65
76 size_t EmptySize();
77
88 size_t Size();
89
92 bool Readable();
93
108
120 void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock& info);
121
132 void MarkAsRunning(ReadInfoBlock& info);
133
158 ErrorCode operator()(RawData data, ReadOperation& op, bool in_isr = false);
159
167 virtual void OnRxDequeue(bool) {}
168
176 void ProcessPendingReads(bool in_isr);
177
193 void FailAndClearAll(ErrorCode reason, bool in_isr);
194};
195
196} // namespace LibXR
无锁队列实现 / Lock-free queue implementation
可写原始数据视图 / Mutable raw data view
ReadPort class for handling read operations.
Definition read_port.hpp:18
bool Readable()
Checks if read operations are supported.
Definition read_port.cpp:26
ReadFun read_fun_
Driver/backend read notification entry. 底层驱动或后端读取通知入口。
Definition read_port.hpp:49
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info)
完成已由队列路径认领的读取操作。 Completes a read operation already claimed by the queue path.
Definition read_port.cpp:34
ReadPort(size_t buffer_size=128)
Constructs a ReadPort with queue sizes.
Definition read_port.cpp:7
ErrorCode block_result_
Final status for the current BLOCK read.
Definition read_port.hpp:53
@ IDLE
No active waiter and no pending completion. 无等待者、无挂起完成。
std::atomic< BusyState > busy_
Shared read-progress handoff state. 共享的读进度交接状态。
Definition read_port.hpp:52
size_t EmptySize()
获取队列的剩余可用空间。 Gets the remaining available space in the queue.
Definition read_port.cpp:14
LockFreeQueue< uint8_t > * queue_data_
RX payload queue. 接收数据字节队列。
Definition read_port.hpp:50
ErrorCode operator()(RawData data, ReadOperation &op, bool in_isr=false)
读取操作符重载,用于执行读取操作。 Overloaded function call operator to perform a read operation.
Definition read_port.cpp:54
void ProcessPendingReads(bool in_isr)
Processes pending reads.
ReadPort & operator=(ReadFun fun)
赋值运算符重载,用于设置读取函数。 Overloaded assignment operator to set the read function.
Definition read_port.cpp:28
size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the queue.
Definition read_port.cpp:20
void FailAndClearAll(ErrorCode reason, bool in_isr)
失败完成并清空当前所有挂起读操作。
virtual void OnRxDequeue(bool)
RX 数据从软件队列成功出队后的通知。 Notification after bytes are popped from RX data queue.
ReadInfoBlock info_
In-flight read request metadata. 当前在途读取请求的元数据。
Definition read_port.hpp:51
void MarkAsRunning(ReadInfoBlock &info)
标记读取操作为运行中。 Marks the read operation as running.
Definition read_port.cpp:52
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read notifications.
Read information block structure.