#include "irq_internal.hpp" namespace irqcycle { View Model::Impl::project(const State& s,int id) { View v; const auto& reg=s.registrations.at(id); const auto& c=s.counters.at(id); v.id=id; v.generation=reg.generation; v.state=reg.state; v.oneshot=reg.oneshot; v.priority=reg.priority; v.mode=reg.mode; v.token=reg.token; const auto work=s.activations.find(id); if (work!=s.activations.end()) { v.running=work->second.executing; v.ready=!work->second.executing; v.ticket=work->second.ticket; v.masked=reg.oneshot; } v.pending=s.pending.count(id); v.received=c.received; v.hard=c.hard; v.none=c.none; v.handled=c.handled; v.wakes=c.wakes; v.masked_arrivals=c.masked_arrivals; v.begun=c.begun; v.ended=c.ended; v.replays=c.replays; return v; } Summary Model::summary() const { Summary sum; const auto& s=impl_->state; for (const auto& entry:s.registrations) { const auto v=Impl::project(s,entry.first); ++sum.slots; sum.open+=(v.state==1); sum.closing+=(v.state==2); sum.closed+=(v.state==3); sum.ready+=v.ready; sum.running+=v.running; sum.pending+=v.pending; sum.masked+=v.masked; sum.received+=v.received; sum.hard+=v.hard; sum.none+=v.none; sum.handled+=v.handled; sum.wakes+=v.wakes; sum.masked_arrivals+=v.masked_arrivals; sum.begun+=v.begun; sum.ended+=v.ended; sum.replays+=v.replays; } for (const auto& token:s.tokens) { if (token.second) ++sum.completed_tokens; else ++sum.waiting_tokens; } return sum; } bool Model::invariant() const { const auto& s=impl_->state; std::set scheduled; std::set tickets,tokens; for (std::size_t priority=0;prioritysecond.priority!=priority) return false; if (work==s.activations.end() || work->second.executing || work->second.ticket!=entry.first) return false; } } if (s.counters.size()!=s.registrations.size()) return false; for (const auto& entry:s.registrations) { const int id=entry.first; const auto& r=entry.second; if (!Impl::valid_id(id) || !r.generation || r.state<1 || r.state>3) return false; if (r.oneshot>1 || r.priority>7 || r.mode>2 || !s.counters.count(id)) return false; const auto& c=s.counters.at(id); auto work=s.activations.find(id); const bool active=work!=s.activations.end(); const bool executing=active && work->second.executing; if ((scheduled.count(id)!=0)!=(active && !executing)) return false; if (active && ((executing && work->second.ticket) || (!executing && (!work->second.ticket || work->second.ticket>=s.next_ticket)))) return false; if (s.pending.count(id) && !active) return false; if (c.hard!=c.none+c.handled+c.wakes || c.hard(executing)) return false; if (r.state==1) { if (r.token) return false; } else { auto token=s.tokens.find(r.token); if (token==s.tokens.end() || !tokens.insert(r.token).second) return false; if (r.state==3 && (!token->second || active || s.pending.count(id))) return false; if (r.state==2 && (token->second || !active)) return false; } } for (const auto& work:s.activations) if (!s.registrations.count(work.first)) return false; for (int id:s.pending) if (!s.activations.count(id)) return false; std::uint64_t serial=1; for (const auto& token:s.tokens) { if (token.first!=serial++ || (!token.second && !tokens.count(token.first))) return false; } return serial==s.next_token; } std::string encode(const Result& value) { return std::to_string(value.code)+" "+std::to_string(value.value); } std::string encode(const View& v) { std::ostringstream out; out<state; std::ostringstream out; out<<"S "<