blob: 8e72e78bee3f1abfb6d30fdc4ba13f5a9f42ba3b [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/*
Junxiao Shi7e693be2017-01-30 00:14:05 +00003 * Copyright (c) 2013-2017 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"
Junxiao Shi5bcab562017-07-16 17:19:14 +000023#include "detail/monotonic-deadline-timer.hpp"
Junxiao Shi86dfa532016-08-10 03:00:11 +000024#include <boost/scope_exit.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080025
26namespace ndn {
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -080027namespace util {
28namespace scheduler {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080029
Junxiao Shid50f2b42016-08-10 02:59:59 +000030class EventInfo : noncopyable
Alexander Afanasyevf6468892014-01-29 01:04:14 -080031{
Junxiao Shid50f2b42016-08-10 02:59:59 +000032public:
33 EventInfo(time::nanoseconds after, const EventCallback& callback)
34 : expireTime(time::steady_clock::now() + after)
35 , isExpired(false)
36 , callback(callback)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080037 {
38 }
39
Junxiao Shid50f2b42016-08-10 02:59:59 +000040 time::nanoseconds
41 expiresFromNow() const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080042 {
Junxiao Shid50f2b42016-08-10 02:59:59 +000043 return std::max(expireTime - time::steady_clock::now(), time::nanoseconds::zero());
Alexander Afanasyevf6468892014-01-29 01:04:14 -080044 }
45
Junxiao Shid50f2b42016-08-10 02:59:59 +000046public:
47 time::steady_clock::TimePoint expireTime;
48 bool isExpired;
49 EventCallback callback;
50 EventQueue::const_iterator queueIt;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080051};
52
Junxiao Shid50f2b42016-08-10 02:59:59 +000053bool
54EventId::operator!() const
Alexander Afanasyevf6468892014-01-29 01:04:14 -080055{
Junxiao Shid50f2b42016-08-10 02:59:59 +000056 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)
Junxiao Shi5bcab562017-07-16 17:19:14 +000079 : m_deadlineTimer(make_unique<detail::MonotonicDeadlineTimer>(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()) {
Junxiao Shi5bcab562017-07-16 17:19:14 +0000111 m_deadlineTimer->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();
Junxiao Shi5bcab562017-07-16 17:19:14 +0000124 m_deadlineTimer->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()) {
Junxiao Shi5bcab562017-07-16 17:19:14 +0000131 m_deadlineTimer->expires_from_now((*m_queue.begin())->expiresFromNow());
132 m_deadlineTimer->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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700151 time::steady_clock::TimePoint now = time::steady_clock::now();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000152 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 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