aboutsummaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/util/timer.h
blob: f1cd8f743a54b4030305e4adddac002aa443abe7 (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
52
53
54
55
56
57
58
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/util/executor.h>
#include <vespa/vespalib/util/sync.h>
#include <vespa/fastos/thread.h>
#include <vector>

class FNET_Transport;

namespace vespalib {

class TimerTask;

/**
 * Timer is a class capable of running Tasks at a regular
 * interval. The timer can be reset to clear all tasks currently being
 * scheduled.
 */
class Timer
{
private:
    typedef std::unique_ptr<TimerTask> TimerTaskPtr;
    typedef std::vector<TimerTaskPtr> TaskList;
    FastOS_ThreadPool _threadPool;
    std::unique_ptr<FNET_Transport> _transport;
    vespalib::Lock _lock;
    TaskList _taskList;

public:
    /**
     * Create a new timer, capable of scheduling tasks at fixed intervals.
     */
    Timer();

    /**
     * Destroys this timer, finishing the current task executing and then
     * finishing.
     */
    ~Timer();

    /**
     * Schedule new task to be executed at specified intervals.
     *
     * @param task The task to schedule.
     * @param delay The delay to wait before first execution.
     * @param interval The interval in seconds.
     */
    void scheduleAtFixedRate(vespalib::Executor::Task::UP task, double delay, double interval);

    /**
     * Reset timer, clearing the list of task to execute.
     */
    void reset();
};

}