#include "storage.hpp" namespace instance { Resource Ledger::Impl::export_slot(Integer index) const { Resource result; if (!valid_slot(index)) { result.code = Code::RANGE; return result; } const Slot &slot = slots[static_cast(index)]; result.active = slot.active; result.epoch = slot.serial; result.token = slot.token; result.owner = slot.owner; result.value = 0; result.waiters.reserve(slot.queue.size()); for (Integer participant : slot.queue) { result.waiters.push_back(participant); } return result; } Participant Ledger::Impl::export_actor(Integer participant) const { Participant result; if (!valid_actor(participant)) { result.code = Code::RANGE; return result; } const Actor &actor = actors[static_cast(participant)]; result.kind = actor.kind; result.slot = actor.slot; result.epoch = actor.epoch; return result; } Reply Ledger::read(Handle handle) const { Reply result; result.code = impl_->resolve(handle); if (result.code != Code::OK) { return result; } const Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; result.value = 0; return result; } Resource Ledger::inspect(Integer slot) const { return impl_->export_slot(slot); } Participant Ledger::actor(Integer participant) const { return impl_->export_actor(participant); } Summary Ledger::summary() const { Summary result; for (const Impl::Slot &slot : impl_->slots) { if (slot.active) { result.active += 1; } if (slot.owner != -1) { result.owners += 1; } result.waiting += static_cast(slot.queue.size()); } return result; } Reply Ledger::position(Handle handle, Integer participant) const { Reply answer; answer.code = impl_->resolve_actor(handle, participant); if (answer.code != Code::OK) { return answer; } const Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (impl_->owns(slot, participant)) { answer.value = -1; return answer; } Integer index = 0; for (Integer waiter : slot.queue) { if (waiter == participant) { answer.value = index; return answer; } index += 1; } answer.code = Code::NOT_WAITING; return answer; } }