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