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>
438 lines
10 KiB
C++
438 lines
10 KiB
C++
#include "mailbox.hpp"
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <utility>
|
|
|
|
namespace mq {
|
|
|
|
Mailbox::Mailbox(Mailbox &&other) noexcept
|
|
{
|
|
swap_state(other);
|
|
}
|
|
|
|
Mailbox &Mailbox::operator=(Mailbox &&other) noexcept
|
|
{
|
|
if (this != &other) {
|
|
Mailbox fresh;
|
|
fresh.swap_state(other);
|
|
swap_state(fresh);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
void Mailbox::swap_state(Mailbox &other) noexcept
|
|
{
|
|
using std::swap;
|
|
swap(config_, other.config_);
|
|
swap(now_, other.now_);
|
|
swap(used_bytes_, other.used_bytes_);
|
|
swap(next_ticket_, other.next_ticket_);
|
|
swap(counters_, other.counters_);
|
|
swap(closed_, other.closed_);
|
|
records_.swap(other.records_);
|
|
}
|
|
|
|
int Mailbox::validate(const Config &config)
|
|
{
|
|
if (config.count == 0 || config.count > 64) {
|
|
return -22;
|
|
}
|
|
if (config.bytes == 0 || config.bytes > 4096) {
|
|
return -22;
|
|
}
|
|
if (config.body == 0 || config.body > 256) {
|
|
return -22;
|
|
}
|
|
if (config.body > config.bytes || config.priority > 31) {
|
|
return -22;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void Mailbox::add(Number &counter, Number amount)
|
|
{
|
|
const Number maximum = std::numeric_limits<Number>::max();
|
|
if (amount > maximum - counter) {
|
|
counter = maximum;
|
|
} else {
|
|
counter += amount;
|
|
}
|
|
}
|
|
|
|
void Mailbox::reset()
|
|
{
|
|
Mailbox fresh;
|
|
swap_state(fresh);
|
|
}
|
|
|
|
int Mailbox::configure(const Config &config)
|
|
{
|
|
const int status = validate(config);
|
|
if (status != 0) {
|
|
return status;
|
|
}
|
|
Mailbox fresh;
|
|
fresh.config_ = config;
|
|
swap_state(fresh);
|
|
return 0;
|
|
}
|
|
|
|
int Mailbox::resize(const Config &config)
|
|
{
|
|
const int status = validate(config);
|
|
if (status != 0) {
|
|
return status;
|
|
}
|
|
if (records_.size() > config.count || used_bytes_ > config.bytes) {
|
|
return -28;
|
|
}
|
|
for (const Record &record : records_) {
|
|
if (record.body.size() > config.body || record.priority > config.priority) {
|
|
return -28;
|
|
}
|
|
}
|
|
config_ = config;
|
|
add(counters_.revision, 1);
|
|
return 0;
|
|
}
|
|
|
|
std::size_t Mailbox::locate(Number id) const
|
|
{
|
|
for (std::size_t index = 0; index < records_.size(); ++index) {
|
|
if (records_[index].id == id) {
|
|
return index;
|
|
}
|
|
}
|
|
return records_.size();
|
|
}
|
|
|
|
bool Mailbox::precedes(const Record &left, const Record &right)
|
|
{
|
|
if (left.priority != right.priority) {
|
|
return left.priority > right.priority;
|
|
}
|
|
return left.ticket > right.ticket;
|
|
}
|
|
|
|
std::size_t Mailbox::first() const
|
|
{
|
|
if (records_.empty()) {
|
|
return 0;
|
|
}
|
|
std::size_t best = 0;
|
|
for (std::size_t index = 1; index < records_.size(); ++index) {
|
|
if (precedes(records_[index], records_[best])) {
|
|
best = index;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
int Mailbox::empty_status() const
|
|
{
|
|
return closed_ ? -61 : -11;
|
|
}
|
|
|
|
int Mailbox::send(const Input &input, Number &ticket_out)
|
|
{
|
|
if (closed_) {
|
|
return -32;
|
|
}
|
|
if (input.id == 0 || input.priority > config_.priority) {
|
|
return -22;
|
|
}
|
|
const Number length = static_cast<Number>(input.body.size());
|
|
if (length > config_.body) {
|
|
return -90;
|
|
}
|
|
if (input.ttl > clock_limit) {
|
|
return -34;
|
|
}
|
|
if (input.ttl != 0 && input.ttl > clock_limit - now_) {
|
|
return -34;
|
|
}
|
|
if (locate(input.id) != records_.size()) {
|
|
return -17;
|
|
}
|
|
if (next_ticket_ == std::numeric_limits<Number>::max()) {
|
|
return -75;
|
|
}
|
|
if (records_.size() >= config_.count || length > config_.bytes - used_bytes_) {
|
|
return -28;
|
|
}
|
|
Record record;
|
|
record.id = input.id;
|
|
record.priority = input.priority;
|
|
record.ticket = next_ticket_;
|
|
record.expires = input.ttl == 0 ? 0 : now_ + input.ttl;
|
|
record.body = input.body;
|
|
records_.push_back(std::move(record));
|
|
used_bytes_ += length;
|
|
ticket_out = next_ticket_;
|
|
++next_ticket_;
|
|
add(counters_.sent, 1);
|
|
add(counters_.sent_bytes, length);
|
|
add(counters_.revision, 1);
|
|
return 0;
|
|
}
|
|
|
|
int Mailbox::peek(Record &out) const
|
|
{
|
|
if (records_.empty()) {
|
|
return empty_status();
|
|
}
|
|
out = records_[first()];
|
|
return 0;
|
|
}
|
|
|
|
void Mailbox::erase_at(std::size_t index)
|
|
{
|
|
used_bytes_ -= static_cast<Number>(records_[index].body.size());
|
|
records_.erase(records_.begin() + static_cast<std::ptrdiff_t>(index));
|
|
}
|
|
|
|
int Mailbox::receive(Record &out)
|
|
{
|
|
if (records_.empty()) {
|
|
return empty_status();
|
|
}
|
|
const std::size_t index = first();
|
|
const Record result = records_[index];
|
|
erase_at(index);
|
|
add(counters_.received, 1);
|
|
add(counters_.received_bytes, static_cast<Number>(result.body.size()));
|
|
add(counters_.revision, 1);
|
|
out = result;
|
|
return 0;
|
|
}
|
|
|
|
int Mailbox::cancel(Number id)
|
|
{
|
|
if (id == 0) {
|
|
return -22;
|
|
}
|
|
const std::size_t index = locate(id);
|
|
if (index == records_.size()) {
|
|
return -2;
|
|
}
|
|
erase_at(index);
|
|
add(counters_.canceled, 1);
|
|
add(counters_.revision, 1);
|
|
return 0;
|
|
}
|
|
|
|
int Mailbox::reprioritize(Number id, Number priority)
|
|
{
|
|
if (id == 0 || priority > config_.priority) {
|
|
return -22;
|
|
}
|
|
const std::size_t index = locate(id);
|
|
if (index == records_.size()) {
|
|
return -2;
|
|
}
|
|
records_[index].priority = priority;
|
|
add(counters_.reprioritized, 1);
|
|
add(counters_.revision, 1);
|
|
return 0;
|
|
}
|
|
|
|
int Mailbox::advance(Number delta)
|
|
{
|
|
if (delta > clock_limit || delta > clock_limit - now_) {
|
|
return -34;
|
|
}
|
|
now_ += delta;
|
|
for (std::size_t index = 0; index < records_.size();) {
|
|
const Number expires = records_[index].expires;
|
|
if (expires != 0 && expires <= now_) {
|
|
erase_at(index);
|
|
add(counters_.expired, 1);
|
|
} else {
|
|
++index;
|
|
}
|
|
}
|
|
add(counters_.revision, 1);
|
|
return 0;
|
|
}
|
|
|
|
int Mailbox::inspect_id(Number id, Record &out) const
|
|
{
|
|
if (id == 0) {
|
|
return -22;
|
|
}
|
|
const std::size_t index = locate(id);
|
|
if (index == records_.size()) {
|
|
return -2;
|
|
}
|
|
out = records_[index];
|
|
return 0;
|
|
}
|
|
|
|
int Mailbox::renew(Number id, Number ttl, Number &old_expiry_out)
|
|
{
|
|
if (id == 0) {
|
|
return -22;
|
|
}
|
|
const std::size_t index = locate(id);
|
|
if (index == records_.size()) {
|
|
return -2;
|
|
}
|
|
if (ttl > clock_limit || (ttl != 0 && ttl > clock_limit - now_)) {
|
|
return -34;
|
|
}
|
|
Record &record = records_[index];
|
|
const Number old_expiry = record.expires;
|
|
if (ttl == 0) {
|
|
record.expires = 0;
|
|
} else {
|
|
record.expires = now_ + ttl;
|
|
}
|
|
old_expiry_out = old_expiry;
|
|
add(counters_.revision, 1);
|
|
return 0;
|
|
}
|
|
|
|
int Mailbox::cancel_priority(Number priority, std::vector<Number> &ids_out)
|
|
{
|
|
if (priority > config_.priority) {
|
|
return -22;
|
|
}
|
|
std::vector<Record> matches;
|
|
for (const Record &record : records_) {
|
|
if (record.priority == priority) {
|
|
matches.push_back(record);
|
|
}
|
|
}
|
|
std::sort(matches.begin(), matches.end(),
|
|
[](const Record &left, const Record &right) {
|
|
return left.ticket < right.ticket;
|
|
});
|
|
std::vector<Number> ids;
|
|
ids.reserve(matches.size());
|
|
for (const Record &record : matches) {
|
|
ids.push_back(record.id);
|
|
}
|
|
for (Number id : ids) {
|
|
const std::size_t index = locate(id);
|
|
erase_at(index);
|
|
}
|
|
add(counters_.canceled, static_cast<Number>(ids.size()));
|
|
add(counters_.revision, 1);
|
|
ids_out = std::move(ids);
|
|
return 0;
|
|
}
|
|
|
|
void Mailbox::set_closed(bool closed)
|
|
{
|
|
if (closed_ != closed) {
|
|
closed_ = closed;
|
|
add(counters_.revision, 1);
|
|
}
|
|
}
|
|
|
|
bool Mailbox::closed() const
|
|
{
|
|
return closed_;
|
|
}
|
|
|
|
void Mailbox::snapshot(std::vector<Record> &out) const
|
|
{
|
|
std::vector<Record> ordered = records_;
|
|
std::sort(ordered.begin(), ordered.end(), precedes);
|
|
out = std::move(ordered);
|
|
}
|
|
|
|
int Mailbox::drain(Number limit, Number budget, std::vector<Record> &out)
|
|
{
|
|
if (limit == 0 || limit > 64) {
|
|
return -22;
|
|
}
|
|
if (budget > 4096) {
|
|
return -34;
|
|
}
|
|
if (records_.empty()) {
|
|
return empty_status();
|
|
}
|
|
std::vector<Record> ordered;
|
|
snapshot(ordered);
|
|
if (ordered.front().body.size() > budget) {
|
|
return -90;
|
|
}
|
|
Number total = 0;
|
|
std::size_t selected = 0;
|
|
while (selected < ordered.size() && selected < limit) {
|
|
const Number length = static_cast<Number>(ordered[selected].body.size());
|
|
if (length > budget - total) {
|
|
break;
|
|
}
|
|
total += length;
|
|
++selected;
|
|
}
|
|
ordered.resize(selected);
|
|
for (const Record &record : ordered) {
|
|
erase_at(locate(record.id));
|
|
}
|
|
add(counters_.received, static_cast<Number>(selected));
|
|
add(counters_.received_bytes, total);
|
|
add(counters_.revision, 1);
|
|
out = std::move(ordered);
|
|
return 0;
|
|
}
|
|
|
|
int Mailbox::apply(const Edit &edit, Number &ticket_out)
|
|
{
|
|
if (edit.kind == 1) {
|
|
return send(edit.input, ticket_out);
|
|
}
|
|
if (edit.kind == 2) {
|
|
const int status = cancel(edit.input.id);
|
|
if (status == 0) {
|
|
ticket_out = 0;
|
|
}
|
|
return status;
|
|
}
|
|
if (edit.kind == 3) {
|
|
const int status = reprioritize(edit.input.id, edit.input.priority);
|
|
if (status == 0) {
|
|
ticket_out = 0;
|
|
}
|
|
return status;
|
|
}
|
|
return -22;
|
|
}
|
|
|
|
int Mailbox::batch(const std::vector<Edit> &edits, std::vector<Number> &tickets_out)
|
|
{
|
|
if (edits.size() > 16) {
|
|
return -22;
|
|
}
|
|
Mailbox &draft = *this;
|
|
std::vector<Number> tickets;
|
|
tickets.reserve(edits.size());
|
|
for (const Edit &edit : edits) {
|
|
Number ticket = 0;
|
|
const int status = draft.apply(edit, ticket);
|
|
if (status != 0) {
|
|
return status;
|
|
}
|
|
tickets.push_back(ticket);
|
|
}
|
|
tickets_out = std::move(tickets);
|
|
return 0;
|
|
}
|
|
|
|
View Mailbox::inspect() const
|
|
{
|
|
View view;
|
|
view.config = config_;
|
|
view.now = now_;
|
|
view.count = static_cast<Number>(records_.size());
|
|
view.bytes = used_bytes_;
|
|
view.next_ticket = next_ticket_;
|
|
view.counters = counters_;
|
|
view.closed = closed_;
|
|
return view;
|
|
}
|
|
|
|
}
|