forked from sorokin/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
111 lines (93 loc) · 2.07 KB
/
timer.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "timer.h"
#include <cassert>
#include <iostream>
timer::timer()
{}
void timer::add(timer_element* e)
{
queue.insert(value_t(e->wakeup, e));
}
void timer::remove(timer_element *e)
{
auto i = queue.find(value_t(e->wakeup, e));
assert(i != queue.end());
queue.erase(i);
}
bool timer::empty() const
{
return queue.empty();
}
timer::clock_t::time_point timer::top() const
{
assert(!empty());
return queue.begin()->first;
}
void timer::notify(clock_t::time_point now)
{
for (;;)
{
if (queue.empty())
break;
auto i = queue.begin();
if (i->first > now)
break;
i->second->t = nullptr;
try
{
i->second->callback();
}
catch (std::exception const& e)
{
std::cerr << "error: " << e.what() << std::endl;
}
catch (...)
{
std::cerr << "unknown exception in timer::notify()" << std::endl;
}
queue.erase(i);
}
}
timer_element::timer_element()
: t(nullptr)
{}
timer_element::timer_element(timer_element::callback_t callback)
: t(nullptr)
, callback(std::move(callback))
{}
timer_element::timer_element(timer& t, clock_t::duration interval, callback_t callback)
: t(&t)
, wakeup(clock_t::now() + interval)
, callback(std::move(callback))
{
t.add(this);
}
timer_element::timer_element(timer& t, clock_t::time_point wakeup, callback_t callback)
: t(&t)
, wakeup(wakeup)
, callback(std::move(callback))
{
t.add(this);
}
timer_element::~timer_element()
{
if (t)
t->remove(this);
}
void timer_element::set_callback(timer_element::callback_t callback)
{
this->callback = std::move(callback);
}
void timer_element::restart(timer& t, clock_t::duration interval)
{
this->t->remove(this);
this->t = &t;
this->wakeup = clock_t::now() + interval;
this->t->add(this);
}
void timer_element::restart(timer& t, clock_t::time_point wakeup)
{
this->t->remove(this);
this->t = &t;
this->wakeup = wakeup;
this->t->add(this);
}