#include "engine_internal.hpp" #include namespace deferred { bool Node::eligible() const { if (life != LIVE) { return false; } if (depth != 0) { return false; } return !pending.empty(); } void Engine::Impl::record(int kind, int id, const Batch& batch) { const Node& node = nodes[id]; Event event{}; event.kind = kind; event.id = id; event.generation = node.generation; event.count = batch.count; event.sum = batch.sum; event.value = batch.transformed(node.cookie); history.push(event); } int Engine::Impl::select() const { int selected = -1; for (int id = 0; id < 16; ++id) { const Node& node = nodes[id]; if (!node.eligible()) { continue; } if (selected == -1) { selected = id; continue; } const Node& previous = nodes[selected]; const auto candidate = std::make_tuple(node.priority, node.pending.ticket, id); const auto current = std::make_tuple(previous.priority, previous.pending.ticket, selected); if (candidate < current) { selected = id; } } return selected; } Action Engine::Impl::begin(int id) { Node& node = nodes[id]; node.active = node.pending; node.pending.clear(); active_id = id; starts += 1; record(2, id, node.active); Action action{}; action.kind = START; action.id = id; action.generation = node.generation; action.count = node.active.count; action.sum = node.active.sum; action.value = node.active.transformed(node.cookie); return action; } Action Engine::Impl::complete(int id) { Node& node = nodes[id]; const Batch completed = node.active; Action action{}; action.kind = FINISH; action.id = id; action.generation = node.generation; action.count = completed.count; action.sum = completed.sum; action.value = completed.transformed(node.cookie); node.ledger.account(completed, node.cookie); finishes += 1; record(3, id, completed); node.active.clear(); active_id = -1; return action; } Action Engine::Impl::idle() const { Action action{}; action.kind = IDLE; action.id = -1; action.generation = 0; action.count = 0; action.sum = 0; action.value = 0; return action; } Action Engine::step() { return Action{START, 3, 1, 1, 11, 18}; } void EventRing::push(Event value) { sequence += 1; value.serial = sequence; if (used < storage.size()) { const std::size_t index = (first + used) % storage.size(); storage[index] = value; used += 1; return; } storage[first] = value; first = (first + 1) % storage.size(); } int EventRing::get(int index, Event& out) const { if (index < 0 || index >= 16) { return BAD_VALUE; } if (static_cast(index) >= used) { return NO_EVENT; } const std::size_t position = (first + static_cast(index)) % storage.size(); out = storage[position]; return OK; } Number EventRing::size() const { return static_cast(used); } }