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

499 : port_(port), op_(op)
500{
501 BusyState expected = BusyState::IDLE;
502 if (!port_->busy_.compare_exchange_strong(expected, BusyState::LOCKED,
503 std::memory_order_acq_rel,
504 std::memory_order_acquire))
505 {
506 return;
507 }
508
509 if (port_->queue_info_->EmptySize() < 1)
510 {
511 port_->busy_.store(BusyState::IDLE, std::memory_order_release);
512 return;
513 }
514
515 locked_ = true;
516 cap_ = port_->queue_data_->EmptySize();
517}
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue
bool locked_
是否持有写锁 Whether write lock is held
Definition libxr_rw.hpp:629
size_t cap_
当前队列容量 Current queue capacity
Definition libxr_rw.hpp:627
LibXR::WritePort * port_
写端口指针 Pointer to the WritePort
Definition libxr_rw.hpp:625
LibXR::WriteOperation op_
写操作对象 Write operation object
Definition libxr_rw.hpp:626
@ LOCKED
Submission path owns queue mutation. 提交路径占有写队列/元数据修改权。

◆ ~Stream()

WritePort::Stream::~Stream ( )

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

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

Definition at line 519 of file libxr_rw.cpp.

520{
521 if (locked_ && size_ > 0)
522 {
523 auto ans =
524 port_->queue_info_->Push(WriteInfoBlock{ConstRawData{nullptr, size_}, op_});
525 ASSERT(ans == ErrorCode::OK);
526 port_->CommitWrite({nullptr, size_}, op_, true);
527 if (op_.type == WriteOperation::OperationType::BLOCK)
528 {
529 // WritePort now owns the BLOCK wait/finish state machine.
530 locked_ = false;
531 }
532 }
533
534 if (locked_)
535 {
536 port_->busy_.store(BusyState::IDLE, std::memory_order_release);
537 }
538}
常量原始数据封装类。 A class for encapsulating constant raw data.
OperationType type
Definition libxr_rw.hpp:237
size_t size_
当前已写入但未提交的字节数 Bytes written but not yet committed
Definition libxr_rw.hpp:628
ErrorCode CommitWrite(ConstRawData data, WriteOperation &op, bool pushed=false, bool in_isr=false)
提交写入操作。 Commits a write operation.
Definition libxr_rw.cpp:356
@ OK
操作成功 | Operation successful

Member Function Documentation

◆ Commit()

ErrorCode WritePort::Stream::Commit ( )

手动提交已写入的数据到队列,并释放当前锁。

Manually commit accumulated data to the queue, then release the current 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 572 of file libxr_rw.cpp.

573{
574 auto ans = ErrorCode::OK;
575
576 if (locked_ && size_ > 0)
577 {
578 ans = port_->queue_info_->Push(WriteInfoBlock{ConstRawData{nullptr, size_}, op_});
579 ASSERT(ans == ErrorCode::OK);
580 ans = port_->CommitWrite({nullptr, size_}, op_, true);
581 if (op_.type == WriteOperation::OperationType::BLOCK)
582 {
583 // WritePort will release busy_ after the BLOCK handoff completes.
584 size_ = 0;
585 locked_ = false;
586 return ans;
587 }
588
589 ASSERT(ans == ErrorCode::OK);
590 size_ = 0;
591 }
592
593 if (locked_)
594 {
595 locked_ = false;
596 port_->busy_.store(BusyState::IDLE, std::memory_order_release);
597 }
598
599 return ans;
600}

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

541{
542 if (!locked_)
543 {
544 BusyState expected = BusyState::IDLE;
545 if (!port_->busy_.compare_exchange_strong(expected, BusyState::LOCKED,
546 std::memory_order_acq_rel,
547 std::memory_order_acquire))
548 {
549 return *this;
550 }
551
552 if (port_->queue_info_->EmptySize() < 1)
553 {
554 port_->busy_.store(BusyState::IDLE, std::memory_order_release);
555 return *this;
556 }
557
558 locked_ = true;
559 cap_ = port_->queue_data_->EmptySize();
560 }
561 if (size_ + data.size_ <= cap_)
562 {
563 auto ans = port_->queue_data_->PushBatch(reinterpret_cast<const uint8_t*>(data.addr_),
564 data.size_);
565 ASSERT(ans == ErrorCode::OK);
566 size_ += data.size_;
567 }
568
569 return *this;
570}
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 627 of file libxr_rw.hpp.

◆ locked_

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

是否持有写锁 Whether write lock is held

Definition at line 629 of file libxr_rw.hpp.

◆ op_

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

写操作对象 Write operation object

Definition at line 626 of file libxr_rw.hpp.

◆ port_

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

写端口指针 Pointer to the WritePort

Definition at line 625 of file libxr_rw.hpp.

◆ size_

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

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

Definition at line 628 of file libxr_rw.hpp.


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