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:
1 parent
ec9e466d7a
commit
c6b7306614
2 files changed
+37
-26
No files matched your search
@@ -137,17 +137,15 @@ namespace Void::Fsm::Detail
|
||||
Async::MpscQueue<EventEnvelope> m_queue;
|
||||
|
||||
std::unordered_map<std::type_index, std::deque<std::any>> m_buffers;
|
||||
using WaiterId = std::uint64_t;
|
||||
struct Waiter {
|
||||
WaiterId id{0};
|
||||
std::vector<const std::type_info*> types;
|
||||
std::coroutine_handle<> cont{};
|
||||
EventEnvelope* slot{nullptr};
|
||||
bool* timed_out{nullptr};
|
||||
TimerId timer{0};
|
||||
|
||||
[[nodiscard]] bool active() const noexcept
|
||||
{
|
||||
return static_cast<bool>(cont);
|
||||
}
|
||||
[[nodiscard]] bool wants(const std::type_info& t) const
|
||||
{
|
||||
for (const auto* ty : types) {
|
||||
@@ -158,7 +156,10 @@ namespace Void::Fsm::Detail
|
||||
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 {
|
||||
std::chrono::steady_clock::time_point deadline;
|
||||
|
||||
+31
-21
@@ -1,5 +1,6 @@
|
||||
#include <Void/Fsm/Detail/Engine.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Fsm::Detail
|
||||
@@ -38,25 +39,32 @@ namespace Void::Fsm::Detail
|
||||
std::coroutine_handle<> cont, EventEnvelope* slot, bool* timed_out,
|
||||
std::optional<std::chrono::steady_clock::time_point> deadline)
|
||||
{
|
||||
m_waiter.types.assign(types.begin(), types.end());
|
||||
m_waiter.cont = cont;
|
||||
m_waiter.slot = slot;
|
||||
m_waiter.timed_out = timed_out;
|
||||
m_waiter.timer = 0;
|
||||
Waiter w;
|
||||
w.id = m_next_waiter++;
|
||||
w.types.assign(types.begin(), types.end());
|
||||
w.cont = cont;
|
||||
w.slot = slot;
|
||||
w.timed_out = timed_out;
|
||||
w.timer = 0;
|
||||
if (deadline.has_value()) {
|
||||
m_waiter.timer = add_timer(*deadline, [this] {
|
||||
// Timeout: resume the waiter with timed_out set.
|
||||
if (!m_waiter.active()) {
|
||||
const WaiterId id = w.id;
|
||||
w.timer = add_timer(*deadline, [this, id] {
|
||||
// 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;
|
||||
}
|
||||
if (m_waiter.timed_out != nullptr) {
|
||||
*m_waiter.timed_out = true;
|
||||
if (it->timed_out != nullptr) {
|
||||
*it->timed_out = true;
|
||||
}
|
||||
auto cont2 = m_waiter.cont;
|
||||
m_waiter = Waiter{};
|
||||
auto cont2 = it->cont;
|
||||
m_waiters.erase(it);
|
||||
m_executor.post(cont2);
|
||||
});
|
||||
}
|
||||
m_waiters.push_back(std::move(w));
|
||||
}
|
||||
|
||||
ExecutionContext::TimerId ExecutionContext::add_timer(
|
||||
@@ -112,15 +120,17 @@ namespace Void::Fsm::Detail
|
||||
|
||||
void ExecutionContext::deliver(EventEnvelope& env)
|
||||
{
|
||||
// Match a waiting state first (lets a multi-type waiter receive an
|
||||
// event without it ever sitting in a buffer).
|
||||
if (m_waiter.active() && m_waiter.wants(*env.type)) {
|
||||
*m_waiter.slot = std::move(env);
|
||||
cancel_timer(m_waiter.timer);
|
||||
auto cont = m_waiter.cont;
|
||||
m_waiter = Waiter{};
|
||||
m_executor.post(cont);
|
||||
return;
|
||||
// Route to the first parked waiter (FIFO) that wants this type — lets
|
||||
// it receive the event without ever sitting in a buffer.
|
||||
for (auto it = m_waiters.begin(); it != m_waiters.end(); ++it) {
|
||||
if (it->wants(*env.type)) {
|
||||
*it->slot = std::move(env);
|
||||
cancel_timer(it->timer);
|
||||
auto cont = it->cont;
|
||||
m_waiters.erase(it);
|
||||
m_executor.post(cont);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Otherwise buffer for a later await.
|
||||
m_buffers[std::type_index{*env.type}].push_back(std::move(env.value));
|
||||
|
||||
Reference in new issue
Block a user