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> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | #include <ostream> |
| 11 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | #include "aos/common/type_traits.h" |
| 13 | |
| 14 | namespace aos { |
| 15 | namespace time { |
| 16 | |
| 17 | // A nice structure for representing times. |
| 18 | // 0 <= nsec_ < kNSecInSec should always be true. All functions here will make |
| 19 | // sure that that is true if it was on all inputs (including *this). |
| 20 | // |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 21 | // Negative times are supported so that all of the normal arithmetic identities |
| 22 | // work. nsec_ is still always positive. |
| 23 | // |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 24 | // The arithmetic and comparison operators are overloaded because they make |
| 25 | // complete sense and are very useful. The default copy and assignment stuff is |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 26 | // left because it works fine. Multiplication of Times by Times is |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 27 | // not implemented because I can't think of any uses for them and there are |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 28 | // multiple ways to do it. Division of Times by Times is implemented as the |
| 29 | // ratio of them. Multiplication, division, and modulus of Times by integers are |
| 30 | // implemented as interpreting the argument as nanoseconds. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 31 | struct Time { |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 32 | #ifdef SWIG |
| 33 | // All of the uses of constexpr here can safely be simply removed. |
| 34 | // NOTE: This means that relying on the fact that constexpr implicitly makes |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 35 | // member functions const is not valid, so they all have to be explicitly marked |
| 36 | // const. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 37 | #define constexpr |
| 38 | #endif // SWIG |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 39 | public: |
| 40 | static const int32_t kNSecInSec = 1000000000; |
| 41 | static const int32_t kNSecInMSec = 1000000; |
| 42 | static const int32_t kNSecInUSec = 1000; |
| 43 | static const int32_t kMSecInSec = 1000; |
| 44 | static const int32_t kUSecInSec = 1000000; |
Brian Silverman | 96e6d5a | 2014-03-24 15:55:40 -0700 | [diff] [blame] | 45 | constexpr Time(int32_t sec = 0, int32_t nsec = 0) |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 46 | : sec_(sec), nsec_(CheckConstexpr(nsec)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 47 | } |
| 48 | #ifndef SWIG |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 49 | explicit constexpr Time(const struct timespec &value) |
| 50 | : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_nsec)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 51 | } |
| 52 | struct timespec ToTimespec() const { |
| 53 | struct timespec ans; |
| 54 | ans.tv_sec = sec_; |
| 55 | ans.tv_nsec = nsec_; |
| 56 | return ans; |
| 57 | } |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 58 | explicit constexpr Time(const struct timeval &value) |
| 59 | : sec_(value.tv_sec), nsec_(CheckConstexpr(value.tv_usec * kNSecInUSec)) { |
brians | 57dd582 | 2013-02-27 21:44:15 +0000 | [diff] [blame] | 60 | } |
| 61 | struct timeval ToTimeval() const { |
| 62 | struct timeval ans; |
| 63 | ans.tv_sec = sec_; |
| 64 | ans.tv_usec = nsec_ / kNSecInUSec; |
| 65 | return ans; |
| 66 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 67 | #endif // SWIG |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 68 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 69 | // CLOCK_MONOTONIC on linux and CLOCK_REALTIME on the cRIO because the |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 70 | // cRIO doesn't have any others. |
| 71 | // CLOCK_REALTIME is the default realtime clock and CLOCK_MONOTONIC doesn't |
| 72 | // change when somebody changes the wall clock (like the ntp deamon or |
| 73 | // whatever). See clock_gettime(2) for details. |
| 74 | // |
| 75 | // This is the clock that code that just wants to sleep for a certain amount |
| 76 | // of time or measure how long something takes should use. |
| 77 | #ifndef __VXWORKS__ |
| 78 | static const clockid_t kDefaultClock = CLOCK_MONOTONIC; |
| 79 | #else |
| 80 | static const clockid_t kDefaultClock = CLOCK_REALTIME; |
| 81 | #endif |
| 82 | // Creates a Time representing the current value of the specified clock or |
| 83 | // dies. |
| 84 | static Time Now(clockid_t clock = kDefaultClock); |
| 85 | |
| 86 | // Constructs a Time representing seconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 87 | static constexpr Time InSeconds(double seconds) { |
| 88 | return (seconds < 0.0) ? |
| 89 | Time(static_cast<int32_t>(seconds) - 1, |
| 90 | (seconds - static_cast<int32_t>(seconds) + 1.0) * kNSecInSec) : |
| 91 | Time(static_cast<int32_t>(seconds), |
| 92 | (seconds - static_cast<int32_t>(seconds)) * kNSecInSec); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Constructs a time representing microseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 96 | static constexpr Time InNS(int64_t nseconds) { |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 97 | return (nseconds < 0) ? |
| 98 | Time(nseconds / static_cast<int64_t>(kNSecInSec) - 1, |
| 99 | (nseconds % kNSecInSec) + kNSecInSec) : |
| 100 | Time(nseconds / static_cast<int64_t>(kNSecInSec), |
| 101 | nseconds % kNSecInSec); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Constructs a time representing microseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 105 | static constexpr Time InUS(int useconds) { |
| 106 | return (useconds < 0) ? |
| 107 | Time(useconds / kUSecInSec - 1, |
| 108 | (useconds % kUSecInSec) * kNSecInUSec + kNSecInSec) : |
| 109 | Time(useconds / kUSecInSec, (useconds % kUSecInSec) * kNSecInUSec); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Constructs a time representing mseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 113 | static constexpr Time InMS(int mseconds) { |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 114 | return (mseconds < 0) ? |
| 115 | Time(mseconds / kMSecInSec - 1, |
| 116 | (mseconds % kMSecInSec) * kNSecInMSec + kNSecInSec) : |
| 117 | Time(mseconds / kMSecInSec, (mseconds % kMSecInSec) * kNSecInMSec); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Checks whether or not this time is within amount nanoseconds of other. |
| 121 | bool IsWithin(const Time &other, int64_t amount) const; |
| 122 | |
| 123 | // Returns the time represented all in nanoseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 124 | int64_t constexpr ToNSec() const { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 125 | return static_cast<int64_t>(sec_) * static_cast<int64_t>(kNSecInSec) + |
| 126 | static_cast<int64_t>(nsec_); |
| 127 | } |
| 128 | #ifdef __VXWORKS__ |
| 129 | // Returns the time represented all in system clock ticks. The system clock |
| 130 | // rate is retrieved using sysClkRateGet(). |
| 131 | int ToTicks() const { |
| 132 | return ToNSec() / static_cast<int64_t>(kNSecInSec / sysClkRateGet()); |
| 133 | } |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 134 | // Constructs a Time representing ticks. |
| 135 | static Time InTicks(int ticks) { |
Brian Silverman | ffe3d71 | 2013-03-15 21:35:59 -0700 | [diff] [blame] | 136 | return Time::InSeconds(static_cast<double>(ticks) / sysClkRateGet()); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 137 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 138 | #endif |
| 139 | |
| 140 | // Returns the time represented in milliseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 141 | int64_t constexpr ToMSec() const { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 142 | return static_cast<int64_t>(sec_) * static_cast<int64_t>(kMSecInSec) + |
| 143 | (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInMSec)); |
| 144 | } |
| 145 | |
Brian Silverman | 7645d2f | 2013-03-30 18:53:56 -0700 | [diff] [blame] | 146 | // Returns the time represent in microseconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 147 | int64_t constexpr ToUSec() const { |
Brian Silverman | 7645d2f | 2013-03-30 18:53:56 -0700 | [diff] [blame] | 148 | return static_cast<int64_t>(sec_) * static_cast<int64_t>(kUSecInSec) + |
| 149 | (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInUSec)); |
| 150 | } |
| 151 | |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 152 | // Returns the time represented in fractional seconds. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 153 | double constexpr ToSeconds() const { |
brians | f0165ca | 2013-03-02 06:17:47 +0000 | [diff] [blame] | 154 | return static_cast<double>(sec_) + static_cast<double>(nsec_) / kNSecInSec; |
| 155 | } |
| 156 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 157 | #ifndef SWIG |
| 158 | Time &operator+=(const Time &rhs); |
| 159 | Time &operator-=(const Time &rhs); |
| 160 | Time &operator*=(int32_t rhs); |
| 161 | Time &operator/=(int32_t rhs); |
| 162 | Time &operator%=(int32_t rhs); |
Brian Silverman | 0079a9d | 2013-10-24 15:57:35 -0700 | [diff] [blame] | 163 | Time &operator%=(double rhs) = delete; |
| 164 | Time &operator*=(double rhs) = delete; |
| 165 | Time &operator/=(double rhs) = delete; |
| 166 | const Time operator*(double rhs) const = delete; |
| 167 | const Time operator/(double rhs) const = delete; |
| 168 | const Time operator%(double rhs) const = delete; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 169 | #endif |
| 170 | const Time operator+(const Time &rhs) const; |
| 171 | const Time operator-(const Time &rhs) const; |
| 172 | const Time operator*(int32_t rhs) const; |
| 173 | const Time operator/(int32_t rhs) const; |
Brian Silverman | 2e0dcfd | 2013-03-30 22:44:40 -0700 | [diff] [blame] | 174 | double operator/(const Time &rhs) const; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 175 | const Time operator%(int32_t rhs) const; |
| 176 | |
Brian Silverman | 0079a9d | 2013-10-24 15:57:35 -0700 | [diff] [blame] | 177 | const Time operator-() const; |
| 178 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 179 | bool operator==(const Time &rhs) const; |
| 180 | bool operator!=(const Time &rhs) const; |
| 181 | bool operator<(const Time &rhs) const; |
| 182 | bool operator>(const Time &rhs) const; |
| 183 | bool operator<=(const Time &rhs) const; |
| 184 | bool operator>=(const Time &rhs) const; |
| 185 | |
| 186 | #ifndef SWIG |
| 187 | // For gtest etc. |
| 188 | friend std::ostream &operator<<(std::ostream &os, const Time &time); |
| 189 | #endif // SWIG |
| 190 | |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 191 | int32_t constexpr sec() const { return sec_; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 192 | void set_sec(int32_t sec) { sec_ = sec; } |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 193 | int32_t constexpr nsec() const { return nsec_; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 194 | void set_nsec(int32_t nsec) { |
| 195 | nsec_ = nsec; |
| 196 | Check(); |
| 197 | } |
| 198 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 199 | // Absolute value. |
| 200 | Time abs() const { |
| 201 | if (*this > Time(0, 0)) return *this; |
Brian Silverman | f3cbebd | 2013-03-19 16:53:18 -0700 | [diff] [blame] | 202 | if (nsec_ == 0) return Time(-sec_, 0); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 203 | return Time(-sec_ - 1, kNSecInSec - nsec_); |
| 204 | } |
| 205 | |
| 206 | // Enables returning the mock time value for Now instead of checking the |
| 207 | // system clock. This should only be used when testing things depending on |
| 208 | // time, or many things may/will break. |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 209 | static void EnableMockTime(const Time &now); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 210 | // Sets now when time is being mocked. |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 211 | static void SetMockTime(const Time &now); |
| 212 | // Convenience function to just increment the mock time by a certain amount in |
| 213 | // a thread safe way. |
| 214 | static void IncrementMockTime(const Time &amount); |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 215 | // Disables mocking time. |
| 216 | static void DisableMockTime(); |
| 217 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 218 | private: |
| 219 | int32_t sec_, nsec_; |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 220 | |
| 221 | // LOG(FATAL)s if nsec is >= kNSecInSec or negative. |
| 222 | static void CheckImpl(int32_t nsec); |
| 223 | void Check() { CheckImpl(nsec_); } |
| 224 | // A constexpr version of CheckImpl that returns the given value when it |
Brian Silverman | 6659bc3 | 2013-10-16 10:31:32 -0700 | [diff] [blame] | 225 | // succeeds or evaluates to non-constexpr and returns 0 when it fails. |
| 226 | // This will result in the usual LOG(FATAL) if this is used where it isn't |
| 227 | // required to be constexpr or a compile error if it is. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 228 | static constexpr int32_t CheckConstexpr(int32_t nsec) { |
| 229 | return (nsec >= kNSecInSec || nsec < 0) ? CheckImpl(nsec), 0 : nsec; |
| 230 | } |
| 231 | |
| 232 | #ifdef SWIG |
| 233 | #undef constexpr |
| 234 | #endif // SWIG |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | // Sleeps for the amount of time represented by time counted by clock. |
| 238 | void SleepFor(const Time &time, clockid_t clock = Time::kDefaultClock); |
| 239 | // Sleeps until clock is at the time represented by time. |
| 240 | void SleepUntil(const Time &time, clockid_t clock = Time::kDefaultClock); |
| 241 | |
| 242 | } // namespace time |
| 243 | } // namespace aos |
| 244 | |
| 245 | #endif // AOS_COMMON_TIME_H_ |