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-2024 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_CXX_UTIL_SCHEDULER_HPP
23 #define NDN_CXX_UTIL_SCHEDULER_HPP
24 
28 #include "ndn-cxx/util/time.hpp"
29 
30 #include <boost/operators.hpp>
31 #include <boost/system/error_code.hpp>
32 
33 #include <set>
34 
35 namespace ndn {
36 
37 namespace detail {
38 class SteadyTimer;
39 } // namespace detail
40 
41 namespace scheduler {
42 
43 class Scheduler;
44 struct EventInfo;
45 
48 using EventCallback = std::function<void()>;
49 
61 class EventId : public detail::CancelHandle, private boost::equality_comparable<EventId>
62 {
63 public:
67  EventId() noexcept = default;
68 
74  explicit
75  operator bool() const noexcept;
76 
81  void
82  reset() noexcept;
83 
84 private:
85  EventId(Scheduler& sched, weak_ptr<EventInfo> info);
86 
87 private: // non-member operators
88  // NOTE: the following "hidden friend" operators are available via
89  // argument-dependent lookup only and must be defined inline.
90  // boost::equality_comparable provides != operator.
91 
96  friend bool
97  operator==(const EventId& lhs, const EventId& rhs) noexcept
98  {
99  return (!lhs && !rhs) ||
100  (!lhs.m_info.owner_before(rhs.m_info) &&
101  !rhs.m_info.owner_before(lhs.m_info));
102  }
103 
104  friend std::ostream&
105  operator<<(std::ostream& os, const EventId& eventId)
106  {
107  return os << eventId.m_info.lock();
108  }
109 
110 private:
111  weak_ptr<EventInfo> m_info;
112 
113  friend Scheduler;
114 };
115 
133 
137 class Scheduler : noncopyable
138 {
139 public:
140  explicit
141  Scheduler(boost::asio::io_context& ioCtx);
142 
144 
149  EventId
150  schedule(time::nanoseconds after, EventCallback callback);
151 
155  void
156  cancelAllEvents();
157 
158 private:
159  void
160  cancelImpl(const shared_ptr<EventInfo>& info);
161 
164  void
165  scheduleNext();
166 
172  void
173  executeEvent(const boost::system::error_code& code);
174 
175 private:
176  class EventQueueCompare
177  {
178  public:
179  bool
180  operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const noexcept;
181  };
182 
183  using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>;
184  EventQueue m_queue;
185 
186  unique_ptr<detail::SteadyTimer> m_timer;
187  bool m_isEventExecuting = false;
188 
189  friend EventId;
190  friend EventInfo;
191 };
192 
193 } // namespace scheduler
194 
195 using scheduler::Scheduler;
196 
197 } // namespace ndn
198 
199 #endif // NDN_CXX_UTIL_SCHEDULER_HPP
Handle to cancel an operation.
A handle for a scheduled event.
Definition: scheduler.hpp:62
friend std::ostream & operator<<(std::ostream &os, const EventId &eventId)
Definition: scheduler.hpp:105
void reset() noexcept
Clear this EventId without canceling the associated event.
Definition: scheduler.cpp:58
EventId() noexcept=default
Constructs an empty EventId.
Generic time-based event scheduler.
Definition: scheduler.hpp:138
void cancelAllEvents()
Cancel all scheduled events.
Definition: scheduler.cpp:111
Scheduler(boost::asio::io_context &ioCtx)
Definition: scheduler.cpp:70
EventId schedule(time::nanoseconds after, EventCallback callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:78
Common includes and macros used throughout the library.
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
Definition: scheduler.hpp:48
::boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:54
Definition: data.cpp:25
SignatureInfo info