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>
252 lines
6.5 KiB
C
252 lines
6.5 KiB
C
#include "dispatch.h"
|
|
|
|
static int in_range(int value, int minimum, int maximum)
|
|
{
|
|
return value >= minimum && value <= maximum;
|
|
}
|
|
|
|
static int lane_index(int value)
|
|
{
|
|
return in_range(value, 0, LANE_COUNT - 1);
|
|
}
|
|
|
|
static void lane_init(Lane *lane)
|
|
{
|
|
lane->enabled = 1;
|
|
lane->credit = 1;
|
|
queue_init(&lane->queue);
|
|
lane->pulses = 0;
|
|
lane->expired = 0;
|
|
lane->delivered = 0;
|
|
lane->refused = 0;
|
|
lane->cancelled = 0;
|
|
lane->moved_in = 0;
|
|
lane->moved_out = 0;
|
|
}
|
|
|
|
void service_init(Service *service)
|
|
{
|
|
unsigned index;
|
|
service->mode = 0;
|
|
service->cursor = 0;
|
|
service->ticks = 0;
|
|
service->published = 0;
|
|
service->blocked = 0;
|
|
service->invalid = 0;
|
|
for (index = 0; index < LANE_COUNT; ++index) {
|
|
lane_init(&service->lanes[index]);
|
|
port_reset(&service->receivers[index]);
|
|
}
|
|
}
|
|
|
|
static int arguments_valid(const Command *command)
|
|
{
|
|
const int *a = command->arg;
|
|
switch (command->op) {
|
|
case 'R':
|
|
case 'T':
|
|
case 'S':
|
|
return command->argc == 0;
|
|
case 'M':
|
|
return command->argc == 1 && in_range(a[0], 0, 1);
|
|
case 'L':
|
|
return command->argc == 4 && lane_index(a[0]) &&
|
|
in_range(a[1], 0, 1) && in_range(a[2], 0, 8) &&
|
|
in_range(a[3], 0, 8);
|
|
case 'G':
|
|
return command->argc == 2 && lane_index(a[0]) &&
|
|
in_range(a[1], 0, 8);
|
|
case 'D':
|
|
return command->argc == 1 && lane_index(a[0]);
|
|
case 'C':
|
|
return command->argc == 2 && in_range(a[0], 1, 1000000) &&
|
|
in_range(a[1], 1, 15);
|
|
case 'X':
|
|
return command->argc == 3 && lane_index(a[0]) && lane_index(a[1]) &&
|
|
a[0] != a[1] && in_range(a[2], 0, 8);
|
|
case 'P':
|
|
return command->argc == 4 && in_range(a[0], 1, 1000000) &&
|
|
in_range(a[1], -100000, 100000) &&
|
|
in_range(a[2], 1, 255) && in_range(a[3], 1, 15);
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static int set_lane(Service *service, const Command *command)
|
|
{
|
|
unsigned index = (unsigned)command->arg[0];
|
|
Lane *lane = &service->lanes[index];
|
|
lane->enabled = command->arg[1];
|
|
lane->credit = command->arg[2];
|
|
port_configure(&service->receivers[index], command->arg[3]);
|
|
return 0;
|
|
}
|
|
|
|
static int grant_credit(Service *service, const Command *command)
|
|
{
|
|
Lane *lane = &service->lanes[command->arg[0]];
|
|
int amount = command->arg[1];
|
|
if (lane->credit + amount > QUEUE_CAPACITY) {
|
|
return -3;
|
|
}
|
|
lane->credit += amount;
|
|
return 0;
|
|
}
|
|
|
|
static int discard_lane(Service *service, const Command *command)
|
|
{
|
|
Lane *lane = &service->lanes[command->arg[0]];
|
|
unsigned removed = queue_clear(&lane->queue);
|
|
lane->cancelled += removed;
|
|
return 0;
|
|
}
|
|
|
|
static int publish(Service *service, const Command *command)
|
|
{
|
|
unsigned index;
|
|
unsigned mask = (unsigned)command->arg[3];
|
|
Message message;
|
|
for (index = 0; index < LANE_COUNT; ++index) {
|
|
unsigned bit = 1u << index;
|
|
if ((mask & bit) != 0 &&
|
|
service->lanes[index].queue.count == QUEUE_CAPACITY) {
|
|
service->blocked += 1;
|
|
return -2;
|
|
}
|
|
}
|
|
message.id = command->arg[0];
|
|
message.value = command->arg[1];
|
|
message.lease = command->arg[2];
|
|
for (index = 0; index < LANE_COUNT; ++index) {
|
|
if ((mask & (1u << index)) != 0) {
|
|
queue_push(&service->lanes[index].queue, message);
|
|
}
|
|
}
|
|
service->published += 1;
|
|
return 0;
|
|
}
|
|
|
|
|
|
static int cancel_message(Service *service, const Command *command)
|
|
{
|
|
unsigned index;
|
|
unsigned mask = (unsigned)command->arg[1];
|
|
for (index = 0; index < LANE_COUNT; ++index) {
|
|
if ((mask & (1u << index)) != 0) {
|
|
Lane *lane = &service->lanes[index];
|
|
unsigned removed = queue_remove_id(&lane->queue, command->arg[0]);
|
|
lane->cancelled += removed;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int transfer_messages(Service *service, const Command *command)
|
|
{
|
|
Lane *source = &service->lanes[command->arg[0]];
|
|
Lane *target = &service->lanes[command->arg[1]];
|
|
unsigned count = (unsigned)command->arg[2];
|
|
int rc = queue_transfer(&source->queue, &target->queue, count);
|
|
if (rc == -2) {
|
|
service->blocked += 1;
|
|
}
|
|
if (rc != 0) {
|
|
return rc;
|
|
}
|
|
source->moved_out += count;
|
|
target->moved_in += count;
|
|
return 0;
|
|
}
|
|
|
|
static int maintain_lane(Lane *lane)
|
|
{
|
|
lane->pulses += 1;
|
|
lane->expired += queue_age(&lane->queue);
|
|
return 1;
|
|
}
|
|
|
|
static int offer_lane(Service *service, unsigned index)
|
|
{
|
|
Lane *lane = &service->lanes[index];
|
|
const Message *message;
|
|
int accepted;
|
|
if (!lane->enabled || lane->credit == 0) {
|
|
return 0;
|
|
}
|
|
message = queue_front(&lane->queue);
|
|
if (message == NULL) {
|
|
return 0;
|
|
}
|
|
accepted = port_offer(&service->receivers[index], message);
|
|
if (accepted) {
|
|
queue_pop(&lane->queue);
|
|
lane->credit -= 1;
|
|
lane->delivered += 1;
|
|
} else {
|
|
lane->refused += 1;
|
|
}
|
|
return accepted;
|
|
}
|
|
|
|
static int tick(Service *service)
|
|
{
|
|
unsigned offset;
|
|
int any = 0;
|
|
service->ticks += 1;
|
|
for (offset = 0; offset < LANE_COUNT; ++offset) {
|
|
Lane *lane = &service->lanes[offset];
|
|
if ((offset == 0 || lane->enabled) && maintain_lane(lane)) {
|
|
/* Eligibility is applied again when scanning. */
|
|
}
|
|
}
|
|
for (offset = 0; offset < LANE_COUNT; ++offset) {
|
|
unsigned index = (service->cursor + offset) % LANE_COUNT;
|
|
if (service->mode == 0) {
|
|
int accepted = offer_lane(service, index);
|
|
if (accepted) {
|
|
break;
|
|
}
|
|
} else {
|
|
any = any || offer_lane(service, index);
|
|
}
|
|
}
|
|
service->cursor = (service->cursor + 1) % LANE_COUNT;
|
|
(void)any;
|
|
return 0;
|
|
}
|
|
|
|
int service_command(Service *service, const Command *command)
|
|
{
|
|
if (!arguments_valid(command)) {
|
|
service->invalid += 1;
|
|
return -1;
|
|
}
|
|
switch (command->op) {
|
|
case 'R':
|
|
service_init(service);
|
|
return 0;
|
|
case 'M':
|
|
service->mode = command->arg[0];
|
|
return 0;
|
|
case 'L':
|
|
return set_lane(service, command);
|
|
case 'G':
|
|
return grant_credit(service, command);
|
|
case 'D':
|
|
return discard_lane(service, command);
|
|
case 'C':
|
|
return cancel_message(service, command);
|
|
case 'X':
|
|
return transfer_messages(service, command);
|
|
case 'P':
|
|
return publish(service, command);
|
|
case 'T':
|
|
return tick(service);
|
|
case 'S':
|
|
return 0;
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|