From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
4.3 KiB
C
160 lines
4.3 KiB
C
#include "internal.h"
|
|
#include <string.h>
|
|
int worker_usable(const Worker *worker)
|
|
{
|
|
if (worker == NULL) {
|
|
return 0;
|
|
}
|
|
if (worker->state < WORKER_IDLE || worker->state > WORKER_STOPPED) {
|
|
return 0;
|
|
}
|
|
if (worker->lanes < 1 || worker->lanes > WORKER_LANES) {
|
|
return 0;
|
|
}
|
|
if (worker->period < 1 || worker->period > 64) {
|
|
return 0;
|
|
}
|
|
if (worker->capacity < 1 || worker->capacity > 255) {
|
|
return 0;
|
|
}
|
|
return bank_consistent(&worker->bank);
|
|
}
|
|
int worker_init(Worker *worker, unsigned lanes, unsigned period, unsigned capacity)
|
|
{
|
|
Worker fresh;
|
|
if (worker == NULL) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if (lanes < 1 || lanes > WORKER_LANES) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if (period < 1 || period > 64) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if (capacity < 1 || capacity > 255) {
|
|
return WORKER_INVALID;
|
|
}
|
|
memset(&fresh, 0, sizeof(fresh));
|
|
fresh.state = WORKER_IDLE;
|
|
fresh.lanes = lanes;
|
|
fresh.period = period;
|
|
fresh.capacity = capacity;
|
|
bank_init(&fresh.bank, lanes, capacity);
|
|
snapshot_clear(&fresh.snapshot);
|
|
*worker = fresh;
|
|
return WORKER_OK;
|
|
}
|
|
int worker_post(Worker *worker, unsigned mask)
|
|
{
|
|
int result;
|
|
if (!worker_usable(worker)) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if ((mask & ~bank_mask(worker->lanes)) != 0) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if (worker->state == WORKER_STOPPED) {
|
|
return WORKER_CLOSED;
|
|
}
|
|
result = bank_can_post(&worker->bank, mask);
|
|
if (result != WORKER_OK) {
|
|
return result;
|
|
}
|
|
bank_post(&worker->bank, mask);
|
|
return WORKER_OK;
|
|
}
|
|
static void begin_round(Worker *worker)
|
|
{
|
|
bank_capture(&worker->bank, &worker->snapshot);
|
|
worker->remaining = worker->period;
|
|
worker->state = WORKER_ACTIVE;
|
|
worker->started += 1;
|
|
}
|
|
static void end_round(Worker *worker, CycleDecision decision)
|
|
{
|
|
bank_flush(&worker->bank, &worker->snapshot);
|
|
snapshot_clear(&worker->snapshot);
|
|
worker->remaining = 0;
|
|
if (decision.publish) {
|
|
worker->completed += 1;
|
|
worker->state = WORKER_IDLE;
|
|
} else {
|
|
worker->cancelled += 1;
|
|
worker->state = WORKER_STOPPED;
|
|
}
|
|
}
|
|
int worker_step(Worker *worker, unsigned arrivals, unsigned cancel,
|
|
unsigned ticks, WorkerEvent *event)
|
|
{
|
|
int result;
|
|
CycleDecision decision;
|
|
if (event == NULL) {
|
|
return WORKER_INVALID;
|
|
}
|
|
*event = WORKER_NONE;
|
|
if (!worker_usable(worker)) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if (cancel > 1 || ticks > 64) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if ((arrivals & ~bank_mask(worker->lanes)) != 0) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if (worker->state == WORKER_STOPPED) {
|
|
return WORKER_CLOSED;
|
|
}
|
|
result = bank_can_post(&worker->bank, arrivals);
|
|
if (result != WORKER_OK) {
|
|
return result;
|
|
}
|
|
bank_post(&worker->bank, 0);
|
|
worker->steps += 1;
|
|
if (worker->state == WORKER_IDLE) {
|
|
if (cancel) {
|
|
worker->state = WORKER_STOPPED;
|
|
*event = WORKER_STOP;
|
|
return WORKER_OK;
|
|
}
|
|
begin_round(worker);
|
|
}
|
|
decision = cycle_decide(worker->remaining, cancel, ticks);
|
|
worker->remaining = decision.remaining;
|
|
if (decision.finished) {
|
|
end_round(worker, decision);
|
|
}
|
|
*event = decision.event;
|
|
return WORKER_OK;
|
|
}
|
|
int worker_view(const Worker *worker, WorkerView *view)
|
|
{
|
|
unsigned lane;
|
|
WorkerView fresh;
|
|
if (worker == NULL || view == NULL) {
|
|
return WORKER_INVALID;
|
|
}
|
|
memset(&fresh, 0, sizeof(fresh));
|
|
if (worker->state == WORKER_EMPTY) {
|
|
*view = fresh;
|
|
return WORKER_OK;
|
|
}
|
|
if (!worker_usable(worker)) {
|
|
return WORKER_INVALID;
|
|
}
|
|
fresh.state = (unsigned)worker->state;
|
|
fresh.lanes = worker->lanes;
|
|
fresh.period = worker->period;
|
|
fresh.capacity = worker->capacity;
|
|
fresh.remaining = worker->remaining;
|
|
fresh.started = worker->started;
|
|
fresh.completed = worker->completed;
|
|
fresh.cancelled = worker->cancelled;
|
|
fresh.steps = worker->steps;
|
|
for (lane = 0; lane < WORKER_LANES; ++lane) {
|
|
fresh.pending[lane] = worker->bank.count[lane];
|
|
fresh.snapshot[lane] = worker->snapshot.count[lane];
|
|
}
|
|
*view = fresh;
|
|
return WORKER_OK;
|
|
}
|