#include "model_internal.hpp" #include namespace ps { Model::Model() : state(new Impl) { } Model::~Model() = default; void Model::reset() { state.reset(new Impl); } bool valid_device(int device) { return device >= 0 && device < 4; } bool valid_actor(int actor) { return actor >= 0 && actor < 8; } int device_error(const Impl &state, int device) { if (!valid_device(device)) { return -22; } if (!state.devices[device].live) { return -19; } return 0; } int watch_error(const Impl &state, int actor, std::uint64_t token) { if (!valid_actor(actor)) { return -22; } const auto &watch = state.actors[actor].view; if (watch.phase == IDLE || watch.token != token) { return -116; } return 0; } unsigned readiness(const Device &device) { unsigned result = device.flags; if (!device.bytes.empty()) { result |= READ; } if (device.credits != 0 && !(device.flags & HUP)) { result |= WRITE; } return result; } unsigned filter_ready(unsigned bits, unsigned requested) { return (bits & requested & (READ | WRITE)) | (bits & (ERROR | HUP)); } bool is_terminal(int phase) { return phase >= READY && phase <= GONE; } int terminal_result(const WatchView &watch) { switch (watch.phase) { case READY: return 1; case CANCELLED: return -125; case INTERRUPTED: return -4; case GONE: return -19; default: return 0; } } void subscribe(Impl &state, int actor) { state.actors[actor].view.registered = 1; } void unsubscribe(Impl &state, int actor) { state.actors[actor].view.registered = 0; } int finish_watch(Impl &state, int actor, int phase) { auto &watch = state.actors[actor].view; unsubscribe(state, actor); watch.phase = phase; watch.ready = 0; watch.pending = 0; return 0; } std::vector queue_data(const Device &device) { return {device.bytes.begin(), device.bytes.end()}; } void queue_append(Device &device, const std::vector &data) { device.bytes.insert(device.bytes.end(), data.begin(), data.end()); } void queue_consume(Device &device, int count) { device.bytes.erase(device.bytes.begin(), device.bytes.begin() + count); } }