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>
172 lines
4.8 KiB
C
172 lines
4.8 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;
|
|
}
|
|
int worker_step(Worker *worker, unsigned arrivals, unsigned cancel,
|
|
unsigned ticks, WorkerEvent *event)
|
|
{
|
|
Worker next;
|
|
unsigned lane;
|
|
unsigned elapsed;
|
|
if (event == NULL) {
|
|
return WORKER_INVALID;
|
|
}
|
|
*event = WORKER_NONE;
|
|
if (!worker_usable(worker) || cancel > 1 || ticks > 64) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if (arrivals >= (1u << worker->lanes)) {
|
|
return WORKER_INVALID;
|
|
}
|
|
if (worker->state == WORKER_STOPPED) {
|
|
return WORKER_CLOSED;
|
|
}
|
|
next = *worker;
|
|
for (lane = 0; lane < next.lanes; ++lane) {
|
|
if ((arrivals & (1u << lane)) != 0) {
|
|
if (next.bank.count[lane] == next.capacity) {
|
|
return WORKER_FULL;
|
|
}
|
|
next.bank.count[lane] += 1;
|
|
}
|
|
}
|
|
next.steps += 1;
|
|
if (next.state == WORKER_IDLE && cancel) {
|
|
next.state = WORKER_STOPPED;
|
|
*event = WORKER_STOP;
|
|
} else {
|
|
if (next.state == WORKER_IDLE) {
|
|
for (lane = 0; lane < WORKER_LANES; ++lane) {
|
|
next.snapshot.count[lane] = next.bank.count[lane];
|
|
}
|
|
next.snapshot.present = 1;
|
|
next.remaining = next.period;
|
|
next.started += 1;
|
|
next.state = WORKER_ACTIVE;
|
|
}
|
|
elapsed = 0;
|
|
while (elapsed < ticks && next.remaining > 0) {
|
|
next.remaining -= 1;
|
|
elapsed += 1;
|
|
}
|
|
if (cancel || next.remaining == 0) {
|
|
for (lane = 0; lane < next.lanes; ++lane) {
|
|
unsigned keep = 0;
|
|
unsigned item;
|
|
for (item = next.snapshot.count[lane]; item < next.bank.count[lane]; ++item) {
|
|
keep += 1;
|
|
}
|
|
next.bank.count[lane] = keep;
|
|
next.snapshot.count[lane] = 0;
|
|
}
|
|
next.snapshot.present = 0;
|
|
next.remaining = 0;
|
|
if (cancel) {
|
|
next.cancelled += 1;
|
|
next.state = WORKER_STOPPED;
|
|
*event = WORKER_CANCEL;
|
|
} else {
|
|
next.completed += 1;
|
|
next.state = WORKER_IDLE;
|
|
*event = WORKER_DONE;
|
|
}
|
|
} else {
|
|
*event = WORKER_WAIT;
|
|
}
|
|
}
|
|
*worker = next;
|
|
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;
|
|
}
|