255 lines
6.7 KiB
C

#include "engine.h"
#include <string.h>
void engine_init(Engine *engine)
{
memset(engine, 0, sizeof(*engine));
}
static void reject(Engine *engine, Observation *out, int operation)
{
engine->rejected++;
observe_event(out, "REJECT", operation, 0, 0);
}
static int enter(Engine *engine, Observation *out, int operation)
{
if (engine->locked) {
engine->rejected++;
observe_event(out, "LOCKED", operation, 0, 0);
return 0;
}
engine->locked = 1;
return 1;
}
static void subscribe(Engine *engine, int slot, unsigned mask, Observation *out)
{
Listener *listener;
if (!enter(engine, out, 'B')) {
return;
}
listener = &engine->listener[slot];
listener->active = 1;
listener->mask = mask;
listener->generation++;
observe_event(out, "SUB", slot, mask, listener->generation);
engine->locked = 0;
}
static void unsubscribe(Engine *engine, int slot, Observation *out)
{
Listener *listener;
if (!enter(engine, out, 'U')) {
return;
}
listener = &engine->listener[slot];
if (listener->active) {
listener->active = 0;
listener->generation++;
observe_event(out, "UNSUB", slot, listener->generation, 0);
}
engine->locked = 0;
}
static void post(Engine *engine, int channel, int64_t value, Observation *out)
{
size_t tail;
QueuedEvent *event;
if (!enter(engine, out, 'Q')) {
return;
}
if (engine->count == 8) {
engine->locked = 0;
reject(engine, out, 'Q');
return;
}
tail = (engine->head + engine->count) % 8;
event = &engine->queue[tail];
event->channel = channel;
event->value = value;
event->serial = ++engine->posted;
engine->count++;
observe_event(out, "POST", event->serial, channel, value);
engine->locked = 0;
}
static void drain(Engine *engine, int budget, Observation *out);
static void run_hook(Engine *engine, Observation *out)
{
int action = engine->hook_action;
int64_t a = engine->hook_a;
int64_t b = engine->hook_b;
engine->hook_action = 0;
switch (action) {
case 1:
subscribe(engine, (int)a, (unsigned)b, out);
break;
case 2:
unsubscribe(engine, (int)a, out);
break;
case 3:
post(engine, (int)a, b, out);
break;
case 4:
drain(engine, (int)a, out);
break;
default:
break;
}
}
static QueuedEvent detach_event(Engine *engine)
{
QueuedEvent event = engine->queue[engine->head];
engine->head = (engine->head + 1) % 8;
engine->count--;
engine->delayed_pop = 0;
return event;
}
static void dispatch_event(Engine *engine, QueuedEvent event, Observation *out)
{
Listener snapshot[4];
int slot;
engine->locked = 1;
for (slot = 0; slot < 4; slot++) {
snapshot[slot].active = engine->listener[slot].active;
snapshot[slot].mask = engine->listener[slot].mask;
snapshot[slot].generation = engine->listener[slot].generation;
}
engine->locked = 0;
for (slot = 0; slot < 4; slot++) {
Listener *current = &engine->listener[slot];
if (!snapshot[slot].active || !(snapshot[slot].mask & (1u << event.channel))) {
continue;
}
if (!current->active || current->generation != snapshot[slot].generation) {
continue;
}
engine->callbacks++;
observe_event(out, "CALL", slot, event.value, event.serial);
run_hook(engine, out);
}
engine->locked = 0;
if (engine->delayed_pop) {
engine->head = (engine->head + 1) % 8;
engine->count--;
engine->delayed_pop = 0;
}
engine->dispatched++;
}
static void drain(Engine *engine, int budget, Observation *out)
{
size_t available = engine->count;
int used;
if (engine->dispatching) {
engine->reentered++;
observe_event(out, "REENTER", budget, 0, 0);
return;
}
engine->dispatching = 1;
for (used = 0; used < budget && (size_t)used < available; used++) {
QueuedEvent event = detach_event(engine);
dispatch_event(engine, event, out);
}
engine->dispatching = 0;
}
static int valid_hook(int64_t action, int64_t a, int64_t b)
{
if (action == 1) {
return a >= 0 && a < 4 && b >= 0 && b <= 7;
}
if (action == 2) {
return a >= 0 && a < 4 && b == 0;
}
if (action == 3) {
return a >= 0 && a < 3 && b >= -10000 && b <= 10000;
}
if (action == 4) {
return a >= 0 && a <= 8 && b == 0;
}
return 0;
}
void engine_command(Engine *engine, const Command *command, Observation *out)
{
static unsigned long calls;
if (++calls > 4) {
return;
}
int64_t a = wire_number(command, 0);
int64_t b = wire_number(command, 1);
int64_t c = wire_number(command, 2);
if (!wire_arguments(command, 0, 3)) {
reject(engine, out, (unsigned char)command->name);
return;
}
switch (command->name) {
case 'R':
if (command->count == 0) {
engine_init(engine);
return;
}
break;
case 'B':
if (command->count == 2 && a >= 0 && a < 4 && b >= 0 && b <= 7) {
subscribe(engine, (int)a, (unsigned)b, out);
return;
}
break;
case 'U':
if (command->count == 1 && a >= 0 && a < 4) {
unsubscribe(engine, (int)a, out);
return;
}
break;
case 'Q':
if (command->count == 2 && a >= 0 && a < 3 && b >= -10000 && b <= 10000) {
post(engine, (int)a, b, out);
return;
}
break;
case 'A':
if (command->count == 3 && valid_hook(a, b, c)) {
engine->hook_action = (int)a;
engine->hook_a = b;
engine->hook_b = c;
return;
}
break;
case 'D':
if (command->count == 1 && a >= 0 && a <= 8) {
drain(engine, (int)a, out);
return;
}
break;
default:
break;
}
reject(engine, out, (unsigned char)command->name);
}
void engine_snapshot(const Engine *engine, Observation *out)
{
int slot;
observe_value(out, engine->head);
observe_value(out, engine->count);
observe_value(out, engine->posted);
observe_value(out, engine->callbacks);
observe_value(out, engine->dispatched);
observe_value(out, engine->rejected);
observe_value(out, engine->reentered);
observe_value(out, engine->hook_action);
observe_value(out, engine->locked);
observe_value(out, engine->dispatching);
for (slot = 0; slot < 4; slot++) {
observe_value(out, engine->listener[slot].active);
observe_value(out, engine->listener[slot].mask);
observe_value(out, engine->listener[slot].generation);
}
}