blob: 995011afc7780ca8ebe882f6e60550e8ead2b617 [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
brians343bc112013-02-10 01:53:46 +000071void 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
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}
123Time &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}
130const Time Time::operator%(int32_t rhs) const {
131 return Time(*this) %= rhs;
132}
133
134bool Time::operator==(const Time &rhs) const {
135 return sec_ == rhs.sec_ && nsec_ == rhs.nsec_;
136}
137bool Time::operator!=(const Time &rhs) const {
138 return !(*this == rhs);
139}
140bool Time::operator<(const Time &rhs) const {
141 return sec_ < rhs.sec_ || (sec_ == rhs.sec_ && nsec_ < rhs.nsec_);
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}
152
153bool 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
158std::ostream &operator<<(std::ostream &os, const Time& time) {
159 return os << "Time{" << time.sec_ << "s, " << time.nsec_ << "ns}";
160}
161
162void 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
182void 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