#include "irq_router.hpp" #include #include #include #include #include namespace irq { namespace { bool valid_device(std::int64_t device) { return device >= 0 && device < device_count; } bool valid_line(std::int64_t line) { return line >= 0 && line < line_count; } bool valid_mask(std::int64_t mask) { return mask >= 0 && mask <= valid_bits; } bool valid_flag(std::int64_t flag) { return flag == 0 || flag == 1; } std::uint64_t count_bits(std::uint32_t bits) { std::uint64_t total = 0; while (bits != 0) { bits &= bits - 1; ++total; } return total; } int index_of(std::int64_t value) { return static_cast(value); } std::uint32_t bits_of(std::int64_t value) { return static_cast(value); } } struct Router::Impl { Snapshot state; std::uint32_t pending_for(const Device& entry) const { if (!entry.present) { return 0; } return entry.pending; } std::uint32_t ready_for(const Device& entry) const { if (!entry.present) { return 0; } if (!entry.enabled) { return 0; } return entry.pending & entry.mask; } std::uint32_t deferred_for(const Device& entry) const { std::uint32_t pending = pending_for(entry); std::uint32_t ready = ready_for(entry); return pending & ~ready; } void summarize_member(Summary& summary, int id) const { const Device& entry = device(id); std::uint32_t member_bit = std::uint32_t{1} << id; std::uint32_t pending = pending_for(entry); std::uint32_t ready = ready_for(entry); std::uint32_t deferred = deferred_for(entry); summary.members |= member_bit; if (entry.enabled) { summary.enabled_members |= member_bit; } if (ready != 0) { summary.ready_members |= member_bit; } summary.pending_union |= pending; summary.ready_union |= ready; summary.deferred_union |= deferred; summary.pending_bits += count_bits(pending); summary.ready_bits += count_bits(ready); summary.deferred_bits += count_bits(deferred); } Summary summarize_line(int target) const { Summary summary; for (int id = 0; id < device_count; ++id) { const Device& entry = device(id); if (!entry.present) { continue; } if (entry.line != target) { continue; } summarize_member(summary, id); } return summary; } Status accept() { ++state.accepted; return OK; } Status reject(Status error) { ++state.rejected; return error; } Device& device(std::int64_t id) { return state.devices[static_cast(id)]; } const Device& device(std::int64_t id) const { return state.devices[static_cast(id)]; } Line& line(std::int64_t id) { return state.lines[static_cast(id)]; } bool compatible(int target, bool shared, int ignored) const { for (int id = 0; id < device_count; ++id) { if (id == ignored) { continue; } const Device& other = device(id); if (!other.present) { continue; } if (other.line != target) { continue; } if (!shared || !other.shared) { return false; } } return true; } bool final_routes_compatible(const std::array& routes) const { for (int left = 0; left < device_count; ++left) { const Device& a = device(left); if (!a.present) { continue; } for (int right = left + 1; right < device_count; ++right) { const Device& b = device(right); if (!b.present) { continue; } if (routes[left] != routes[right]) { continue; } if (!a.shared || !b.shared) { return false; } } } return true; } Status validate_changes(const std::vector& changes, bool route) { if (changes.size() > static_cast(device_count)) { return RANGE; } for (const Change& change : changes) { if (!valid_device(change.device)) { return RANGE; } if (route) { if (!valid_line(change.value)) { return RANGE; } } else { if (!valid_mask(change.value)) { return RANGE; } } } std::array seen{}; for (const Change& change : changes) { int id = index_of(change.device); if (seen[id]) { return DUP; } seen[id] = true; } for (const Change& change : changes) { if (!device(change.device).present) { return NOENT; } } return OK; } Delivery invoke(int id) { Device& selected = device(id); std::uint32_t owned = selected.pending & selected.mask & 7u; Delivery result; result.device = id; result.acknowledged = owned; if (owned == 0) { ++selected.none; result.result = 0; return result; } selected.pending &= ~owned; ++selected.handled; selected.ack_bits += count_bits(owned); result.result = 1; return result; } std::vector select(int target, int budget) const { std::vector result; const Line& route = state.lines[target]; for (int offset = 0; offset < device_count; ++offset) { if (static_cast(result.size()) == budget) { break; } int id = (route.cursor + offset) % device_count; const Device& candidate = device(id); if (!candidate.present) { continue; } if (!candidate.enabled) { continue; } if (candidate.line != target) { continue; } result.push_back(id); } return result; } void start_pulse(int target) { state.deliveries.clear(); state.last_line = target; state.last_result = 0; ++line(target).dispatches; } void record_delivery(int target, const Delivery& delivery) { Line& route = line(target); ++route.visits; route.ack_bits += count_bits(delivery.acknowledged); state.last_result |= delivery.result; state.deliveries.push_back(delivery); route.cursor = (delivery.device + 1) % device_count; } void finish_pulse(int target) { Line& route = line(target); if (state.last_result != 0) { ++route.handled; } else { ++route.none; } } }; Router::Router() : impl(new Impl) { reset(); } Router::~Router() = default; void Router::reset() { impl->state = Snapshot{}; } Status Router::attach(std::int64_t id, std::int64_t target, std::int64_t shared, std::int64_t mask) { if (!valid_device(id)) { return impl->reject(RANGE); } if (!valid_line(target)) { return impl->reject(RANGE); } if (!valid_flag(shared)) { return impl->reject(RANGE); } if (!valid_mask(mask)) { return impl->reject(RANGE); } Device& entry = impl->device(id); if (entry.present) { return impl->reject(EXISTS); } if (!impl->compatible(index_of(target), shared != 0, -1)) { return impl->reject(BUSY); } Device fresh; fresh.present = true; fresh.line = index_of(target); fresh.shared = shared != 0; fresh.enabled = true; fresh.mask = bits_of(mask); entry = fresh; return impl->accept(); } Status Router::detach(std::int64_t id) { if (!valid_device(id)) { return impl->reject(RANGE); } Device& entry = impl->device(id); if (!entry.present) { return impl->reject(NOENT); } entry = Device{}; return impl->accept(); } Status Router::set_mask(std::int64_t id, std::int64_t mask) { if (!valid_device(id)) { return impl->reject(RANGE); } if (!valid_mask(mask)) { return impl->reject(RANGE); } Device& entry = impl->device(id); if (!entry.present) { return impl->reject(NOENT); } entry.mask = bits_of(mask); return impl->accept(); } Status Router::raise(std::int64_t id, std::int64_t bits) { if (!valid_device(id)) { return impl->reject(RANGE); } if (!valid_mask(bits)) { return impl->reject(RANGE); } Device& entry = impl->device(id); if (!entry.present) { return impl->reject(NOENT); } entry.pending |= bits_of(bits); ++entry.raises; return impl->accept(); } Status Router::clear(std::int64_t id, std::int64_t bits) { if (!valid_device(id)) { return impl->reject(RANGE); } if (!valid_mask(bits)) { return impl->reject(RANGE); } Device& entry = impl->device(id); if (!entry.present) { return impl->reject(NOENT); } std::uint32_t cleared = entry.pending & bits_of(bits); entry.pending &= ~cleared; entry.cleared_bits += count_bits(cleared); return impl->accept(); } Status Router::enable(std::int64_t id, std::int64_t enabled) { if (!valid_device(id)) { return impl->reject(RANGE); } if (!valid_flag(enabled)) { return impl->reject(RANGE); } Device& entry = impl->device(id); if (!entry.present) { return impl->reject(NOENT); } entry.enabled = enabled != 0; return impl->accept(); } Status Router::move(std::int64_t id, std::int64_t target) { if (!valid_device(id)) { return impl->reject(RANGE); } if (!valid_line(target)) { return impl->reject(RANGE); } Device& entry = impl->device(id); if (!entry.present) { return impl->reject(NOENT); } if (!impl->compatible(index_of(target), entry.shared, index_of(id))) { return impl->reject(BUSY); } entry.line = index_of(target); return impl->accept(); } Status Router::batch_raise(const std::vector& changes) { Status error = impl->validate_changes(changes, false); if (error != OK) { return impl->reject(error); } for (const Change& change : changes) { Device& entry = impl->device(change.device); entry.pending |= bits_of(change.value); ++entry.raises; } return impl->accept(); } Status Router::rebind(const std::vector& changes) { Status error = impl->validate_changes(changes, true); if (error != OK) { return impl->reject(error); } std::array routes; for (int id = 0; id < device_count; ++id) { routes[id] = impl->device(id).line; } for (const Change& change : changes) { routes[index_of(change.device)] = index_of(change.value); } if (!impl->final_routes_compatible(routes)) { return impl->reject(BUSY); } for (const Change& change : changes) { impl->device(change.device).line = index_of(change.value); } return impl->accept(); } Status Router::transfer(std::int64_t source, std::int64_t target, std::int64_t bits) { if (!valid_device(source)) { return impl->reject(RANGE); } if (!valid_device(target)) { return impl->reject(RANGE); } if (!valid_mask(bits)) { return impl->reject(RANGE); } Device& from = impl->device(source); Device& to = impl->device(target); if (!from.present || !to.present) { return impl->reject(NOENT); } std::uint32_t selected = from.pending & bits_of(bits); if (source != target) { from.pending &= ~selected; to.pending |= selected; } return impl->accept(); } Status Router::pulse(std::int64_t target, std::int64_t budget) { if (!valid_line(target)) { return impl->reject(RANGE); } if (budget < 0 || budget > device_count) { return impl->reject(RANGE); } std::vector selected = impl->select(index_of(target), index_of(budget)); impl->start_pulse(index_of(target)); for (int id : selected) { Delivery delivery = impl->invoke(id); impl->record_delivery(index_of(target), delivery); } impl->finish_pulse(index_of(target)); return impl->accept(); } Snapshot Router::snapshot() const { Snapshot result; result.accepted = impl->state.accepted; result.rejected = impl->state.rejected; result.last_line = impl->state.last_line; result.last_result = impl->state.last_result; result.deliveries = impl->state.deliveries; for (int id = 0; id < device_count; ++id) { const Device& source = impl->device(id); Device& target = result.devices[id]; target.present = source.present; target.line = source.line; target.shared = source.shared; target.enabled = source.enabled; target.mask = source.mask; target.pending = source.pending; target.handled = source.handled; target.none = source.none; target.ack_bits = source.ack_bits; target.raises = source.raises; target.cleared_bits = source.cleared_bits; } for (int id = 0; id < line_count; ++id) { const Line& source = impl->state.lines[id]; Line& target = result.lines[id]; target.cursor = source.cursor; target.dispatches = source.dispatches; target.handled = source.handled; target.none = source.none; target.visits = source.visits; target.ack_bits = source.ack_bits; result.summaries[id] = impl->summarize_line(id); } return result; } }