diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2023-03-31 10:54:08 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2023-03-31 12:28:07 +0300 |
commit | fc1cffcfa7f0497a1f97b384a24bcbf23362f3be (patch) | |
tree | c15f7ab5b9e9b20fd0ef8fc07d598d28e8b32004 /library/cpp/unified_agent_client/clock.cpp | |
parent | 8a749596d40e91c896a1907afcd108d9221fbde1 (diff) | |
download | ydb-e9cbe5c5cf67db853d223fd365c9f05b695f7b96.tar.gz |
Ydb stable 23-1-1923.1.19
x-stable-origin-commit: c5d5a396e89d0a72e0267a55e93d8404d4fb54fe
Diffstat (limited to 'library/cpp/unified_agent_client/clock.cpp')
-rw-r--r-- | library/cpp/unified_agent_client/clock.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/library/cpp/unified_agent_client/clock.cpp b/library/cpp/unified_agent_client/clock.cpp new file mode 100644 index 0000000000..192c998a02 --- /dev/null +++ b/library/cpp/unified_agent_client/clock.cpp @@ -0,0 +1,48 @@ +#include "clock.h" + +namespace NUnifiedAgent { + void TClock::Configure() { + Y_VERIFY(!Configured_); + + Configured_ = true; + } + + void TClock::SetBase(TInstant value) { + Y_VERIFY(Configured_); + + Base_.store(value.GetValue()); + } + + void TClock::ResetBase() { + Base_.store(0); + } + + void TClock::ResetBaseWithShift() { + Y_VERIFY(Configured_); + + Shift_.store(static_cast<i64>(Base_.exchange(0)) - static_cast<i64>(::Now().GetValue())); + } + + void TClock::SetShift(TDuration value) { + Y_VERIFY(Configured_); + + Shift_.fetch_add(value.GetValue()); + } + + void TClock::ResetShift() { + Shift_.store(0); + } + + TInstant TClock::Get() { + auto base = Base_.load(); + if (base == 0) { + base = ::Now().GetValue(); + } + base += Shift_.load(); + return TInstant::FromValue(base); + } + + bool TClock::Configured_{false}; + std::atomic<ui64> TClock::Base_{0}; + std::atomic<i64> TClock::Shift_{0}; +} |