clock.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "clock.h"
  2. namespace NUnifiedAgent {
  3. void TClock::Configure() {
  4. Y_ABORT_UNLESS(!Configured_);
  5. Configured_ = true;
  6. }
  7. void TClock::SetBase(TInstant value) {
  8. Y_ABORT_UNLESS(Configured_);
  9. Base_.store(value.GetValue());
  10. }
  11. void TClock::ResetBase() {
  12. Base_.store(0);
  13. }
  14. void TClock::ResetBaseWithShift() {
  15. Y_ABORT_UNLESS(Configured_);
  16. Shift_.store(static_cast<i64>(Base_.exchange(0)) - static_cast<i64>(::Now().GetValue()));
  17. }
  18. void TClock::SetShift(TDuration value) {
  19. Y_ABORT_UNLESS(Configured_);
  20. Shift_.fetch_add(value.GetValue());
  21. }
  22. void TClock::ResetShift() {
  23. Shift_.store(0);
  24. }
  25. TInstant TClock::Get() {
  26. auto base = Base_.load();
  27. if (base == 0) {
  28. base = ::Now().GetValue();
  29. }
  30. base += Shift_.load();
  31. return TInstant::FromValue(base);
  32. }
  33. bool TClock::Configured_{false};
  34. std::atomic<ui64> TClock::Base_{0};
  35. std::atomic<i64> TClock::Shift_{0};
  36. }