libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
stdio.cpp
1#include "stdio.hpp"
2
3#include <limits>
4
5using namespace LibXR;
6
7// STDIO 编译格式桥接层 / STDIO compiled-format bridge.
8STDIO::CompiledSink::CompiledSink(WritePort::Stream& stream) : stream_(stream) {}
9
10ErrorCode STDIO::CompiledSink::Write(std::string_view chunk)
11{
12 if (saturated_)
13 {
14 return ErrorCode::OK;
15 }
16
17 auto ec = stream_.Acquire();
18 if (ec != ErrorCode::OK)
19 {
20 return ec;
21 }
22
23 size_t copy_size = chunk.size();
24 size_t stream_writable = stream_.EmptySize();
25 if (copy_size > stream_writable)
26 {
27 copy_size = stream_writable;
28 }
29
30 if (copy_size == 0)
31 {
32 saturated_ = true;
33 return ErrorCode::OK;
34 }
35
36 // copy_size <= chunk.size(); substr would link the libstdc++ exception support.
37 ec = stream_.Write(std::string_view(chunk.data(), copy_size));
38 if (ec != ErrorCode::OK)
39 {
40 return ec;
41 }
42
43 retained_size_ += copy_size;
44 if (copy_size != chunk.size() || stream_.EmptySize() == 0)
45 {
46 saturated_ = true;
47 }
48 return ErrorCode::OK;
49}
50
51bool STDIO::BeginWriteSession()
52{
53 if (!STDIO::write_ || !STDIO::write_->Writable())
54 {
55 return false;
56 }
57
58 if (!write_mutex_)
59 {
61 }
62
63 return write_mutex_->Lock() == ErrorCode::OK;
64}
65
66int STDIO::WriteCompiledToStream(WritePort::Stream& stream, void* context,
67 CompiledWriteFun write_fun)
68{
69 CompiledSink sink(stream);
70 auto ec = write_fun(context, sink);
71 return FinishWriteSession(stream, sink.RetainedSize(), ec);
72}
73
74int STDIO::WriteCompiledSession(void* context, CompiledWriteFun write_fun)
75{
76 DEV_ASSERT(write_mutex_ != nullptr);
77 DEV_ASSERT(write_fun != nullptr);
78
79 if (write_stream_ != nullptr)
80 {
81 return WriteCompiledToStream(*write_stream_, context, write_fun);
82 }
83
84 static WriteOperation op; // NOLINT
85 WritePort::Stream stream(write_, op);
86 return WriteCompiledToStream(stream, context, write_fun);
87}
88
89int STDIO::FinishWriteSession(WritePort::Stream& stream, size_t retained_size,
90 ErrorCode format_result)
91{
92 DEV_ASSERT(write_mutex_ != nullptr);
93
94 // 基于 Stream 的 STDIO 现在明确采用“前缀保留”语义:格式化失败时,
95 // 本会话已保留的字节仍然提交,不再尝试伪回滚。
96 // Stream-backed STDIO is now explicitly prefix-preserving on formatting
97 // failure: any bytes already retained in this session are committed instead
98 // of being pseudo-rolled-back.
99 auto ec = stream.Commit();
100
102
103 if (format_result != ErrorCode::OK || ec != ErrorCode::OK ||
104 retained_size > static_cast<size_t>(std::numeric_limits<int>::max()))
105 {
106 return -1;
107 }
108
109 return static_cast<int>(retained_size);
110}
互斥锁类,提供线程同步机制 Mutex class providing thread synchronization mechanisms.
Definition mutex.hpp:18
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 Lock the mutex, blocking if it is already locked.
Definition mutex.cpp:16
void Unlock()
解锁互斥锁 Unlock the mutex.
Definition mutex.cpp:40
static LibXR::WritePort::Stream * write_stream_
可选的外部托管写流 / Optional externally owned write stream.
Definition stdio.hpp:29
static LibXR::Mutex * write_mutex_
写入端口互斥锁 / Write port mutex.
Definition stdio.hpp:27
static WritePort * write_
写入端口 / Write port instance.
Definition stdio.hpp:26
合并多次写入的流接口 / Stream interface for batched writes.
ErrorCode Acquire()
尝试取得端口写入权 / Try to acquire the port's producer access.
ErrorCode Commit()
提交批次并释放写入权 / Submit the batch and release producer access.
size_t EmptySize() const
获取当前批次可追加的空间 / Get current free space for appending.
ErrorCode Write(ConstRawData data)
向当前批次追加数据 / Append data to the current batch.
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ OK
操作成功 / Operation successful