100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth range given to batch 22), each with at least one independent contract-edge defect and plain public docs. Claude reviewed 100/100 USABLE against current digests; no ID or source overlap with main or batch 22. Difficulty is unmeasured. progress.md records the wrap-up: all Codex sessions and Qwen schedulers stopped for the user's rollout, Qwen results so far (57 HARD of 373), and what is left on each batch branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
176 lines
5.1 KiB
C++
176 lines
5.1 KiB
C++
#include "storage.hpp"
|
|
#include <limits>
|
|
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<std::size_t>(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<std::size_t>(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<std::size_t>(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<std::size_t>(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<std::size_t>(handle.slot)];
|
|
if (!impl_->owns(slot, participant)) {
|
|
return Code::NOT_OWNER;
|
|
}
|
|
if (delta > 0) {
|
|
const Integer maximum = std::numeric_limits<Integer>::max();
|
|
if (slot.value > maximum - delta) {
|
|
return Code::OVERFLOW;
|
|
}
|
|
}
|
|
if (delta < 0) {
|
|
const Integer minimum = std::numeric_limits<Integer>::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<std::size_t>(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<std::size_t>(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<std::size_t>(handle.slot)];
|
|
if (slot.owner != -1) {
|
|
answer.code = Code::BUSY;
|
|
return answer;
|
|
}
|
|
answer.value = static_cast<Integer>(slot.queue.size());
|
|
for (Integer participant : slot.queue) {
|
|
impl_->unbind(participant);
|
|
}
|
|
slot.queue.clear();
|
|
return answer;
|
|
}
|
|
}
|