blob: 99254240f3c39d55760690af2775efacd22a8b32 (
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
59
60
61
62
63
64
65
66
67
68
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "scheduledexecutor.h"
#include <vespa/fnet/scheduler.h>
#include <vespa/fnet/task.h>
#include <vespa/fnet/transport.h>
namespace vespalib {
using Task = vespalib::Executor::Task;
class TimerTask : public FNET_Task
{
private:
TimerTask(const TimerTask &);
TimerTask&operator=(const TimerTask &);
FNET_Scheduler *_scheduler;
Task::UP _task;
duration _interval;
public:
TimerTask(FNET_Scheduler *scheduler, Task::UP task, duration interval)
: FNET_Task(scheduler),
_task(std::move(task)),
_interval(interval)
{ }
~TimerTask() {
Kill();
}
void PerformTask() override {
_task->run();
Schedule(to_s(_interval));
}
};
ScheduledExecutor::ScheduledExecutor(FNET_Transport & transport)
: _transport(transport),
_lock(),
_taskList()
{ }
ScheduledExecutor::~ScheduledExecutor()
{
reset();
}
void
ScheduledExecutor::scheduleAtFixedRate(vespalib::Executor::Task::UP task, duration delay, duration interval)
{
std::lock_guard guard(_lock);
auto tTask = std::make_unique<TimerTask>(_transport.GetScheduler(), std::move(task), interval);
_taskList.push_back(std::move(tTask));
_taskList.back()->Schedule(to_s(delay));
}
void
ScheduledExecutor::reset()
{
std::lock_guard guard(_lock);
for (auto & task : _taskList) {
task->Unschedule();
}
_taskList.clear();
}
}
|