blob: ae67b542fcd6b6dbe89bf2139149f43a8f58e884 (
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include <chrono>
#include <memory>
namespace vespalib::metrics {
using TimeStamp = std::chrono::duration<double, std::ratio<1,1>>;
/**
* Simple interface abstracting both timing and time measurement for
* threads wanting to do stuff at regular intervals and also knowing
* at what time stuff was done. The 'next' function blocks until the
* next tick is due and returns the current number of seconds since
* epoch. The parameter passed to the 'next' function should be its
* previous return value, except the first time it is called, then 0
* should be used. A convenience function called 'first' is added for
* this purpose.
**/
struct Tick {
using UP = std::unique_ptr<Tick>;
virtual TimeStamp next(TimeStamp prev) = 0;
TimeStamp first() { return next(TimeStamp(0.0)); }
virtual void kill() = 0;
virtual ~Tick() {}
};
struct TickProxy : Tick {
std::shared_ptr<Tick> tick;
TickProxy(std::shared_ptr<Tick> tick_in) : tick(std::move(tick_in)) {}
TimeStamp next(TimeStamp prev) override { return tick->next(prev); }
void kill() override { tick->kill(); }
};
} // namespace vespalib::metrics
|