brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/common/time.h" |
| 2 | |
| 3 | #ifdef __VXWORKS__ |
| 4 | #include <taskLib.h> |
| 5 | #endif |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 6 | #include <errno.h> |
| 7 | #include <string.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | |
| 9 | #include "aos/common/logging/logging.h" |
| 10 | #include "aos/common/inttypes.h" |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame^] | 11 | #include "aos/common/mutex.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | namespace time { |
| 15 | |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame^] | 16 | // State required to enable and use mock time. |
| 17 | namespace { |
| 18 | // True if mock time is enabled. |
| 19 | // This does not need to be checked with the mutex held because setting time to |
| 20 | // be enabled or disabled is atomic, and all future operations are atomic |
| 21 | // anyways. If there is a race condition setting or clearing whether time is |
| 22 | // enabled or not, it will still be a race condition if current_mock_time is |
| 23 | // also set atomically with enabled. |
| 24 | bool mock_time_enabled = false; |
| 25 | // Mutex to make time reads and writes thread safe. |
| 26 | Mutex time_mutex; |
| 27 | // Current time when time is mocked. |
| 28 | Time current_mock_time(0, 0); |
| 29 | |
| 30 | // TODO(aschuh): This doesn't include SleepFor and SleepUntil. |
| 31 | // TODO(aschuh): Create a clock source object and change the default? |
| 32 | // That would let me create a MockTime clock source. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 33 | } |
Austin Schuh | d78ab54 | 2013-03-01 22:22:19 -0800 | [diff] [blame^] | 34 | |
| 35 | void Time::EnableMockTime(const Time now) { |
| 36 | mock_time_enabled = true; |
| 37 | MutexLocker time_mutex_locker(&time_mutex); |
| 38 | current_mock_time = now; |
| 39 | } |
| 40 | |
| 41 | void Time::DisableMockTime() { |
| 42 | MutexLocker time_mutex_locker(&time_mutex); |
| 43 | mock_time_enabled = false; |
| 44 | } |
| 45 | |
| 46 | void Time::SetMockTime(const Time now) { |
| 47 | MutexLocker time_mutex_locker(&time_mutex); |
| 48 | if (!mock_time_enabled) { |
| 49 | LOG(FATAL, "Tried to set mock time and mock time is not enabled\n"); |
| 50 | } |
| 51 | current_mock_time = now; |
| 52 | } |
| 53 | |
| 54 | Time Time::Now(clockid_t clock) { |
| 55 | if (mock_time_enabled) { |
| 56 | MutexLocker time_mutex_locker(&time_mutex); |
| 57 | return current_mock_time; |
| 58 | } else { |
| 59 | timespec temp; |
| 60 | if (clock_gettime(clock, &temp) != 0) { |
| 61 | // TODO(aschuh): There needs to be a pluggable low level logging interface |
| 62 | // so we can break this dependency loop. This also would help during |
| 63 | // startup. |
| 64 | LOG(FATAL, "clock_gettime(%jd, %p) failed with %d: %s\n", |
| 65 | static_cast<uintmax_t>(clock), &temp, errno, strerror(errno)); |
| 66 | } |
| 67 | return Time(temp); |
| 68 | } |
| 69 | } |
| 70 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 71 | void Time::Check() { |
| 72 | if (nsec_ >= kNSecInSec || nsec_ < 0) { |
| 73 | LOG(FATAL, "0 <= nsec_(%"PRId32") < %"PRId32" isn't true.\n", |
| 74 | nsec_, kNSecInSec); |
| 75 | } |
| 76 | static_assert(aos::shm_ok<Time>::value, |
| 77 | "it should be able to go through shared memory"); |
| 78 | } |
| 79 | |
| 80 | Time &Time::operator+=(const Time &rhs) { |
| 81 | sec_ += rhs.sec_; |
| 82 | nsec_ += rhs.nsec_; |
| 83 | if (nsec_ >= kNSecInSec) { |
| 84 | nsec_ -= kNSecInSec; |
| 85 | sec_ += 1; |
| 86 | } |
| 87 | return *this; |
| 88 | } |
| 89 | const Time Time::operator+(const Time &rhs) const { |
| 90 | return Time(*this) += rhs; |
| 91 | } |
| 92 | Time &Time::operator-=(const Time &rhs) { |
| 93 | sec_ -= rhs.sec_; |
| 94 | nsec_ -= rhs.nsec_; |
| 95 | if (nsec_ < 0) { |
| 96 | nsec_ += kNSecInSec; |
| 97 | sec_ -= 1; |
| 98 | } |
| 99 | return *this; |
| 100 | } |
| 101 | const Time Time::operator-(const Time &rhs) const { |
| 102 | return Time(*this) -= rhs; |
| 103 | } |
| 104 | Time &Time::operator*=(int32_t rhs) { |
| 105 | const int64_t temp = static_cast<int64_t>(nsec_) * |
| 106 | static_cast<int64_t>(rhs); |
| 107 | sec_ *= rhs; // better not overflow, or the result is just too big |
| 108 | nsec_ = temp % kNSecInSec; |
| 109 | sec_ += (temp - nsec_) / kNSecInSec; |
| 110 | return *this; |
| 111 | } |
| 112 | const Time Time::operator*(int32_t rhs) const { |
| 113 | return Time(*this) *= rhs; |
| 114 | } |
| 115 | Time &Time::operator/=(int32_t rhs) { |
| 116 | nsec_ = (sec_ % rhs) * (kNSecInSec / rhs) + nsec_ / rhs; |
| 117 | sec_ /= rhs; |
| 118 | return *this; |
| 119 | } |
| 120 | const Time Time::operator/(int32_t rhs) const { |
| 121 | return Time(*this) /= rhs; |
| 122 | } |
| 123 | Time &Time::operator%=(int32_t rhs) { |
| 124 | nsec_ = ToNSec() % rhs; |
| 125 | const int wraps = nsec_ / kNSecInSec; |
| 126 | sec_ = wraps; |
| 127 | nsec_ -= kNSecInSec * wraps; |
| 128 | return *this; |
| 129 | } |
| 130 | const Time Time::operator%(int32_t rhs) const { |
| 131 | return Time(*this) %= rhs; |
| 132 | } |
| 133 | |
| 134 | bool Time::operator==(const Time &rhs) const { |
| 135 | return sec_ == rhs.sec_ && nsec_ == rhs.nsec_; |
| 136 | } |
| 137 | bool Time::operator!=(const Time &rhs) const { |
| 138 | return !(*this == rhs); |
| 139 | } |
| 140 | bool Time::operator<(const Time &rhs) const { |
| 141 | return sec_ < rhs.sec_ || (sec_ == rhs.sec_ && nsec_ < rhs.nsec_); |
| 142 | } |
| 143 | bool Time::operator>(const Time &rhs) const { |
| 144 | return sec_ > rhs.sec_ || (sec_ == rhs.sec_ && nsec_ > rhs.nsec_); |
| 145 | } |
| 146 | bool Time::operator<=(const Time &rhs) const { |
| 147 | return sec_ < rhs.sec_ || (sec_ == rhs.sec_ && nsec_ <= rhs.nsec_); |
| 148 | } |
| 149 | bool Time::operator>=(const Time &rhs) const { |
| 150 | return sec_ > rhs.sec_ || (sec_ == rhs.sec_ && nsec_ >= rhs.nsec_); |
| 151 | } |
| 152 | |
| 153 | bool Time::IsWithin(const Time &other, int64_t amount) const { |
| 154 | const int64_t temp = ToNSec() - other.ToNSec(); |
| 155 | return temp <= amount && temp >= -amount; |
| 156 | } |
| 157 | |
| 158 | std::ostream &operator<<(std::ostream &os, const Time& time) { |
| 159 | return os << "Time{" << time.sec_ << "s, " << time.nsec_ << "ns}"; |
| 160 | } |
| 161 | |
| 162 | void SleepFor(const Time &time, clockid_t clock) { |
| 163 | #ifdef __VXWORKS__ |
| 164 | SleepUntil(Time::Now(clock) + time, clock); |
| 165 | #else |
| 166 | timespec converted(time.ToTimespec()), remaining; |
| 167 | int failure = EINTR; |
| 168 | do { |
| 169 | // This checks whether the last time through the loop actually failed or got |
| 170 | // interrupted. |
| 171 | if (failure != EINTR) { |
| 172 | LOG(FATAL, "clock_nanosleep(%jd, 0, %p, %p) returned %d: %s\n", |
| 173 | static_cast<intmax_t>(clock), &converted, &remaining, |
| 174 | failure, strerror(failure)); |
| 175 | } |
| 176 | failure = clock_nanosleep(clock, 0, &converted, &remaining); |
| 177 | memcpy(&converted, &remaining, sizeof(converted)); |
| 178 | } while (failure != 0); |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | void SleepUntil(const Time &time, clockid_t clock) { |
| 183 | #ifdef __VXWORKS__ |
| 184 | if (clock != CLOCK_REALTIME) { |
| 185 | LOG(FATAL, "vxworks only supports CLOCK_REALTIME\n"); |
| 186 | } |
| 187 | // Vxworks nanosleep is definitely broken (fails horribly at doing remaining |
| 188 | // right), and I don't really want to know how else it's broken, so I'm using |
| 189 | // taskDelay instead because that's simpler. |
| 190 | // The +1 is because sleep functions are supposed to sleep for at least the |
| 191 | // requested amount, so we have to round up to the next clock tick. |
| 192 | while (taskDelay((time - Time::Now(clock)).ToTicks() + 1) != 0) { |
| 193 | if (errno != EINTR) { |
| 194 | LOG(FATAL, "taskDelay(some ticks) failed with %d: %s\n", |
| 195 | errno, strerror(errno)); |
| 196 | } |
| 197 | } |
| 198 | #else |
| 199 | timespec converted(time.ToTimespec()); |
| 200 | int failure; |
| 201 | while ((failure = clock_nanosleep(clock, TIMER_ABSTIME, |
| 202 | &converted, NULL)) != 0) { |
| 203 | if (failure != EINTR) { |
| 204 | LOG(FATAL, "clock_nanosleep(%jd, TIMER_ABSTIME, %p, NULL)" |
| 205 | " returned %d: %s\n", static_cast<intmax_t>(clock), &converted, |
| 206 | failure, strerror(failure)); |
| 207 | } |
| 208 | } |
| 209 | #endif |
| 210 | } |
| 211 | |
| 212 | } // namespace time |
| 213 | } // namespace aos |