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