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
 
bool fallback_to_normal_write_ = false
 

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 427 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 338 of file libxr_rw.cpp.

339 : port_(port), op_(op)
340{
341 if (!port->queue_data_ || !port->Writable())
342 {
344 return;
345 }
346 LockState expected = LockState::UNLOCKED;
347 if (port_->lock_.compare_exchange_strong(expected, LockState::LOCKED))
348 {
349 if (port_->queue_info_->EmptySize() < 1)
350 {
351 locked_ = false;
352 port_->lock_.store(LockState::UNLOCKED, std::memory_order_release);
353 return;
354 }
355 locked_ = true;
356 cap_ = port_->queue_data_->EmptySize();
357 }
358}
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue
bool locked_
是否持有写锁 Whether write lock is held
Definition libxr_rw.hpp:471
size_t cap_
当前队列容量 Current queue capacity
Definition libxr_rw.hpp:469
LibXR::WritePort * port_
写端口指针 Pointer to the WritePort
Definition libxr_rw.hpp:467
LibXR::WriteOperation op_
写操作对象 Write operation object
Definition libxr_rw.hpp:468
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
Definition libxr_rw.cpp:200

◆ ~Stream()

WritePort::Stream::~Stream ( )

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

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

Definition at line 360 of file libxr_rw.cpp.

361{
362 if (locked_ && size_ > 0)
363 {
364 port_->queue_info_->Push(WriteInfoBlock{RawData{nullptr, size_}, op_});
365 port_->CommitWrite({nullptr, size_}, op_, true);
366 port_->lock_.store(LockState::UNLOCKED, std::memory_order_release);
367 }
368}
原始数据封装类。 A class for encapsulating raw data.
size_t size_
当前已写入但未提交的字节数 Bytes written but not yet committed
Definition libxr_rw.hpp:470

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 402 of file libxr_rw.cpp.

403{
404 auto ans = ErrorCode::OK;
406 {
407 if (locked_ && size_ > 0)
408 {
409 port_->queue_info_->Push(WriteInfoBlock{RawData{nullptr, size_}, op_});
410 ans = port_->CommitWrite({nullptr, size_}, op_, true);
411 size_ = 0;
412 }
413
414 if (port_->queue_info_->EmptySize() < 1)
415 {
416 locked_ = false;
417 port_->lock_.store(LockState::UNLOCKED, std::memory_order_release);
418 }
419 else
420 {
421 cap_ = port_->queue_data_->EmptySize();
422 }
423 }
424
425 return ans;
426}

◆ 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 370 of file libxr_rw.cpp.

371{
373 {
374 (*port_)(data, op_);
375 }
376 else
377 {
378 if (!locked_)
379 {
380 LockState expected = LockState::UNLOCKED;
381 if (port_->lock_.compare_exchange_strong(expected, LockState::LOCKED))
382 {
383 locked_ = true;
384 cap_ = port_->queue_data_->EmptySize();
385 }
386 else
387 {
388 return *this;
389 }
390 }
391 if (size_ + data.size_ <= cap_)
392 {
393 port_->queue_data_->PushBatch(reinterpret_cast<const uint8_t*>(data.addr_),
394 data.size_);
395 size_ += data.size_;
396 }
397 }
398
399 return *this;
400}
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 469 of file libxr_rw.hpp.

◆ fallback_to_normal_write_

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

回退为普通写模式(不可批量) Fallback to normal write (if batch not supported)

Definition at line 472 of file libxr_rw.hpp.

◆ locked_

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

是否持有写锁 Whether write lock is held

Definition at line 471 of file libxr_rw.hpp.

◆ op_

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

写操作对象 Write operation object

Definition at line 468 of file libxr_rw.hpp.

◆ port_

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

写端口指针 Pointer to the WritePort

Definition at line 467 of file libxr_rw.hpp.

◆ size_

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

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

Definition at line 470 of file libxr_rw.hpp.


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