feat(async,time): add injected-clock TimerQueue and clock saturation helpers (M-TIME-03 B1)
- NEW Async/Timer.hpp: TimerQueue (stable (deadline, seq) min-heap, bounded tombstones, explicit poll_expired, callbacks outside the lock, close/destroy deliver TimerEvent::closed), shared-state TimerHandle safe after queue destruction, sleep_until/sleep_for settling exactly once and resuming via post_completion; documented sleep-task lifetime and cancel-under-lock rules - Time/Clock.hpp: saturating_add, deadline_after - Tests: TimerQueue (15), DeadlineDomains (2), ClockSaturation (1); race loops count failures to stay under the runner's 1,000-assertion retention (X-QA-02) Review: W11-REVIEW-B1 (CHANGES_REQUIRED F1-F3 fixed by root: test counters, header lifetime/cancel docs) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
95785ed47a
commit
99a1100bb1
5 files changed
+2269
No files matched your search
@@ -5,6 +5,8 @@
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <limits>
|
||||
#include <ratio>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Void::Time
|
||||
{
|
||||
@@ -207,4 +209,46 @@ private:
|
||||
return std::chrono::nanoseconds{dl_ns - now_ns};
|
||||
}
|
||||
|
||||
/// Overflow-safe `point + delta` in the monotonic domain. A result that would
|
||||
/// leave the representable range clamps to `time_point::max()` (positive
|
||||
/// overflow) or `time_point::min()` (negative overflow) instead of wrapping.
|
||||
/// Negative `delta` values are allowed.
|
||||
[[nodiscard]] inline MonotonicClock::time_point
|
||||
saturating_add(MonotonicClock::time_point point,
|
||||
std::chrono::nanoseconds delta) noexcept
|
||||
{
|
||||
using TimePoint = MonotonicClock::time_point;
|
||||
using Duration = TimePoint::duration;
|
||||
using Rep = Duration::rep;
|
||||
static_assert(std::is_integral_v<Rep> && std::is_signed_v<Rep>,
|
||||
"saturating_add requires a signed integral time_point rep");
|
||||
static_assert(std::ratio_greater_equal_v<Duration::period, std::nano> &&
|
||||
std::numeric_limits<Rep>::digits >=
|
||||
std::numeric_limits<std::chrono::nanoseconds::rep>::digits,
|
||||
"saturating_add requires a time_point period of at least "
|
||||
"one nanosecond and a rep at least as wide as nanoseconds");
|
||||
|
||||
// Converting to an equal or coarser period only shrinks the magnitude, so
|
||||
// this cast never overflows.
|
||||
const Rep step = std::chrono::duration_cast<Duration>(delta).count();
|
||||
const Rep base = point.time_since_epoch().count();
|
||||
if (step > 0 && base > std::numeric_limits<Rep>::max() - step) {
|
||||
return TimePoint::max();
|
||||
}
|
||||
if (step < 0 && base < std::numeric_limits<Rep>::min() - step) {
|
||||
return TimePoint::min();
|
||||
}
|
||||
return TimePoint{Duration{base + step}};
|
||||
}
|
||||
|
||||
/// Deadline `timeout` after `clock.now()`, computed in `clock`'s own domain
|
||||
/// with `saturating_add` (so a huge or `nanoseconds::max()` timeout yields
|
||||
/// `time_point::max()` rather than an overflowed, already-expired deadline).
|
||||
[[nodiscard]] inline MonotonicClock::time_point
|
||||
deadline_after(const MonotonicClock& clock,
|
||||
std::chrono::nanoseconds timeout) noexcept
|
||||
{
|
||||
return saturating_add(clock.now(), timeout);
|
||||
}
|
||||
|
||||
} // namespace Void::Time
|
||||
Reference in new issue
Block a user