diff options
author | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2023-11-03 11:29:48 +0300 |
---|---|---|
committer | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2023-11-03 12:18:01 +0300 |
commit | 561d86b3dc4218ac76469e111f603556c7e2ffea (patch) | |
tree | 1eda1323fa6b63efdb72227425b099fac1956584 /yt | |
parent | e9f6780cc23b6c321a70201416286291b9932184 (diff) | |
download | ydb-561d86b3dc4218ac76469e111f603556c7e2ffea.tar.gz |
YT-20376: Improved TTimerGuard
Diffstat (limited to 'yt')
-rw-r--r-- | yt/yt/core/profiling/timing-inl.h | 24 | ||||
-rw-r--r-- | yt/yt/core/profiling/timing.h | 9 |
2 files changed, 31 insertions, 2 deletions
diff --git a/yt/yt/core/profiling/timing-inl.h b/yt/yt/core/profiling/timing-inl.h index 81cc08303a..ca826d12d4 100644 --- a/yt/yt/core/profiling/timing-inl.h +++ b/yt/yt/core/profiling/timing-inl.h @@ -43,9 +43,31 @@ TTimerGuard<TTimer>::TTimerGuard(TTimer* timer) } template <class TTimer> +TTimerGuard<TTimer>::TTimerGuard(TTimerGuard&& other) noexcept + : Timer_(std::exchange(other.Timer_, nullptr)) +{ } + +template <class TTimer> +TTimerGuard<TTimer>& TTimerGuard<TTimer>::operator = (TTimerGuard&& other) noexcept +{ + TryStopTimer(); + + Timer_ = std::exchange(other.Timer_); + return *this; +} + +template <class TTimer> TTimerGuard<TTimer>::~TTimerGuard() { - Timer_->Stop(); + TryStopTimer(); +} + +template <class TTimer> +void TTimerGuard<TTimer>::TryStopTimer() noexcept +{ + if (Timer_) { + Timer_->Stop(); + } } //////////////////////////////////////////////////////////////////////////////// diff --git a/yt/yt/core/profiling/timing.h b/yt/yt/core/profiling/timing.h index e293038a0c..43ec347232 100644 --- a/yt/yt/core/profiling/timing.h +++ b/yt/yt/core/profiling/timing.h @@ -112,13 +112,20 @@ public: //! Calls TTimer::Start() on construction and TTimer::Stop() on destruction. template <class TTimer> class TTimerGuard + : public TNonCopyable { public: explicit TTimerGuard(TTimer* timer); + + TTimerGuard(TTimerGuard&& other) noexcept; + TTimerGuard& operator = (TTimerGuard&& other) noexcept; + ~TTimerGuard(); private: - TTimer* const Timer_; + TTimer* Timer_; + + void TryStopTimer() noexcept; }; //////////////////////////////////////////////////////////////////////////////// |