pw_chrono: Adds SystemClock helper to compute deadlines
Adds SystemClock::TimePointAfterAtLeast to compute the nearest
time_point after the specified duration has fully elapsed.
This adds in a single tick to handle the partial tick problem
where for example a timeout of 1 tick can result in anything from
non-blocking to the actual requested tick [0,1]. Instead this
adds a singular tick resulting in at most 1 added tick where the
timeout is instead [1,2].
No-Docs-Update-Reason: Trivial helper, full docs are coming soon.
Change-Id: Ica29a6bbd30133b3d9dfc43333c3fe6fff49ea74
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/42934
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
diff --git a/pw_chrono/public/pw_chrono/system_clock.h b/pw_chrono/public/pw_chrono/system_clock.h
index 3c5be0e..ad6e90e 100644
--- a/pw_chrono/public/pw_chrono/system_clock.h
+++ b/pw_chrono/public/pw_chrono/system_clock.h
@@ -113,6 +113,18 @@
static constexpr duration for_at_least(std::chrono::duration<Rep, Period> d) {
return std::chrono::ceil<duration>(d);
};
+
+ // Computes the nearest time_point after the specified duration has elapsed.
+ //
+ // This is useful for translating delay or timeout durations into deadlines.
+ //
+ // The time_point is computed based on now() plus the specified duration
+ // where a singular clock tick is added to handle partial ticks. This ensures
+ // that a duration of at least 1 tick does not result in [0,1] ticks and
+ // instead in [1,2] ticks.
+ static time_point TimePointAfterAtLeast(duration after_at_least) {
+ return now() + after_at_least + duration(1);
+ }
};
// An abstract interface representing a SystemClock.