Added min_time and mock time support.
This makes it so we can use monotonic_clock in test cases, and have
a good initialization value.
Change-Id: I1cb8f9773ca5b266042c119878e70b677ef3a45d
diff --git a/aos/common/time.cc b/aos/common/time.cc
index 125b016..b2d3675 100644
--- a/aos/common/time.cc
+++ b/aos/common/time.cc
@@ -41,16 +41,6 @@
namespace aos {
-monotonic_clock::time_point monotonic_clock::now() noexcept {
- struct timespec current_time;
- if (clock_gettime(CLOCK_MONOTONIC, ¤t_time) != 0) {
- PLOG(FATAL, "clock_gettime(%jd, %p) failed",
- static_cast<uintmax_t>(CLOCK_MONOTONIC), ¤t_time);
- }
- return time_point(::std::chrono::seconds(current_time.tv_sec) +
- ::std::chrono::nanoseconds(current_time.tv_nsec));
-}
-
namespace time {
// State required to enable and use mock time.
@@ -281,4 +271,26 @@
}
} // namespace time
+
+constexpr monotonic_clock::time_point monotonic_clock::min_time;
+
+monotonic_clock::time_point monotonic_clock::now() noexcept {
+ {
+ if (time::mock_time_enabled.load(::std::memory_order_relaxed)) {
+ MutexLocker time_mutex_locker(&time::time_mutex);
+ return monotonic_clock::time_point(
+ ::std::chrono::nanoseconds(time::current_mock_time.ToNSec()));
+ }
+ }
+
+ struct timespec current_time;
+ if (clock_gettime(CLOCK_MONOTONIC, ¤t_time) != 0) {
+ PLOG(FATAL, "clock_gettime(%jd, %p) failed",
+ static_cast<uintmax_t>(CLOCK_MONOTONIC), ¤t_time);
+ }
+ return time_point(::std::chrono::seconds(current_time.tv_sec) +
+ ::std::chrono::nanoseconds(current_time.tv_nsec));
+}
+
+
} // namespace aos