#include "wait_station.hpp" #include namespace waitstation { ClockBook::ClockBook() : monotonic_(0), realtime_(0), initial_realtime_(0) { } void ClockBook::reset(Integer monotonic, Integer realtime) { monotonic_ = monotonic; realtime_ = realtime; initial_realtime_ = realtime; } Code ClockBook::advance(Integer value) { if (value < 0) { return RANGE; } if (value < monotonic_) { return RANGE; } monotonic_ = value; return OK; } void ClockBook::adjust(Integer value) { realtime_ = value; } Code ClockBook::read(Integer domain, Integer &value) const { if (domain == 0) { value = monotonic_; return OK; } if (domain == 1) { value = realtime_; return OK; } return RANGE; } bool ClockBook::expired(Integer domain, Integer deadline) const { if (domain == 0) { return static_cast<__int128>(monotonic_) + static_cast<__int128>(realtime_) - initial_realtime_ >= deadline; } return realtime_ >= deadline; } Integer ClockBook::remaining(Integer domain, Integer deadline) const { Integer current = 0; read(domain, current); if (deadline <= current) { return 0; } const auto maximum = std::numeric_limits::max(); if (current < 0 && deadline > maximum + current) { return maximum; } return deadline - current; } }