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

Member Enumeration Documentation

◆ State

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

Definition at line 236 of file operation.hpp.

237 {
238 IDLE = 0,
239 PENDING = 1,
240 CLAIMED = 2,
241 DETACHED = 3,
242 };
@ PENDING
等待中 | Pending

Member Function Documentation

◆ Cancel()

void LibXR::AsyncBlockWait::Cancel ( )
inline

Definition at line 251 of file operation.hpp.

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

◆ Start()

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

Definition at line 244 of file operation.hpp.

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

◆ TryPost()

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

Definition at line 293 of file operation.hpp.

294 {
295 ASSERT(sem_ != nullptr);
296
297 State expected = State::PENDING;
298 if (!state_.compare_exchange_strong(expected, State::CLAIMED,
299 std::memory_order_acq_rel,
300 std::memory_order_acquire))
301 {
302 ASSERT(expected == State::DETACHED || expected == State::IDLE);
303 if (expected == State::DETACHED)
304 {
305 expected = State::DETACHED;
306 (void)state_.compare_exchange_strong(expected, State::IDLE,
307 std::memory_order_acq_rel,
308 std::memory_order_acquire);
309 }
310 return false;
311 }
312
313 result_ = ec;
314 sem_->PostFromCallback(in_isr);
315 return true;
316 }
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 253 of file operation.hpp.

254 {
255 ASSERT(sem_ != nullptr);
256 auto wait_ans = sem_->Wait(timeout);
257 if (wait_ans == ErrorCode::OK)
258 {
259#ifdef LIBXR_DEBUG_BUILD
260 ASSERT(state_.load(std::memory_order_acquire) == State::CLAIMED);
261#endif
262 state_.store(State::IDLE, std::memory_order_release);
263 return result_;
264 }
265
266 State expected = State::PENDING;
267 if (state_.compare_exchange_strong(expected, State::DETACHED,
268 std::memory_order_acq_rel,
269 std::memory_order_acquire))
270 {
271 return ErrorCode::TIMEOUT;
272 }
273
274 ASSERT(expected == State::CLAIMED || expected == State::DETACHED ||
275 expected == State::IDLE);
276 if (expected == State::DETACHED)
277 {
278 state_.store(State::IDLE, std::memory_order_release);
279 return ErrorCode::TIMEOUT;
280 }
281 if (expected == State::IDLE)
282 {
283 return ErrorCode::TIMEOUT;
284 }
285
286 auto finish_wait_ans = sem_->Wait(UINT32_MAX);
287 UNUSED(finish_wait_ans);
288 ASSERT(finish_wait_ans == ErrorCode::OK);
289 state_.store(State::IDLE, std::memory_order_release);
290 return result_;
291 }
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 321 of file operation.hpp.

◆ sem_

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

Definition at line 319 of file operation.hpp.

◆ state_

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

Definition at line 320 of file operation.hpp.

320{State::IDLE};

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