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

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

◆ ~Stream()

WritePort::Stream::~Stream ( )

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

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

Definition at line 362 of file libxr_rw.cpp.

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

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

418{
419 auto ans = ErrorCode::OK;
421 {
422 if (locked_ && size_ > 0)
423 {
424 ans = port_->queue_info_->Push(WriteInfoBlock{RawData{nullptr, size_}, op_});
425 ASSERT(ans == ErrorCode::OK);
426 ans = port_->CommitWrite({nullptr, size_}, op_, true);
427 ASSERT(ans == ErrorCode::OK);
428 size_ = 0;
429 }
430
431 if (port_->queue_info_->EmptySize() < 1)
432 {
433 locked_ = false;
434 port_->lock_.store(LockState::UNLOCKED, std::memory_order_release);
435 }
436 else
437 {
438 cap_ = port_->queue_data_->EmptySize();
439 }
440 }
441
442 return ans;
443}

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

377{
379 {
380 (*port_)(data, op_);
381 }
382 else
383 {
384 if (!locked_)
385 {
386 LockState expected = LockState::UNLOCKED;
387 if (port_->lock_.compare_exchange_strong(expected, LockState::LOCKED))
388 {
389 if (port_->queue_info_->EmptySize() < 1)
390 {
391 locked_ = false;
392 port_->lock_.store(LockState::UNLOCKED, std::memory_order_release);
393 return *this;
394 }
395 else
396 {
397 locked_ = true;
398 cap_ = port_->queue_data_->EmptySize();
399 }
400 }
401 else
402 {
403 return *this;
404 }
405 }
406 if (size_ + data.size_ <= cap_)
407 {
408 port_->queue_data_->PushBatch(reinterpret_cast<const uint8_t*>(data.addr_),
409 data.size_);
410 size_ += data.size_;
411 }
412 }
413
414 return *this;
415}
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 470 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 473 of file libxr_rw.hpp.

◆ locked_

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

是否持有写锁 Whether write lock is held

Definition at line 472 of file libxr_rw.hpp.

◆ op_

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

写操作对象 Write operation object

Definition at line 469 of file libxr_rw.hpp.

◆ port_

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

写端口指针 Pointer to the WritePort

Definition at line 468 of file libxr_rw.hpp.

◆ size_

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

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

Definition at line 471 of file libxr_rw.hpp.


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