![]() |
libzmq master
The Intelligent Transport Layer
|
00001 /* 00002 Copyright (c) 2010-2011 250bpm s.r.o. 00003 Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file 00004 00005 This file is part of 0MQ. 00006 00007 0MQ is free software; you can redistribute it and/or modify it under 00008 the terms of the GNU Lesser General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 0MQ is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 #include "poller_base.hpp" 00022 #include "i_poll_events.hpp" 00023 #include "err.hpp" 00024 00025 zmq::poller_base_t::poller_base_t () 00026 { 00027 } 00028 00029 zmq::poller_base_t::~poller_base_t () 00030 { 00031 // Make sure there is no more load on the shutdown. 00032 zmq_assert (get_load () == 0); 00033 } 00034 00035 int zmq::poller_base_t::get_load () 00036 { 00037 return load.get (); 00038 } 00039 00040 void zmq::poller_base_t::adjust_load (int amount_) 00041 { 00042 if (amount_ > 0) 00043 load.add (amount_); 00044 else if (amount_ < 0) 00045 load.sub (-amount_); 00046 } 00047 00048 void zmq::poller_base_t::add_timer (int timeout_, i_poll_events *sink_, int id_) 00049 { 00050 uint64_t expiration = clock.now_ms () + timeout_; 00051 timer_info_t info = {sink_, id_}; 00052 timers.insert (timers_t::value_type (expiration, info)); 00053 } 00054 00055 void zmq::poller_base_t::cancel_timer (i_poll_events *sink_, int id_) 00056 { 00057 // Complexity of this operation is O(n). We assume it is rarely used. 00058 for (timers_t::iterator it = timers.begin (); it != timers.end (); ++it) 00059 if (it->second.sink == sink_ && it->second.id == id_) { 00060 timers.erase (it); 00061 return; 00062 } 00063 00064 // Timer not found. 00065 zmq_assert (false); 00066 } 00067 00068 uint64_t zmq::poller_base_t::execute_timers () 00069 { 00070 // Fast track. 00071 if (timers.empty ()) 00072 return 0; 00073 00074 // Get the current time. 00075 uint64_t current = clock.now_ms (); 00076 00077 // Execute the timers that are already due. 00078 timers_t::iterator it = timers.begin (); 00079 while (it != timers.end ()) { 00080 00081 // If we have to wait to execute the item, same will be true about 00082 // all the following items (multimap is sorted). Thus we can stop 00083 // checking the subsequent timers and return the time to wait for 00084 // the next timer (at least 1ms). 00085 if (it->first > current) 00086 return it->first - current; 00087 00088 // Trigger the timer. 00089 it->second.sink->timer_event (it->second.id); 00090 00091 // Remove it from the list of active timers. 00092 timers_t::iterator o = it; 00093 ++it; 00094 timers.erase (o); 00095 } 00096 00097 // There are no more timers. 00098 return 0; 00099 }