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>
222 lines
6.7 KiB
C++
222 lines
6.7 KiB
C++
#include "engine.hpp"
|
|
#include <algorithm>
|
|
namespace ordered {
|
|
struct Engine::Store {
|
|
struct Item {
|
|
JobView v;
|
|
};
|
|
struct Bucket {
|
|
Number id;
|
|
Number next = 1;
|
|
Stats s;
|
|
std::vector<Item> items;
|
|
};
|
|
std::vector<Bucket> buckets;
|
|
std::vector<Event> history;
|
|
Bucket* find(Number q) {
|
|
for (auto& b : buckets) {
|
|
if (b.id == q) return &b;
|
|
}
|
|
return nullptr;
|
|
}
|
|
Item* lookup(Bucket& b, Number j) {
|
|
for (auto& x : b.items) {
|
|
if (x.v.id == j) return &x;
|
|
}
|
|
return nullptr;
|
|
}
|
|
Code locate(Number q, Number j, Bucket*& b, Item*& x) {
|
|
if (q < 1 || q > 8 || j < 1 || j > 1000000000) return Code::BadValue;
|
|
b = find(q);
|
|
if (!b) return Code::MissingQueue;
|
|
x = lookup(*b, j);
|
|
if (!x) return Code::MissingJob;
|
|
return Code::Ok;
|
|
}
|
|
void log(Bucket& b, Item* x, Kind kind) {
|
|
Event e;
|
|
e.serial = static_cast<Number>(history.size()) + 1;
|
|
e.queue = b.id;
|
|
e.kind = kind;
|
|
e.ticks = b.s.ticks;
|
|
if (x) {
|
|
e.job = x->v.id;
|
|
e.ticket = x->v.ticket;
|
|
e.remaining = x->v.remaining;
|
|
e.value = x->v.value;
|
|
}
|
|
history.push_back(e);
|
|
}
|
|
Work advance(Bucket& b, Number budget) {
|
|
Work w;
|
|
while (budget > 0) {
|
|
Item* chosen = nullptr;
|
|
for (auto& x : b.items) {
|
|
if (x.v.state == State::Running) {
|
|
chosen = &x;
|
|
break;
|
|
}
|
|
if (x.v.state == State::Waiting &&
|
|
(!chosen || x.v.ticket < chosen->v.ticket)) chosen = &x;
|
|
}
|
|
if (!chosen) break;
|
|
if (chosen->v.state == State::Waiting) {
|
|
chosen->v.state = State::Running;
|
|
--b.s.waiting;
|
|
b.s.running = 1;
|
|
++b.s.started;
|
|
log(b, chosen, Kind::Start);
|
|
}
|
|
Number delta = std::min(budget, chosen->v.remaining);
|
|
budget -= delta;
|
|
chosen->v.remaining -= delta;
|
|
b.s.ticks += delta;
|
|
w.spent += delta;
|
|
if (!chosen->v.remaining) {
|
|
chosen->v.state = State::Complete;
|
|
++b.s.completed;
|
|
b.s.running = 0;
|
|
b.s.total_value += chosen->v.value;
|
|
++w.finished;
|
|
log(b, chosen, Kind::Finish);
|
|
}
|
|
}
|
|
return w;
|
|
}
|
|
};
|
|
Engine::Engine() : store_(new Store) {}
|
|
Engine::~Engine() = default;
|
|
Code Engine::create(Number q, Number capacity) {
|
|
if (q < 1 || q > 8 || capacity < 1 || capacity > 64) return Code::BadValue;
|
|
if (store_->find(q)) return Code::Exists;
|
|
Store::Bucket b;
|
|
b.id = q;
|
|
b.s.capacity = capacity;
|
|
store_->buckets.push_back(b);
|
|
return Code::Ok;
|
|
}
|
|
Code Engine::submit(Number q, Number j, Number units, Number value) {
|
|
if (q < 1 || q > 8 || j < 1 || j > 1000000000 || units < 1 ||
|
|
units > 1000000 || value < -1000000000 || value > 1000000000) return Code::BadValue;
|
|
auto* b = store_->find(q);
|
|
if (!b) return Code::MissingQueue;
|
|
if (b->s.closed) return Code::Closed;
|
|
if (store_->lookup(*b, j)) return Code::Exists;
|
|
if (b->s.waiting + b->s.running >= b->s.capacity || b->items.size() >= 256) return Code::Full;
|
|
Store::Item x;
|
|
x.v.id = j;
|
|
x.v.total = units;
|
|
x.v.remaining = units;
|
|
x.v.value = value;
|
|
x.v.ticket = b->next++;
|
|
b->items.push_back(x);
|
|
++b->s.waiting;
|
|
++b->s.accepted;
|
|
++b->s.retained;
|
|
store_->log(*b, &b->items.back(), Kind::Accept);
|
|
return Code::Ok;
|
|
}
|
|
Work Engine::work(Number q, Number budget) {
|
|
Work w;
|
|
if (q < 1 || q > 8 || budget < 0 || budget > 1000000) {
|
|
w.code = Code::BadValue;
|
|
return w;
|
|
}
|
|
auto* b = store_->find(q);
|
|
if (!b) w.code = Code::MissingQueue;
|
|
else if (b->s.closed) w.code = Code::Closed;
|
|
else return store_->advance(*b, budget);
|
|
return w;
|
|
}
|
|
Code Engine::cancel(Number q, Number j) {
|
|
Store::Bucket* b = nullptr;
|
|
Store::Item* x = nullptr;
|
|
Code c = store_->locate(q, j, b, x);
|
|
if (c != Code::Ok) return c;
|
|
if (x->v.state == State::Running) return Code::Busy;
|
|
if (x->v.state != State::Waiting) return Code::BadState;
|
|
x->v.state = State::Cancelled;
|
|
--b->s.waiting;
|
|
++b->s.cancelled;
|
|
store_->log(*b, x, Kind::Cancel);
|
|
return Code::Ok;
|
|
}
|
|
Code Engine::release(Number q, Number j) {
|
|
Store::Bucket* b = nullptr;
|
|
Store::Item* x = nullptr;
|
|
Code c = store_->locate(q, j, b, x);
|
|
if (c != Code::Ok) return c;
|
|
if (x->v.state == State::Waiting || x->v.state == State::Running) return Code::Busy;
|
|
store_->log(*b, x, Kind::Release);
|
|
b->items.erase(std::remove_if(b->items.begin(), b->items.end(),
|
|
[j](const Store::Item& y) { return y.v.id == j; }), b->items.end());
|
|
++b->s.released;
|
|
--b->s.retained;
|
|
return Code::Ok;
|
|
}
|
|
Work Engine::destroy(Number q) {
|
|
Work w;
|
|
if (q < 1 || q > 8) {
|
|
w.code = Code::BadValue;
|
|
return w;
|
|
}
|
|
auto* b = store_->find(q);
|
|
if (!b) {
|
|
w.code = Code::MissingQueue;
|
|
return w;
|
|
}
|
|
if (!b->s.closed) {
|
|
Number remaining = 0;
|
|
for (const auto& x : b->items) {
|
|
if (x.v.state == State::Waiting || x.v.state == State::Running) remaining += x.v.remaining;
|
|
}
|
|
w = store_->advance(*b, remaining);
|
|
b->s.closed = 1;
|
|
store_->log(*b, nullptr, Kind::Close);
|
|
}
|
|
return w;
|
|
}
|
|
JobView Engine::job(Number q, Number j) const {
|
|
JobView v;
|
|
Store::Bucket* b = nullptr;
|
|
Store::Item* x = nullptr;
|
|
v.code = store_->locate(q, j, b, x);
|
|
if (v.code == Code::Ok) return x->v;
|
|
return v;
|
|
}
|
|
Stats Engine::stats(Number q) const {
|
|
Stats s;
|
|
if (q < 1 || q > 8) s.code = Code::BadValue;
|
|
else {
|
|
auto* b = store_->find(q);
|
|
if (!b) s.code = Code::MissingQueue;
|
|
else return b->s;
|
|
}
|
|
return s;
|
|
}
|
|
Jobs Engine::jobs(Number q) const {
|
|
Jobs r;
|
|
if (q < 1 || q > 8) r.code = Code::BadValue;
|
|
else {
|
|
auto* b = store_->find(q);
|
|
if (!b) r.code = Code::MissingQueue;
|
|
else {
|
|
for (const auto& x : b->items) r.entries.push_back(x.v);
|
|
std::sort(r.entries.begin(), r.entries.end(),
|
|
[](const JobView& a, const JobView& z) { return a.ticket < z.ticket; });
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
Events Engine::events(Number after, Number limit) const {
|
|
Events r;
|
|
if (after < 0 || limit < 0 || limit > 256) r.code = Code::BadValue;
|
|
else {
|
|
for (const auto& e : store_->history) {
|
|
if (e.serial > after && static_cast<Number>(r.entries.size()) < limit) r.entries.push_back(e);
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
}
|