Commit 19a70ab6 authored by Simon Giesecke's avatar Simon Giesecke

Optimized and simplified zmq::timers_t::execute and zmq::timers_t::timeout

parent 4f77cfa3
...@@ -135,65 +135,50 @@ int zmq::timers_t::reset (int timer_id_) ...@@ -135,65 +135,50 @@ int zmq::timers_t::reset (int timer_id_)
long zmq::timers_t::timeout () long zmq::timers_t::timeout ()
{ {
timersmap_t::iterator it = _timers.begin (); const uint64_t now = _clock.now_ms ();
long res = -1;
uint64_t now = _clock.now_ms ();
while (it != _timers.end ()) {
cancelled_timers_t::iterator cancelled_it =
_cancelled_timers.find (it->second.timer_id);
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 // Live timer, lets return the timeout
if (cancelled_it == _cancelled_timers.end ()) { res = it->first > now ? static_cast<long> (it->first - now) : 0;
if (it->first > now) break;
return static_cast<long> (it->first - now);
return 0;
} }
// Let's remove it from the beginning of the list
timersmap_t::iterator old = it;
++it;
_timers.erase (old);
_cancelled_timers.erase (cancelled_it);
} }
// Wait forever as no timers are alive // Remove timed-out timers
return -1; _timers.erase (begin, it);
return res;
} }
int zmq::timers_t::execute () int zmq::timers_t::execute ()
{ {
timersmap_t::iterator it = _timers.begin (); const uint64_t now = _clock.now_ms ();
uint64_t now = _clock.now_ms ();
while (it != _timers.end ()) { const timersmap_t::iterator begin = _timers.begin ();
cancelled_timers_t::iterator cancelled_it = const timersmap_t::iterator end = _timers.end ();
_cancelled_timers.find (it->second.timer_id); timersmap_t::iterator it = _timers.begin ();
for (; it != end; ++it) {
// Dead timer, lets remove it and continue if (0 == _cancelled_timers.erase (it->second.timer_id)) {
if (cancelled_it != _cancelled_timers.end ()) { // Timer is not cancelled
timersmap_t::iterator old = it;
++it;
_timers.erase (old);
_cancelled_timers.erase (cancelled_it);
continue;
}
// Map is ordered, if we have to wait for current timer we can stop. // Map is ordered, if we have to wait for current timer we can stop.
if (it->first > now) if (it->first > now)
break; break;
timer_t timer = it->second; const timer_t &timer = it->second;
timer.handler (timer.timer_id, timer.arg); timer.handler (timer.timer_id, timer.arg);
timersmap_t::iterator old = it; _timers.insert (
++it; timersmap_t::value_type (now + timer.interval, timer));
_timers.erase (old); }
_timers.insert (timersmap_t::value_type (now + timer.interval, timer));
} }
_timers.erase (begin, it);
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment