brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #ifndef AOS_COMMON_TIME_H_ |
| 2 | #define AOS_COMMON_TIME_H_ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <time.h> |
brians | 57dd582 | 2013-02-27 21:44:15 +0000 | [diff] [blame] | 6 | #include <sys/time.h> |
Brian | 4a424a2 | 2014-04-02 11:52:45 -0700 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <type_traits> |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 10 | #include <chrono> |
| 11 | #include <thread> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | #include <ostream> |
| 13 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | #include "aos/common/type_traits.h" |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 15 | #include "aos/common/macros.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | |
| 17 | namespace aos { |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 18 | |
| 19 | class 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 Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 31 | return time_point(zero()); |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 32 | } |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 33 | |
| 34 | static constexpr monotonic_clock::duration zero() { return duration(0); } |
Austin Schuh | 858c021 | 2016-11-25 17:23:30 -0800 | [diff] [blame] | 35 | |
| 36 | static constexpr time_point min_time{ |
| 37 | time_point(duration(::std::numeric_limits<duration::rep>::min()))}; |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 40 | namespace time { |
| 41 | |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 42 | // Enables returning the mock time value for Now instead of checking the system |
| 43 | // clock. |
| 44 | void EnableMockTime(monotonic_clock::time_point now); |
| 45 | // Calls SetMockTime with the current actual time. |
| 46 | void UpdateMockTime(); |
| 47 | // Sets now when time is being mocked. |
| 48 | void 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. |
| 51 | void IncrementMockTime(monotonic_clock::duration amount); |
| 52 | // Disables mocking time. |
| 53 | void DisableMockTime(); |
| 54 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 55 | // Sets the global offset for all times so monotonic_clock::now() will return |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 56 | // 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 Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 61 | void OffsetToNow(const monotonic_clock::time_point now); |
Brian Silverman | d057569 | 2015-02-21 16:24:02 -0500 | [diff] [blame] | 62 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 63 | // Construct a time representing the period of hertz. |
| 64 | constexpr ::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 Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 72 | class TimeFreezer { |
| 73 | public: |
Austin Schuh | 6a6f90c | 2016-11-25 21:36:42 -0800 | [diff] [blame] | 74 | TimeFreezer() { EnableMockTime(monotonic_clock::now()); } |
| 75 | ~TimeFreezer() { DisableMockTime(); } |
Brian Silverman | b407c67 | 2014-04-09 11:58:37 -0700 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | DISALLOW_COPY_AND_ASSIGN(TimeFreezer); |
| 79 | }; |
| 80 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame^] | 81 | // Converts a monotonic_clock::duration into a timespec object. |
| 82 | struct timespec to_timespec(::aos::monotonic_clock::duration duration); |
| 83 | |
| 84 | // Converts a monotonic_clock::time_point into a timespec object as time since |
| 85 | // epoch. |
| 86 | struct timespec to_timespec(::aos::monotonic_clock::time_point time); |
| 87 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 88 | } // namespace time |
| 89 | } // namespace aos |
| 90 | |
Austin Schuh | 793d6b9 | 2016-05-01 13:28:14 -0700 | [diff] [blame] | 91 | namespace std { |
| 92 | namespace this_thread { |
| 93 | // Template specialization for monotonic_clock, since we can use clock_nanosleep |
| 94 | // with TIMER_ABSTIME and get very precise absolute time sleeps. |
| 95 | template <> |
| 96 | void sleep_until(const ::aos::monotonic_clock::time_point &end_time); |
| 97 | |
| 98 | } // namespace this_thread |
| 99 | } // namespace std |
| 100 | |
| 101 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 102 | #endif // AOS_COMMON_TIME_H_ |