#include "engine_internal.hpp" namespace deferred { bool Batch::empty() const { return count == 0; } void Batch::clear() { count = 0; sum = 0; ticket = 0; } void Batch::append(Number value, Number first_ticket) { if (empty()) { ticket = first_ticket; } count += 1; sum += value; } Number Batch::transformed(Number cookie) const { return sum + count * cookie; } void Ledger::account(const Batch& batch, Number cookie) { batches += 1; count += batch.count; sum += batch.sum; value += batch.transformed(cookie); } void Ledger::clear() { batches = 0; count = 0; sum = 0; value = 0; } void Node::install(int new_priority, int new_quota, Number new_cookie) { generation += 1; life = LIVE; priority = new_priority; quota = new_quota; cookie = new_cookie; depth = 0; pending.clear(); active.clear(); ledger.clear(); } void Node::retire() { pending.clear(); active.clear(); depth = 0; life = DEAD; } Engine::Engine() : state_(new Impl) { } Engine::~Engine() { delete state_; } bool Engine::Impl::valid_id(int id) { return id >= 0 && id < 16; } bool Engine::Impl::valid_payload(Number value) { return value >= -1000000 && value <= 1000000; } int Engine::Impl::reject(int status) { errors += 1; return status; } int Engine::Impl::lifecycle(int id) const { if (!valid_id(id)) { return BAD_ID; } if (nodes[id].life == VACANT) { return MISSING; } if (nodes[id].life == DEAD) { return DEAD_SLOT; } return OK; } int Engine::Impl::require_live(int id) { const int status = lifecycle(id); if (status != OK) { return reject(status); } return OK; } int Engine::install(int id, int priority, int quota, Number cookie) { Impl& state = *state_; if (!Impl::valid_id(id)) { return state.reject(BAD_ID); } if (priority < 0 || priority > 3) { return state.reject(BAD_VALUE); } if (quota < 1 || quota > 32) { return state.reject(BAD_VALUE); } if (!Impl::valid_payload(cookie)) { return state.reject(BAD_VALUE); } Node& node = state.nodes[id]; if (node.life == LIVE) { return state.reject(EXISTS); } node.install(priority, quota, cookie); state.installs += 1; state.record(1, id, Batch{}); return OK; } int Engine::post(int id, Number value) { Impl& state = *state_; if (!Impl::valid_id(id)) { return state.reject(BAD_ID); } if (!Impl::valid_payload(value)) { return state.reject(BAD_VALUE); } const int status = state.require_live(id); if (status != OK) { return status; } Node& node = state.nodes[id]; if (node.pending.count >= node.quota) { return state.reject(LIMIT); } const bool fresh = node.pending.empty(); node.pending.append(value, state.ticket); if (fresh) { state.ticket += 1; } return OK; } int Engine::Impl::acquire_gate(int id, bool synchronize) { const int status = require_live(id); if (status != OK) { return status; } Node& node = nodes[id]; if (synchronize && !node.active.empty()) { return reject(BUSY); } if (node.depth == 4) { return reject(LIMIT); } node.depth += 1; return OK; } int Engine::disable(int id) { return state_->acquire_gate(id, true); } int Engine::disable_nosync(int id) { return state_->acquire_gate(id, false); } int Engine::enable(int id) { Impl& state = *state_; const int status = state.require_live(id); if (status != OK) { return status; } Node& node = state.nodes[id]; if (node.depth == 0) { return state.reject(UNBALANCED); } node.depth = 0; return OK; } int Engine::kill(int id) { Impl& state = *state_; const int status = state.require_live(id); if (status != OK) { return status; } if (state.active_id == id) { state.complete(id); } state.nodes[id].retire(); state.kills += 1; state.record(4, id, Batch{}); return OK; } }