#include "storage.hpp" #include #include namespace instance { Ledger::Ledger() : impl_(std::make_shared()) { } bool Ledger::Impl::valid_slot(Integer value) { if (value < 0) { return false; } return value < static_cast(8); } bool Ledger::Impl::valid_actor(Integer value) { if (value < 0) { return false; } return value < static_cast(16); } Code Ledger::Impl::resolve(Handle handle) const { if (!valid_slot(handle.slot)) { return Code::RANGE; } const Slot &slot = slots[static_cast(handle.slot)]; if (!slot.active) { return Code::DEAD; } if (slot.serial != handle.epoch) { return Code::STALE; } if (slot.token != handle.token) { return Code::TOKEN; } return Code::OK; } Code Ledger::Impl::resolve_actor(Handle handle, Integer participant) const { if (!valid_slot(handle.slot)) { return Code::RANGE; } if (!valid_actor(participant)) { return Code::RANGE; } return resolve(handle); } bool Ledger::Impl::free_actor(Integer participant) const { const Actor &actor = actors[static_cast(participant)]; return actor.kind == 0; } bool Ledger::Impl::owns(const Slot &slot, Integer participant) const { return slot.owner == participant; } bool Ledger::Impl::queued(const Slot &slot, Integer participant) const { auto found = std::find(slot.queue.begin(), slot.queue.end(), participant); return found != slot.queue.end(); } void Ledger::Impl::bind(Integer participant, Integer kind, Integer slot_index) { Actor &actor = actors[static_cast(participant)]; const Slot &slot = slots[static_cast(slot_index)]; actor.kind = kind; actor.slot = slot_index; actor.epoch = slot.serial; } void Ledger::Impl::unbind(Integer participant) { Actor &actor = actors[static_cast(participant)]; actor.kind = 0; actor.slot = -1; actor.epoch = 0; } void Ledger::Impl::remove_waiter(Slot &slot, Integer participant) { auto found = std::find(slot.queue.begin(), slot.queue.end(), participant); if (found != slot.queue.end()) { slot.queue.erase(found); } } void Ledger::Impl::clear_payload(Slot &slot) { slot.active = false; slot.token = 0; slot.owner = -1; slot.value = 0; slot.queue.clear(); } Reply Ledger::init(Integer slot_index, Integer token) { Reply answer; if (!Impl::valid_slot(slot_index)) { answer.code = Code::RANGE; return answer; } Impl::Slot &slot = impl_->slots[static_cast(slot_index)]; if (slot.active) { answer.code = Code::EXISTS; return answer; } if (slot.serial == std::numeric_limits::max()) { answer.code = Code::OVERFLOW; return answer; } const Integer next = 1; impl_->clear_payload(slot); slot.active = true; slot.serial = next; slot.token = token; answer.value = slot.serial; return answer; } Code Ledger::destroy(Handle handle) { const Code code = impl_->resolve(handle); if (code != Code::OK) { return code; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (slot.owner != -1) { return Code::BUSY; } impl_->clear_payload(slot); return Code::OK; } Code Ledger::retag(Handle handle, Integer token) { const Code code = impl_->resolve(handle); if (code != Code::OK) { return code; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (slot.owner != -1 || !slot.queue.empty()) { return Code::BUSY; } slot.token = token; return Code::OK; } }