scheduler.hpp
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 
22 #ifndef NDN_UTIL_SCHEDULER_HPP
23 #define NDN_UTIL_SCHEDULER_HPP
24 
27 #include "ndn-cxx/util/time.hpp"
28 
29 #include <boost/system/error_code.hpp>
30 #include <set>
31 
32 namespace ndn {
33 namespace util {
34 
35 namespace detail {
36 class SteadyTimer;
37 } // namespace detail
38 
39 namespace scheduler {
40 
41 class Scheduler;
42 class EventInfo;
43 
46 using EventCallback = std::function<void()>;
47 
60 {
61 public:
64  EventId() noexcept = default;
65 
68  [[deprecated]]
69  EventId(std::nullptr_t) noexcept
70  {
71  }
72 
77  explicit
78  operator bool() const noexcept;
79 
83  bool
84  operator==(const EventId& other) const noexcept;
85 
86  bool
87  operator!=(const EventId& other) const noexcept
88  {
89  return !this->operator==(other);
90  }
91 
95  void
96  reset() noexcept;
97 
98 private:
99  EventId(Scheduler& sched, weak_ptr<EventInfo> info);
100 
101 private:
102  weak_ptr<EventInfo> m_info;
103 
104  friend class Scheduler;
105  friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
106 };
107 
108 std::ostream&
109 operator<<(std::ostream& os, const EventId& eventId);
110 
128 {
129 public:
130  using ScopedCancelHandle::ScopedCancelHandle;
131 
132  ScopedEventId() noexcept = default;
133 
136  [[deprecated]]
137  explicit
138  ScopedEventId(Scheduler& scheduler) noexcept
139  {
140  }
141 };
142 
144 {
145 public:
146  bool
147  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept;
148 };
149 
150 using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
151 
154 class Scheduler : noncopyable
155 {
156 public:
157  explicit
158  Scheduler(boost::asio::io_service& ioService);
159 
160  ~Scheduler();
161 
165  EventId
166  scheduleEvent(time::nanoseconds after, const EventCallback& callback);
167 
172  void
173  cancelEvent(const EventId& eid)
174  {
175  eid.cancel();
176  }
177 
180  void
181  cancelAllEvents();
182 
183 private:
184  void
185  cancelImpl(const shared_ptr<EventInfo>& info);
186 
189  void
190  scheduleNext();
191 
197  void
198  executeEvent(const boost::system::error_code& code);
199 
200 private:
201  unique_ptr<detail::SteadyTimer> m_timer;
202  EventQueue m_queue;
203  bool m_isEventExecuting;
204 
205  friend EventId;
206 };
207 
208 } // namespace scheduler
209 
211 
212 } // namespace util
213 
214 // for backwards compatibility
217 
218 } // namespace ndn
219 
220 #endif // NDN_UTIL_SCHEDULER_HPP
bool operator!=(const EventId &other) const noexcept
Definition: scheduler.hpp:87
Definition: data.cpp:26
std::ostream & operator<<(std::ostream &os, LoggerTimestamp)
Write a timestamp to os.
Definition: logger.cpp:126
EventId(std::nullptr_t) noexcept
Allow implicit conversion from nullptr.
Definition: scheduler.hpp:69
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:46
std::multiset< shared_ptr< EventInfo >, EventQueueCompare > EventQueue
Definition: scheduler.hpp:150
A scoped handle of scheduled event.
Definition: scheduler.hpp:127
Handle to cancel an operation.
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:313
void cancelEvent(const EventId &eid)
Cancel a scheduled event.
Definition: scheduler.hpp:173
void cancel() const
Cancel the operation.
A handle of scheduled event.
Definition: scheduler.hpp:59
ScopedEventId(Scheduler &scheduler) noexcept
Definition: scheduler.hpp:138
Cancels an operation automatically upon destruction.