00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef KUDU_UTIL_MONOTIME_H
00018 #define KUDU_UTIL_MONOTIME_H
00019
00020 #include <stdint.h>
00021 #include <string>
00022
00023 #ifdef KUDU_HEADERS_NO_STUBS
00024 #include <gtest/gtest_prod.h>
00025 #else
00026
00027
00028 #include "kudu/client/stubs.h"
00029 #endif
00030
00031 #include "kudu/util/kudu_export.h"
00032
00033 struct timeval;
00034 struct timespec;
00035
00036 namespace kudu {
00037 class MonoTime;
00038
00043 class KUDU_EXPORT MonoDelta {
00044 public:
00053 static MonoDelta FromSeconds(double seconds);
00054 static MonoDelta FromMilliseconds(int64_t ms);
00055 static MonoDelta FromMicroseconds(int64_t us);
00056 static MonoDelta FromNanoseconds(int64_t ns);
00058
00063 MonoDelta();
00064
00066 bool Initialized() const;
00067
00074 bool LessThan(const MonoDelta &rhs) const;
00075
00082 bool MoreThan(const MonoDelta &rhs) const;
00083
00091 bool Equals(const MonoDelta &rhs) const;
00092
00094 std::string ToString() const;
00095
00101 double ToSeconds() const;
00102 int64_t ToMilliseconds() const;
00103 int64_t ToMicroseconds() const;
00104 int64_t ToNanoseconds() const;
00106
00112 void ToTimeVal(struct timeval *tv) const;
00113
00119 void ToTimeSpec(struct timespec *ts) const;
00120
00127 static void NanosToTimeSpec(int64_t nanos, struct timespec* ts);
00128
00129 private:
00130 static const int64_t kUninitialized;
00131
00132 friend class MonoTime;
00133 FRIEND_TEST(TestMonoTime, TestDeltaConversions);
00134
00135 explicit MonoDelta(int64_t delta);
00136 int64_t nano_delta_;
00137 };
00138
00146 class KUDU_EXPORT MonoTime {
00147 public:
00151 static const int64_t kNanosecondsPerSecond = 1000000000L;
00152 static const int64_t kNanosecondsPerMillisecond = 1000000L;
00153 static const int64_t kNanosecondsPerMicrosecond = 1000L;
00154
00155 static const int64_t kMicrosecondsPerSecond = 1000000L;
00157
00161 static MonoTime Now();
00162
00164 static MonoTime Max();
00165
00167 static MonoTime Min();
00168
00176 static const MonoTime& Earliest(const MonoTime& a, const MonoTime& b);
00177
00180 MonoTime();
00181
00183 bool Initialized() const;
00184
00192 MonoDelta GetDeltaSince(const MonoTime &rhs) const;
00193
00198 void AddDelta(const MonoDelta &delta);
00199
00207 bool ComesBefore(const MonoTime &rhs) const;
00208
00210 std::string ToString() const;
00211
00218 bool Equals(const MonoTime& other) const;
00219
00228 MonoTime& operator+=(const MonoDelta& delta);
00229
00235 MonoTime& operator-=(const MonoDelta& delta);
00237
00238 private:
00239 friend class MonoDelta;
00240 FRIEND_TEST(TestMonoTime, TestTimeSpec);
00241 FRIEND_TEST(TestMonoTime, TestDeltaConversions);
00242
00243 explicit MonoTime(const struct timespec &ts);
00244 explicit MonoTime(int64_t nanos);
00245 double ToSeconds() const;
00246 int64_t nanos_;
00247 };
00248
00259 void KUDU_EXPORT SleepFor(const MonoDelta& delta);
00260
00270 bool KUDU_EXPORT operator==(const MonoDelta &lhs, const MonoDelta &rhs);
00271
00278 bool KUDU_EXPORT operator!=(const MonoDelta &lhs, const MonoDelta &rhs);
00279
00286 bool KUDU_EXPORT operator<(const MonoDelta &lhs, const MonoDelta &rhs);
00287
00294 bool KUDU_EXPORT operator<=(const MonoDelta &lhs, const MonoDelta &rhs);
00295
00302 bool KUDU_EXPORT operator>(const MonoDelta &lhs, const MonoDelta &rhs);
00303
00310 bool KUDU_EXPORT operator>=(const MonoDelta &lhs, const MonoDelta &rhs);
00312
00326 bool KUDU_EXPORT operator==(const MonoTime& lhs, const MonoTime& rhs);
00327
00339 bool KUDU_EXPORT operator!=(const MonoTime& lhs, const MonoTime& rhs);
00340
00347 bool KUDU_EXPORT operator<(const MonoTime& lhs, const MonoTime& rhs);
00348
00355 bool KUDU_EXPORT operator<=(const MonoTime& lhs, const MonoTime& rhs);
00356
00363 bool KUDU_EXPORT operator>(const MonoTime& lhs, const MonoTime& rhs);
00364
00371 bool KUDU_EXPORT operator>=(const MonoTime& lhs, const MonoTime& rhs);
00373
00384 MonoTime KUDU_EXPORT operator+(const MonoTime& t, const MonoDelta& delta);
00385
00393 MonoTime KUDU_EXPORT operator-(const MonoTime& t, const MonoDelta& delta);
00394
00407 MonoDelta KUDU_EXPORT operator-(const MonoTime& t_end, const MonoTime& t_begin);
00409
00410 }
00411
00412 #endif