9 : queue_data_(buffer_size > 0 ? new(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
17 ASSERT(queue_data_ !=
nullptr);
23 ASSERT(queue_data_ !=
nullptr);
24 return queue_data_->
Size();
38 busy_.store(BusyState::Idle, std::memory_order_release);
50 BusyState is_busy = busy_.load(std::memory_order_relaxed);
52 if (is_busy == BusyState::Pending)
55 return ErrorCode::BUSY;
60 busy_.store(BusyState::Idle, std::memory_order_release);
62 if (queue_data_ !=
nullptr)
64 auto readable_size = queue_data_->
Size();
66 if (readable_size >= data.
size_ && readable_size != 0)
71 read_size_ = data.
size_;
72 ASSERT(ans == ErrorCode::OK);
73 if (op.
type != ReadOperation::OperationType::BLOCK)
86 auto ans = read_fun_(*
this);
88 if (ans != ErrorCode::OK)
90 BusyState expected = BusyState::Idle;
91 if (busy_.compare_exchange_strong(expected, BusyState::Pending,
92 std::memory_order_acq_rel,
93 std::memory_order_acquire))
99 expected = BusyState::Pending;
105 read_size_ = data.
size_;
106 if (op.
type != ReadOperation::OperationType::BLOCK)
111 return ErrorCode::OK;
117 if (op.
type == ReadOperation::OperationType::BLOCK)
119 return op.
data.sem_info.sem->
Wait(op.
data.sem_info.timeout);
123 return ErrorCode::OK;
128 return ErrorCode::NOT_SUPPORT;
134 ASSERT(queue_data_ !=
nullptr);
138 auto is_busy = busy_.load(std::memory_order_relaxed);
140 if (is_busy == BusyState::Pending)
149 ASSERT(ans == ErrorCode::OK);
154 else if (is_busy == BusyState::Idle)
156 busy_.store(BusyState::Event, std::memory_order_release);
162 if (busy_.load(std::memory_order_relaxed) == BusyState::Pending)
171 ASSERT(ans == ErrorCode::OK);
181 ASSERT(queue_data_ !=
nullptr);
183 queue_data_->
Reset();
188 : queue_info_(new(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
190 queue_data_(buffer_size > 0 ? new(std::align_val_t(LIBXR_CACHE_LINE_SIZE))
198 ASSERT(queue_data_ !=
nullptr);
204 ASSERT(queue_data_ !=
nullptr);
205 return queue_data_->
Size();
219 info.op.
UpdateStatus(in_isr, std::forward<ErrorCode>(ans));
231 if (op.
type != WriteOperation::OperationType::BLOCK)
235 return ErrorCode::OK;
240 if (queue_info_->EmptySize() < 1)
243 return ErrorCode::FULL;
251 return ErrorCode::FULL;
254 auto ans = queue_data_->
PushBatch(
reinterpret_cast<const uint8_t*
>(data.
addr_),
257 ASSERT(ans == ErrorCode::OK);
260 ans = queue_info_->Push(info);
262 ASSERT(ans == ErrorCode::OK);
266 ans = write_fun_(*
this);
270 if (ans == ErrorCode::OK)
272 write_size_ = data.
size_;
273 if (op.
type != WriteOperation::OperationType::BLOCK)
277 return ErrorCode::OK;
280 if (op.
type == WriteOperation::OperationType::BLOCK)
282 return op.
data.sem_info.sem->
Wait(op.
data.sem_info.timeout);
285 return ErrorCode::OK;
290 auto ans = queue_info_->Push(info);
292 ASSERT(ans == ErrorCode::OK);
296 ans = write_fun_(*
this);
300 if (ans == ErrorCode::OK)
302 write_size_ = data.
size_;
303 if (op.
type != WriteOperation::OperationType::BLOCK)
307 return ErrorCode::OK;
310 if (op.
type == WriteOperation::OperationType::BLOCK)
312 return op.
data.sem_info.sem->
Wait(op.
data.sem_info.timeout);
316 return ErrorCode::OK;
322 return ErrorCode::NOT_SUPPORT;
328 ASSERT(queue_data_ !=
nullptr);
330 queue_info_->Reset();
331 queue_data_->
Reset();
337#if LIBXR_PRINTF_BUFFER_SIZE > 0
349 int len = vsnprintf(STDIO::printf_buff_, LIBXR_PRINTF_BUFFER_SIZE, fmt, args);
357 if (
static_cast<size_t>(len) >= LIBXR_PRINTF_BUFFER_SIZE)
359 len = LIBXR_PRINTF_BUFFER_SIZE - 1;
362 ConstRawData data = {
reinterpret_cast<const uint8_t*
>(STDIO::printf_buff_),
363 static_cast<size_t>(len)};
366 return static_cast<int>(
STDIO::write_->operator()(data, op));
常量原始数据封装类。 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).
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
void Unlock()
解锁互斥锁 (Unlock the mutex).
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
void UpdateStatus(bool in_isr, Status &&...status)
Updates operation status based on type.
union LibXR::Operation::@4 data
原始数据封装类。 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.
ReadPort(size_t buffer_size=128)
Constructs a ReadPort with queue sizes.
void Finish(bool in_isr, ErrorCode ans, ReadInfoBlock &info, uint32_t size)
更新读取操作的状态。 Updates the status of the read operation.
virtual void Reset()
Resets the ReadPort.
virtual size_t EmptySize()
获取队列的剩余可用空间。 Gets the remaining available space in the queue.
ErrorCode operator()(RawData data, ReadOperation &op)
读取操作符重载,用于执行读取操作。 Overloaded function call operator to perform a read operation.
virtual void ProcessPendingReads(bool in_isr)
Processes pending reads.
ReadPort & operator=(ReadFun fun)
赋值运算符重载,用于设置读取函数。 Overloaded assignment operator to set the read function.
virtual size_t Size()
获取当前队列的已使用大小。 Gets the currently used size of the 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. 写入端口。
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
WritePort class for handling write operations.
virtual size_t EmptySize()
获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.
virtual size_t Size()
获取当前数据队列的已使用大小。 Gets the used size of the current data queue.
ErrorCode operator()(ConstRawData data, WriteOperation &op)
执行写入操作。 Performs a write operation.
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.
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
void MarkAsRunning(WriteOperation &op)
标记写入操作为运行中。 Marks the write operation as running.
virtual void Reset()
Resets the WritePort.
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info, uint32_t size)
更新写入操作的状态。 Updates the status of the write operation.
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Read information block structure.
RawData data
Data buffer. 数据缓冲区。
ReadOperation op
Read operation instance. 读取操作实例。