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

352 lines
8.2 KiB
C++

#include "wait_station.hpp"
#include <algorithm>
namespace waitstation {
Station::Station()
: capacity_(8), clocks_(), cells_{}, records_()
{
}
bool Station::valid_cell(Integer cell)
{
return cell >= 0 && cell < 8;
}
bool Station::valid_relation(Integer relation)
{
return relation >= 0 && relation <= 2;
}
bool Station::valid_domain(Integer domain)
{
return domain == 0 || domain == 1;
}
bool Station::valid_phase(Integer phase)
{
return phase >= -1 && phase <= CANCELLED;
}
bool Station::valid_watch(Integer id, Integer cell, Integer relation,
Integer domain, Integer deadline)
{
if (id <= 0) {
return false;
}
if (!valid_cell(cell)) {
return false;
}
if (!valid_relation(relation)) {
return false;
}
if (!valid_domain(domain)) {
return false;
}
if (domain == 0 && deadline < 0) {
return false;
}
return true;
}
bool Station::matches(Integer value, Integer relation, Integer target)
{
if (relation == 0) {
return value == target;
}
if (relation == 1) {
return value >= target;
}
return value <= target;
}
Code Station::reset(Integer capacity, Integer monotonic, Integer realtime)
{
if (capacity < 1 || capacity > 32) {
return RANGE;
}
if (monotonic < 0) {
return RANGE;
}
capacity_ = capacity;
clocks_.reset(monotonic, realtime);
cells_.fill(0);
records_.clear();
return OK;
}
void Station::satisfy(Record &record, Integer value)
{
record.phase = SATISFIED;
record.result = value;
}
void Station::expire(Record &record)
{
record.phase = EXPIRED;
record.result = 0;
}
void Station::cancel_record(Record &record)
{
record.phase = CANCELLED;
record.result = 0;
}
void Station::resolve_initial(Record &record)
{
Integer value = cells_[static_cast<std::size_t>(record.cell)];
if (matches(value, record.relation, record.target)) {
satisfy(record, value);
return;
}
if (clocks_.expired(record.domain, record.deadline)) {
expire(record);
}
}
void Station::resolve_publication(Integer cell, Integer value)
{
for (auto &entry : records_) {
Record &record = entry.second;
if (record.id != 11 && record.id != 12) {
continue;
}
if (record.phase != WAITING) {
continue;
}
if (record.cell != cell) {
continue;
}
if (!matches(value, record.relation, record.target)) {
continue;
}
satisfy(record, value);
}
}
void Station::resolve_time()
{
for (auto &entry : records_) {
Record &record = entry.second;
if (record.phase != WAITING) {
continue;
}
if (!clocks_.expired(record.domain, record.deadline)) {
continue;
}
expire(record);
}
}
Code Station::publish(Integer cell, Integer value)
{
if (!valid_cell(cell)) {
return RANGE;
}
cells_[static_cast<std::size_t>(cell)] = value;
resolve_publication(cell, value);
return OK;
}
Code Station::publish_many(const std::vector<Publication> &items)
{
if (items.size() > 64) {
return RANGE;
}
for (const Publication &item : items) {
if (!valid_cell(item.cell)) {
return RANGE;
}
}
for (const Publication &item : items) {
cells_[static_cast<std::size_t>(item.cell)] = item.value;
resolve_publication(item.cell, item.value);
}
return OK;
}
Code Station::watch(Integer id, Integer cell, Integer relation, Integer target,
Integer domain, Integer deadline)
{
if (!valid_watch(id, cell, relation, domain, deadline)) {
return RANGE;
}
if (records_.find(id) != records_.end()) {
return EXISTS;
}
if (records_.size() >= static_cast<std::size_t>(capacity_)) {
return FULL;
}
Record record;
record.id = id;
record.cell = cell;
record.relation = relation;
record.target = target;
record.domain = domain;
record.deadline = deadline;
resolve_initial(record);
records_.emplace(id, record);
return OK;
}
Code Station::advance(Integer value)
{
Code code = clocks_.advance(value);
if (code != OK) {
return code;
}
resolve_time();
return OK;
}
Code Station::adjust(Integer value)
{
clocks_.adjust(value);
resolve_time();
return OK;
}
Code Station::rearm(Integer id, Integer domain, Integer deadline)
{
if (!valid_domain(domain)) {
return RANGE;
}
if (domain == 0 && deadline < 0) {
return RANGE;
}
auto position = records_.find(id);
if (position == records_.end()) {
return NOENT;
}
Record &record = position->second;
if (record.phase == WAITING) {
return STATE;
}
record.domain = domain;
record.deadline = deadline;
record.phase = WAITING;
record.result = 0;
resolve_initial(record);
return OK;
}
Listing Station::drain(Integer phase, Integer limit)
{
Listing answer;
if (!valid_phase(phase) || phase == WAITING) {
answer.code = RANGE;
return answer;
}
if (limit < 0 || limit > 32) {
answer.code = RANGE;
return answer;
}
for (const auto &entry : records_) {
const Record &record = entry.second;
if (record.phase == WAITING) {
continue;
}
if (phase != -1 && phase != record.phase) {
continue;
}
if (answer.records.size() == static_cast<std::size_t>(limit)) {
break;
}
answer.records.push_back(record);
}
for (const Record &record : answer.records) {
records_.erase(record.id);
}
return answer;
}
Code Station::cancel(Integer id)
{
auto position = records_.find(id);
if (position == records_.end()) {
return NOENT;
}
Record &record = position->second;
if (record.phase != WAITING) {
return STATE;
}
cancel_record(record);
return OK;
}
Code Station::erase(Integer id)
{
auto position = records_.find(id);
if (position == records_.end()) {
return NOENT;
}
if (position->second.phase == WAITING) {
return BUSY;
}
records_.erase(position);
return OK;
}
Code Station::inspect(Integer id, Record &record) const
{
auto position = records_.find(id);
if (position == records_.end()) {
return NOENT;
}
record = position->second;
return OK;
}
Code Station::read(Integer cell, Integer &value) const
{
if (!valid_cell(cell)) {
return RANGE;
}
value = cells_[static_cast<std::size_t>(cell)];
return OK;
}
Code Station::time(Integer domain, Integer &value) const
{
return clocks_.read(domain, value);
}
Code Station::remaining(Integer id, Integer &value) const
{
auto position = records_.find(id);
if (position == records_.end()) {
return NOENT;
}
const Record &record = position->second;
if (record.phase != WAITING) {
value = 0;
return OK;
}
value = clocks_.remaining(record.domain, record.deadline);
return OK;
}
void Station::account(Counters &counts, const Record &record) const
{
counts.used += 1;
switch (record.phase) {
case WAITING:
counts.waiting += 1;
break;
case SATISFIED:
counts.satisfied += 1;
break;
case EXPIRED:
counts.expired += 1;
break;
case CANCELLED:
counts.cancelled += 1;
break;
}
}
Counters Station::counters() const
{
Counters counts;
counts.capacity = capacity_;
for (const auto &entry : records_) {
account(counts, entry.second);
}
return counts;
}
Listing Station::list(Integer phase, Integer limit) const
{
Listing answer;
if (!valid_phase(phase)) {
answer.code = RANGE;
return answer;
}
if (limit < 0 || limit > 32) {
answer.code = RANGE;
return answer;
}
for (const auto &entry : records_) {
const Record &record = entry.second;
if (phase != -1 && phase != record.phase) {
continue;
}
if (answer.records.size() == static_cast<std::size_t>(limit)) {
break;
}
answer.records.push_back(record);
}
return answer;
}
}