9#include "libxr_def.hpp"
10#include "libxr_type.hpp"
11#include "semaphore.hpp"
23template <
typename Args>
78 Operation(std::atomic<OperationPollingStatus>& status)
110 template <typename Status>
111 void UpdateStatus(
bool in_isr, Status&& status)
115 case OperationType::CALLBACK:
116 data.callback->Run(in_isr, std::forward<Status>(status));
118 case OperationType::BLOCK:
121 data.sem_info.sem->PostFromCallback(in_isr);
123 case OperationType::POLLING:
124 data.status->store((status ==
ErrorCode::OK) ? OperationPollingStatus::DONE
125 : OperationPollingStatus::ERROR,
126 std::memory_order_release);
128 case OperationType::NONE:
140 if (type == OperationType::POLLING)
142 data.status->store(OperationPollingStatus::RUNNING, std::memory_order_release);
156 std::atomic<OperationPollingStatus>*
status;
188 result_ = ErrorCode::OK;
189 state_.store(State::PENDING, std::memory_order_release);
193 void Cancel() { state_.store(State::IDLE, std::memory_order_release); }
204 DEV_ASSERT(sem_ !=
nullptr);
205 auto wait_ans = sem_->Wait(timeout);
206 if (wait_ans == ErrorCode::OK)
208 DEV_ASSERT(state_.load(std::memory_order_acquire) == State::CLAIMED);
209 state_.store(State::IDLE, std::memory_order_release);
213 State expected = State::PENDING;
214 if (state_.compare_exchange_strong(expected, State::DETACHED,
215 std::memory_order_acq_rel,
216 std::memory_order_acquire))
218 return ErrorCode::TIMEOUT;
221 DEV_ASSERT(expected == State::CLAIMED || expected == State::DETACHED ||
222 expected == State::IDLE);
223 if (expected == State::DETACHED)
225 state_.store(State::IDLE, std::memory_order_release);
226 return ErrorCode::TIMEOUT;
228 if (expected == State::IDLE)
230 return ErrorCode::TIMEOUT;
236 finish_wait_ans = sem_->Wait(UINT32_MAX);
237 }
while (finish_wait_ans == ErrorCode::TIMEOUT);
238 REQUIRE(finish_wait_ans == ErrorCode::OK);
239 state_.store(State::IDLE, std::memory_order_release);
252 DEV_ASSERT_FROM_CALLBACK(sem_ !=
nullptr, in_isr);
254 State expected = State::PENDING;
255 if (!state_.compare_exchange_strong(expected, State::CLAIMED,
256 std::memory_order_acq_rel,
257 std::memory_order_acquire))
259 DEV_ASSERT_FROM_CALLBACK(expected == State::DETACHED || expected == State::IDLE,
261 if (expected == State::DETACHED)
263 expected = State::DETACHED;
264 (void)state_.compare_exchange_strong(
265 expected, State::IDLE, std::memory_order_acq_rel, std::memory_order_acquire);
271 sem_->PostFromCallback(in_isr);
277 std::atomic<State> state_{State::IDLE};
278 ErrorCode result_ = ErrorCode::OK;
292static_assert(std::is_trivially_copyable_v<ReadOperation>);
293static_assert(std::is_trivially_copyable_v<WriteOperation>);
294static_assert(std::is_trivially_destructible_v<ReadOperation>);
295static_assert(std::is_trivially_destructible_v<WriteOperation>);
同步驱动操作的阻塞等待器 / Blocking waiter for synchronous driver operations.
bool TryPost(bool in_isr, ErrorCode ec)
尝试认领完成并唤醒调用者 / Try to claim completion and wake the caller.
void Start(Semaphore &sem)
绑定信号量并开始等待 / Bind a semaphore and start waiting.
ErrorCode Wait(uint32_t timeout)
等待完成或超时交接 / Wait for completion or a timeout handoff.
State
32 位等待状态,兼容 STM32 原子适配 / 32-bit wait state for STM32 atomic support.
void Cancel()
清除等待状态,不发送通知 / Clear the wait state without posting.
通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing
操作的完成通知描述符 / Completion notification descriptor for an operation.
Operation(Semaphore &sem, uint32_t timeout=UINT32_MAX)
构造阻塞操作 / Construct a blocking operation.
Operation(const Operation &)=default
复制通知描述符,共用通知对象 / Copy the descriptor, sharing notification targets.
uint32_t timeout
等待超时值 / Wait timeout.
Operation(Operation &&) noexcept=default
移动描述符,源描述符保持不变 / Move the descriptor without clearing the source.
Operation(Callback &callback)
构造回调操作 / Construct a callback operation.
Operation()
构造无完成通知的操作 / Construct an operation without notification.
@ DONE
成功完成 / Completed successfully.
@ READY
尚未开始 / Not started.
@ RUNNING
正在执行 / In progress.
@ ERROR
错误结束 / Completed with an error.
union LibXR::Operation::@6 data
完成通知对象,由 type 选择有效成员 / Notification target selected by type.
void MarkAsRunning()
标记轮询操作正在执行 / Mark a polling operation as running.
Semaphore * sem
借用的信号量 / Borrowed semaphore.
Callback * callback
借用的回调对象 / Borrowed callback.
Operation(std::atomic< OperationPollingStatus > &status)
构造轮询操作 / Construct a polling operation.
OperationType type
当前通知方式 / Current notification mode.
OperationType
完成通知方式 / Completion notification mode.
@ BLOCK
信号量等待 / Wait with a semaphore.
@ NONE
无完成通知 / No completion notification.
@ CALLBACK
执行回调 / Invoke a callback.
@ POLLING
查询原子状态 / Poll an atomic status.
std::atomic< OperationPollingStatus > * status
借用的轮询状态 / Borrowed polling status.
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
基于 SPSC 字节队列的写端口 / Write endpoint backed by an SPSC byte queue.
LibXR 命名空间 / LibXR namespace.
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ OK
操作成功 / Operation successful