9#include "chunk_queue.hpp"
10#include "libxr_cb.hpp"
11#include "libxr_def.hpp"
12#include "libxr_type.hpp"
13#include "lockfree_queue.hpp"
15#include "semaphore.hpp"
27template <
typename...
Args>
62 data.sem_info.sem = &sem;
63 data.sem_info.timeout = timeout;
73 data.callback = &callback;
83 data.status = &status;
99 case OperationType::CALLBACK:
102 case OperationType::BLOCK:
103 data.sem_info.sem = op.
data.sem_info.sem;
104 data.sem_info.timeout = op.
data.sem_info.timeout;
106 case OperationType::POLLING:
109 case OperationType::NONE:
129 case OperationType::CALLBACK:
130 data.callback = op.data.callback;
132 case OperationType::BLOCK:
133 data.sem_info.sem = op.data.sem_info.sem;
134 data.sem_info.timeout = op.data.sem_info.timeout;
136 case OperationType::POLLING:
137 data.status = op.data.status;
139 case OperationType::NONE:
150 case OperationType::CALLBACK:
153 case OperationType::BLOCK:
157 case OperationType::POLLING:
160 case OperationType::NONE:
184 case OperationType::CALLBACK:
187 case OperationType::BLOCK:
188 data.sem_info.sem = op.
data.sem_info.sem;
189 data.sem_info.timeout = op.
data.sem_info.timeout;
191 case OperationType::POLLING:
194 case OperationType::NONE:
209 case OperationType::CALLBACK:
212 case OperationType::BLOCK:
215 case OperationType::POLLING:
216 *
data.status = OperationPollingStatus::DONE;
218 case OperationType::NONE:
238 if (
type == OperationType::POLLING)
240 *
data.status = OperationPollingStatus::RUNNING;
314 size_t read_size_ = 0;
434 if (op.
type == ReadOperation::OperationType::BLOCK)
436 return op.
data.sem_info.sem->
Wait(op.
data.sem_info.timeout);
438 return ErrorCode::OK;
444 if (queue_block_->
Push(
block) != ErrorCode::OK)
446 return ErrorCode::FULL;
449 auto ans = read_fun_(*
this);
450 if (op.
type == ReadOperation::OperationType::BLOCK)
452 return op.
data.sem_info.sem->
Wait(op.
data.sem_info.timeout);
461 return ErrorCode::NOT_SUPPORT;
472 while (queue_block_->
Peek(
block) == ErrorCode::OK)
474 if (queue_data_->
Size() >=
block.data_.size_)
477 read_size_ =
block.data_.size_;
478 block.op_.UpdateStatus(
true, ErrorCode::OK);
492 queue_block_->
Reset();
493 queue_data_->
Reset();
514 size_t write_size_ = 0;
639 if (op.
type == WriteOperation::OperationType::BLOCK)
641 return op.
data.sem_info.sem->
Wait(op.
data.sem_info.timeout);
643 return ErrorCode::OK;
650 return ErrorCode::FULL;
659 ans = write_fun_(*
this);
660 if (op.
type == WriteOperation::OperationType::BLOCK)
662 return op.
data.sem_info.sem->
Wait(op.
data.sem_info.timeout);
671 return ErrorCode::NOT_SUPPORT;
679 queue_info_->
Reset();
680 queue_data_->
Reset();
695#if LIBXR_PRINTF_BUFFER_SIZE > 0
712#if LIBXR_PRINTF_BUFFER_SIZE > 0
738 static_cast<size_t>(
len)};
741 return static_cast<int>(
STDIO::write_->operator()(data, op));
基础队列类,提供固定大小的循环缓冲区 (Base queue class providing a fixed-size circular buffer).
size_t Size()
获取队列中的元素个数 (Get the number of elements in the queue).
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
void Reset()
重置队列,清空所有数据 (Reset the queue and clear all data).
size_t EmptySize()
获取队列的空闲空间 (Get the available space in the queue).
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
常量原始数据封装类。 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 Pop(ElementData &item)
从队列中弹出数据 / Pops data from the queue
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
ErrorCode Push(ElementData &&item)
向队列中推入数据 / Pushes data 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 Peek(Data &item)
获取队列头部数据但不弹出 / Retrieves the front data of the queue without popping
互斥锁的 RAII 机制封装 (RAII-style mechanism for automatic mutex management).
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Defines an operation with different execution modes.
Operation & operator=(const Operation &op)
Copy assignment operator.
void UpdateStatus(bool in_isr, Args &&...args)
Updates operation status based on type.
Operation(Semaphore &sem, uint32_t timeout=UINT32_MAX)
Constructs a blocking operation with a semaphore and timeout.
Operation & operator=(Operation &&op) noexcept
Move assignment operator.
Operation()
Default constructor, initializes with NONE type.
Operation(Callback< Args... > &callback)
Constructs a callback-based operation.
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
union LibXR::Operation::@4 data
Operation(OperationPollingStatus &status)
Constructs a polling operation.
Operation(Operation &&op) noexcept
构造一个新的 Operation 对象(移动构造函数)。 Constructs a new Operation object (move constructor).
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
Read information block structure.
ReadOperation op_
Read operation instance. 读取操作实例。
ReadInfoBlock()
Default constructor.
ReadInfoBlock(RawData data, ReadOperation &&op)
Constructs a ReadInfoBlock.
RawData data_
Data buffer. 数据缓冲区。
ReadPort class for handling read operations.
ReadPort & operator=(ReadFun fun)
赋值运算符重载,用于设置读取函数。 Overloaded assignment operator to set the read function.
void ProcessPendingReads()
Processes pending reads.
size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the queue.
void Reset()
Resets the ReadPort.
bool Readable()
Checks if read operations are supported.
void UpdateStatus(ReadInfoBlock &info)
标记读取操作为运行中。 Marks the read operation as running.
size_t EmptySize()
获取队列的剩余可用空间。 Gets the remaining available space in the queue.
ReadPort(size_t queue_size=3, size_t buffer_size=128)
Constructs a ReadPort with queue sizes.
void UpdateStatus(bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
更新读取操作的状态。 Updates the status of the read operation.
ErrorCode operator()(RawData data, ReadOperation &op)
读取操作符重载,用于执行读取操作。 Overloaded function call operator to perform a read operation.
STDIO interface for read/write ports.
static ReadPort * read_
Read port instance. 读取端口。
static WritePort * write_
Write port instance. 写入端口。
static int Printf(const char *fmt,...)
Prints a formatted string to the write port (like printf).
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
WritePort class for handling write operations.
size_t EmptySize()
获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.
void UpdateStatus(WriteOperation &op)
标记写入操作为运行中。 Marks the write operation as running.
WritePort(size_t queue_size=3, size_t block_size=128)
构造一个新的 WritePort 对象。 Constructs a new WritePort object.
ErrorCode operator()(ConstRawData data, WriteOperation &op)
执行写入操作。 Performs a write operation.
WritePort & operator=(WriteFun fun)
赋值运算符重载,用于设置写入函数。 Overloaded assignment operator to set the write function.
size_t Size()
获取当前数据队列的已使用大小。 Gets the used size of the current data queue.
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
void UpdateStatus(bool in_isr, ErrorCode ans, WriteOperation &op, uint32_t size)
更新写入操作的状态。 Updates the status of the write operation.
void Reset()
Resets the WritePort.
LibXR Color Control Library / LibXR终端颜色控制库
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Operation< ErrorCode > ReadOperation
Read operation type.
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值
Operation< ErrorCode > WriteOperation
Write operation type.