1/*
2Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3
4This file is part of libzmq, the ZeroMQ core engine in C++.
5
6libzmq is free software; you can redistribute it and/or modify it under
7the terms of the GNU Lesser General Public License (LGPL) as published
8by the Free Software Foundation; either version 3 of the License, or
9(at your option) any later version.
10
11As a special exception, the Contributors give you permission to link
12this library with independent modules to produce an executable,
13regardless of the license terms of these independent modules, and to
14copy and distribute the resulting executable under terms of your choice,
15provided that you also meet, for each linked independent module, the
16terms and conditions of the license of that module. An independent
17module is a module which is not derived from or based on this library.
18If you modify this library, you must extend this exception to your
19version of the library.
20
21libzmq is distributed in the hope that it will be useful, but WITHOUT
22ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24License for more details.
25
26You should have received a copy of the GNU Lesser General Public License
27along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "precompiled.hpp"
31#include "timers.hpp"
32#include "err.hpp"
33
34#include <algorithm>
35
36zmq::timers_t::timers_t () : _tag (0xCAFEDADA), _next_timer_id (0)
37{
38}
39
40zmq::timers_t::~timers_t ()
41{
42 // Mark the timers as dead
43 _tag = 0xdeadbeef;
44}
45
46bool zmq::timers_t::check_tag ()
47{
48 return _tag == 0xCAFEDADA;
49}
50
51int zmq::timers_t::add (size_t interval_, timers_timer_fn handler_, void *arg_)
52{
53 if (handler_ == NULL) {
54 errno = EFAULT;
55 return -1;
56 }
57
58 uint64_t when = _clock.now_ms () + interval_;
59 timer_t timer = {++_next_timer_id, interval_, handler_, arg_};
60 _timers.insert (timersmap_t::value_type (when, timer));
61
62 return timer.timer_id;
63}
64
65struct zmq::timers_t::match_by_id
66{
67 match_by_id (int timer_id_) : _timer_id (timer_id_) {}
68
69 bool operator() (timersmap_t::value_type const &entry_) const
70 {
71 return entry_.second.timer_id == _timer_id;
72 }
73
74 private:
75 int _timer_id;
76};
77
78int zmq::timers_t::cancel (int timer_id_)
79{
80 // check first if timer exists at all
81 if (_timers.end ()
82 == std::find_if (_timers.begin (), _timers.end (),
83 match_by_id (timer_id_))) {
84 errno = EINVAL;
85 return -1;
86 }
87
88 // check if timer was already canceled
89 if (_cancelled_timers.count (timer_id_)) {
90 errno = EINVAL;
91 return -1;
92 }
93
94 _cancelled_timers.insert (timer_id_);
95
96 return 0;
97}
98
99int zmq::timers_t::set_interval (int timer_id_, size_t interval_)
100{
101 const timersmap_t::iterator end = _timers.end ();
102 const timersmap_t::iterator it =
103 std::find_if (_timers.begin (), end, match_by_id (timer_id_));
104 if (it != end) {
105 timer_t timer = it->second;
106 timer.interval = interval_;
107 uint64_t when = _clock.now_ms () + interval_;
108 _timers.erase (it);
109 _timers.insert (timersmap_t::value_type (when, timer));
110
111 return 0;
112 }
113
114 errno = EINVAL;
115 return -1;
116}
117
118int zmq::timers_t::reset (int timer_id_)
119{
120 const timersmap_t::iterator end = _timers.end ();
121 const timersmap_t::iterator it =
122 std::find_if (_timers.begin (), end, match_by_id (timer_id_));
123 if (it != end) {
124 timer_t timer = it->second;
125 uint64_t when = _clock.now_ms () + timer.interval;
126 _timers.erase (it);
127 _timers.insert (timersmap_t::value_type (when, timer));
128
129 return 0;
130 }
131
132 errno = EINVAL;
133 return -1;
134}
135
136long zmq::timers_t::timeout ()
137{
138 const uint64_t now = _clock.now_ms ();
139 long res = -1;
140
141 const timersmap_t::iterator begin = _timers.begin ();
142 const timersmap_t::iterator end = _timers.end ();
143 timersmap_t::iterator it = begin;
144 for (; it != end; ++it) {
145 if (0 == _cancelled_timers.erase (it->second.timer_id)) {
146 // Live timer, lets return the timeout
147 res = std::max (static_cast<long> (it->first - now), 0l);
148 break;
149 }
150 }
151
152 // Remove timed-out timers
153 _timers.erase (begin, it);
154
155 return res;
156}
157
158int zmq::timers_t::execute ()
159{
160 const uint64_t now = _clock.now_ms ();
161
162 const timersmap_t::iterator begin = _timers.begin ();
163 const timersmap_t::iterator end = _timers.end ();
164 timersmap_t::iterator it = _timers.begin ();
165 for (; it != end; ++it) {
166 if (0 == _cancelled_timers.erase (it->second.timer_id)) {
167 // Timer is not cancelled
168
169 // Map is ordered, if we have to wait for current timer we can stop.
170 if (it->first > now)
171 break;
172
173 const timer_t &timer = it->second;
174
175 timer.handler (timer.timer_id, timer.arg);
176
177 _timers.insert (
178 timersmap_t::value_type (now + timer.interval, timer));
179 }
180 }
181 _timers.erase (begin, it);
182
183 return 0;
184}
185