libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
write_port.hpp
1#pragma once
2
3#include <atomic>
4#include <cstddef>
5#include <cstdint>
6#include <string_view>
7
8#include "operation.hpp"
9#include "queue.hpp"
10
11namespace LibXR
12{
13
19{
20 public:
21 // Exposed low-level state and helpers for the write-path core. Some members stay public
22 // because low-level libxr tests and backend glue inspect them directly. Keep the
23 // boundary explicit instead of introducing a fake private wall that tests cannot use.
24 // 写路径核心的低层状态与辅助类型。部分成员保持 public,
25 // 是因为 libxr 的底层测试与后端胶水层会直接检查它们。
26 // 这里保持显式边界即可,不做测试本身也用不上的“伪私有化”。
27
28 // Write BLOCK states:
29 // LOCKED = submit path owns queue mutation
30 // BLOCK_PUBLISHING = BLOCK submit path is publishing queue metadata
31 // BLOCK_WAITING = waiter armed, completion not claimed yet
32 // BLOCK_CLAIMED = final wakeup belongs to the waiter
33 // BLOCK_DETACHED = timeout detached the waiter
34 // RESETTING = fail-and-clear path owns queue mutation
35 // The same semaphore may be reused only after the previous BLOCK call
36 // returns and the port goes back to IDLE.
37 // 写 BLOCK 状态:
38 // LOCKED = 提交路径占有队列修改权
39 // BLOCK_PUBLISHING = BLOCK 提交路径正在发布队列元数据
40 // BLOCK_WAITING = waiter 已挂起,完成尚未 claim
41 // BLOCK_CLAIMED = 最终唤醒已经归 waiter 所有
42 // BLOCK_DETACHED = timeout 已把 waiter 分离
43 // RESETTING = fail-and-clear 路径占有队列修改权
44 // 同一个信号量只能在上一次 BLOCK 调用返回、端口回到 IDLE 后复用。
45 enum class BusyState : uint32_t
46 {
47 LOCKED =
48 0,
51 BLOCK_WAITING = 2,
54 3,
55 BLOCK_DETACHED = 4,
57 RESETTING = 5,
59 IDLE = UINT32_MAX
61 };
62
64 nullptr;
66 nullptr;
68 nullptr;
69 std::atomic<BusyState> busy_{
73
74 // Stream batch facade.
75 // Stream 负责一次批次的累积写入与提交。
76 class Stream
77 {
78 public:
86
92 ~Stream();
93
110 [[nodiscard]] ErrorCode Write(ConstRawData data);
111
118 [[nodiscard]] ErrorCode Write(std::string_view text)
119 {
120 return Write(ConstRawData{text.data(), text.size()});
121 }
122
129 Stream& operator<<(const ConstRawData& data);
130
146
152 [[nodiscard]] ErrorCode Acquire();
153
162 [[nodiscard]] size_t EmptySize() const
163 {
165 }
166
167 private:
177 [[nodiscard]] ErrorCode SubmitBuffered();
178
183 void Release();
184
188 0;
190 0;
193 bool owns_port_ = false;
195 };
196
213 WritePort(size_t queue_size = 3, size_t buffer_size = 128);
214
222 size_t EmptySize();
223
231 size_t Size();
232
240 bool Writable();
241
256
272 void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock& info);
273
285
304 ErrorCode operator()(ConstRawData data, WriteOperation& op, bool in_isr = false);
305
327 void FailAndClearAll(ErrorCode reason, bool in_isr);
328
343 ErrorCode CommitWrite(ConstRawData data, WriteOperation& op, bool pushed = false,
344 bool in_isr = false);
345};
346
347} // namespace LibXR
只读原始数据视图 / Immutable raw data view
单生产者单消费者无锁队列。
ErrorCode SubmitBuffered()
将当前已缓存批次提交给 WritePort。
ErrorCode Acquire()
为当前流批次获取一次可写入的端口所有权。
ErrorCode Commit()
手动提交已写入的数据到队列,并释放当前锁。
size_t batch_capacity_
当前批次可用的总容量 Total capacity reserved for the current batch
void Release()
将当前批次的端口所有权归还给 WritePort。
size_t EmptySize() const
获取当前批次还可追加的剩余字节数。
LibXR::WritePort * port_
写端口指针 Pointer to the WritePort
LibXR::WriteOperation op_
写操作对象 Write operation object
~Stream()
析构时自动提交已累积的数据并释放锁。
ErrorCode Write(ConstRawData data)
追加一个原始数据片段到当前流批次。
ErrorCode Write(std::string_view text)
追加一个文本片段到当前流批次。
Stream(LibXR::WritePort *port, LibXR::WriteOperation op)
构造流写入对象,并尝试锁定端口。
Stream & operator<<(const ConstRawData &data)
追加写入数据的语法糖,忽略返回状态并支持链式调用。
WritePort class for handling write operations.
size_t EmptySize()
获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.
void FailAndClearAll(ErrorCode reason, bool in_isr)
失败完成并清空当前所有挂起写操作。
size_t Size()
获取当前数据队列的已使用大小。 Gets the used size of the current data queue.
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info)
更新写入操作的状态。 Updates the status of the write operation.
ErrorCode block_result_
SPSCQueue< WriteInfoBlock > * queue_info_
Metadata queue for pending write batches. 挂起写批次的元数据队列。
WritePort(size_t queue_size=3, size_t buffer_size=128)
构造一个新的 WritePort 对象。 Constructs a new WritePort object.
WritePort & operator=(WriteFun fun)
赋值运算符重载,用于设置写入函数。 Overloaded assignment operator to set the write function.
std::atomic< BusyState > busy_
Shared submit/wait handoff state. 共享的提交/等待交接状态。
@ BLOCK_CLAIMED
Final wakeup belongs to the current waiter. 最终唤醒已归当前等待者所有。
@ LOCKED
Submission path owns queue mutation. 提交路径占有写队列/元数据修改权。
SPSCQueue< uint8_t > * queue_data_
Payload queue for pending write bytes. 挂起写入字节的数据队列。
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
WriteFun write_fun_
Driver/backend write entry. 底层驱动或后端写入入口。
ErrorCode CommitWrite(ConstRawData data, WriteOperation &op, bool pushed=false, bool in_isr=false)
提交写入操作。 Commits a write operation.
void MarkAsRunning(WriteOperation &op)
标记写入操作为运行中。 Marks the write operation as running.
ErrorCode operator()(ConstRawData data, WriteOperation &op, bool in_isr=false)
执行写入操作。 Performs a write operation.
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful
ErrorCode(* WriteFun)(WritePort &port, bool in_isr)
Function pointer type for write operations.