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
139  {
140  }
141 };
142 
145 class Scheduler : noncopyable
146 {
147 public:
148  explicit
149  Scheduler(boost::asio::io_service& ioService);
150 
151  ~Scheduler();
152 
156  EventId
157  schedule(time::nanoseconds after, EventCallback callback);
158 
161  [[deprecated("use schedule(after, callback)")]]
162  EventId
163  scheduleEvent(time::nanoseconds after, EventCallback callback)
164  {
165  return schedule(after, std::move(callback));
166  }
167 
170  [[deprecated("use EventId::cancel()")]]
171  void
172  cancelEvent(const EventId& eid)
173  {
174  eid.cancel();
175  }
176 
179  void
180  cancelAllEvents();
181 
182 private:
183  void
184  cancelImpl(const shared_ptr<EventInfo>& info);
185 
188  void
189  scheduleNext();
190 
196  void
197  executeEvent(const boost::system::error_code& code);
198 
199 private:
200  class EventQueueCompare
201  {
202  public:
203  bool
204  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept;
205  };
206 
207  using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
208  EventQueue m_queue;
209 
210  unique_ptr<util::detail::SteadyTimer> m_timer;
211  bool m_isEventExecuting = false;
212 
213  friend EventId;
214  friend EventInfo;
215 };
216 
217 } // namespace scheduler
218 
219 // for backwards compatibility
220 using Scheduler [[deprecated]] = scheduler::Scheduler;
221 
222 } // namespace util
223 
224 namespace scheduler = util::scheduler;
226 
227 // for backwards compatibility
229 
230 } // namespace ndn
231 
232 #endif // NDN_UTIL_SCHEDULER_HPP
bool operator!=(const EventId &other) const noexcept
Definition: scheduler.hpp:87
Definition: data.cpp:26
Generic time-based scheduler.
Definition: scheduler.hpp:145
std::ostream & operator<<(std::ostream &os, LoggerTimestamp)
Write a timestamp to os.
Definition: logger.cpp:126
EventId scheduleEvent(time::nanoseconds after, EventCallback callback)
Definition: scheduler.hpp:163
ScopedEventId(Scheduler &) noexcept
Definition: scheduler.hpp:138
scheduler::Scheduler deprecated
Definition: scheduler.hpp:220
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
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:312
void cancelEvent(const EventId &eid)
Definition: scheduler.hpp:172
void cancel() const
Cancel the operation.
A handle of scheduled event.
Definition: scheduler.hpp:59
Cancels an operation automatically upon destruction.