Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 100 lines (80 sloc) 2.86 kb
8d7bf66 @sustrik common base for all pollers created; the only thing it handles at the mo...
sustrik authored
1 /*
8e21d64 @sustrik Copyright dates adjusted to reflect reality
sustrik authored
2 Copyright (c) 2010-2011 250bpm s.r.o.
3 Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
8d7bf66 @sustrik common base for all pollers created; the only thing it handles at the mo...
sustrik authored
4
5 This file is part of 0MQ.
6
7 0MQ is free software; you can redistribute it and/or modify it under
b358df9 @sustrik Name of "GNU Lesser Public License" corrected.
sustrik authored
8 the terms of the GNU Lesser General Public License as published by
8d7bf66 @sustrik common base for all pollers created; the only thing it handles at the mo...
sustrik authored
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 0MQ is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b358df9 @sustrik Name of "GNU Lesser Public License" corrected.
sustrik authored
15 GNU Lesser General Public License for more details.
8d7bf66 @sustrik common base for all pollers created; the only thing it handles at the mo...
sustrik authored
16
b358df9 @sustrik Name of "GNU Lesser Public License" corrected.
sustrik authored
17 You should have received a copy of the GNU Lesser General Public License
8d7bf66 @sustrik common base for all pollers created; the only thing it handles at the mo...
sustrik authored
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "poller_base.hpp"
238640a @sustrik timers properly implemented
sustrik authored
22 #include "i_poll_events.hpp"
8d7bf66 @sustrik common base for all pollers created; the only thing it handles at the mo...
sustrik authored
23 #include "err.hpp"
24
25 zmq::poller_base_t::poller_base_t ()
26 {
27 }
28
29 zmq::poller_base_t::~poller_base_t ()
30 {
238640a @sustrik timers properly implemented
sustrik authored
31 // Make sure there is no more load on the shutdown.
8d7bf66 @sustrik common base for all pollers created; the only thing it handles at the mo...
sustrik authored
32 zmq_assert (get_load () == 0);
33 }
34
35 int zmq::poller_base_t::get_load ()
36 {
37 return load.get ();
38 }
39
40 void zmq::poller_base_t::adjust_load (int amount_)
41 {
42 if (amount_ > 0)
43 load.add (amount_);
44 else if (amount_ < 0)
45 load.sub (-amount_);
46 }
238640a @sustrik timers properly implemented
sustrik authored
47
48 void zmq::poller_base_t::add_timer (int timeout_, i_poll_events *sink_, int id_)
49 {
50 uint64_t expiration = clock.now_ms () + timeout_;
51 timer_info_t info = {sink_, id_};
f9e6d94 @martinpales zmq::poller_base_t : workaround for sunstudio compiler in add_timer()
martinpales authored
52 timers.insert (timers_t::value_type (expiration, info));
238640a @sustrik timers properly implemented
sustrik authored
53 }
54
55 void zmq::poller_base_t::cancel_timer (i_poll_events *sink_, int id_)
56 {
57 // Complexity of this operation is O(n). We assume it is rarely used.
56bdba5 @sustrik Fix cppcheck warnings: Prefer prefix ++/-- operators for non-primitive ...
sustrik authored
58 for (timers_t::iterator it = timers.begin (); it != timers.end (); ++it)
238640a @sustrik timers properly implemented
sustrik authored
59 if (it->second.sink == sink_ && it->second.id == id_) {
60 timers.erase (it);
61 return;
62 }
63
64 // Timer not found.
65 zmq_assert (false);
66 }
67
68 uint64_t zmq::poller_base_t::execute_timers ()
69 {
30c1cba @sustrik latency optimisation for the case where there are no timers
sustrik authored
70 // Fast track.
71 if (timers.empty ())
72 return 0;
73
238640a @sustrik timers properly implemented
sustrik authored
74 // Get the current time.
75 uint64_t current = clock.now_ms ();
76
77 // Execute the timers that are already due.
78 timers_t::iterator it = timers.begin ();
79 while (it != timers.end ()) {
80
81 // If we have to wait to execute the item, same will be true about
82 // all the following items (multimap is sorted). Thus we can stop
83 // checking the subsequent timers and return the time to wait for
84 // the next timer (at least 1ms).
85 if (it->first > current)
86 return it->first - current;
87
88 // Trigger the timer.
89 it->second.sink->timer_event (it->second.id);
90
91 // Remove it from the list of active timers.
92 timers_t::iterator o = it;
93 ++it;
94 timers.erase (o);
95 }
96
97 // There are no more timers.
98 return 0;
99 }
Something went wrong with that request. Please try again.