forked from zeromq/libzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimers.cpp
157 lines (125 loc) · 3.74 KB
/
timers.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* SPDX-License-Identifier: MPL-2.0 */
#include "precompiled.hpp"
#include "timers.hpp"
#include "err.hpp"
#include <algorithm>
zmq::timers_t::timers_t () : _tag (0xCAFEDADA), _next_timer_id (0)
{
}
zmq::timers_t::~timers_t ()
{
// Mark the timers as dead
_tag = 0xdeadbeef;
}
bool zmq::timers_t::check_tag () const
{
return _tag == 0xCAFEDADA;
}
int zmq::timers_t::add (size_t interval_, timers_timer_fn handler_, void *arg_)
{
if (handler_ == NULL) {
errno = EFAULT;
return -1;
}
uint64_t when = _clock.now_ms () + interval_;
timer_t timer = {++_next_timer_id, interval_, handler_, arg_};
_timers.insert (timersmap_t::value_type (when, timer));
return timer.timer_id;
}
struct zmq::timers_t::match_by_id
{
match_by_id (int timer_id_) : _timer_id (timer_id_) {}
bool operator() (timersmap_t::value_type const &entry_) const
{
return entry_.second.timer_id == _timer_id;
}
private:
int _timer_id;
};
int zmq::timers_t::cancel (int timer_id_)
{
// check first if timer exists at all
if (_timers.end ()
== std::find_if (_timers.begin (), _timers.end (),
match_by_id (timer_id_))) {
errno = EINVAL;
return -1;
}
// check if timer was already canceled
if (_cancelled_timers.count (timer_id_)) {
errno = EINVAL;
return -1;
}
_cancelled_timers.insert (timer_id_);
return 0;
}
int zmq::timers_t::set_interval (int timer_id_, size_t interval_)
{
const timersmap_t::iterator end = _timers.end ();
const timersmap_t::iterator it =
std::find_if (_timers.begin (), end, match_by_id (timer_id_));
if (it != end) {
timer_t timer = it->second;
timer.interval = interval_;
uint64_t when = _clock.now_ms () + interval_;
_timers.erase (it);
_timers.insert (timersmap_t::value_type (when, timer));
return 0;
}
errno = EINVAL;
return -1;
}
int zmq::timers_t::reset (int timer_id_)
{
const timersmap_t::iterator end = _timers.end ();
const timersmap_t::iterator it =
std::find_if (_timers.begin (), end, match_by_id (timer_id_));
if (it != end) {
timer_t timer = it->second;
uint64_t when = _clock.now_ms () + timer.interval;
_timers.erase (it);
_timers.insert (timersmap_t::value_type (when, timer));
return 0;
}
errno = EINVAL;
return -1;
}
long zmq::timers_t::timeout ()
{
const uint64_t now = _clock.now_ms ();
long res = -1;
const timersmap_t::iterator begin = _timers.begin ();
const timersmap_t::iterator end = _timers.end ();
timersmap_t::iterator it = begin;
for (; it != end; ++it) {
if (0 == _cancelled_timers.erase (it->second.timer_id)) {
// Live timer, lets return the timeout
res = std::max (static_cast<long> (it->first - now), 0l);
break;
}
}
// Remove timed-out timers
_timers.erase (begin, it);
return res;
}
int zmq::timers_t::execute ()
{
const uint64_t now = _clock.now_ms ();
const timersmap_t::iterator begin = _timers.begin ();
const timersmap_t::iterator end = _timers.end ();
timersmap_t::iterator it = _timers.begin ();
for (; it != end; ++it) {
if (0 == _cancelled_timers.erase (it->second.timer_id)) {
// Timer is not cancelled
// Map is ordered, if we have to wait for current timer we can stop.
if (it->first > now)
break;
const timer_t &timer = it->second;
timer.handler (timer.timer_id, timer.arg);
_timers.insert (
timersmap_t::value_type (now + timer.interval, timer));
}
}
_timers.erase (begin, it);
return 0;
}