| 1 | /* |
| 2 | Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file |
| 3 | |
| 4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
| 5 | |
| 6 | libzmq is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU Lesser General Public License (LGPL) as published |
| 8 | by the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | As a special exception, the Contributors give you permission to link |
| 12 | this library with independent modules to produce an executable, |
| 13 | regardless of the license terms of these independent modules, and to |
| 14 | copy and distribute the resulting executable under terms of your choice, |
| 15 | provided that you also meet, for each linked independent module, the |
| 16 | terms and conditions of the license of that module. An independent |
| 17 | module is a module which is not derived from or based on this library. |
| 18 | If you modify this library, you must extend this exception to your |
| 19 | version of the library. |
| 20 | |
| 21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
| 22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 24 | License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU Lesser General Public License |
| 27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | */ |
| 29 | |
| 30 | #include "precompiled.hpp" |
| 31 | #include "poller_base.hpp" |
| 32 | #include "i_poll_events.hpp" |
| 33 | #include "err.hpp" |
| 34 | |
| 35 | zmq::poller_base_t::poller_base_t () |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | zmq::poller_base_t::~poller_base_t () |
| 40 | { |
| 41 | // Make sure there is no more load on the shutdown. |
| 42 | zmq_assert (get_load () == 0); |
| 43 | } |
| 44 | |
| 45 | int zmq::poller_base_t::get_load () const |
| 46 | { |
| 47 | return _load.get (); |
| 48 | } |
| 49 | |
| 50 | void zmq::poller_base_t::adjust_load (int amount_) |
| 51 | { |
| 52 | if (amount_ > 0) |
| 53 | _load.add (amount_); |
| 54 | else if (amount_ < 0) |
| 55 | _load.sub (-amount_); |
| 56 | } |
| 57 | |
| 58 | void zmq::poller_base_t::add_timer (int timeout_, i_poll_events *sink_, int id_) |
| 59 | { |
| 60 | uint64_t expiration = _clock.now_ms () + timeout_; |
| 61 | timer_info_t info = {sink_, id_}; |
| 62 | _timers.insert (timers_t::value_type (expiration, info)); |
| 63 | } |
| 64 | |
| 65 | void zmq::poller_base_t::cancel_timer (i_poll_events *sink_, int id_) |
| 66 | { |
| 67 | // Complexity of this operation is O(n). We assume it is rarely used. |
| 68 | for (timers_t::iterator it = _timers.begin (), end = _timers.end (); |
| 69 | it != end; ++it) |
| 70 | if (it->second.sink == sink_ && it->second.id == id_) { |
| 71 | _timers.erase (it); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Timer not found. |
| 76 | zmq_assert (false); |
| 77 | } |
| 78 | |
| 79 | uint64_t zmq::poller_base_t::execute_timers () |
| 80 | { |
| 81 | // Fast track. |
| 82 | if (_timers.empty ()) |
| 83 | return 0; |
| 84 | |
| 85 | // Get the current time. |
| 86 | const uint64_t current = _clock.now_ms (); |
| 87 | |
| 88 | // Execute the timers that are already due. |
| 89 | const timers_t::iterator begin = _timers.begin (); |
| 90 | const timers_t::iterator end = _timers.end (); |
| 91 | uint64_t res = 0; |
| 92 | timers_t::iterator it = begin; |
| 93 | for (; it != end; ++it) { |
| 94 | // If we have to wait to execute the item, same will be true about |
| 95 | // all the following items (multimap is sorted). Thus we can stop |
| 96 | // checking the subsequent timers. |
| 97 | if (it->first > current) { |
| 98 | res = it->first - current; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | // Trigger the timer. |
| 103 | it->second.sink->timer_event (it->second.id); |
| 104 | } |
| 105 | |
| 106 | // Remove them from the list of active timers. |
| 107 | _timers.erase (begin, it); |
| 108 | |
| 109 | // Return the time to wait for the next timer (at least 1ms), or 0, if |
| 110 | // there are no more timers. |
| 111 | return res; |
| 112 | } |
| 113 | |
| 114 | zmq::worker_poller_base_t::worker_poller_base_t (const thread_ctx_t &ctx_) : |
| 115 | _ctx (ctx_) |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | void zmq::worker_poller_base_t::stop_worker () |
| 120 | { |
| 121 | _worker.stop (); |
| 122 | } |
| 123 | |
| 124 | void zmq::worker_poller_base_t::start (const char *name_) |
| 125 | { |
| 126 | zmq_assert (get_load () > 0); |
| 127 | _ctx.start_thread (_worker, worker_routine, this, name_); |
| 128 | } |
| 129 | |
| 130 | void zmq::worker_poller_base_t::check_thread () |
| 131 | { |
| 132 | #ifdef _DEBUG |
| 133 | zmq_assert (!_worker.get_started () || _worker.is_current_thread ()); |
| 134 | #endif |
| 135 | } |
| 136 | |
| 137 | void zmq::worker_poller_base_t::worker_routine (void *arg_) |
| 138 | { |
| 139 | (static_cast<worker_poller_base_t *> (arg_))->loop (); |
| 140 | } |
| 141 | |