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>
144 lines
3.6 KiB
C++
144 lines
3.6 KiB
C++
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::Step;
|
|
namespace Fsm = Void::Fsm;
|
|
|
|
// Orthogonal regions: run_parallel<A, B>() runs both sub-machines concurrently
|
|
// on the shared executor and joins all. Each child parks on its own event at
|
|
// the same time — only possible with the multi-waiter mailbox.
|
|
|
|
namespace
|
|
{
|
|
struct GoLeft {
|
|
};
|
|
struct GoRight {
|
|
};
|
|
|
|
class Left : public Fsm::Machine<Left>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
struct W : State {
|
|
Task<Step> run()
|
|
{
|
|
co_await await_event<GoLeft>();
|
|
co_return succeed(1);
|
|
}
|
|
};
|
|
using States = Fsm::States<W>;
|
|
};
|
|
|
|
class Right : public Fsm::Machine<Right>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
struct W : State {
|
|
Task<Step> run()
|
|
{
|
|
co_await await_event<GoRight>();
|
|
co_return succeed(2);
|
|
}
|
|
};
|
|
using States = Fsm::States<W>;
|
|
};
|
|
|
|
// Joins both children: their results sum to 3.
|
|
class Both : public Fsm::Machine<Both>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
struct Run : State {
|
|
Task<Step> run()
|
|
{
|
|
auto [a, b] = co_await run_parallel<Left, Right>();
|
|
co_return succeed((a.has_value() ? *a : 0) +
|
|
(b.has_value() ? *b : 0));
|
|
}
|
|
};
|
|
using States = Fsm::States<Run>;
|
|
|
|
explicit Both(Fsm::MachineOptions o = {}) : Fsm::Machine<Both>(o) {}
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test parallel_regions_join_all()
|
|
{
|
|
Both fsm;
|
|
fsm.start();
|
|
// Both children are parked on their own events at the same time.
|
|
fsm.send(GoLeft{}, GoRight{});
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 3);
|
|
}
|
|
|
|
// @tag integration
|
|
Void::Testing::Test parallel_regions_join_all_embedder()
|
|
{
|
|
// Deterministic: drive in embedder mode, both children park, then release.
|
|
Both fsm{Fsm::MachineOptions{.own_worker = false}};
|
|
fsm.start();
|
|
while (fsm.pump() > 0) {
|
|
} // Run -> spawns Left+Right -> both park on events
|
|
fsm.send(GoRight{}); // release right first
|
|
while (fsm.pump() > 0) {
|
|
}
|
|
Void::Testing::expect(!fsm.finished()); // left still parked
|
|
fsm.send(GoLeft{});
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 3);
|
|
}
|
|
|
|
// ── run_parallel_any (first to finish) ──────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
// Races Left and Right; reports which won and its value.
|
|
class Race : public Fsm::Machine<Race>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
struct Run : State {
|
|
Task<Step> run()
|
|
{
|
|
auto v = co_await run_parallel_any<Left, Right>();
|
|
// index 1 == Right (value 2); index 0 == Left (value 1)
|
|
if (v.index() == 1 && std::get<1>(v).has_value()) {
|
|
co_return succeed(100 + *std::get<1>(v));
|
|
}
|
|
if (v.index() == 0 && std::get<0>(v).has_value()) {
|
|
co_return succeed(*std::get<0>(v));
|
|
}
|
|
co_return succeed(-1);
|
|
}
|
|
};
|
|
using States = Fsm::States<Run>;
|
|
|
|
explicit Race(Fsm::MachineOptions o = {}) : Fsm::Machine<Race>(o) {}
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test parallel_any_resolves_on_first_finisher()
|
|
{
|
|
Race fsm{Fsm::MachineOptions{.own_worker = false}};
|
|
fsm.start();
|
|
while (fsm.pump() > 0) {
|
|
} // both park
|
|
// Right's resume is posted first, so Right finishes first and wins; both
|
|
// events are delivered so neither child leaks a parked frame.
|
|
fsm.send(GoRight{}, GoLeft{});
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 102); // 100 + Right's 2
|
|
}
|