#include "runtime.hpp" namespace cancelrt { Reply Runtime::run(std::int64_t id, std::int64_t budget, std::int64_t cancel_after) { if (!valid_id(id)) { return fail(Code::RANGE); } if (!bounded(budget, 0, 32)) { return fail(Code::RANGE); } if (!bounded(cancel_after, -1, 32)) { return fail(Code::RANGE); } auto found = jobs_.find(static_cast(id)); if (found == jobs_.end()) { return fail(Code::MISSING); } Job job = replay(found->second); if (job.active) { return fail(Code::BUSY); } if (job.output.size() == job.input.size()) { Reply reply = observe(job); reply.stop = Stop::DONE; return reply; } found->second.push_back({Kind::ACQUIRE}); int remaining = static_cast(job.input.size() - job.output.size()); int allowance = static_cast(budget); Stop stop = Stop::YIELD; if (cancel_after >= 0 && cancel_after < allowance && cancel_after < remaining) { allowance = static_cast(cancel_after); stop = Stop::INTR; } int produced = allowance < remaining ? allowance : remaining; if (produced == remaining) { stop = Stop::DONE; } for (int offset = 0; offset < produced; ++offset) { auto index = job.output.size() + static_cast(offset); auto value = job.input[index] * job.factor + job.bias + static_cast(index); found->second.push_back({Kind::COMMIT, value}); } found->second.push_back({Kind::RELEASE}); Reply reply = observe(replay(found->second)); reply.produced = produced; reply.stop = stop; return reply; } }