FSM (5): multi-waiter mailbox (engine foundation)

ExecutionContext had a single Waiter slot, so only one coroutine could park
on events at a time. Replace it with a FIFO list of waiters: set_waiter
appends (each with a unique id + its own timer); deliver routes an event to
the first parked waiter that wants its type, else buffers; a timeout fires
only its own waiter (found by id). Single-consumer invariant unchanged — all
mailbox mutation stays on the consumer thread.

Pure refactor (no API change); unblocks parallel regions and deferred
events. 995 tests (992 green, 3 skipped), ASan clean, -Werror clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-15 01:24:02 +03:00
1 parent ec9e466d7a
commit c6b7306614
2 files changed
+37 -26

No files matched your search

+6 -5
View File
@@ -137,17 +137,15 @@ namespace Void::Fsm::Detail
Async::MpscQueue<EventEnvelope> m_queue; Async::MpscQueue<EventEnvelope> m_queue;
std::unordered_map<std::type_index, std::deque<std::any>> m_buffers; std::unordered_map<std::type_index, std::deque<std::any>> m_buffers;
using WaiterId = std::uint64_t;
struct Waiter { struct Waiter {
WaiterId id{0};
std::vector<const std::type_info*> types; std::vector<const std::type_info*> types;
std::coroutine_handle<> cont{}; std::coroutine_handle<> cont{};
EventEnvelope* slot{nullptr}; EventEnvelope* slot{nullptr};
bool* timed_out{nullptr}; bool* timed_out{nullptr};
TimerId timer{0}; TimerId timer{0};
[[nodiscard]] bool active() const noexcept
{
return static_cast<bool>(cont);
}
[[nodiscard]] bool wants(const std::type_info& t) const [[nodiscard]] bool wants(const std::type_info& t) const
{ {
for (const auto* ty : types) { for (const auto* ty : types) {
@@ -158,7 +156,10 @@ namespace Void::Fsm::Detail
return false; return false;
} }
}; };
Waiter m_waiter; // Several waiters can be parked at once (parallel regions, deferred
// events). An event resumes the first (FIFO) that wants its type.
std::vector<Waiter> m_waiters;
WaiterId m_next_waiter{1};
struct Timer { struct Timer {
std::chrono::steady_clock::time_point deadline; std::chrono::steady_clock::time_point deadline;
+31 -21
View File
@@ -1,5 +1,6 @@
#include <Void/Fsm/Detail/Engine.hpp> #include <Void/Fsm/Detail/Engine.hpp>
#include <algorithm>
#include <utility> #include <utility>
namespace Void::Fsm::Detail namespace Void::Fsm::Detail
@@ -38,25 +39,32 @@ namespace Void::Fsm::Detail
std::coroutine_handle<> cont, EventEnvelope* slot, bool* timed_out, std::coroutine_handle<> cont, EventEnvelope* slot, bool* timed_out,
std::optional<std::chrono::steady_clock::time_point> deadline) std::optional<std::chrono::steady_clock::time_point> deadline)
{ {
m_waiter.types.assign(types.begin(), types.end()); Waiter w;
m_waiter.cont = cont; w.id = m_next_waiter++;
m_waiter.slot = slot; w.types.assign(types.begin(), types.end());
m_waiter.timed_out = timed_out; w.cont = cont;
m_waiter.timer = 0; w.slot = slot;
w.timed_out = timed_out;
w.timer = 0;
if (deadline.has_value()) { if (deadline.has_value()) {
m_waiter.timer = add_timer(*deadline, [this] { const WaiterId id = w.id;
// Timeout: resume the waiter with timed_out set. w.timer = add_timer(*deadline, [this, id] {
if (!m_waiter.active()) { // Timeout: resume that waiter (if still parked) with timed_out.
const auto it =
std::find_if(m_waiters.begin(), m_waiters.end(),
[id](const Waiter& x) { return x.id == id; });
if (it == m_waiters.end()) {
return; return;
} }
if (m_waiter.timed_out != nullptr) { if (it->timed_out != nullptr) {
*m_waiter.timed_out = true; *it->timed_out = true;
} }
auto cont2 = m_waiter.cont; auto cont2 = it->cont;
m_waiter = Waiter{}; m_waiters.erase(it);
m_executor.post(cont2); m_executor.post(cont2);
}); });
} }
m_waiters.push_back(std::move(w));
} }
ExecutionContext::TimerId ExecutionContext::add_timer( ExecutionContext::TimerId ExecutionContext::add_timer(
@@ -112,15 +120,17 @@ namespace Void::Fsm::Detail
void ExecutionContext::deliver(EventEnvelope& env) void ExecutionContext::deliver(EventEnvelope& env)
{ {
// Match a waiting state first (lets a multi-type waiter receive an // Route to the first parked waiter (FIFO) that wants this type — lets
// event without it ever sitting in a buffer). // it receive the event without ever sitting in a buffer.
if (m_waiter.active() && m_waiter.wants(*env.type)) { for (auto it = m_waiters.begin(); it != m_waiters.end(); ++it) {
*m_waiter.slot = std::move(env); if (it->wants(*env.type)) {
cancel_timer(m_waiter.timer); *it->slot = std::move(env);
auto cont = m_waiter.cont; cancel_timer(it->timer);
m_waiter = Waiter{}; auto cont = it->cont;
m_executor.post(cont); m_waiters.erase(it);
return; m_executor.post(cont);
return;
}
} }
// Otherwise buffer for a later await. // Otherwise buffer for a later await.
m_buffers[std::type_index{*env.type}].push_back(std::move(env.value)); m_buffers[std::type_index{*env.type}].push_back(std::move(env.value));