FSM (6): parallel / orthogonal regions (run_parallel, run_parallel_any)
Builds on the multi-waiter mailbox: a state can run several sub-machines concurrently on the shared executor. - co_await run_parallel<A, B, ...>() spawns each child (Async::spawn) and joins ALL, yielding std::tuple<A::Outcome, ...>. Children run interleaved on the single executor — when one parks on an event, another runs (only possible now that the mailbox holds multiple waiters). - co_await run_parallel_any<A, B, ...>() resolves on the FIRST to finish, yielding std::variant<A::Outcome, ...> of the winner. Losers keep running cooperatively (coroutines aren't preemptible), so the children are kept alive by the spawned tasks (shared_ptr keepalive) until they finish — a loser parked forever leaks its frame until the machine stops (documented). - Join is a small latch coroutine (completion counter + a parked parent handle posted when it reaches zero / on first arrival); single-consumer, no lock. Children default-constructed, share events/services. 3 new tests (Parallel): join-all (worker + deterministic embedder, both children park concurrently), first-finisher race. 998 tests (995 green, 3 skipped), ASan/UBSan clean (validates the spawned-task lifetimes — no UAF/ leak), -Werror clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c6b7306614
commit
9d6be56847
3 files changed
+309
No files matched your search
@@ -274,6 +274,25 @@ namespace Void::Fsm
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/// Orthogonal regions: run `Subs...` concurrently on the
|
||||
/// shared executor and join all, yielding a
|
||||
/// std::tuple<Subs::Outcome...> (children share events/
|
||||
/// services; map the tuple to a sibling transition).
|
||||
template <typename... Subs>
|
||||
[[nodiscard]] auto run_parallel() const
|
||||
{
|
||||
return m_machine->template run_parallel<Subs...>();
|
||||
}
|
||||
|
||||
/// Race orthogonal regions: resolve on the first to finish,
|
||||
/// yielding std::variant<Subs::Outcome...> (losers keep
|
||||
/// running cooperatively).
|
||||
template <typename... Subs>
|
||||
[[nodiscard]] auto run_parallel_any() const
|
||||
{
|
||||
return m_machine->template run_parallel_any<Subs...>();
|
||||
}
|
||||
|
||||
/// Await whichever of several event types arrives first,
|
||||
/// yielding a std::variant<Es...>. Handles multiple events
|
||||
/// at one level (e.g. a parent watching Cancel alongside a
|
||||
@@ -769,6 +788,152 @@ namespace Void::Fsm
|
||||
co_return child.template take_outcome<Sub>();
|
||||
}
|
||||
|
||||
/// Orthogonal regions: run `Subs...` concurrently on the shared
|
||||
/// executor (interleaved — when one parks on an event, another
|
||||
/// runs) and join all, yielding a tuple of their Outcomes. Children
|
||||
/// are default-constructed and share this machine's
|
||||
/// events/services.
|
||||
template <typename... Subs>
|
||||
[[nodiscard]] Coro::Task<std::tuple<Detail::OutcomeOf<Subs>...>>
|
||||
run_parallel()
|
||||
{
|
||||
auto children = std::make_unique<std::tuple<Subs...>>();
|
||||
std::apply(
|
||||
[&](auto&... c) { (c.attach(m_ctx, m_services), ...); },
|
||||
*children);
|
||||
|
||||
struct Latch {
|
||||
int remaining;
|
||||
std::coroutine_handle<> waiter{};
|
||||
Async::Executor* exec;
|
||||
void arrive()
|
||||
{
|
||||
if (--remaining == 0 && waiter) {
|
||||
exec->post(waiter);
|
||||
}
|
||||
}
|
||||
};
|
||||
auto latch = std::make_shared<Latch>(
|
||||
static_cast<int>(sizeof...(Subs)),
|
||||
std::coroutine_handle<>{}, &m_ctx->executor());
|
||||
|
||||
std::apply(
|
||||
[&](auto&... c) {
|
||||
(Async::spawn(m_ctx->executor(),
|
||||
[](auto& child, std::shared_ptr<Latch> l)
|
||||
-> Coro::Task<void> {
|
||||
co_await child.drive();
|
||||
l->arrive();
|
||||
}(c, latch)),
|
||||
...);
|
||||
},
|
||||
*children);
|
||||
|
||||
struct Awaiter {
|
||||
std::shared_ptr<Latch> latch;
|
||||
[[nodiscard]] bool await_ready() const noexcept
|
||||
{
|
||||
return latch->remaining == 0;
|
||||
}
|
||||
void await_suspend(std::coroutine_handle<> h) const
|
||||
{
|
||||
latch->waiter = h;
|
||||
}
|
||||
void await_resume() const noexcept {}
|
||||
};
|
||||
co_await Awaiter{latch};
|
||||
|
||||
co_return std::apply(
|
||||
[](auto&... c) {
|
||||
return std::tuple<Detail::OutcomeOf<Subs>...>{
|
||||
c.template take_outcome<Subs>()...};
|
||||
},
|
||||
*children);
|
||||
}
|
||||
|
||||
/// Like run_parallel but resolves on the **first** child to finish,
|
||||
/// yielding a std::variant<Subs::Outcome...> holding the winner's
|
||||
/// outcome. The losers keep running cooperatively (coroutines
|
||||
/// aren't preemptible) — the children are kept alive by the spawned
|
||||
/// tasks until they finish, so a loser parked forever leaks its
|
||||
/// frame until the machine stops. Document at call sites.
|
||||
template <typename... Subs>
|
||||
[[nodiscard]] Coro::Task<std::variant<Detail::OutcomeOf<Subs>...>>
|
||||
run_parallel_any()
|
||||
{
|
||||
// Shared so losers (which outlive this call) keep it alive.
|
||||
auto children = std::make_shared<std::tuple<Subs...>>();
|
||||
std::apply(
|
||||
[&](auto&... c) { (c.attach(m_ctx, m_services), ...); },
|
||||
*children);
|
||||
|
||||
struct Latch {
|
||||
int first{-1};
|
||||
std::coroutine_handle<> waiter{};
|
||||
Async::Executor* exec;
|
||||
void arrive(int idx)
|
||||
{
|
||||
if (first < 0) {
|
||||
first = idx;
|
||||
if (waiter) {
|
||||
exec->post(waiter);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
auto latch = std::make_shared<Latch>(
|
||||
-1, std::coroutine_handle<>{}, &m_ctx->executor());
|
||||
|
||||
int idx = 0;
|
||||
std::apply(
|
||||
[&](auto&... c) {
|
||||
(
|
||||
[&] {
|
||||
const int my = idx++;
|
||||
Async::spawn(
|
||||
m_ctx->executor(),
|
||||
[](auto& child,
|
||||
std::shared_ptr<std::tuple<Subs...>>
|
||||
keep,
|
||||
std::shared_ptr<Latch>
|
||||
l,
|
||||
int i) -> Coro::Task<void> {
|
||||
co_await child.drive();
|
||||
l->arrive(i);
|
||||
(void)keep;
|
||||
}(c, children, latch, my));
|
||||
}(),
|
||||
...);
|
||||
},
|
||||
*children);
|
||||
|
||||
struct Awaiter {
|
||||
std::shared_ptr<Latch> latch;
|
||||
[[nodiscard]] bool await_ready() const noexcept
|
||||
{
|
||||
return latch->first >= 0;
|
||||
}
|
||||
void await_suspend(std::coroutine_handle<> h) const
|
||||
{
|
||||
latch->waiter = h;
|
||||
}
|
||||
void await_resume() const noexcept {}
|
||||
};
|
||||
co_await Awaiter{latch};
|
||||
|
||||
const auto winner = static_cast<std::size_t>(latch->first);
|
||||
co_return [&]<std::size_t... I>(std::index_sequence<I...>)
|
||||
-> std::variant<Detail::OutcomeOf<Subs>...> {
|
||||
std::variant<Detail::OutcomeOf<Subs>...> v;
|
||||
((I == winner ? (void)(v.template emplace<I>(
|
||||
std::get<I>(*children)
|
||||
.template take_outcome<Subs>()))
|
||||
: void()),
|
||||
...);
|
||||
return v;
|
||||
}(std::index_sequence_for<Subs...>{});
|
||||
}
|
||||
|
||||
/// Build a Launch step: run `Sub` (constructed from `args`) on this
|
||||
/// machine's shared executor, then enter `Next` carrying Sub's
|
||||
/// outcome as a LaunchResult. Reached via State::startMachine.
|
||||
|
||||
Reference in new issue
Block a user