blob: 167b6e00fb5135fe0bf4b85bddf974bdc23ea726 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5bcab562017-07-16 17:19:14 +00002/*
Davide Pesavento5afbb0b2018-01-01 17:24:18 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevf6468892014-01-29 01:04:14 -080020 */
21
22#include "scheduler.hpp"
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050023#include "detail/steady-timer.hpp"
Davide Pesaventobdcedf42017-10-15 14:56:28 -040024
Junxiao Shi86dfa532016-08-10 03:00:11 +000025#include <boost/scope_exit.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080026
27namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080028namespace util {
29namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080030
Junxiao Shid50f2b42016-08-10 02:59:59 +000031class EventInfo : noncopyable
Alexander Afanasyevf6468892014-01-29 01:04:14 -080032{
Junxiao Shid50f2b42016-08-10 02:59:59 +000033public:
34 EventInfo(time::nanoseconds after, const EventCallback& callback)
35 : expireTime(time::steady_clock::now() + after)
36 , isExpired(false)
37 , callback(callback)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080038 {
39 }
40
Junxiao Shid50f2b42016-08-10 02:59:59 +000041 time::nanoseconds
42 expiresFromNow() const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080043 {
Junxiao Shid50f2b42016-08-10 02:59:59 +000044 return std::max(expireTime - time::steady_clock::now(), time::nanoseconds::zero());
Alexander Afanasyevf6468892014-01-29 01:04:14 -080045 }
46
Junxiao Shid50f2b42016-08-10 02:59:59 +000047public:
48 time::steady_clock::TimePoint expireTime;
49 bool isExpired;
50 EventCallback callback;
51 EventQueue::const_iterator queueIt;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080052};
53
Davide Pesaventobdcedf42017-10-15 14:56:28 -040054EventId::operator bool() const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080055{
Davide Pesaventobdcedf42017-10-15 14:56:28 -040056 return !m_info.expired() && !m_info.lock()->isExpired;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080057}
58
Junxiao Shid50f2b42016-08-10 02:59:59 +000059bool
60EventId::operator==(const EventId& other) const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080061{
Junxiao Shid50f2b42016-08-10 02:59:59 +000062 return (!(*this) && !other) ||
63 !(m_info.owner_before(other.m_info) || other.m_info.owner_before(m_info));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080064}
65
Junxiao Shid50f2b42016-08-10 02:59:59 +000066std::ostream&
67operator<<(std::ostream& os, const EventId& eventId)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068{
Junxiao Shid50f2b42016-08-10 02:59:59 +000069 return os << eventId.m_info.lock();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080070}
71
Junxiao Shid50f2b42016-08-10 02:59:59 +000072bool
73EventQueueCompare::operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const
74{
75 return a->expireTime < b->expireTime;
76}
Alexander Afanasyevf6468892014-01-29 01:04:14 -080077
78Scheduler::Scheduler(boost::asio::io_service& ioService)
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050079 : m_timer(make_unique<detail::SteadyTimer>(ioService))
Yingdi Yuf2a82092014-02-03 16:49:15 -080080 , m_isEventExecuting(false)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080081{
82}
83
Junxiao Shi5bcab562017-07-16 17:19:14 +000084Scheduler::~Scheduler() = default;
85
Alexander Afanasyevf6468892014-01-29 01:04:14 -080086EventId
Junxiao Shi5bcab562017-07-16 17:19:14 +000087Scheduler::scheduleEvent(time::nanoseconds after, const EventCallback& callback)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080088{
Junxiao Shid50f2b42016-08-10 02:59:59 +000089 BOOST_ASSERT(callback != nullptr);
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070090
Junxiao Shid50f2b42016-08-10 02:59:59 +000091 EventQueue::iterator i = m_queue.insert(make_shared<EventInfo>(after, callback));
92 (*i)->queueIt = i;
Yingdi Yuf2a82092014-02-03 16:49:15 -080093
Junxiao Shid50f2b42016-08-10 02:59:59 +000094 if (!m_isEventExecuting && i == m_queue.begin()) {
95 // the new event is the first one to expire
96 this->scheduleNext();
97 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -080098
Junxiao Shid50f2b42016-08-10 02:59:59 +000099 return EventId(*i);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800100}
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700101
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800102void
103Scheduler::cancelEvent(const EventId& eventId)
104{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000105 shared_ptr<EventInfo> info = eventId.m_info.lock();
106 if (info == nullptr || info->isExpired) {
107 return; // event already expired or cancelled
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800108 }
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700109
Junxiao Shid50f2b42016-08-10 02:59:59 +0000110 if (info->queueIt == m_queue.begin()) {
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500111 m_timer->cancel();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000112 }
113 m_queue.erase(info->queueIt);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800114
Junxiao Shid50f2b42016-08-10 02:59:59 +0000115 if (!m_isEventExecuting) {
116 this->scheduleNext();
117 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800118}
119
120void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700121Scheduler::cancelAllEvents()
122{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000123 m_queue.clear();
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500124 m_timer->cancel();
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700125}
126
127void
Junxiao Shid50f2b42016-08-10 02:59:59 +0000128Scheduler::scheduleNext()
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800129{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000130 if (!m_queue.empty()) {
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500131 m_timer->expires_from_now((*m_queue.begin())->expiresFromNow());
132 m_timer->async_wait(bind(&Scheduler::executeEvent, this, _1));
Junxiao Shid50f2b42016-08-10 02:59:59 +0000133 }
134}
135
136void
137Scheduler::executeEvent(const boost::system::error_code& error)
138{
139 if (error) { // e.g., cancelled
140 return;
141 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800142
143 m_isEventExecuting = true;
144
Junxiao Shi7e693be2017-01-30 00:14:05 +0000145 BOOST_SCOPE_EXIT(this_) {
146 this_->m_isEventExecuting = false;
147 this_->scheduleNext();
148 } BOOST_SCOPE_EXIT_END
Junxiao Shi86dfa532016-08-10 03:00:11 +0000149
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800150 // process all expired events
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500151 auto now = time::steady_clock::now();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000152 while (!m_queue.empty()) {
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500153 auto head = m_queue.begin();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000154 shared_ptr<EventInfo> info = *head;
155 if (info->expireTime > now) {
156 break;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800157 }
158
Junxiao Shid50f2b42016-08-10 02:59:59 +0000159 m_queue.erase(head);
160 info->isExpired = true;
161 info->callback();
162 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800163}
164
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800165} // namespace scheduler
166} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800167} // namespace ndn