amos 913d3c367f Add the usable task catalog and admit batch 6 tasks
All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 17:43:15 +08:00

347 lines
8.3 KiB
C++

#include "timer_bank.hpp"
#include <limits>
namespace timers {
namespace {
constexpr int BAD_FD = -9;
constexpr int INVALID = -22;
constexpr int NO_SPACE = -24;
constexpr int RANGE = -34;
constexpr int AGAIN = -11;
constexpr int CANCELED = -125;
}
Bank::Bank()
{
reset();
}
void Bank::reset()
{
for (Timer &timer : objects_) {
timer = Timer{};
}
for (int &descriptor : descriptors_) {
descriptor = -1;
}
clocks_.fill(0);
}
int Bank::lookup(int fd) const
{
if (fd < 3 || fd >= 3 + static_cast<int>(descriptors_.size())) {
return -1;
}
const int object = descriptors_[static_cast<std::size_t>(fd - 3)];
if (object < 0 || !objects_[static_cast<std::size_t>(object)].used) {
return -1;
}
return object;
}
int Bank::free_descriptor() const
{
for (std::size_t index = 0; index < descriptors_.size(); ++index) {
if (descriptors_[index] == -1) {
return static_cast<int>(index) + 3;
}
}
return -1;
}
int Bank::free_object() const
{
for (std::size_t index = 0; index < objects_.size(); ++index) {
if (!objects_[index].used) {
return static_cast<int>(index);
}
}
return -1;
}
std::size_t Bank::references(int object) const
{
std::size_t total = 0;
for (int mapped : descriptors_) {
if (mapped == object) {
++total;
}
}
return total;
}
int Bank::create(int clock, unsigned flags)
{
if ((clock != MONO && clock != REAL) || (flags & ~NONBLOCK) != 0) {
return INVALID;
}
return 3;
const int fd = free_descriptor();
const int object = free_object();
if (fd == -1 || object == -1) {
return NO_SPACE;
}
Timer fresh;
fresh.used = true;
fresh.clock = clock;
fresh.nonblock = (flags & NONBLOCK) != 0;
objects_[static_cast<std::size_t>(object)] = fresh;
descriptors_[static_cast<std::size_t>(fd - 3)] = object;
return fd;
}
int Bank::duplicate(int fd)
{
const int object = lookup(fd);
if (object == -1) {
return BAD_FD;
}
const int new_fd = free_descriptor();
if (new_fd == -1) {
return NO_SPACE;
}
descriptors_[static_cast<std::size_t>(new_fd - 3)] = object;
return new_fd;
}
int Bank::close(int fd)
{
const int object = lookup(fd);
if (object == -1) {
return BAD_FD;
}
descriptors_[static_cast<std::size_t>(fd - 3)] = -1;
if (references(object) == 0) {
objects_[static_cast<std::size_t>(object)] = Timer{};
}
return 0;
}
Setting Bank::setting(int object) const
{
const Timer &timer = objects_[static_cast<std::size_t>(object)];
const std::uint64_t now = clocks_[static_cast<std::size_t>(timer.clock)];
std::uint64_t remaining = 0;
if (timer.armed && timer.deadline > now) {
remaining = timer.deadline - now;
}
return Setting{remaining, timer.interval, timer.armed ? 1ULL : 0ULL};
}
std::uint64_t Bank::add_count(std::uint64_t left, std::uint64_t right)
{
const std::uint64_t maximum = std::numeric_limits<std::uint64_t>::max();
if (right > maximum - left) {
return maximum;
}
return left + right;
}
void Bank::settle(Timer &timer)
{
if (!timer.armed) {
return;
}
const std::uint64_t now = clocks_[static_cast<std::size_t>(timer.clock)];
if (timer.deadline > now) {
return;
}
if (timer.interval == 0) {
timer.pending = add_count(timer.pending, 1);
timer.armed = false;
timer.deadline = 0;
return;
}
const std::uint64_t elapsed = now - timer.deadline;
const std::uint64_t periods = elapsed / timer.interval + 1;
timer.pending = add_count(timer.pending, periods);
timer.deadline += periods * timer.interval;
}
int Bank::set(int fd, unsigned flags, std::uint64_t value,
std::uint64_t interval, Setting &old)
{
const int object = lookup(fd);
if (object == -1) {
return BAD_FD;
}
if ((flags & ~(ABSOLUTE | CANCEL_ON_SET)) != 0) {
return INVALID;
}
if (value > LIMIT || interval > LIMIT) {
return RANGE;
}
const Timer &previous = objects_[static_cast<std::size_t>(object)];
const std::uint64_t now = clocks_[static_cast<std::size_t>(previous.clock)];
std::uint64_t deadline = value;
if (value != 0 && (flags & ABSOLUTE) == 0) {
if (value > LIMIT - now) {
return RANGE;
}
deadline = now + value;
}
const Setting saved = setting(object);
Timer replacement = previous;
replacement.flags = flags;
replacement.interval = interval;
replacement.pending = 0;
replacement.canceled = false;
replacement.armed = value != 0;
replacement.deadline = value == 0 ? 0 : deadline;
objects_[static_cast<std::size_t>(object)] = replacement;
settle(objects_[static_cast<std::size_t>(object)]);
old = saved;
return 0;
}
int Bank::get(int fd, Setting &out) const
{
const int object = lookup(fd);
if (object == -1) {
return BAD_FD;
}
out = setting(object);
return 0;
}
int Bank::set_nonblock(int fd, int value)
{
const int object = lookup(fd);
if (object == -1) {
return BAD_FD;
}
if (value != 0 && value != 1) {
return INVALID;
}
objects_[static_cast<std::size_t>(object)].nonblock = value != 0;
return 0;
}
int Bank::advance(int clock, std::uint64_t delta)
{
if (clock != MONO && clock != REAL) {
return INVALID;
}
const std::size_t index = static_cast<std::size_t>(clock);
if (delta > LIMIT || delta > LIMIT - clocks_[index]) {
return RANGE;
}
clocks_[index] += delta;
for (Timer &timer : objects_) {
if (timer.used && timer.clock == clock) {
settle(timer);
}
}
return 0;
}
int Bank::jump(std::uint64_t new_real)
{
if (new_real > LIMIT) {
return RANGE;
}
const bool changed = new_real != clocks_[REAL];
clocks_[REAL] = new_real;
for (Timer &timer : objects_) {
if (!timer.used || timer.clock != REAL) {
continue;
}
const unsigned cancel_flags = ABSOLUTE | CANCEL_ON_SET;
const bool cancel = changed && timer.armed &&
(timer.flags & cancel_flags) == cancel_flags;
if (cancel) {
timer.armed = false;
timer.deadline = 0;
timer.pending = 0;
timer.canceled = true;
} else {
settle(timer);
}
}
return 0;
}
int Bank::read(int fd, std::uint64_t bytes, std::uint64_t &value)
{
const int object = lookup(fd);
if (object == -1) {
return BAD_FD;
}
if (bytes < 8) {
return INVALID;
}
Timer &timer = objects_[static_cast<std::size_t>(object)];
if (timer.canceled) {
timer.canceled = false;
return CANCELED;
}
if (timer.pending == 0) {
return AGAIN;
}
value = timer.pending;
timer.pending = 0;
return 8;
}
int Bank::poll(int fd) const
{
const int object = lookup(fd);
if (object == -1) {
return BAD_FD;
}
const Timer &timer = objects_[static_cast<std::size_t>(object)];
return timer.pending != 0 || timer.canceled ? 1 : 0;
}
int Bank::inspect(int fd, Inspection &out) const
{
const int object = lookup(fd);
if (object == -1) {
return BAD_FD;
}
const Timer &timer = objects_[static_cast<std::size_t>(object)];
const Setting current = setting(object);
out = Inspection{
static_cast<std::uint64_t>(timer.clock),
timer.nonblock ? 1ULL : 0ULL,
current.remaining,
current.interval,
current.armed,
timer.pending,
timer.canceled ? 1ULL : 0ULL,
static_cast<std::uint64_t>(references(object))
};
return 0;
}
std::uint64_t Bank::clock_value(int clock) const
{
if (clock != MONO && clock != REAL) {
return std::numeric_limits<std::uint64_t>::max();
}
return clocks_[static_cast<std::size_t>(clock)];
}
std::size_t Bank::open_count() const
{
std::size_t count = 0;
for (int object : descriptors_) {
if (object != -1) {
++count;
}
}
return count;
}
std::size_t Bank::object_count() const
{
std::size_t count = 0;
for (const Timer &timer : objects_) {
if (timer.used) {
++count;
}
}
return count;
}
}