blob: c64f8b82367f06abe064190d5ec3bdb3b48ae388 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#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>
brians57dd5822013-02-27 21:44:15 +00009#include <sys/time.h>
brians343bc112013-02-10 01:53:46 +000010#else
11#include <sysLib.h>
brians57dd5822013-02-27 21:44:15 +000012#include <sys/times.h>
brians343bc112013-02-10 01:53:46 +000013#endif
14#include <ostream>
15
16#include "aos/aos_stdint.h"
17#include "aos/common/type_traits.h"
18
19namespace aos {
20namespace 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//
26// The arithmetic and comparison operators are overloaded because they make
27// complete sense and are very useful. The default copy and assignment stuff is
28// left because it works fine. Multiplication and division of Times by Times are
29// not implemented because I can't think of any uses for them and there are
30// multiple ways to do it.
31struct Time {
32 public:
33 static const int32_t kNSecInSec = 1000000000;
34 static const int32_t kNSecInMSec = 1000000;
35 static const int32_t kNSecInUSec = 1000;
36 static const int32_t kMSecInSec = 1000;
37 static const int32_t kUSecInSec = 1000000;
38 Time(int32_t sec, int32_t nsec) : sec_(sec), nsec_(nsec) {
39 Check();
40 }
41 #ifndef SWIG
42 explicit Time(const struct timespec &value)
43 : sec_(value.tv_sec), nsec_(value.tv_nsec) {
44 Check();
45 }
46 struct timespec ToTimespec() const {
47 struct timespec ans;
48 ans.tv_sec = sec_;
49 ans.tv_nsec = nsec_;
50 return ans;
51 }
brians57dd5822013-02-27 21:44:15 +000052 explicit Time(const struct timeval &value)
53 : sec_(value.tv_sec), nsec_(value.tv_usec * kNSecInUSec) {
54 Check();
55 }
56 struct timeval ToTimeval() const {
57 struct timeval ans;
58 ans.tv_sec = sec_;
59 ans.tv_usec = nsec_ / kNSecInUSec;
60 return ans;
61 }
brians343bc112013-02-10 01:53:46 +000062 #endif // SWIG
63 // CLOCK_MONOTONIC on the fitpc and CLOCK_REALTIME on the cRIO because the
64 // cRIO doesn't have any others.
65 // CLOCK_REALTIME is the default realtime clock and CLOCK_MONOTONIC doesn't
66 // change when somebody changes the wall clock (like the ntp deamon or
67 // whatever). See clock_gettime(2) for details.
68 //
69 // This is the clock that code that just wants to sleep for a certain amount
70 // of time or measure how long something takes should use.
71 #ifndef __VXWORKS__
72 static const clockid_t kDefaultClock = CLOCK_MONOTONIC;
73 #else
74 static const clockid_t kDefaultClock = CLOCK_REALTIME;
75 #endif
76 // Creates a Time representing the current value of the specified clock or
77 // dies.
78 static Time Now(clockid_t clock = kDefaultClock);
79
80 // Constructs a Time representing seconds.
81 static Time InSeconds(double seconds) {
82 return Time(static_cast<int32_t>(seconds),
83 (seconds - static_cast<int32_t>(seconds)) * kNSecInSec);
84 }
85
86 // Constructs a time representing microseconds.
87 static Time InNS(int64_t nseconds) {
88 return Time(nseconds / static_cast<int64_t>(kNSecInSec),
89 nseconds % kNSecInSec);
90 }
91
92 // Constructs a time representing microseconds.
93 static Time InUS(int useconds) {
94 return Time(useconds / kUSecInSec, (useconds % kUSecInSec) * kNSecInUSec);
95 }
96
97 // Constructs a time representing mseconds.
98 static Time InMS(int mseconds) {
99 return Time(mseconds / kMSecInSec, (mseconds % kMSecInSec) * kNSecInMSec);
100 }
101
102 // Checks whether or not this time is within amount nanoseconds of other.
103 bool IsWithin(const Time &other, int64_t amount) const;
104
105 // Returns the time represented all in nanoseconds.
106 int64_t ToNSec() const {
107 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kNSecInSec) +
108 static_cast<int64_t>(nsec_);
109 }
110#ifdef __VXWORKS__
111 // Returns the time represented all in system clock ticks. The system clock
112 // rate is retrieved using sysClkRateGet().
113 int ToTicks() const {
114 return ToNSec() / static_cast<int64_t>(kNSecInSec / sysClkRateGet());
115 }
116#endif
117
118 // Returns the time represented in milliseconds.
119 int64_t ToMSec() const {
120 return static_cast<int64_t>(sec_) * static_cast<int64_t>(kMSecInSec) +
121 (static_cast<int64_t>(nsec_) / static_cast<int64_t>(kNSecInMSec));
122 }
123
124 #ifndef SWIG
125 Time &operator+=(const Time &rhs);
126 Time &operator-=(const Time &rhs);
127 Time &operator*=(int32_t rhs);
128 Time &operator/=(int32_t rhs);
129 Time &operator%=(int32_t rhs);
130 #endif
131 const Time operator+(const Time &rhs) const;
132 const Time operator-(const Time &rhs) const;
133 const Time operator*(int32_t rhs) const;
134 const Time operator/(int32_t rhs) const;
135 const Time operator%(int32_t rhs) const;
136
137 bool operator==(const Time &rhs) const;
138 bool operator!=(const Time &rhs) const;
139 bool operator<(const Time &rhs) const;
140 bool operator>(const Time &rhs) const;
141 bool operator<=(const Time &rhs) const;
142 bool operator>=(const Time &rhs) const;
143
144 #ifndef SWIG
145 // For gtest etc.
146 friend std::ostream &operator<<(std::ostream &os, const Time &time);
147 #endif // SWIG
148
149 int32_t sec() const { return sec_; }
150 void set_sec(int32_t sec) { sec_ = sec; }
151 int32_t nsec() const { return nsec_; }
152 void set_nsec(int32_t nsec) {
153 nsec_ = nsec;
154 Check();
155 }
156
157 private:
158 int32_t sec_, nsec_;
159 // LOG(FATAL)s if nsec_ is >= kNSecInSec.
160 void Check();
161};
162
163// Sleeps for the amount of time represented by time counted by clock.
164void SleepFor(const Time &time, clockid_t clock = Time::kDefaultClock);
165// Sleeps until clock is at the time represented by time.
166void SleepUntil(const Time &time, clockid_t clock = Time::kDefaultClock);
167
168} // namespace time
169} // namespace aos
170
171#endif // AOS_COMMON_TIME_H_