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 "lockfree_queue.hpp"
9#include "operation.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
63 WriteFun write_fun_ = nullptr;
65 nullptr;
67 nullptr;
68 std::atomic<BusyState> busy_{BusyState::IDLE};
70
71 // Stream batch facade.
72 // Stream 负责一次批次的累积写入与提交。
73 class Stream
74 {
75 public:
83
89 ~Stream();
90
107 [[nodiscard]] ErrorCode Write(ConstRawData data);
108
115 [[nodiscard]] ErrorCode Write(std::string_view text)
116 {
117 return Write(ConstRawData{text.data(), text.size()});
118 }
119
126 Stream& operator<<(const ConstRawData& data);
127
143
149 [[nodiscard]] ErrorCode Acquire();
150
159 [[nodiscard]] size_t EmptySize() const
160 {
162 }
163
164 private:
174 [[nodiscard]] ErrorCode SubmitBuffered();
175
180 void Release();
181
184 size_t batch_capacity_ = 0;
185 size_t buffered_size_ = 0;
186 bool owns_port_ = false;
187 };
188
205 WritePort(size_t queue_size = 3, size_t buffer_size = 128);
206
214 size_t EmptySize();
215
223 size_t Size();
224
232 bool Writable();
233
248
264 void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock& info);
265
277
296 ErrorCode operator()(ConstRawData data, WriteOperation& op, bool in_isr = false);
297
319 void FailAndClearAll(ErrorCode reason, bool in_isr);
320
335 ErrorCode CommitWrite(ConstRawData data, WriteOperation& op, bool pushed = false,
336 bool in_isr = false);
337};
338
339} // namespace LibXR
只读原始数据视图 / Immutable raw data view
无锁队列实现 / Lock-free queue implementation
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()
析构时自动提交已累积的数据并释放锁。
bool owns_port_
当前 Stream 是否持有该批次的端口所有权 Whether this Stream currently owns the batch
size_t buffered_size_
当前批次已追加到共享 data queue、但尚未发布对应元数据的字节数 Bytes already appended into the shared data queue for the curren...
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_
Final status for the current BLOCK write. 当前 BLOCK 写入的最终结果。
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. 提交路径占有写队列/元数据修改权。
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.
LockFreeQueue< uint8_t > * queue_data_
Payload queue for pending write bytes. 挂起写入字节的数据队列。
LockFreeQueue< WriteInfoBlock > * queue_info_
Metadata queue for pending write batches. 挂起写批次的元数据队列。
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.