3#include "libxr_def.hpp"
20 ASSERT(queue_data_ !=
nullptr);
26 ASSERT(queue_data_ !=
nullptr);
27 return queue_data_->
Size();
40 if (info.
op.
type == ReadOperation::OperationType::BLOCK)
47 std::memory_order_acq_rel,
48 std::memory_order_acquire))
72 BusyState is_busy = busy_.load(std::memory_order_acquire);
83 auto readable_size = queue_data_->
Size();
85 if (readable_size >= data.
size_ && readable_size != 0)
96 if (op.
type != ReadOperation::OperationType::BLOCK)
107 auto ans = read_fun_(*
this, in_isr);
113 std::memory_order_acq_rel,
114 std::memory_order_acquire))
125 if (op.
type == ReadOperation::OperationType::BLOCK)
134 if (op.
type == ReadOperation::OperationType::BLOCK)
137 auto wait_ans = op.
data.sem_info.sem->
Wait(op.
data.sem_info.timeout);
142#ifdef LIBXR_DEBUG_BUILD
143 auto state = busy_.load(std::memory_order_acquire);
154 std::memory_order_acq_rel,
155 std::memory_order_acquire))
176 auto finish_wait_ans = op.
data.sem_info.sem->
Wait(UINT32_MAX);
177 UNUSED(finish_wait_ans);
195 ASSERT(queue_data_ !=
nullptr);
197 auto is_busy = busy_.load(std::memory_order_relaxed);
201 auto size = queue_data_->
Size();
202 if (size > 0 && size >= info_.
data.
size_)
204 if (info_.
op.
type == ReadOperation::OperationType::BLOCK)
210 std::memory_order_acq_rel,
211 std::memory_order_acquire))
237 ASSERT(queue_data_ !=
nullptr);
238 queue_data_->
Reset();
240 auto state = busy_.load(std::memory_order_acquire);
247 std::memory_order_acq_rel,
248 std::memory_order_acquire))
253 state = busy_.load(std::memory_order_acquire);
276 ASSERT(queue_data_ !=
nullptr);
282 ASSERT(queue_data_ !=
nullptr);
283 return queue_data_->
Size();
296 if (info.
op.
type == WriteOperation::OperationType::BLOCK)
304 std::memory_order_acq_rel,
305 std::memory_order_acquire))
316 std::memory_order_acq_rel,
317 std::memory_order_acquire);
333 if (op.
type != WriteOperation::OperationType::BLOCK)
342 std::memory_order_acq_rel,
343 std::memory_order_acquire))
359 if (!meta_pushed && queue_info_->EmptySize() < 1)
380 ans = queue_info_->Push(info);
387 if (op.
type == WriteOperation::OperationType::BLOCK)
394 ans = write_fun_(*
this, in_isr);
398 if (op.
type == WriteOperation::OperationType::BLOCK)
409 if (op.
type != WriteOperation::OperationType::BLOCK)
416 if (op.
type == WriteOperation::OperationType::BLOCK)
419 auto wait_ans = op.
data.sem_info.sem->
Wait(op.
data.sem_info.timeout);
424#ifdef LIBXR_DEBUG_BUILD
425 auto state = busy_.load(std::memory_order_acquire);
436 std::memory_order_acq_rel,
437 std::memory_order_acquire))
451 auto finish_wait_ans = op.
data.sem_info.sem->
Wait(UINT32_MAX);
452 UNUSED(finish_wait_ans);
469 ASSERT(queue_data_ !=
nullptr);
470 queue_data_->
Reset();
471 queue_info_->Reset();
473 auto state = busy_.load(std::memory_order_acquire);
480 std::memory_order_acq_rel,
481 std::memory_order_acquire))
486 state = busy_.load(std::memory_order_acquire);
499 : port_(port), op_(op)
503 std::memory_order_acq_rel,
504 std::memory_order_acquire))
509 if (
port_->queue_info_->EmptySize() < 1)
511 port_->busy_.store(BusyState::IDLE, std::memory_order_release);
521 if (locked_ && size_ > 0)
526 port_->CommitWrite({
nullptr, size_}, op_,
true);
527 if (op_.type == WriteOperation::OperationType::BLOCK)
546 std::memory_order_acq_rel,
547 std::memory_order_acquire))
552 if (port_->queue_info_->EmptySize() < 1)
559 cap_ = port_->queue_data_->EmptySize();
561 if (size_ + data.
size_ <= cap_)
563 auto ans = port_->queue_data_->PushBatch(
reinterpret_cast<const uint8_t*
>(data.
addr_),
576 if (locked_ && size_ > 0)
580 ans = port_->CommitWrite({
nullptr, size_}, op_,
true);
581 if (op_.type == WriteOperation::OperationType::BLOCK)
605#if LIBXR_PRINTF_BUFFER_SIZE > 0
620 int len = vsnprintf(STDIO::printf_buff_, LIBXR_PRINTF_BUFFER_SIZE, fmt, args);
628 if (
static_cast<size_t>(len) >= LIBXR_PRINTF_BUFFER_SIZE)
630 len = LIBXR_PRINTF_BUFFER_SIZE - 1;
633 ConstRawData data = {
reinterpret_cast<const uint8_t*
>(STDIO::printf_buff_),
634 static_cast<size_t>(len)};
638 if (write_stream_ ==
nullptr)
644 (*write_stream_) << data;
645 ans = write_stream_->Commit();
常量原始数据封装类。 A class for encapsulating constant raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
无锁队列实现 / Lock-free queue implementation
void Reset()
重置队列 / Resets the queue
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue
size_t Size() const
获取当前队列中的元素数量 / Returns the number of elements currently in the queue
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
互斥锁的 RAII 机制封装 (RAII-style mechanism for automatic mutex management).
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
union LibXR::Operation::@5 data
void UpdateStatus(bool in_isr, Status &&status)
Updates operation status based on type.
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
ReadPort class for handling read operations.
bool Readable()
Checks if read operations are supported.
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info)
更新读取操作的状态。 Updates the status of the read operation.
ReadPort(size_t buffer_size=128)
Constructs a ReadPort with queue sizes.
void Reset()
Resets the ReadPort.
ErrorCode block_result_
Final status for the current BLOCK read.
@ IDLE
No active waiter and no pending completion. 无等待者、无挂起完成。
size_t EmptySize()
获取队列的剩余可用空间。 Gets the remaining available space in the queue.
ErrorCode operator()(RawData data, ReadOperation &op, bool in_isr=false)
读取操作符重载,用于执行读取操作。 Overloaded function call operator to perform a read operation.
void ProcessPendingReads(bool in_isr)
Processes pending reads.
ReadPort & operator=(ReadFun fun)
赋值运算符重载,用于设置读取函数。 Overloaded assignment operator to set the read function.
size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the queue.
virtual void OnRxDequeue(bool)
RX 数据从软件队列成功出队后的通知。 Notification after bytes are popped from RX data queue.
void MarkAsRunning(ReadInfoBlock &info)
标记读取操作为运行中。 Marks the read operation as running.
static int Printf(const char *fmt,...)
Prints a formatted string to the write port (like printf).
static WritePort * write_
Write port instance. 写入端口。
void PostFromCallback(bool in_isr)
从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
WritePort 的流式写入操作器,支持链式 << 操作和批量提交。
bool locked_
是否持有写锁 Whether write lock is held
ErrorCode Commit()
手动提交已写入的数据到队列,并释放当前锁。
size_t cap_
当前队列容量 Current queue capacity
LibXR::WritePort * port_
写端口指针 Pointer to the WritePort
~Stream()
析构时自动提交已累积的数据并释放锁。
Stream(LibXR::WritePort *port, LibXR::WriteOperation op)
构造流写入对象,并尝试锁定端口。
Stream & operator<<(const ConstRawData &data)
追加写入数据,支持链式调用。
WritePort class for handling write operations.
size_t EmptySize()
获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.
size_t Size()
获取当前数据队列的已使用大小。 Gets the used size of the current data queue.
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info)
更新写入操作的状态。 Updates the status of the write operation.
ErrorCode block_result_
Final status for the current BLOCK write.
WritePort(size_t queue_size=3, size_t buffer_size=128)
构造一个新的 WritePort 对象。 Constructs a new WritePort object.
WritePort & operator=(WriteFun fun)
赋值运算符重载,用于设置写入函数。 Overloaded assignment operator to set the write function.
@ BLOCK_CLAIMED
Final wakeup belongs to the current waiter. 最终唤醒已归当前等待者所有。
@ LOCKED
Submission path owns queue mutation. 提交路径占有写队列/元数据修改权。
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
ErrorCode CommitWrite(ConstRawData data, WriteOperation &op, bool pushed=false, bool in_isr=false)
提交写入操作。 Commits a write operation.
void MarkAsRunning(WriteOperation &op)
标记写入操作为运行中。 Marks the write operation as running.
void Reset()
Resets the WritePort.
ErrorCode operator()(ConstRawData data, WriteOperation &op, bool in_isr=false)
执行写入操作。 Performs a write operation.
@ NOT_SUPPORT
不支持 | Not supported
@ OK
操作成功 | Operation successful
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read operations.
ErrorCode(* WriteFun)(WritePort &port, bool in_isr)
Function pointer type for write operations.
constexpr size_t CACHE_LINE_SIZE
缓存行大小 / Cache line size
Read information block structure.
RawData data
Data buffer. 数据缓冲区。
ReadOperation op
Read operation instance. 读取操作实例。
WriteOperation op
Write operation instance. 写入操作实例。