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>
124 lines
3.9 KiB
C++
124 lines
3.9 KiB
C++
#include "pool.hpp"
|
|
#include <algorithm>
|
|
namespace fold {
|
|
Pool::Pool() { workers_.resize(1); }
|
|
bool Pool::worker_in_range(std::int64_t index) const
|
|
{
|
|
return index>=0 && index<static_cast<std::int64_t>(workers_.size());
|
|
}
|
|
int Pool::configure(std::int64_t workers, std::int64_t capacity)
|
|
{
|
|
if (workers<1 || workers>8 || capacity<4 || capacity>256) return RANGE;
|
|
workers_=std::vector<Local>(workers);
|
|
journal_.clear(); cursor_=0; commits_.clear();
|
|
capacity_=capacity; accepted_=0; closed_=false; owner_=-1;
|
|
return OK;
|
|
}
|
|
int Pool::admit(std::int64_t size) const
|
|
{
|
|
if (closed_) return STATE;
|
|
if (size>capacity_-static_cast<std::int64_t>(journal_.size()-cursor_)) return FULL;
|
|
if (size>262144-accepted_) return LIMIT;
|
|
return OK;
|
|
}
|
|
std::array<int,4> Pool::encode(std::int64_t value)
|
|
{
|
|
std::array<int,4> bytes{};
|
|
auto bits=static_cast<std::uint32_t>(value);
|
|
for (int i=0;i<4;++i) bytes[i]=(bits>>(i*8))&255;
|
|
return bytes;
|
|
}
|
|
std::int64_t Pool::decode(const std::vector<int>& value)
|
|
{
|
|
std::int64_t number=0;
|
|
for (int i=3;i>=0;--i) number=number*256+value[i];
|
|
return number>=2147483648LL ? number-4294967296LL : number;
|
|
}
|
|
int Pool::write(std::int64_t value, std::int64_t count)
|
|
{
|
|
if (value < -2147483648LL || value>2147483647LL || count<0 || count>64) return RANGE;
|
|
int code=admit(count*4);
|
|
if (code) return code;
|
|
auto encoded=encode(value);
|
|
while (count-- > 0) journal_.insert(journal_.end(),encoded.begin(),encoded.end());
|
|
accepted_=journal_.size();
|
|
return OK;
|
|
}
|
|
int Pool::byte(std::int64_t value)
|
|
{
|
|
if (value<0 || value>255) return RANGE;
|
|
int code=admit(1);
|
|
if (code) return code;
|
|
journal_.push_back(static_cast<int>(value));
|
|
accepted_=journal_.size();
|
|
return OK;
|
|
}
|
|
Step Pool::take(std::int64_t index, std::int64_t budget)
|
|
{
|
|
if (!worker_in_range(index) || budget<0 || budget>256) return {RANGE,0,0};
|
|
auto& local=workers_[index];
|
|
if (local.done) return {STATE,0,0};
|
|
if (owner_!=-1 && owner_!=index) return {BUSY,0,0};
|
|
Step result;
|
|
auto end=std::min<std::size_t>(journal_.size(),cursor_+budget);
|
|
while (cursor_<end) {
|
|
local.carry.push_back(journal_[cursor_++]);
|
|
++result.bytes;
|
|
if (local.carry.size()==4) {
|
|
local.records.push_back(decode(local.carry));
|
|
local.carry.clear(); ++result.records;
|
|
}
|
|
}
|
|
owner_=local.carry.empty() ? -1 : static_cast<int>(index);
|
|
return result;
|
|
}
|
|
int Pool::close() { closed_=true; return OK; }
|
|
void Pool::commit(Local& local)
|
|
{
|
|
commits_.push_back(static_cast<std::size_t>(&local-workers_.data()));
|
|
local.done=true;
|
|
}
|
|
int Pool::finish(std::int64_t index)
|
|
{
|
|
if (!worker_in_range(index)) return RANGE;
|
|
auto& local=workers_[index];
|
|
if (local.done) return OK;
|
|
if (!closed_ || cursor_!=journal_.size()) return STATE;
|
|
if (!local.carry.empty()) return TAIL;
|
|
commit(local);
|
|
return OK;
|
|
}
|
|
WorkerView Pool::view(const Local& local)
|
|
{
|
|
WorkerView result;
|
|
for (auto value:local.records) { result.sum+=value; ++result.count; }
|
|
result.carry=local.carry; result.done=local.done;
|
|
return result;
|
|
}
|
|
Total Pool::totals() const
|
|
{
|
|
Total total;
|
|
for (auto index:commits_) {
|
|
for (auto value:workers_[index].records) { total.sum+=value; ++total.count; }
|
|
}
|
|
total.done=commits_.size(); total.closed=closed_;
|
|
total.queued=journal_.size()-cursor_; total.owner=owner_;
|
|
return total;
|
|
}
|
|
int Pool::worker(std::int64_t index, WorkerView& output) const
|
|
{
|
|
if (!worker_in_range(index)) return RANGE;
|
|
output=view(workers_[index]);
|
|
return OK;
|
|
}
|
|
Snapshot Pool::snapshot() const
|
|
{
|
|
Snapshot result;
|
|
result.workers=workers_.size(); result.capacity=capacity_; result.accepted=journal_.size();
|
|
result.total=totals();
|
|
result.queue.assign(journal_.begin()+cursor_,journal_.end());
|
|
for (const auto& local:workers_) result.local.push_back(view(local));
|
|
return result;
|
|
}
|
|
}
|