scheduler.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
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.
20  */
21 
23 #include "ndn-cxx/util/impl/steady-timer.hpp"
24 
25 #include <boost/scope_exit.hpp>
26 
27 namespace ndn {
28 namespace scheduler {
29 
32 class EventInfo : noncopyable
33 {
34 public:
35  EventInfo(time::nanoseconds after, EventCallback&& cb)
36  : callback(std::move(cb))
37  , expireTime(time::steady_clock::now() + after)
38  {
39  }
40 
41  NDN_CXX_NODISCARD time::nanoseconds
42  expiresFromNow() const
43  {
44  return std::max(expireTime - time::steady_clock::now(), 0_ns);
45  }
46 
47 public:
48  EventCallback callback;
49  Scheduler::EventQueue::const_iterator queueIt;
51  bool isExpired = false;
52 };
53 
54 EventId::EventId(Scheduler& sched, weak_ptr<EventInfo> info)
55  : CancelHandle([&sched, info] { sched.cancelImpl(info.lock()); })
56  , m_info(std::move(info))
57 {
58 }
59 
60 EventId::operator bool() const noexcept
61 {
62  auto sp = m_info.lock();
63  return sp != nullptr && !sp->isExpired;
64 }
65 
66 void
67 EventId::reset() noexcept
68 {
69  *this = {};
70 }
71 
72 std::ostream&
73 operator<<(std::ostream& os, const EventId& eventId)
74 {
75  return os << eventId.m_info.lock();
76 }
77 
78 bool
79 Scheduler::EventQueueCompare::operator()(const shared_ptr<EventInfo>& a,
80  const shared_ptr<EventInfo>& b) const noexcept
81 {
82  return a->expireTime < b->expireTime;
83 }
84 
85 Scheduler::Scheduler(boost::asio::io_service& ioService)
86  : m_timer(make_unique<util::detail::SteadyTimer>(ioService))
87 {
88 }
89 
90 Scheduler::~Scheduler() = default;
91 
92 EventId
93 Scheduler::schedule(time::nanoseconds after, EventCallback callback)
94 {
95  BOOST_ASSERT(callback != nullptr);
96 
97  auto i = m_queue.insert(make_shared<EventInfo>(after, std::move(callback)));
98  (*i)->queueIt = i;
99 
100  if (!m_isEventExecuting && i == m_queue.begin()) {
101  // the new event is the first one to expire
102  this->scheduleNext();
103  }
104 
105  return EventId(*this, *i);
106 }
107 
108 void
109 Scheduler::cancelImpl(const shared_ptr<EventInfo>& info)
110 {
111  if (info == nullptr || info->isExpired) {
112  return;
113  }
114 
115  if (info->queueIt == m_queue.begin()) {
116  m_timer->cancel();
117  }
118  m_queue.erase(info->queueIt);
119 
120  if (!m_isEventExecuting) {
121  this->scheduleNext();
122  }
123 }
124 
125 void
127 {
128  m_queue.clear();
129  m_timer->cancel();
130 }
131 
132 void
133 Scheduler::scheduleNext()
134 {
135  if (!m_queue.empty()) {
136  m_timer->expires_from_now((*m_queue.begin())->expiresFromNow());
137  m_timer->async_wait([this] (const auto& error) { this->executeEvent(error); });
138  }
139 }
140 
141 void
142 Scheduler::executeEvent(const boost::system::error_code& error)
143 {
144  if (error) { // e.g., cancelled
145  return;
146  }
147 
148  m_isEventExecuting = true;
149 
150  BOOST_SCOPE_EXIT(this_) {
151  this_->m_isEventExecuting = false;
152  this_->scheduleNext();
153  } BOOST_SCOPE_EXIT_END
154 
155  // process all expired events
156  auto now = time::steady_clock::now();
157  while (!m_queue.empty()) {
158  auto head = m_queue.begin();
159  shared_ptr<EventInfo> info = *head;
160  if (info->expireTime > now) {
161  break;
162  }
163 
164  m_queue.erase(head);
165  info->isExpired = true;
166  info->callback();
167  }
168 }
169 
170 } // namespace scheduler
171 } // namespace ndn
time_point TimePoint
Definition: time.hpp:225
Definition: data.cpp:26
void reset() noexcept
Clear this EventId without canceling.
Definition: scheduler.cpp:67
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:47
static time_point now() noexcept
Definition: time.cpp:80
EventId schedule(time::nanoseconds after, EventCallback callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:93
A handle for a scheduled event.
Definition: scheduler.hpp:60
EventId() noexcept=default
Constructs an empty EventId.
std::ostream & operator<<(std::ostream &os, const EventId &eventId)
Definition: scheduler.cpp:73
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Generic time-based scheduler.
Definition: scheduler.hpp:134
Scheduler(boost::asio::io_service &ioService)
Definition: scheduler.cpp:85
void cancelAllEvents()
Cancel all scheduled events.
Definition: scheduler.cpp:126