blob: e5419329c2182fd97b0193139217833c2d679bc6 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_COMMON_TIME_H_
2#define AOS_COMMON_TIME_H_
3
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
brians343bc112013-02-10 01:53:46 +000014#include "aos/common/type_traits.h"
Brian Silvermanb407c672014-04-09 11:58:37 -070015#include "aos/common/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
Austin Schuh6a6f90c2016-11-25 21:36:42 -080042// Enables returning the mock time value for Now instead of checking the system
43// clock.
44void EnableMockTime(monotonic_clock::time_point now);
45// Calls SetMockTime with the current actual time.
46void UpdateMockTime();
47// Sets now when time is being mocked.
48void SetMockTime(monotonic_clock::time_point now);
49// Convenience function to just increment the mock time by a certain amount in
50// a thread safe way.
51void IncrementMockTime(monotonic_clock::duration amount);
52// Disables mocking time.
53void DisableMockTime();
54
Austin Schuhf2a50ba2016-12-24 16:16:26 -080055// Sets the global offset for all times so monotonic_clock::now() will return
Brian Silvermand0575692015-02-21 16:24:02 -050056// now.
57// There is no synchronization here, so this is only safe when only a single
58// task is running.
59// This is only allowed when the shared memory core infrastructure has been
60// initialized in this process.
Austin Schuhf2a50ba2016-12-24 16:16:26 -080061void OffsetToNow(const monotonic_clock::time_point now);
Brian Silvermand0575692015-02-21 16:24:02 -050062
Austin Schuhf2a50ba2016-12-24 16:16:26 -080063// Construct a time representing the period of hertz.
64constexpr ::std::chrono::nanoseconds FromRate(int hertz) {
65 return ::std::chrono::duration_cast<::std::chrono::nanoseconds>(
66 ::std::chrono::seconds(1)) /
67 hertz;
68}
69
70// RAII class that freezes monotonic_clock::now() (to avoid making large numbers
71// of syscalls to find the real time).
Brian Silvermanb407c672014-04-09 11:58:37 -070072class TimeFreezer {
73 public:
Austin Schuh6a6f90c2016-11-25 21:36:42 -080074 TimeFreezer() { EnableMockTime(monotonic_clock::now()); }
75 ~TimeFreezer() { DisableMockTime(); }
Brian Silvermanb407c672014-04-09 11:58:37 -070076
77 private:
78 DISALLOW_COPY_AND_ASSIGN(TimeFreezer);
79};
80
brians343bc112013-02-10 01:53:46 +000081} // namespace time
82} // namespace aos
83
Austin Schuh793d6b92016-05-01 13:28:14 -070084namespace std {
85namespace this_thread {
86// Template specialization for monotonic_clock, since we can use clock_nanosleep
87// with TIMER_ABSTIME and get very precise absolute time sleeps.
88template <>
89void sleep_until(const ::aos::monotonic_clock::time_point &end_time);
90
91} // namespace this_thread
92} // namespace std
93
94
brians343bc112013-02-10 01:53:46 +000095#endif // AOS_COMMON_TIME_H_