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 
34 namespace util {
35 namespace detail {
36 class SteadyTimer;
37 } // namespace detail
38 } // namespace util
39 
40 namespace scheduler {
41 
42 class Scheduler;
43 class EventInfo;
44 
47 using EventCallback = std::function<void()>;
48 
61 {
62 public:
65  EventId() noexcept = default;
66 
71  explicit
72  operator bool() const noexcept;
73 
77  void
78  reset() noexcept;
79 
80 private:
81  // NOTE: the following "hidden friend" operators are available via
82  // argument-dependent lookup only and must be defined inline.
83 
87  friend bool
88  operator==(const EventId& lhs, const EventId& rhs) noexcept
89  {
90  return (!lhs && !rhs) ||
91  (!lhs.m_info.owner_before(rhs.m_info) &&
92  !rhs.m_info.owner_before(lhs.m_info));
93  }
94 
95  friend bool
96  operator!=(const EventId& lhs, const EventId& rhs) noexcept
97  {
98  return !(lhs == rhs);
99  }
100 
101 private:
102  EventId(Scheduler& sched, weak_ptr<EventInfo> info);
103 
104 private:
105  weak_ptr<EventInfo> m_info;
106 
107  friend class Scheduler;
108  friend std::ostream& operator<<(std::ostream& os, const EventId& eventId);
109 };
110 
111 std::ostream&
112 operator<<(std::ostream& os, const EventId& eventId);
113 
131 
134 class Scheduler : noncopyable
135 {
136 public:
137  explicit
138  Scheduler(boost::asio::io_service& ioService);
139 
140  ~Scheduler();
141 
145  EventId
146  schedule(time::nanoseconds after, EventCallback callback);
147 
150  void
151  cancelAllEvents();
152 
153 private:
154  void
155  cancelImpl(const shared_ptr<EventInfo>& info);
156 
159  void
160  scheduleNext();
161 
167  void
168  executeEvent(const boost::system::error_code& code);
169 
170 private:
171  class EventQueueCompare
172  {
173  public:
174  bool
175  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept;
176  };
177 
178  using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
179  EventQueue m_queue;
180 
181  unique_ptr<util::detail::SteadyTimer> m_timer;
182  bool m_isEventExecuting = false;
183 
184  friend EventId;
185  friend EventInfo;
186 };
187 
188 } // namespace scheduler
189 
191 
192 } // namespace ndn
193 
194 #endif // NDN_UTIL_SCHEDULER_HPP
Definition: data.cpp:26
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:47
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:31
friend bool operator==(const EventId &lhs, const EventId &rhs) noexcept
Determine whether this and other refer to the same event, or are both empty/expired/cancelled.
Definition: scheduler.hpp:88
A handle for a scheduled event.
Definition: scheduler.hpp:60
Generic time-based scheduler.
Definition: scheduler.hpp:134
Handle to cancel an operation.
friend bool operator!=(const EventId &lhs, const EventId &rhs) noexcept
Definition: scheduler.hpp:96