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

#include <vespa/fastos/thread.h>
#include <vespa/fastos/cond.h>
#include <vespa/fastos/timestamp.h>

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 : public FastOS_Runnable
{
private:
    Clock(const Clock &);
    Clock & operator = (const Clock &);

    mutable fastos::TimeStamp _timeNS;
    int               _timePeriodMS;
    FastOS_Cond       _cond;
    bool              _stop;
    bool              _running;

    void setTime() const;

    void Run(FastOS_ThreadInterface *thisThread, void *arguments) override;

public:
    Clock(double timePeriod=0.100);
    ~Clock();

    fastos::TimeStamp getTimeNS() const {
        if (!_running) {
            setTime();
        }
        return _timeNS;
    }
    fastos::TimeStamp getTimeNSAssumeRunning() const { return _timeNS; }

    void stop();
};

}