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