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 | /* |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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" |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 23 | #include "detail/steady-timer.hpp" |
Davide Pesavento | bdcedf4 | 2017-10-15 14:56:28 -0400 | [diff] [blame] | 24 | |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 25 | #include <boost/scope_exit.hpp> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 28 | namespace util { |
| 29 | namespace scheduler { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 30 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 31 | class EventInfo : noncopyable |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 32 | { |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 33 | public: |
| 34 | EventInfo(time::nanoseconds after, const EventCallback& callback) |
| 35 | : expireTime(time::steady_clock::now() + after) |
| 36 | , isExpired(false) |
| 37 | , callback(callback) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 41 | time::nanoseconds |
| 42 | expiresFromNow() const |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 43 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 44 | return std::max(expireTime - time::steady_clock::now(), 0_ns); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 47 | public: |
| 48 | time::steady_clock::TimePoint expireTime; |
| 49 | bool isExpired; |
| 50 | EventCallback callback; |
| 51 | EventQueue::const_iterator queueIt; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
Davide Pesavento | bdcedf4 | 2017-10-15 14:56:28 -0400 | [diff] [blame] | 54 | EventId::operator bool() const |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 55 | { |
Davide Pesavento | bdcedf4 | 2017-10-15 14:56:28 -0400 | [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) |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 79 | : m_timer(make_unique<detail::SteadyTimer>(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()) { |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 111 | m_timer->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(); |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 124 | m_timer->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()) { |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 131 | m_timer->expires_from_now((*m_queue.begin())->expiresFromNow()); |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 132 | m_timer->async_wait([this] (const auto& error) { this->executeEvent(error); }); |
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 |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 151 | auto now = time::steady_clock::now(); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 152 | while (!m_queue.empty()) { |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 153 | auto head = m_queue.begin(); |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 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 |