blob: c4f77a8f2d852354ed58dab028820fb94b108792 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/common/time.h"
2
3#ifdef __VXWORKS__
4#include <taskLib.h>
5#endif
Brian Silvermanf665d692013-02-17 22:11:39 -08006#include <errno.h>
7#include <string.h>
brians343bc112013-02-10 01:53:46 +00008
9#include "aos/common/logging/logging.h"
10#include "aos/common/inttypes.h"
Austin Schuhd78ab542013-03-01 22:22:19 -080011#include "aos/common/mutex.h"
brians343bc112013-02-10 01:53:46 +000012
13namespace aos {
14namespace time {
15
Austin Schuhd78ab542013-03-01 22:22:19 -080016// State required to enable and use mock time.
17namespace {
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.
24bool mock_time_enabled = false;
25// Mutex to make time reads and writes thread safe.
26Mutex time_mutex;
27// Current time when time is mocked.
28Time 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.
brians343bc112013-02-10 01:53:46 +000033}
Austin Schuhd78ab542013-03-01 22:22:19 -080034
35void Time::EnableMockTime(const Time now) {
36 mock_time_enabled = true;
37 MutexLocker time_mutex_locker(&time_mutex);
38 current_mock_time = now;
39}
40
41void Time::DisableMockTime() {
42 MutexLocker time_mutex_locker(&time_mutex);
43 mock_time_enabled = false;
44}
45
46void 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
54Time 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
Brian Silverman52aeeac2013-08-28 16:20:53 -070071void Time::CheckImpl(int32_t nsec) {
brians343bc112013-02-10 01:53:46 +000072 static_assert(aos::shm_ok<Time>::value,
73 "it should be able to go through shared memory");
Brian Silverman52aeeac2013-08-28 16:20:53 -070074 if (nsec >= kNSecInSec || nsec < 0) {
75 LOG(FATAL, "0 <= nsec(%" PRId32 ") < %" PRId32 " isn't true.\n",
76 nsec, kNSecInSec);
77 }
brians343bc112013-02-10 01:53:46 +000078}
79
80Time &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}
89const Time Time::operator+(const Time &rhs) const {
90 return Time(*this) += rhs;
91}
92Time &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}
101const Time Time::operator-(const Time &rhs) const {
102 return Time(*this) -= rhs;
103}
104Time &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}
112const Time Time::operator*(int32_t rhs) const {
113 return Time(*this) *= rhs;
114}
115Time &Time::operator/=(int32_t rhs) {
116 nsec_ = (sec_ % rhs) * (kNSecInSec / rhs) + nsec_ / rhs;
117 sec_ /= rhs;
118 return *this;
119}
120const Time Time::operator/(int32_t rhs) const {
121 return Time(*this) /= rhs;
122}
Brian Silverman2e0dcfd2013-03-30 22:44:40 -0700123double Time::operator/(const Time &rhs) const {
124 return ToSeconds() / rhs.ToSeconds();
125}
brians343bc112013-02-10 01:53:46 +0000126Time &Time::operator%=(int32_t rhs) {
127 nsec_ = ToNSec() % rhs;
128 const int wraps = nsec_ / kNSecInSec;
129 sec_ = wraps;
130 nsec_ -= kNSecInSec * wraps;
131 return *this;
132}
133const Time Time::operator%(int32_t rhs) const {
134 return Time(*this) %= rhs;
135}
136
137bool Time::operator==(const Time &rhs) const {
138 return sec_ == rhs.sec_ && nsec_ == rhs.nsec_;
139}
140bool Time::operator!=(const Time &rhs) const {
141 return !(*this == rhs);
142}
143bool Time::operator<(const Time &rhs) const {
144 return sec_ < rhs.sec_ || (sec_ == rhs.sec_ && nsec_ < rhs.nsec_);
145}
146bool Time::operator>(const Time &rhs) const {
147 return sec_ > rhs.sec_ || (sec_ == rhs.sec_ && nsec_ > rhs.nsec_);
148}
149bool Time::operator<=(const Time &rhs) const {
150 return sec_ < rhs.sec_ || (sec_ == rhs.sec_ && nsec_ <= rhs.nsec_);
151}
152bool Time::operator>=(const Time &rhs) const {
153 return sec_ > rhs.sec_ || (sec_ == rhs.sec_ && nsec_ >= rhs.nsec_);
154}
155
156bool Time::IsWithin(const Time &other, int64_t amount) const {
157 const int64_t temp = ToNSec() - other.ToNSec();
158 return temp <= amount && temp >= -amount;
159}
160
161std::ostream &operator<<(std::ostream &os, const Time& time) {
162 return os << "Time{" << time.sec_ << "s, " << time.nsec_ << "ns}";
163}
164
165void SleepFor(const Time &time, clockid_t clock) {
166#ifdef __VXWORKS__
167 SleepUntil(Time::Now(clock) + time, clock);
168#else
169 timespec converted(time.ToTimespec()), remaining;
170 int failure = EINTR;
171 do {
172 // This checks whether the last time through the loop actually failed or got
173 // interrupted.
174 if (failure != EINTR) {
175 LOG(FATAL, "clock_nanosleep(%jd, 0, %p, %p) returned %d: %s\n",
176 static_cast<intmax_t>(clock), &converted, &remaining,
177 failure, strerror(failure));
178 }
179 failure = clock_nanosleep(clock, 0, &converted, &remaining);
180 memcpy(&converted, &remaining, sizeof(converted));
181 } while (failure != 0);
182#endif
183}
184
185void SleepUntil(const Time &time, clockid_t clock) {
186#ifdef __VXWORKS__
187 if (clock != CLOCK_REALTIME) {
188 LOG(FATAL, "vxworks only supports CLOCK_REALTIME\n");
189 }
190 // Vxworks nanosleep is definitely broken (fails horribly at doing remaining
191 // right), and I don't really want to know how else it's broken, so I'm using
192 // taskDelay instead because that's simpler.
193 // The +1 is because sleep functions are supposed to sleep for at least the
194 // requested amount, so we have to round up to the next clock tick.
195 while (taskDelay((time - Time::Now(clock)).ToTicks() + 1) != 0) {
196 if (errno != EINTR) {
197 LOG(FATAL, "taskDelay(some ticks) failed with %d: %s\n",
198 errno, strerror(errno));
199 }
200 }
201#else
202 timespec converted(time.ToTimespec());
203 int failure;
204 while ((failure = clock_nanosleep(clock, TIMER_ABSTIME,
205 &converted, NULL)) != 0) {
206 if (failure != EINTR) {
207 LOG(FATAL, "clock_nanosleep(%jd, TIMER_ABSTIME, %p, NULL)"
208 " returned %d: %s\n", static_cast<intmax_t>(clock), &converted,
209 failure, strerror(failure));
210 }
211 }
212#endif
213}
214
215} // namespace time
216} // namespace aos