Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 7e693be | 2017-01-30 00:14:05 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "scheduler.hpp" |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 23 | #include "detail/monotonic-deadline-timer.hpp" |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 24 | #include <boost/scope_exit.hpp> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 27 | namespace util { |
| 28 | namespace scheduler { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 29 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 30 | class EventInfo : noncopyable |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 31 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 32 | public: |
| 33 | EventInfo(time::nanoseconds after, const EventCallback& callback) |
| 34 | : expireTime(time::steady_clock::now() + after) |
| 35 | , isExpired(false) |
| 36 | , callback(callback) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 40 | time::nanoseconds |
| 41 | expiresFromNow() const |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 42 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 43 | return std::max(expireTime - time::steady_clock::now(), time::nanoseconds::zero()); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 46 | public: |
| 47 | time::steady_clock::TimePoint expireTime; |
| 48 | bool isExpired; |
| 49 | EventCallback callback; |
| 50 | EventQueue::const_iterator queueIt; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 53 | bool |
| 54 | EventId::operator!() const |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 55 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 56 | return m_info.expired() || m_info.lock()->isExpired; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 59 | bool |
| 60 | EventId::operator==(const EventId& other) const |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 61 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 62 | return (!(*this) && !other) || |
| 63 | !(m_info.owner_before(other.m_info) || other.m_info.owner_before(m_info)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 66 | std::ostream& |
| 67 | operator<<(std::ostream& os, const EventId& eventId) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 68 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 69 | return os << eventId.m_info.lock(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 72 | bool |
| 73 | EventQueueCompare::operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const |
| 74 | { |
| 75 | return a->expireTime < b->expireTime; |
| 76 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 77 | |
| 78 | Scheduler::Scheduler(boost::asio::io_service& ioService) |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 79 | : m_deadlineTimer(make_unique<detail::MonotonicDeadlineTimer>(ioService)) |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 80 | , m_isEventExecuting(false) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 81 | { |
| 82 | } |
| 83 | |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 84 | Scheduler::~Scheduler() = default; |
| 85 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 86 | EventId |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 87 | Scheduler::scheduleEvent(time::nanoseconds after, const EventCallback& callback) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 88 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 89 | BOOST_ASSERT(callback != nullptr); |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 90 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 91 | EventQueue::iterator i = m_queue.insert(make_shared<EventInfo>(after, callback)); |
| 92 | (*i)->queueIt = i; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 93 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 94 | if (!m_isEventExecuting && i == m_queue.begin()) { |
| 95 | // the new event is the first one to expire |
| 96 | this->scheduleNext(); |
| 97 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 98 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 99 | return EventId(*i); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 100 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 101 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 102 | void |
| 103 | Scheduler::cancelEvent(const EventId& eventId) |
| 104 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 105 | shared_ptr<EventInfo> info = eventId.m_info.lock(); |
| 106 | if (info == nullptr || info->isExpired) { |
| 107 | return; // event already expired or cancelled |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 108 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 109 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 110 | if (info->queueIt == m_queue.begin()) { |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 111 | m_deadlineTimer->cancel(); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 112 | } |
| 113 | m_queue.erase(info->queueIt); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 114 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 115 | if (!m_isEventExecuting) { |
| 116 | this->scheduleNext(); |
| 117 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 121 | Scheduler::cancelAllEvents() |
| 122 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 123 | m_queue.clear(); |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 124 | m_deadlineTimer->cancel(); |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 128 | Scheduler::scheduleNext() |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 129 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 130 | if (!m_queue.empty()) { |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 131 | m_deadlineTimer->expires_from_now((*m_queue.begin())->expiresFromNow()); |
| 132 | m_deadlineTimer->async_wait(bind(&Scheduler::executeEvent, this, _1)); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | Scheduler::executeEvent(const boost::system::error_code& error) |
| 138 | { |
| 139 | if (error) { // e.g., cancelled |
| 140 | return; |
| 141 | } |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 142 | |
| 143 | m_isEventExecuting = true; |
| 144 | |
Junxiao Shi | 7e693be | 2017-01-30 00:14:05 +0000 | [diff] [blame] | 145 | BOOST_SCOPE_EXIT(this_) { |
| 146 | this_->m_isEventExecuting = false; |
| 147 | this_->scheduleNext(); |
| 148 | } BOOST_SCOPE_EXIT_END |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 149 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 150 | // process all expired events |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 151 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 152 | while (!m_queue.empty()) { |
| 153 | EventQueue::iterator head = m_queue.begin(); |
| 154 | shared_ptr<EventInfo> info = *head; |
| 155 | if (info->expireTime > now) { |
| 156 | break; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 159 | m_queue.erase(head); |
| 160 | info->isExpired = true; |
| 161 | info->callback(); |
| 162 | } |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 165 | } // namespace scheduler |
| 166 | } // namespace util |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 167 | } // namespace ndn |