blob: 6f69aa745f434e78a476f0d060417fc8baa8815d [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_TIME_H_
2#define AOS_TIME_H_
brians343bc112013-02-10 01:53:46 +00003
4#include <stdint.h>
5#include <time.h>
brians57dd5822013-02-27 21:44:15 +00006#include <sys/time.h>
Brian4a424a22014-04-02 11:52:45 -07007#include <stdint.h>
8
9#include <type_traits>
Austin Schuh793d6b92016-05-01 13:28:14 -070010#include <chrono>
11#include <thread>
brians343bc112013-02-10 01:53:46 +000012#include <ostream>
13
John Park33858a32018-09-28 23:05:48 -070014#include "aos/type_traits/type_traits.h"
15#include "aos/macros.h"
brians343bc112013-02-10 01:53:46 +000016
17namespace aos {
Austin Schuh793d6b92016-05-01 13:28:14 -070018
19class monotonic_clock {
20 public:
21 typedef ::std::chrono::nanoseconds::rep rep;
22 typedef ::std::chrono::nanoseconds::period period;
23 typedef ::std::chrono::nanoseconds duration;
24 typedef ::std::chrono::time_point<monotonic_clock> time_point;
25
26 static monotonic_clock::time_point now() noexcept;
27 static constexpr bool is_steady = true;
28
29 // Returns the epoch (0).
30 static constexpr monotonic_clock::time_point epoch() {
Austin Schuh8aec1ed2016-05-01 13:29:20 -070031 return time_point(zero());
Austin Schuh793d6b92016-05-01 13:28:14 -070032 }
Austin Schuh8aec1ed2016-05-01 13:29:20 -070033
34 static constexpr monotonic_clock::duration zero() { return duration(0); }
Austin Schuh858c0212016-11-25 17:23:30 -080035
36 static constexpr time_point min_time{
37 time_point(duration(::std::numeric_limits<duration::rep>::min()))};
Austin Schuh793d6b92016-05-01 13:28:14 -070038};
39
brians343bc112013-02-10 01:53:46 +000040namespace time {
41
Brian Silvermanc03a30c2019-02-16 18:21:56 -080042#ifdef __linux__
43
Austin Schuh6a6f90c2016-11-25 21:36:42 -080044// Enables returning the mock time value for Now instead of checking the system
45// clock.
46void EnableMockTime(monotonic_clock::time_point now);
47// Calls SetMockTime with the current actual time.
48void UpdateMockTime();
49// Sets now when time is being mocked.
50void SetMockTime(monotonic_clock::time_point now);
51// Convenience function to just increment the mock time by a certain amount in
52// a thread safe way.
53void IncrementMockTime(monotonic_clock::duration amount);
54// Disables mocking time.
55void DisableMockTime();
56
Austin Schuhf2a50ba2016-12-24 16:16:26 -080057// Sets the global offset for all times so monotonic_clock::now() will return
Brian Silvermand0575692015-02-21 16:24:02 -050058// now.
59// There is no synchronization here, so this is only safe when only a single
60// task is running.
61// This is only allowed when the shared memory core infrastructure has been
62// initialized in this process.
Austin Schuhf2a50ba2016-12-24 16:16:26 -080063void OffsetToNow(const monotonic_clock::time_point now);
Brian Silvermand0575692015-02-21 16:24:02 -050064
Austin Schuhf2a50ba2016-12-24 16:16:26 -080065// Construct a time representing the period of hertz.
66constexpr ::std::chrono::nanoseconds FromRate(int hertz) {
67 return ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
68 ::std::chrono::seconds(1)) /
69 hertz;
70}
71
72// RAII class that freezes monotonic_clock::now() (to avoid making large numbers
73// of syscalls to find the real time).
Brian Silvermanb407c672014-04-09 11:58:37 -070074class TimeFreezer {
75 public:
Austin Schuh6a6f90c2016-11-25 21:36:42 -080076 TimeFreezer() { EnableMockTime(monotonic_clock::now()); }
77 ~TimeFreezer() { DisableMockTime(); }
Brian Silvermanb407c672014-04-09 11:58:37 -070078
79 private:
80 DISALLOW_COPY_AND_ASSIGN(TimeFreezer);
81};
82
Brian Silvermanc03a30c2019-02-16 18:21:56 -080083#endif // __linux__
84
Neil Balch229001a2018-01-07 18:22:52 -080085// Converts a monotonic_clock::duration into a timespec object.
86struct timespec to_timespec(::aos::monotonic_clock::duration duration);
87
88// Converts a monotonic_clock::time_point into a timespec object as time since
89// epoch.
90struct timespec to_timespec(::aos::monotonic_clock::time_point time);
91
Brian Silvermanc03a30c2019-02-16 18:21:56 -080092namespace time_internal {
93
94template <class T>
95struct is_duration : std::false_type {};
96template <class Rep, class Period>
97struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type {};
98
99} // namespace time_internal
100
101// Returns the greatest duration t representable in ToDuration that is less or
102// equal to d.
103// Implementation copied from
104// https://en.cppreference.com/w/cpp/chrono/duration/floor.
105// TODO(Brian): Remove once we have C++17 support.
106template <class To, class Rep, class Period,
107 class = std::enable_if_t<time_internal::is_duration<To>{}>>
108constexpr To floor(const std::chrono::duration<Rep, Period> &d) {
109 To t = std::chrono::duration_cast<To>(d);
110 if (t > d) return t - To{1};
111 return t;
112}
113
114// Returns the value t representable in ToDuration that is the closest to d. If
115// there are two such values, returns the even value (that is, the value t such
116// that t % 2 == 0).
117// Implementation copied from
118// https://en.cppreference.com/w/cpp/chrono/duration/round.
119// TODO(Brian): Remove once we have C++17 support.
120template <class To, class Rep, class Period,
121 class = std::enable_if_t<
122 time_internal::is_duration<To>{} &&
123 !std::chrono::treat_as_floating_point<typename To::rep>{}>>
124constexpr To round(const std::chrono::duration<Rep, Period> &d) {
125 To t0 = aos::time::floor<To>(d);
126 To t1 = t0 + To{1};
127 auto diff0 = d - t0;
128 auto diff1 = t1 - d;
129 if (diff0 == diff1) {
130 if (t0.count() & 1) return t1;
131 return t0;
132 } else if (diff0 < diff1) {
133 return t0;
134 }
135 return t1;
136}
137
138// Returns the nearest time point to tp representable in ToDuration, rounding to
139// even in halfway cases, like std::chrono::round in C++17.
140// Implementation copied from
141// https://en.cppreference.com/w/cpp/chrono/time_point/round.
142// TODO(Brian): Remove once we have C++17 support.
143template <class To, class Clock, class FromDuration,
144 class = std::enable_if_t<
145 time_internal::is_duration<To>{} &&
146 !std::chrono::treat_as_floating_point<typename To::rep>{}>>
147constexpr std::chrono::time_point<Clock, To> round(
148 const std::chrono::time_point<Clock, FromDuration> &tp) {
149 return std::chrono::time_point<Clock, To>{
150 aos::time::round<To>(tp.time_since_epoch())};
151}
152
brians343bc112013-02-10 01:53:46 +0000153} // namespace time
154} // namespace aos
155
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800156#ifdef __linux__
157
Austin Schuh793d6b92016-05-01 13:28:14 -0700158namespace std {
159namespace this_thread {
160// Template specialization for monotonic_clock, since we can use clock_nanosleep
161// with TIMER_ABSTIME and get very precise absolute time sleeps.
162template <>
163void sleep_until(const ::aos::monotonic_clock::time_point &end_time);
164
165} // namespace this_thread
166} // namespace std
167
Brian Silvermanc03a30c2019-02-16 18:21:56 -0800168#endif // __linux__
Austin Schuh793d6b92016-05-01 13:28:14 -0700169
John Park33858a32018-09-28 23:05:48 -0700170#endif // AOS_TIME_H_