amos 2f32624d3b Batch 18 (20261026-C-hwe-codegen-18): add 100 calibrated HWE code-gen tasks; pause production for rollout
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>
2026-10-27 17:52:13 +08:00

261 lines
5.8 KiB
C++

#include "interlock.hpp"
#include <limits>
namespace maskwrite {
Interlock::Interlock()
: words_{}, sampled_{}, pending_{}, touched_{}, queue_{},
owner_(-1), phase_(IDLE), count_(0), generation_(0)
{
}
bool Interlock::actor_ok(Number actor)
{
return actor >= 0 && actor < 8;
}
bool Interlock::reg_ok(Number reg)
{
return reg >= 0 && reg < static_cast<Number>(RegisterCount);
}
bool Interlock::kind_ok(Number kind)
{
return kind >= SET && kind <= REPLACE;
}
bool Interlock::word_ok(Number value)
{
return value >= 0 && value <= 4294967295LL;
}
Status Interlock::require_owner(Number actor) const
{
if (!actor_ok(actor)) {
return RANGE;
}
if (owner_ != actor) {
return OWNER;
}
return OK;
}
void Interlock::clear_transaction()
{
owner_ = -1;
phase_ = IDLE;
count_ = 0;
for (std::size_t reg = 0; reg < RegisterCount; ++reg) {
sampled_[reg] = 0;
pending_[reg] = 0;
touched_[reg] = false;
}
for (Operation& operation : queue_) {
operation.reg = 0;
operation.kind = 0;
operation.mask = 0;
operation.value = 0;
}
}
Status Interlock::reset()
{
for (std::uint32_t& word : words_) {
word = 0;
}
generation_ = 0;
clear_transaction();
return OK;
}
Status Interlock::load(Number reg, Number value)
{
if (!reg_ok(reg)) {
return RANGE;
}
if (!word_ok(value)) {
return RANGE;
}
if (owner_ != -1) {
return BUSY;
}
words_[static_cast<std::size_t>(reg)] = static_cast<std::uint32_t>(value);
return OK;
}
Status Interlock::begin(Number actor)
{
if (!actor_ok(actor)) {
return RANGE;
}
if (owner_ != -1 && phase_ != CAPTURED) {
return BUSY;
}
clear_transaction();
owner_ = actor;
phase_ = BUILD;
return OK;
}
Status Interlock::stage(Number actor, Number reg, Number kind,
Number mask, Number value)
{
if (!actor_ok(actor)) {
return RANGE;
}
if (!reg_ok(reg)) {
return RANGE;
}
if (!kind_ok(kind)) {
return RANGE;
}
if (!word_ok(mask)) {
return RANGE;
}
if (!word_ok(value)) {
return RANGE;
}
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != BUILD) {
return PHASE;
}
if (count_ == QueueLimit) {
return FULL;
}
Operation next;
next.reg = reg;
next.kind = kind;
next.mask = static_cast<std::uint32_t>(mask);
next.value = static_cast<std::uint32_t>(value);
queue_[count_] = next;
++count_;
return OK;
}
void Interlock::replay(const Operation& operation)
{
std::size_t reg = static_cast<std::size_t>(operation.reg);
std::uint32_t current = pending_[reg];
switch (operation.kind) {
case SET:
current |= operation.mask;
break;
case CLEAR:
current &= ~operation.mask;
break;
case REPLACE:
current = (current & ~operation.mask) | operation.value;
break;
default:
break;
}
pending_[reg] = current;
touched_[reg] = true;
}
Status Interlock::capture(Number actor)
{
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != BUILD) {
return PHASE;
}
if (count_ == 0) {
return EMPTY;
}
for (std::size_t reg = 0; reg < RegisterCount; ++reg) {
sampled_[reg] = words_[reg];
pending_[reg] = words_[reg];
touched_[reg] = false;
}
for (std::size_t index = 0; index < count_; ++index) {
replay(queue_[index]);
}
phase_ = CAPTURED;
return OK;
}
Status Interlock::commit(Number actor)
{
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != CAPTURED) {
return PHASE;
}
for (std::size_t reg = 0; reg < RegisterCount; ++reg) {
if (touched_[reg]) {
words_[reg] = pending_[reg];
}
}
++generation_;
clear_transaction();
return OK;
}
Status Interlock::abort(Number actor)
{
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
clear_transaction();
return OK;
}
Status Interlock::word(Number reg, std::uint32_t& result) const
{
if (!reg_ok(reg)) {
return RANGE;
}
result = words_[static_cast<std::size_t>(reg)];
return OK;
}
Status Interlock::prepared(Number actor, Number reg, std::uint32_t& result) const
{
if (!actor_ok(actor)) {
return RANGE;
}
if (!reg_ok(reg)) {
return RANGE;
}
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != CAPTURED) {
return PHASE;
}
result = pending_[static_cast<std::size_t>(reg)];
return OK;
}
Status Interlock::changed(Number actor, Number reg, std::uint32_t& result) const
{
if (!actor_ok(actor)) {
return RANGE;
}
if (!reg_ok(reg)) {
return RANGE;
}
Status permission = require_owner(actor);
if (permission != OK) {
return permission;
}
if (phase_ != CAPTURED) {
return PHASE;
}
std::size_t index = static_cast<std::size_t>(reg);
result = sampled_[index] ^ pending_[index];
return OK;
}
Status Interlock::operation(Number index, Operation& result) const
{
if (index < 0 || index >= static_cast<Number>(QueueLimit)) {
return RANGE;
}
if (static_cast<std::size_t>(index) >= count_) {
return EMPTY;
}
result = queue_[static_cast<std::size_t>(index)];
return OK;
}
State Interlock::state() const
{
State result;
result.owner = owner_;
result.phase = phase_;
result.count = static_cast<Number>(count_);
result.generation = generation_;
return result;
}
}