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