10#include "libxr_cb.hpp"
11#include "libxr_def.hpp"
12#include "libxr_mem.hpp"
13#include "libxr_type.hpp"
14#include "lockfree_queue.hpp"
16#include "semaphore.hpp"
28template <
typename Args>
71 data.sem_info.sem = &sem;
72 data.sem_info.timeout = timeout;
82 data.callback = &callback;
92 data.status = &status;
108 case OperationType::CALLBACK:
111 case OperationType::BLOCK:
112 data.sem_info.sem = op.
data.sem_info.sem;
113 data.sem_info.timeout = op.
data.sem_info.timeout;
115 case OperationType::POLLING:
118 case OperationType::NONE:
138 case OperationType::CALLBACK:
139 data.callback = op.data.callback;
141 case OperationType::BLOCK:
142 data.sem_info.sem = op.data.sem_info.sem;
143 data.sem_info.timeout = op.data.sem_info.timeout;
145 case OperationType::POLLING:
146 data.status = op.data.status;
148 case OperationType::NONE:
164 typename InitOperation,
165 typename = std::enable_if_t<std::is_same_v<std::decay_t<InitOperation>,
Operation>>>
168 *
this = std::forward<InitOperation>(op);
177 template <
typename Status>
182 case OperationType::CALLBACK:
183 data.callback->Run(in_isr, std::forward<Status>(status));
185 case OperationType::BLOCK:
189 data.sem_info.sem->PostFromCallback(in_isr);
191 case OperationType::POLLING:
193 : OperationPollingStatus::ERROR;
195 case OperationType::NONE:
215 if (
type == OperationType::POLLING)
217 *
data.status = OperationPollingStatus::RUNNING;
251 enum class State : uint32_t
263 state_.store(State::PENDING, std::memory_order_release);
266 void Cancel() { state_.store(State::IDLE, std::memory_order_release); }
270 ASSERT(sem_ !=
nullptr);
271 auto wait_ans = sem_->
Wait(timeout);
274#ifdef LIBXR_DEBUG_BUILD
275 ASSERT(state_.load(std::memory_order_acquire) == State::CLAIMED);
277 state_.store(State::IDLE, std::memory_order_release);
281 State expected = State::PENDING;
282 if (state_.compare_exchange_strong(expected, State::DETACHED,
283 std::memory_order_acq_rel,
284 std::memory_order_acquire))
289 ASSERT(expected == State::CLAIMED || expected == State::DETACHED ||
290 expected == State::IDLE);
291 if (expected == State::DETACHED)
293 state_.store(State::IDLE, std::memory_order_release);
296 if (expected == State::IDLE)
301 auto finish_wait_ans = sem_->
Wait(UINT32_MAX);
302 UNUSED(finish_wait_ans);
304 state_.store(State::IDLE, std::memory_order_release);
310 ASSERT(sem_ !=
nullptr);
312 State expected = State::PENDING;
313 if (!state_.compare_exchange_strong(expected, State::CLAIMED,
314 std::memory_order_acq_rel,
315 std::memory_order_acquire))
317 ASSERT(expected == State::DETACHED || expected == State::IDLE);
318 if (expected == State::DETACHED)
320 expected = State::DETACHED;
321 (void)state_.compare_exchange_strong(expected, State::IDLE,
322 std::memory_order_acq_rel,
323 std::memory_order_acquire);
335 std::atomic<State> state_{State::IDLE};
648 WritePort(
size_t queue_size = 3,
size_t buffer_size = 128);
760 bool in_isr =
false);
776#if LIBXR_PRINTF_BUFFER_SIZE > 0
778 printf_buff_[LIBXR_PRINTF_BUFFER_SIZE];
791 static int Printf(
const char* fmt, ...);
Shared BLOCK waiter handoff for synchronous driver operations.
通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing
常量原始数据封装类。 A class for encapsulating constant raw data.
无锁队列实现 / Lock-free queue implementation
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Defines an operation with different execution modes.
Operation & operator=(const Operation &op)
Copy assignment operator.
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(Callback &callback)
Constructs a callback-based operation.
union LibXR::Operation::@5 data
Operation()
Default constructor, initializes with NONE type.
void UpdateStatus(bool in_isr, Status &&status)
Updates operation status based on type.
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
Operation(OperationPollingStatus &status)
Constructs a polling operation.
Operation(InitOperation &&op)
构造一个新的 Operation 对象(初始化操作)。 Constructs a new Operation object (initialization operation).
原始数据封装类。 A class for encapsulating raw 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.
STDIO interface for read/write ports.
static int Printf(const char *fmt,...)
Prints a formatted string to the write port (like printf).
static LibXR::Mutex * write_mutex_
Write port mutex. 写入端口互斥锁。
static ReadPort * read_
Read port instance. 读取端口。
static WritePort * write_
Write port instance. 写入端口。
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
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
size_t size_
当前已写入但未提交的字节数 Bytes written but not yet committed
ErrorCode Commit()
手动提交已写入的数据到队列,并释放当前锁。
size_t cap_
当前队列容量 Current queue capacity
LibXR::WritePort * port_
写端口指针 Pointer to the WritePort
LibXR::WriteOperation op_
写操作对象 Write operation object
~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.
@ 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.
Operation< ErrorCode > ReadOperation
Read operation type.
Operation< ErrorCode > WriteOperation
Write operation type.
Read information block structure.
RawData data
Data buffer. 数据缓冲区。
ReadOperation op
Read operation instance. 读取操作实例。
ConstRawData data
Data buffer. 数据缓冲区。
WriteOperation op
Write operation instance. 写入操作实例。