aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/clock.cpp
blob: 192c998a0223b5c9bc364227492dcfc139758e10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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};
}