aboutsummaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/util/clock.cpp
blob: 1e474155ff44aff49633150a19c03ba409b396ae (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
49
50
51
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "clock.h"

using namespace fastos;

namespace vespalib {


Clock::Clock(double timePeriod) :
     _timeNS(0u),
     _timePeriodMS(static_cast<uint32_t>(timePeriod*1000)),
     _cond(),
     _stop(false),
     _running(false)
{
    setTime();
}

Clock::~Clock()
{
    assert(!_running);
}

void Clock::setTime() const
{
    _timeNS = ClockSystem::adjustTick2Sec(ClockSystem::now());
}

void Clock::Run(FastOS_ThreadInterface *thread, void *arguments)
{
    (void) arguments;
    _running = true;
    _cond.Lock();
    while ( ! thread->GetBreakFlag() && !_stop) {
        setTime();
        _cond.TimedWait(_timePeriodMS);
    }
    _cond.Unlock();
    _running = false;
}

void
Clock::stop(void)
{
    _cond.Lock();
    _stop = true;
    _cond.Broadcast();
    _cond.Unlock();
}

}