One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
3.5 KiB
C++
156 lines
3.5 KiB
C++
#include "service_port.hpp"
|
|
#include <stdexcept>
|
|
namespace timerlab {
|
|
ServicePort::ServicePort(unsigned capacity) : slots_(capacity)
|
|
{
|
|
if (capacity < 1 || capacity > 16) {
|
|
throw std::invalid_argument("capacity");
|
|
}
|
|
totals_.capacity = capacity;
|
|
}
|
|
ServicePort::Slot *ServicePort::lookup(Token client)
|
|
{
|
|
if (client == 0) {
|
|
++totals_.errors;
|
|
return nullptr;
|
|
}
|
|
const unsigned index = static_cast<unsigned>((client - 1) % 16);
|
|
const Token generation = (client - 1) / 16;
|
|
if (index >= slots_.size()) {
|
|
++totals_.errors;
|
|
return nullptr;
|
|
}
|
|
Slot &slot = slots_[index];
|
|
if (!slot.live || slot.generation != generation) {
|
|
++totals_.errors;
|
|
return nullptr;
|
|
}
|
|
return &slot;
|
|
}
|
|
Token ServicePort::create()
|
|
{
|
|
for (unsigned index = 0; index < slots_.size(); ++index) {
|
|
Slot &slot = slots_[index];
|
|
if (slot.live) {
|
|
continue;
|
|
}
|
|
++slot.generation;
|
|
slot.live = true;
|
|
slot.busy = false;
|
|
slot.ready = 0;
|
|
slot.status = 0;
|
|
slot.result = 0;
|
|
++totals_.created;
|
|
return slot.generation * 16 + index + 1;
|
|
}
|
|
++totals_.exhausted;
|
|
return 0;
|
|
}
|
|
int ServicePort::destroy(Token client)
|
|
{
|
|
Slot *slot = lookup(client);
|
|
if (slot == nullptr) {
|
|
return -1;
|
|
}
|
|
if (slot->busy) {
|
|
++totals_.errors;
|
|
return -2;
|
|
}
|
|
slot->live = false;
|
|
slot->ready = 0;
|
|
slot->result = 0;
|
|
++totals_.destroyed;
|
|
return 0;
|
|
}
|
|
int ServicePort::send(Token client, std::uint64_t sequence,
|
|
std::int64_t payload, unsigned delay, bool failure,
|
|
int gain, unsigned route)
|
|
{
|
|
Slot *slot = lookup(client);
|
|
if (slot == nullptr) {
|
|
return -1;
|
|
}
|
|
if (slot->busy) {
|
|
++totals_.errors;
|
|
return -2;
|
|
}
|
|
slot->busy = true;
|
|
slot->ready = totals_.clock + delay;
|
|
slot->status = failure ? 1 : 0;
|
|
slot->result = failure ? 0 : payload * gain + route * 101 + sequence;
|
|
++totals_.submitted;
|
|
return 0;
|
|
}
|
|
int ServicePort::receive(Token client, Reply &reply)
|
|
{
|
|
Slot *slot = lookup(client);
|
|
if (slot == nullptr) {
|
|
return -1;
|
|
}
|
|
if (!slot->busy) {
|
|
++totals_.errors;
|
|
return -2;
|
|
}
|
|
if (totals_.clock < slot->ready) {
|
|
return 1;
|
|
}
|
|
reply.status = slot->status;
|
|
reply.value = slot->result;
|
|
slot->busy = false;
|
|
++totals_.replied;
|
|
return 0;
|
|
}
|
|
int ServicePort::cancel(Token client)
|
|
{
|
|
Slot *slot = lookup(client);
|
|
if (slot == nullptr) {
|
|
return -1;
|
|
}
|
|
if (!slot->busy) {
|
|
++totals_.errors;
|
|
return -2;
|
|
}
|
|
slot->busy = false;
|
|
++totals_.canceled;
|
|
return 0;
|
|
}
|
|
void ServicePort::advance(Tick delta)
|
|
{
|
|
totals_.clock += delta;
|
|
}
|
|
Tick ServicePort::now() const
|
|
{
|
|
return totals_.clock;
|
|
}
|
|
PortView ServicePort::view() const
|
|
{
|
|
PortView result = totals_;
|
|
for (const Slot &slot : slots_) {
|
|
if (slot.live) {
|
|
++result.live;
|
|
if (slot.busy) {
|
|
++result.busy;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
std::vector<ClientView> ServicePort::clients() const
|
|
{
|
|
std::vector<ClientView> result;
|
|
for (unsigned index = 0; index < slots_.size(); ++index) {
|
|
const Slot &slot = slots_[index];
|
|
if (!slot.live) {
|
|
continue;
|
|
}
|
|
ClientView client;
|
|
client.token = slot.generation * 16 + index + 1;
|
|
client.busy = slot.busy;
|
|
client.ready = slot.ready;
|
|
result.push_back(client);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
}
|