libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::AsyncBlockWait Class Reference

Shared BLOCK waiter handoff for synchronous driver operations. More...

#include <libxr_rw.hpp>

Collaboration diagram for LibXR::AsyncBlockWait:
[legend]

Public Types

enum class  State : uint32_t { IDLE = 0 , PENDING = 1 , CLAIMED = 2 , DETACHED = 3 }
 

Public Member Functions

void Start (Semaphore &sem)
 
void Cancel ()
 
ErrorCode Wait (uint32_t timeout)
 
bool TryPost (bool in_isr, ErrorCode ec)
 

Private Attributes

Semaphoresem_ = nullptr
 
std::atomic< State > state_ {State::IDLE}
 
ErrorCode result_ = ErrorCode::OK
 

Detailed Description

Shared BLOCK waiter handoff for synchronous driver operations.

Timeout detaches the waiting caller. A late completion may still clear the in-flight state, but it no longer belongs to that timed-out caller.

Definition at line 246 of file libxr_rw.hpp.

Member Enumeration Documentation

◆ State

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

Definition at line 251 of file libxr_rw.hpp.

252 {
253 IDLE = 0,
254 PENDING = 1,
255 CLAIMED = 2,
256 DETACHED = 3,
257 };
@ PENDING
等待中 | Pending

Member Function Documentation

◆ Cancel()

void LibXR::AsyncBlockWait::Cancel ( )
inline

Definition at line 266 of file libxr_rw.hpp.

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

◆ Start()

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

Definition at line 259 of file libxr_rw.hpp.

260 {
261 sem_ = &sem;
262 result_ = ErrorCode::OK;
263 state_.store(State::PENDING, std::memory_order_release);
264 }
@ OK
操作成功 | Operation successful

◆ TryPost()

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

Definition at line 308 of file libxr_rw.hpp.

309 {
310 ASSERT(sem_ != nullptr);
311
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))
316 {
317 ASSERT(expected == State::DETACHED || expected == State::IDLE);
318 if (expected == State::DETACHED)
319 {
320 expected = State::DETACHED;
321 (void)state_.compare_exchange_strong(expected, State::IDLE,
322 std::memory_order_acq_rel,
323 std::memory_order_acquire);
324 }
325 return false;
326 }
327
328 result_ = ec;
329 sem_->PostFromCallback(in_isr);
330 return true;
331 }
void PostFromCallback(bool in_isr)
从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)

◆ Wait()

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

Definition at line 268 of file libxr_rw.hpp.

269 {
270 ASSERT(sem_ != nullptr);
271 auto wait_ans = sem_->Wait(timeout);
272 if (wait_ans == ErrorCode::OK)
273 {
274#ifdef LIBXR_DEBUG_BUILD
275 ASSERT(state_.load(std::memory_order_acquire) == State::CLAIMED);
276#endif
277 state_.store(State::IDLE, std::memory_order_release);
278 return result_;
279 }
280
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))
285 {
286 return ErrorCode::TIMEOUT;
287 }
288
289 ASSERT(expected == State::CLAIMED || expected == State::DETACHED ||
290 expected == State::IDLE);
291 if (expected == State::DETACHED)
292 {
293 state_.store(State::IDLE, std::memory_order_release);
294 return ErrorCode::TIMEOUT;
295 }
296 if (expected == State::IDLE)
297 {
298 return ErrorCode::TIMEOUT;
299 }
300
301 auto finish_wait_ans = sem_->Wait(UINT32_MAX);
302 UNUSED(finish_wait_ans);
303 ASSERT(finish_wait_ans == ErrorCode::OK);
304 state_.store(State::IDLE, std::memory_order_release);
305 return result_;
306 }
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:53
@ TIMEOUT
超时 | Timeout

Field Documentation

◆ result_

ErrorCode LibXR::AsyncBlockWait::result_ = ErrorCode::OK
private

Definition at line 336 of file libxr_rw.hpp.

◆ sem_

Semaphore* LibXR::AsyncBlockWait::sem_ = nullptr
private

Definition at line 334 of file libxr_rw.hpp.

◆ state_

std::atomic<State> LibXR::AsyncBlockWait::state_ {State::IDLE}
private

Definition at line 335 of file libxr_rw.hpp.

335{State::IDLE};

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