blob: 9e52b2531dab584fac948692436baf6e185258fc [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 {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040044 return std::max(expireTime - time::steady_clock::now(), 0_ns);
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 Pesavento3a3e1882018-07-17 14:49:15 -040054EventId::operator bool() const noexcept
Alexander Afanasyevf6468892014-01-29 01:04:14 -080055{
Davide Pesavento3a3e1882018-07-17 14:49:15 -040056 auto sp = m_info.lock();
57 return sp != nullptr && !sp->isExpired;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080058}
59
Junxiao Shid50f2b42016-08-10 02:59:59 +000060bool
Davide Pesavento3a3e1882018-07-17 14:49:15 -040061EventId::operator==(const EventId& other) const noexcept
Alexander Afanasyevf6468892014-01-29 01:04:14 -080062{
Davide Pesavento3a3e1882018-07-17 14:49:15 -040063 return (!*this && !other) ||
Junxiao Shid50f2b42016-08-10 02:59:59 +000064 !(m_info.owner_before(other.m_info) || other.m_info.owner_before(m_info));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080065}
66
Junxiao Shid50f2b42016-08-10 02:59:59 +000067std::ostream&
68operator<<(std::ostream& os, const EventId& eventId)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080069{
Junxiao Shid50f2b42016-08-10 02:59:59 +000070 return os << eventId.m_info.lock();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080071}
72
Junxiao Shid50f2b42016-08-10 02:59:59 +000073bool
Davide Pesavento3a3e1882018-07-17 14:49:15 -040074EventQueueCompare::operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept
Junxiao Shid50f2b42016-08-10 02:59:59 +000075{
76 return a->expireTime < b->expireTime;
77}
Alexander Afanasyevf6468892014-01-29 01:04:14 -080078
79Scheduler::Scheduler(boost::asio::io_service& ioService)
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050080 : m_timer(make_unique<detail::SteadyTimer>(ioService))
Yingdi Yuf2a82092014-02-03 16:49:15 -080081 , m_isEventExecuting(false)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080082{
83}
84
Junxiao Shi5bcab562017-07-16 17:19:14 +000085Scheduler::~Scheduler() = default;
86
Alexander Afanasyevf6468892014-01-29 01:04:14 -080087EventId
Junxiao Shi5bcab562017-07-16 17:19:14 +000088Scheduler::scheduleEvent(time::nanoseconds after, const EventCallback& callback)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080089{
Junxiao Shid50f2b42016-08-10 02:59:59 +000090 BOOST_ASSERT(callback != nullptr);
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070091
Junxiao Shid50f2b42016-08-10 02:59:59 +000092 EventQueue::iterator i = m_queue.insert(make_shared<EventInfo>(after, callback));
93 (*i)->queueIt = i;
Yingdi Yuf2a82092014-02-03 16:49:15 -080094
Junxiao Shid50f2b42016-08-10 02:59:59 +000095 if (!m_isEventExecuting && i == m_queue.begin()) {
96 // the new event is the first one to expire
97 this->scheduleNext();
98 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -080099
Junxiao Shid50f2b42016-08-10 02:59:59 +0000100 return EventId(*i);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800101}
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700102
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800103void
104Scheduler::cancelEvent(const EventId& eventId)
105{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000106 shared_ptr<EventInfo> info = eventId.m_info.lock();
107 if (info == nullptr || info->isExpired) {
108 return; // event already expired or cancelled
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800109 }
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700110
Junxiao Shid50f2b42016-08-10 02:59:59 +0000111 if (info->queueIt == m_queue.begin()) {
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500112 m_timer->cancel();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000113 }
114 m_queue.erase(info->queueIt);
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800115
Junxiao Shid50f2b42016-08-10 02:59:59 +0000116 if (!m_isEventExecuting) {
117 this->scheduleNext();
118 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800119}
120
121void
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700122Scheduler::cancelAllEvents()
123{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000124 m_queue.clear();
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500125 m_timer->cancel();
Alexander Afanasyev7ae4bf52014-07-11 17:12:41 -0700126}
127
128void
Junxiao Shid50f2b42016-08-10 02:59:59 +0000129Scheduler::scheduleNext()
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800130{
Junxiao Shid50f2b42016-08-10 02:59:59 +0000131 if (!m_queue.empty()) {
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500132 m_timer->expires_from_now((*m_queue.begin())->expiresFromNow());
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400133 m_timer->async_wait([this] (const auto& error) { this->executeEvent(error); });
Junxiao Shid50f2b42016-08-10 02:59:59 +0000134 }
135}
136
137void
138Scheduler::executeEvent(const boost::system::error_code& error)
139{
140 if (error) { // e.g., cancelled
141 return;
142 }
Yingdi Yuf2a82092014-02-03 16:49:15 -0800143
144 m_isEventExecuting = true;
145
Junxiao Shi7e693be2017-01-30 00:14:05 +0000146 BOOST_SCOPE_EXIT(this_) {
147 this_->m_isEventExecuting = false;
148 this_->scheduleNext();
149 } BOOST_SCOPE_EXIT_END
Junxiao Shi86dfa532016-08-10 03:00:11 +0000150
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800151 // process all expired events
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500152 auto now = time::steady_clock::now();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000153 while (!m_queue.empty()) {
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500154 auto head = m_queue.begin();
Junxiao Shid50f2b42016-08-10 02:59:59 +0000155 shared_ptr<EventInfo> info = *head;
156 if (info->expireTime > now) {
157 break;
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800158 }
159
Junxiao Shid50f2b42016-08-10 02:59:59 +0000160 m_queue.erase(head);
161 info->isExpired = true;
162 info->callback();
163 }
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800164}
165
Alexander Afanasyev9a9952f2015-01-28 19:06:48 -0800166} // namespace scheduler
167} // namespace util
Alexander Afanasyevf6468892014-01-29 01:04:14 -0800168} // namespace ndn