#include "storage.hpp" #include namespace instance { Code Ledger::lock(Handle handle, Integer participant) { const Code code = impl_->resolve_actor(handle, participant); if (code != Code::OK) { return code; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (impl_->owns(slot, participant)) { return Code::OWNER; } if (impl_->queued(slot, participant)) { return Code::QUEUED; } if (!impl_->free_actor(participant)) { return Code::ENGAGED; } if (slot.owner == -1 && slot.queue.empty()) { slot.owner = participant; impl_->bind(participant, 1, handle.slot); return Code::OK; } slot.queue.push_back(participant); impl_->bind(participant, 2, handle.slot); return Code::WAIT; } Code Ledger::unlock(Handle handle, Integer participant) { const Code code = impl_->resolve_actor(handle, participant); if (code != Code::OK) { return code; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (!impl_->owns(slot, participant)) { return Code::NOT_OWNER; } slot.owner = -1; impl_->unbind(participant); return Code::OK; } Code Ledger::claim(Handle handle, Integer participant) { const Code code = impl_->resolve_actor(handle, participant); if (code != Code::OK) { return code; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (impl_->owns(slot, participant)) { return Code::OWNER; } if (!impl_->queued(slot, participant)) { return Code::NOT_WAITING; } if (slot.owner != -1) { return Code::BUSY; } if (slot.queue.front() != participant) { return Code::TURN; } slot.queue.pop_front(); slot.owner = participant; impl_->bind(participant, 1, handle.slot); return Code::OK; } Code Ledger::cancel(Handle handle, Integer participant) { const Code code = impl_->resolve_actor(handle, participant); if (code != Code::OK) { return code; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (impl_->owns(slot, participant)) { return Code::OWNER; } if (!impl_->queued(slot, participant)) { return Code::NOT_WAITING; } impl_->remove_waiter(slot, participant); impl_->unbind(participant); return Code::OK; } Code Ledger::add(Handle handle, Integer participant, Integer delta) { const Code code = impl_->resolve_actor(handle, participant); if (code != Code::OK) { return code; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (!impl_->owns(slot, participant)) { return Code::NOT_OWNER; } if (delta > 0) { const Integer maximum = std::numeric_limits::max(); if (slot.value > maximum - delta) { return Code::OVERFLOW; } } if (delta < 0) { const Integer minimum = std::numeric_limits::min(); if (slot.value < minimum - delta) { return Code::OVERFLOW; } } slot.value += delta; return Code::OK; } Code Ledger::try_lock(Handle handle, Integer participant) { const Code code = impl_->resolve_actor(handle, participant); if (code != Code::OK) { return code; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (impl_->owns(slot, participant)) { return Code::OWNER; } if (impl_->queued(slot, participant)) { return Code::QUEUED; } if (!impl_->free_actor(participant)) { return Code::ENGAGED; } if (slot.owner != -1 || !slot.queue.empty()) { return Code::BUSY; } slot.owner = participant; impl_->bind(participant, 1, handle.slot); return Code::OK; } Code Ledger::handoff(Handle handle, Integer owner, Integer target) { if (!Impl::valid_slot(handle.slot)) { return Code::RANGE; } if (!Impl::valid_actor(owner)) { return Code::RANGE; } if (!Impl::valid_actor(target)) { return Code::RANGE; } const Code code = impl_->resolve(handle); if (code != Code::OK) { return code; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (!impl_->owns(slot, owner)) { return Code::NOT_OWNER; } if (!impl_->queued(slot, target)) { return Code::NOT_WAITING; } if (slot.queue.front() != target) { return Code::TURN; } impl_->unbind(owner); slot.queue.pop_front(); slot.owner = target; impl_->bind(target, 1, handle.slot); return Code::OK; } Reply Ledger::cancel_all(Handle handle) { Reply answer; answer.code = impl_->resolve(handle); if (answer.code != Code::OK) { return answer; } Impl::Slot &slot = impl_->slots[static_cast(handle.slot)]; if (slot.owner != -1) { answer.code = Code::BUSY; return answer; } answer.value = static_cast(slot.queue.size()); for (Integer participant : slot.queue) { impl_->unbind(participant); } slot.queue.clear(); return answer; } }