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 <operation.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 234 of file operation.hpp.

Member Enumeration Documentation

◆ State

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

Definition at line 239 of file operation.hpp.

240 {
241 IDLE = 0,
242 PENDING = 1,
243 CLAIMED = 2,
244 DETACHED = 3,
245 };
@ PENDING
等待中 | Pending

Member Function Documentation

◆ Cancel()

void LibXR::AsyncBlockWait::Cancel ( )
inline

Definition at line 254 of file operation.hpp.

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

◆ Start()

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

Definition at line 247 of file operation.hpp.

248 {
249 sem_ = &sem;
250 result_ = ErrorCode::OK;
251 state_.store(State::PENDING, std::memory_order_release);
252 }
@ OK
操作成功 | Operation successful

◆ TryPost()

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

Definition at line 296 of file operation.hpp.

297 {
298 ASSERT(sem_ != nullptr);
299
300 State expected = State::PENDING;
301 if (!state_.compare_exchange_strong(expected, State::CLAIMED,
302 std::memory_order_acq_rel,
303 std::memory_order_acquire))
304 {
305 ASSERT(expected == State::DETACHED || expected == State::IDLE);
306 if (expected == State::DETACHED)
307 {
308 expected = State::DETACHED;
309 (void)state_.compare_exchange_strong(
310 expected, State::IDLE, std::memory_order_acq_rel, std::memory_order_acquire);
311 }
312 return false;
313 }
314
315 result_ = ec;
316 sem_->PostFromCallback(in_isr);
317 return true;
318 }
void PostFromCallback(bool in_isr)
从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)
Definition semaphore.cpp:99

◆ Wait()

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

Definition at line 256 of file operation.hpp.

257 {
258 ASSERT(sem_ != nullptr);
259 auto wait_ans = sem_->Wait(timeout);
260 if (wait_ans == ErrorCode::OK)
261 {
262#ifdef LIBXR_DEBUG_BUILD
263 ASSERT(state_.load(std::memory_order_acquire) == State::CLAIMED);
264#endif
265 state_.store(State::IDLE, std::memory_order_release);
266 return result_;
267 }
268
269 State expected = State::PENDING;
270 if (state_.compare_exchange_strong(expected, State::DETACHED,
271 std::memory_order_acq_rel,
272 std::memory_order_acquire))
273 {
274 return ErrorCode::TIMEOUT;
275 }
276
277 ASSERT(expected == State::CLAIMED || expected == State::DETACHED ||
278 expected == State::IDLE);
279 if (expected == State::DETACHED)
280 {
281 state_.store(State::IDLE, std::memory_order_release);
282 return ErrorCode::TIMEOUT;
283 }
284 if (expected == State::IDLE)
285 {
286 return ErrorCode::TIMEOUT;
287 }
288
289 auto finish_wait_ans = sem_->Wait(UINT32_MAX);
290 UNUSED(finish_wait_ans);
291 ASSERT(finish_wait_ans == ErrorCode::OK);
292 state_.store(State::IDLE, std::memory_order_release);
293 return result_;
294 }
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:52
@ TIMEOUT
超时 | Timeout

Field Documentation

◆ result_

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

Definition at line 323 of file operation.hpp.

◆ sem_

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

Definition at line 321 of file operation.hpp.

◆ state_

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

Definition at line 322 of file operation.hpp.

322{State::IDLE};

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