55 lines
1.5 KiB
C
55 lines
1.5 KiB
C
#include "model.h"
|
|
|
|
void spool_put(Model *state, int id, int length, int seed, Observation *o)
|
|
{
|
|
int tail = (state->head + state->count) % 3;
|
|
state->ids[tail] = id;
|
|
state->lengths[tail] = length;
|
|
state->seeds[tail] = seed;
|
|
state->offsets[tail] = 0;
|
|
state->count++;
|
|
state->queued++;
|
|
observe_event(o, "QUEUED", id, length, state->count);
|
|
}
|
|
|
|
void spool_step(Model *state, int allowance, int error, Observation *o)
|
|
{
|
|
int head = state->head;
|
|
int remaining;
|
|
int written;
|
|
if (state->count == 0) {
|
|
return;
|
|
}
|
|
remaining = state->lengths[head] - state->offsets[head];
|
|
if (remaining == 0) {
|
|
state->done++;
|
|
observe_event(o, "DONE", state->ids[head], state->lengths[head], state->done);
|
|
spool_pop(state);
|
|
return;
|
|
}
|
|
state->attempts++;
|
|
if (error != 0) {
|
|
spool_result(state, error, o);
|
|
return;
|
|
}
|
|
written = remaining < allowance ? remaining : allowance;
|
|
spool_bytes(state, state->offsets[head], written, o);
|
|
state->offsets[head] += written;
|
|
if (state->offsets[head] == state->lengths[head]) {
|
|
state->done++;
|
|
observe_event(o, "DONE", state->ids[head], state->lengths[head], state->done);
|
|
spool_pop(state);
|
|
}
|
|
}
|
|
|
|
void spool_pop(Model *state)
|
|
{
|
|
int head = state->head;
|
|
state->ids[head] = 0;
|
|
state->lengths[head] = 0;
|
|
state->seeds[head] = 0;
|
|
state->offsets[head] = 0;
|
|
state->head = (state->head + 1) % 3;
|
|
state->count--;
|
|
}
|