libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::AsyncBlockWait Class Reference

同步驱动操作的阻塞等待器 / Blocking waiter for synchronous driver operations. More...

#include <operation.hpp>

Public Types

enum class  State : uint32_t { IDLE = 0 , PENDING = 1 , CLAIMED = 2 , DETACHED = 3 }
 32 位等待状态,兼容 STM32 原子适配 / 32-bit wait state for STM32 atomic support. More...
 

Public Member Functions

void Start (Semaphore &sem)
 绑定信号量并开始等待 / Bind a semaphore and start waiting.
 
void Cancel ()
 清除等待状态,不发送通知 / Clear the wait state without posting.
 
ErrorCode Wait (uint32_t timeout)
 等待完成或超时交接 / Wait for completion or a timeout handoff.
 
bool TryPost (bool in_isr, ErrorCode ec)
 尝试认领完成并唤醒调用者 / Try to claim completion and wake the caller.
 

Detailed Description

同步驱动操作的阻塞等待器 / Blocking waiter for synchronous driver operations.

超时解除调用者的等待,迟到的完成只清退状态,不再唤醒已超时的调用者。 Timeout detaches the caller. A late completion clears the state without waking it.

Definition at line 169 of file operation.hpp.

Member Enumeration Documentation

◆ State

enum class LibXR::AsyncBlockWait::State : uint32_t
strong

32 位等待状态,兼容 STM32 原子适配 / 32-bit wait state for STM32 atomic support.

Enumerator
IDLE 

无等待 / No wait.

PENDING 

等待完成 / Awaiting completion.

CLAIMED 

完成方已认领通知 / Completion notification claimed.

DETACHED 

调用者已超时 / Caller timed out.

Definition at line 173 of file operation.hpp.

174 {
175 IDLE = 0,
176 PENDING = 1,
177 CLAIMED = 2,
178 DETACHED = 3,
179 };
@ DETACHED
调用者已超时 / Caller timed out.
@ IDLE
无等待 / No wait.
@ CLAIMED
完成方已认领通知 / Completion notification claimed.
@ PENDING
等待完成 / Awaiting completion.

Member Function Documentation

◆ Cancel()

void LibXR::AsyncBlockWait::Cancel ( )
inline

清除等待状态,不发送通知 / Clear the wait state without posting.

Definition at line 193 of file operation.hpp.

193{ state_.store(State::IDLE, std::memory_order_release); }

◆ Start()

void LibXR::AsyncBlockWait::Start ( Semaphore & sem)
inline

绑定信号量并开始等待 / Bind a semaphore and start waiting.

Parameters
sem本次等待使用的信号量 / Semaphore for this wait.

Definition at line 185 of file operation.hpp.

186 {
187 sem_ = &sem;
188 result_ = ErrorCode::OK;
189 state_.store(State::PENDING, std::memory_order_release);
190 }
@ OK
操作成功 / Operation successful

◆ TryPost()

bool LibXR::AsyncBlockWait::TryPost ( bool in_isr,
ErrorCode ec )
inline

尝试认领完成并唤醒调用者 / Try to claim completion and wake the caller.

Parameters
in_isr当前是否在中断中 / Whether called in an ISR.
ec完成结果 / Completion result.
Returns
成功发送通知为 true,调用者已解除等待则为 false。 True when posted, false when the caller is no longer waiting.

Definition at line 250 of file operation.hpp.

251 {
252 DEV_ASSERT_FROM_CALLBACK(sem_ != nullptr, in_isr);
253
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))
258 {
259 DEV_ASSERT_FROM_CALLBACK(expected == State::DETACHED || expected == State::IDLE,
260 in_isr);
261 if (expected == State::DETACHED)
262 {
263 expected = State::DETACHED;
264 (void)state_.compare_exchange_strong(
265 expected, State::IDLE, std::memory_order_acq_rel, std::memory_order_acquire);
266 }
267 return false;
268 }
269
270 result_ = ec;
271 sem_->PostFromCallback(in_isr);
272 return true;
273 }
State
32 位等待状态,兼容 STM32 原子适配 / 32-bit wait state for STM32 atomic support.
void PostFromCallback(bool in_isr)
从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)
Definition semaphore.cpp:96

◆ Wait()

ErrorCode LibXR::AsyncBlockWait::Wait ( uint32_t timeout)
inline

等待完成或超时交接 / Wait for completion or a timeout handoff.

Parameters
timeout传给信号量的超时值 / Timeout passed to the semaphore.
Returns
完成结果,或解除等待后的 TIMEOUT / Completion result or detached TIMEOUT.
Note
完成方已认领时,须等对应通知后才返回,可能超过指定时间。 A claimed completion must post before return, possibly exceeding the timeout.

Definition at line 202 of file operation.hpp.

203 {
204 DEV_ASSERT(sem_ != nullptr);
205 auto wait_ans = sem_->Wait(timeout);
206 if (wait_ans == ErrorCode::OK)
207 {
208 DEV_ASSERT(state_.load(std::memory_order_acquire) == State::CLAIMED);
209 state_.store(State::IDLE, std::memory_order_release);
210 return result_;
211 }
212
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))
217 {
218 return ErrorCode::TIMEOUT;
219 }
220
221 DEV_ASSERT(expected == State::CLAIMED || expected == State::DETACHED ||
222 expected == State::IDLE);
223 if (expected == State::DETACHED)
224 {
225 state_.store(State::IDLE, std::memory_order_release);
226 return ErrorCode::TIMEOUT;
227 }
228 if (expected == State::IDLE)
229 {
230 return ErrorCode::TIMEOUT;
231 }
232
233 ErrorCode finish_wait_ans;
234 do
235 {
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);
240 return result_;
241 }
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:49
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ TIMEOUT
超时 / Timeout

The documentation for this class was generated from the following file: