MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ConditionAwaiter.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Kriya {
6
7/**
8 * @class ConditionAwaiter
9 * @brief Suspends a FreeRoutine until a caller-supplied predicate returns true.
10 *
11 * await_ready() evaluates the condition immediately; if already true the
12 * coroutine does not suspend. Otherwise await_suspend() arms the promise so
13 * the scheduler's CONDITIONAL thread will evaluate the condition on each pass
14 * and resume the handle when it returns true.
15 *
16 * The condition is moved into the promise on suspension and cleared on
17 * resumption. It must be safe to call from the scheduler thread.
18 *
19 * @code
20 * auto routine = [&]() -> Vruta::FreeRoutine {
21 * while (true) {
22 * co_await ConditionAwaiter{ [&]{ return flag.load(); } };
23 * do_work();
24 * }
25 * };
26 * @endcode
27 */
29public:
30 /**
31 * @brief Construct with a condition predicate.
32 * @param condition Callable returning bool. Evaluated on the scheduler thread.
33 */
34 explicit ConditionAwaiter(std::function<bool()> condition)
35 : m_condition(std::move(condition))
36 {
37 }
38
39 /**
40 * @brief Evaluate the condition before suspending.
41 * @return true if the condition is already met; the coroutine will not suspend.
42 */
43 [[nodiscard]] bool await_ready() const
44 {
45 return m_condition && m_condition();
46 }
47
48 /**
49 * @brief Arm the promise with the condition and the coroutine handle.
50 * @param handle Handle to the suspended FreeRoutine coroutine frame.
51 *
52 * Requires that the coroutine type is FreeRoutine; the promise cast
53 * is asserted at compile time via the typed handle overload.
54 */
55 void await_suspend(std::coroutine_handle<Vruta::conditional_promise> handle)
56 {
57 auto& p = handle.promise();
58 p.condition = std::move(m_condition);
59 p.armed.store(true, std::memory_order_release);
60 }
61
62 /**
63 * @brief No value to return on resumption.
64 */
65 void await_resume() const { }
66
67private:
68 std::function<bool()> m_condition;
69};
70
71} // namespace MayaFlux::Kriya
void await_suspend(std::coroutine_handle< Vruta::conditional_promise > handle)
Arm the promise with the condition and the coroutine handle.
void await_resume() const
No value to return on resumption.
ConditionAwaiter(std::function< bool()> condition)
Construct with a condition predicate.
bool await_ready() const
Evaluate the condition before suspending.
Suspends a FreeRoutine until a caller-supplied predicate returns true.