libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::WritePort::Stream Class Reference

WritePort 的流式写入操作器,支持链式 << 操作和批量提交。 More...

#include <libxr_rw.hpp>

Collaboration diagram for LibXR::WritePort::Stream:
[legend]

Public Member Functions

 Stream (LibXR::WritePort *port, LibXR::WriteOperation op)
 构造流写入对象,并尝试锁定端口。
 
 ~Stream ()
 析构时自动提交已累积的数据并释放锁。
 
Streamoperator<< (const ConstRawData &data)
 追加写入数据,支持链式调用。
 
ErrorCode Commit ()
 手动提交已写入的数据到队列,并尝试续锁。
 

Private Attributes

LibXR::WritePortport_
 写端口指针 Pointer to the WritePort
 
LibXR::WriteOperation op_
 写操作对象 Write operation object
 
size_t cap_
 当前队列容量 Current queue capacity
 
size_t size_ = 0
 当前已写入但未提交的字节数 Bytes written but not yet committed
 
bool locked_ = false
 是否持有写锁 Whether write lock is held
 

Detailed Description

WritePort 的流式写入操作器,支持链式 << 操作和批量提交。

Stream-like writer for WritePort, supporting chainable << operations and batch commit.

构造时会尝试锁定 WritePort,并批量写入,减少碎片化写操作和队列压力。 Automatically acquires the WritePort lock (if possible), enabling batch writes to reduce fragmented write operations and queue pressure.

Definition at line 437 of file libxr_rw.hpp.

Constructor & Destructor Documentation

◆ Stream()

WritePort::Stream::Stream ( LibXR::WritePort * port,
LibXR::WriteOperation op )

构造流写入对象,并尝试锁定端口。

Constructs a Stream object and tries to acquire WritePort lock.

Parameters
port指向 WritePort 的指针 Pointer to WritePort.
op写操作对象(可重用)Write operation object (can be reused).

Definition at line 288 of file libxr_rw.cpp.

289 : port_(port), op_(op)
290{
291 LockState expected = LockState::UNLOCKED;
292 if (port_->lock_.compare_exchange_strong(expected, LockState::LOCKED))
293 {
294 if (port_->queue_info_->EmptySize() < 1)
295 {
296 locked_ = false;
297 port_->lock_.store(LockState::UNLOCKED, std::memory_order_release);
298 return;
299 }
300 locked_ = true;
301 cap_ = port_->queue_data_->EmptySize();
302 }
303}
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue
bool locked_
是否持有写锁 Whether write lock is held
Definition libxr_rw.hpp:481
size_t cap_
当前队列容量 Current queue capacity
Definition libxr_rw.hpp:479
LibXR::WritePort * port_
写端口指针 Pointer to the WritePort
Definition libxr_rw.hpp:477
LibXR::WriteOperation op_
写操作对象 Write operation object
Definition libxr_rw.hpp:478

◆ ~Stream()

WritePort::Stream::~Stream ( )

析构时自动提交已累积的数据并释放锁。

Destructor: automatically commits any accumulated data and releases the lock.

Definition at line 305 of file libxr_rw.cpp.

306{
307 if (locked_ && size_ > 0)
308 {
309 port_->queue_info_->Push(WriteInfoBlock{ConstRawData{nullptr, size_}, op_});
310 port_->CommitWrite({nullptr, size_}, op_, true);
311 }
312
313 if (locked_)
314 {
315 port_->lock_.store(LockState::UNLOCKED, std::memory_order_release);
316 }
317}
常量原始数据封装类。 A class for encapsulating constant raw data.
size_t size_
当前已写入但未提交的字节数 Bytes written but not yet committed
Definition libxr_rw.hpp:480
ErrorCode CommitWrite(ConstRawData data, WriteOperation &op, bool pushed=false, bool in_isr=false)
提交写入操作。 Commits a write operation.
Definition libxr_rw.cpp:225

Member Function Documentation

◆ Commit()

ErrorCode WritePort::Stream::Commit ( )

手动提交已写入的数据到队列,并尝试续锁。

Manually commit accumulated data to the queue, and try to extend the lock.

调用后已写入数据会立即入队,size 计数归零。适合周期性手动 flush。 After calling, written data is enqueued, size counter reset. Suitable for periodic manual flush.

Returns
返回操作的 ErrorCode,指示操作结果。 Returns an ErrorCode indicating the result of the operation.

Definition at line 353 of file libxr_rw.cpp.

354{
355 auto ans = ErrorCode::OK;
356
357 if (locked_ && size_ > 0)
358 {
359 ans = port_->queue_info_->Push(WriteInfoBlock{ConstRawData{nullptr, size_}, op_});
360 ASSERT(ans == ErrorCode::OK);
361 ans = port_->CommitWrite({nullptr, size_}, op_, true);
362 ASSERT(ans == ErrorCode::OK);
363 size_ = 0;
364 }
365
366 if (port_->queue_info_->EmptySize() < 1)
367 {
368 locked_ = false;
369 port_->lock_.store(LockState::UNLOCKED, std::memory_order_release);
370 }
371 else
372 {
373 cap_ = port_->queue_data_->EmptySize();
374 }
375
376 return ans;
377}

◆ operator<<()

WritePort::Stream & WritePort::Stream::operator<< ( const ConstRawData & data)

追加写入数据,支持链式调用。

Appends data for writing, supporting chain calls.

Parameters
data要写入的数据 Data to write.
Returns
返回自身引用 Enables chainable call.

Definition at line 319 of file libxr_rw.cpp.

320{
321 if (!locked_)
322 {
323 LockState expected = LockState::UNLOCKED;
324 if (port_->lock_.compare_exchange_strong(expected, LockState::LOCKED))
325 {
326 if (port_->queue_info_->EmptySize() < 1)
327 {
328 locked_ = false;
329 port_->lock_.store(LockState::UNLOCKED, std::memory_order_release);
330 return *this;
331 }
332 else
333 {
334 locked_ = true;
335 cap_ = port_->queue_data_->EmptySize();
336 }
337 }
338 else
339 {
340 return *this;
341 }
342 }
343 if (size_ + data.size_ <= cap_)
344 {
345 port_->queue_data_->PushBatch(reinterpret_cast<const uint8_t*>(data.addr_),
346 data.size_);
347 size_ += data.size_;
348 }
349
350 return *this;
351}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue

Field Documentation

◆ cap_

size_t LibXR::WritePort::Stream::cap_
private

当前队列容量 Current queue capacity

Definition at line 479 of file libxr_rw.hpp.

◆ locked_

bool LibXR::WritePort::Stream::locked_ = false
private

是否持有写锁 Whether write lock is held

Definition at line 481 of file libxr_rw.hpp.

◆ op_

LibXR::WriteOperation LibXR::WritePort::Stream::op_
private

写操作对象 Write operation object

Definition at line 478 of file libxr_rw.hpp.

◆ port_

LibXR::WritePort* LibXR::WritePort::Stream::port_
private

写端口指针 Pointer to the WritePort

Definition at line 477 of file libxr_rw.hpp.

◆ size_

size_t LibXR::WritePort::Stream::size_ = 0
private

当前已写入但未提交的字节数 Bytes written but not yet committed

Definition at line 480 of file libxr_rw.hpp.


The documentation for this class was generated from the following files: