aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/clock.h
blob: 4cd579b8f69bf0bb1f8087c21bd7df4219b8626e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/util/time.h>
#include <atomic>
#include <memory>

namespace vespalib {

/**
 * Clock is a clock that updates the time at defined intervals.
 * It is intended used where you want to check the time with low cost, but where
 * resolution is not that important.
 */

class Clock
{
private:
    const std::atomic<steady_time> &_timeNS;
public:
    Clock(const std::atomic<steady_time> & source) noexcept;
    Clock(const Clock &) = delete;
    Clock & operator =(const Clock &) = delete;
    Clock(Clock &&) = delete;
    Clock & operator =(Clock &&) = delete;
    ~Clock();

    vespalib::steady_time getTimeNS() const noexcept {
        return vespalib::steady_time(_timeNS.load(std::memory_order_relaxed));
    }
};

}