diff options
author | snaury <snaury@ydb.tech> | 2023-05-17 00:09:28 +0300 |
---|---|---|
committer | snaury <snaury@ydb.tech> | 2023-05-17 00:09:28 +0300 |
commit | 56a6dcb981398ed4ff100c1cee255d0737c80ec3 (patch) | |
tree | 5944876280d020cf70fdabc149d704cdeb8694ea /library/cpp/time_provider/monotonic.h | |
parent | e95347322acfd16b2fd57c40f30718d6b3e42631 (diff) | |
download | ydb-56a6dcb981398ed4ff100c1cee255d0737c80ec3.tar.gz |
Switch TMonotonic to CLOCK_BOOTTIME instead of a separate type
Diffstat (limited to 'library/cpp/time_provider/monotonic.h')
-rw-r--r-- | library/cpp/time_provider/monotonic.h | 134 |
1 files changed, 28 insertions, 106 deletions
diff --git a/library/cpp/time_provider/monotonic.h b/library/cpp/time_provider/monotonic.h index 9b5e5df149..a1258e3342 100644 --- a/library/cpp/time_provider/monotonic.h +++ b/library/cpp/time_provider/monotonic.h @@ -4,63 +4,22 @@ namespace NMonotonic { - template <class TDerived> - class TMonotonicBase: public TTimeBase<TDerived> { - using TBase = TTimeBase<TDerived>; - - public: - using TBase::TBase; - - using TBase::Days; - using TBase::Hours; - using TBase::MicroSeconds; - using TBase::MilliSeconds; - using TBase::Minutes; - using TBase::Seconds; - - static constexpr TDerived Max() noexcept { - return TDerived::FromValue(::Max<ui64>()); - } - - static constexpr TDerived Zero() noexcept { - return TDerived::FromValue(0); - } - - static constexpr TDerived MicroSeconds(ui64 us) noexcept { - return TDerived::FromValue(TInstant::MicroSeconds(us).GetValue()); - } - - static constexpr TDerived MilliSeconds(ui64 ms) noexcept { - return TDerived::FromValue(TInstant::MilliSeconds(ms).GetValue()); - } - - static constexpr TDerived Seconds(ui64 s) noexcept { - return TDerived::FromValue(TInstant::Seconds(s).GetValue()); - } - - static constexpr TDerived Minutes(ui64 m) noexcept { - return TDerived::FromValue(TInstant::Minutes(m).GetValue()); - } - - static constexpr TDerived Hours(ui64 h) noexcept { - return TDerived::FromValue(TInstant::Hours(h).GetValue()); - } - - static constexpr TDerived Days(ui64 d) noexcept { - return TDerived::FromValue(TInstant::Days(d).GetValue()); - } - }; - /** * Returns current monotonic time in microseconds + * + * On Linux uses CLOCK_BOOTTIME under the hood, so it includes time passed + * during suspend and makes it safe for measuring lease times. */ ui64 GetMonotonicMicroSeconds(); /** * Similar to TInstant, but measuring monotonic time + * + * On Linux uses CLOCK_BOOTTIME under the hood, so it includes time passed + * during suspend and makes it safe for measuring lease times. */ - class TMonotonic: public TMonotonicBase<TMonotonic> { - using TBase = TMonotonicBase<TMonotonic>; + class TMonotonic: public TTimeBase<TMonotonic> { + using TBase = TTimeBase<TMonotonic>; protected: constexpr explicit TMonotonic(TValue value) noexcept @@ -87,60 +46,45 @@ namespace NMonotonic { using TBase::Minutes; using TBase::Seconds; - template <class T> - inline TMonotonic& operator+=(const T& t) noexcept { - return (*this = (*this + t)); + static constexpr TMonotonic Max() noexcept { + return TMonotonic::FromValue(::Max<ui64>()); } - template <class T> - inline TMonotonic& operator-=(const T& t) noexcept { - return (*this = (*this - t)); + static constexpr TMonotonic Zero() noexcept { + return TMonotonic::FromValue(0); } - }; - /** - * Returns current CLOCK_BOOTTIME time in microseconds - */ - ui64 GetBootTimeMicroSeconds(); - - /** - * Similar to TInstant, but measuring CLOCK_BOOTTIME time - */ - class TBootTime: public TMonotonicBase<TBootTime> { - using TBase = TMonotonicBase<TBootTime>; + static constexpr TMonotonic MicroSeconds(ui64 us) noexcept { + return TMonotonic::FromValue(TInstant::MicroSeconds(us).GetValue()); + } - protected: - constexpr explicit TBootTime(TValue value) noexcept - : TBase(value) - { + static constexpr TMonotonic MilliSeconds(ui64 ms) noexcept { + return TMonotonic::FromValue(TInstant::MilliSeconds(ms).GetValue()); } - public: - constexpr TBootTime() noexcept { + static constexpr TMonotonic Seconds(ui64 s) noexcept { + return TMonotonic::FromValue(TInstant::Seconds(s).GetValue()); } - static constexpr TBootTime FromValue(TValue value) noexcept { - return TBootTime(value); + static constexpr TMonotonic Minutes(ui64 m) noexcept { + return TMonotonic::FromValue(TInstant::Minutes(m).GetValue()); } - static inline TBootTime Now() { - return TBootTime::MicroSeconds(GetBootTimeMicroSeconds()); + static constexpr TMonotonic Hours(ui64 h) noexcept { + return TMonotonic::FromValue(TInstant::Hours(h).GetValue()); } - using TBase::Days; - using TBase::Hours; - using TBase::MicroSeconds; - using TBase::MilliSeconds; - using TBase::Minutes; - using TBase::Seconds; + static constexpr TMonotonic Days(ui64 d) noexcept { + return TMonotonic::FromValue(TInstant::Days(d).GetValue()); + } template <class T> - inline TBootTime& operator+=(const T& t) noexcept { + inline TMonotonic& operator+=(const T& t) noexcept { return (*this = (*this + t)); } template <class T> - inline TBootTime& operator-=(const T& t) noexcept { + inline TMonotonic& operator-=(const T& t) noexcept { return (*this = (*this - t)); } }; @@ -148,7 +92,6 @@ namespace NMonotonic { } // namespace NMonotonic Y_DECLARE_PODTYPE(NMonotonic::TMonotonic); -Y_DECLARE_PODTYPE(NMonotonic::TBootTime); namespace std { template <> @@ -157,13 +100,6 @@ namespace std { return hash<NMonotonic::TMonotonic::TValue>()(key.GetValue()); } }; - - template <> - struct hash<NMonotonic::TBootTime> { - size_t operator()(const NMonotonic::TBootTime& key) const noexcept { - return hash<NMonotonic::TBootTime::TValue>()(key.GetValue()); - } - }; } namespace NMonotonic { @@ -182,20 +118,6 @@ namespace NMonotonic { return TMonotonic::FromValue(result.GetValue()); } - constexpr TDuration operator-(const TBootTime& l, const TBootTime& r) { - return TInstant::FromValue(l.GetValue()) - TInstant::FromValue(r.GetValue()); - } - - constexpr TBootTime operator+(const TBootTime& l, const TDuration& r) { - TInstant result = TInstant::FromValue(l.GetValue()) + r; - return TBootTime::FromValue(result.GetValue()); - } - - constexpr TBootTime operator-(const TBootTime& l, const TDuration& r) { - TInstant result = TInstant::FromValue(l.GetValue()) - r; - return TBootTime::FromValue(result.GetValue()); - } - } // namespace NMonotonic // TODO: remove, alias for compatibility |