Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_UTIL_SCHEDULER_HPP |
| 23 | #define NDN_UTIL_SCHEDULER_HPP |
| 24 | |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 25 | #include "time.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | #include <set> |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 27 | #include <boost/asio/io_service.hpp> |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 29 | namespace ndn { |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 30 | namespace util { |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 31 | |
| 32 | namespace detail { |
| 33 | class MonotonicDeadlineTimer; |
| 34 | } // namespace detail |
| 35 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 36 | namespace scheduler { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 38 | /** |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 39 | * \brief Function to be invoked when a scheduled event expires |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 40 | */ |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 41 | using EventCallback = std::function<void()>; |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * \brief Stores internal information about a scheduled event |
| 45 | */ |
| 46 | class EventInfo; |
| 47 | |
| 48 | /** |
| 49 | * \brief Identifies a scheduled event |
| 50 | */ |
| 51 | class EventId |
| 52 | { |
| 53 | public: |
| 54 | /** |
| 55 | * \brief Constructs an empty EventId |
| 56 | * \note EventId is implicitly convertible from nullptr. |
| 57 | */ |
| 58 | EventId(std::nullptr_t = nullptr) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * \retval true The event is valid. |
| 64 | * \retval false This EventId is empty, or the event is expired or cancelled. |
| 65 | */ |
| 66 | explicit |
| 67 | operator bool() const |
| 68 | { |
| 69 | return !this->operator!(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * \retval true This EventId is empty, or the event is expired or cancelled. |
| 74 | * \retval false The event is valid. |
| 75 | */ |
| 76 | bool |
| 77 | operator!() const; |
| 78 | |
| 79 | /** |
| 80 | * \return whether this and other refer to the same event, or are both empty/expired/cancelled |
| 81 | */ |
| 82 | bool |
| 83 | operator==(const EventId& other) const; |
| 84 | |
| 85 | bool |
| 86 | operator!=(const EventId& other) const |
| 87 | { |
| 88 | return !this->operator==(other); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * \brief clear this EventId |
| 93 | * \note This does not cancel the event. |
| 94 | * \post !(*this) |
| 95 | */ |
| 96 | void |
| 97 | reset() |
| 98 | { |
| 99 | m_info.reset(); |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | explicit |
| 104 | EventId(const weak_ptr<EventInfo>& info) |
| 105 | : m_info(info) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | weak_ptr<EventInfo> m_info; |
| 111 | |
| 112 | friend class Scheduler; |
| 113 | friend std::ostream& operator<<(std::ostream& os, const EventId& eventId); |
| 114 | }; |
| 115 | |
| 116 | std::ostream& |
| 117 | operator<<(std::ostream& os, const EventId& eventId); |
| 118 | |
| 119 | class EventQueueCompare |
| 120 | { |
| 121 | public: |
| 122 | bool |
| 123 | operator()(const shared_ptr<EventInfo>& a, const shared_ptr<EventInfo>& b) const; |
| 124 | }; |
| 125 | |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 126 | using EventQueue = std::multiset<shared_ptr<EventInfo>, EventQueueCompare>; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * \brief Generic scheduler |
| 130 | */ |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 131 | class Scheduler : noncopyable |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 132 | { |
| 133 | public: |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 134 | explicit |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 135 | Scheduler(boost::asio::io_service& ioService); |
| 136 | |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 137 | ~Scheduler(); |
| 138 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 139 | /** |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 140 | * \brief Schedule a one-time event after the specified delay |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 141 | * \return EventId that can be used to cancel the scheduled event |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 142 | */ |
| 143 | EventId |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 144 | scheduleEvent(time::nanoseconds after, const EventCallback& callback); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 145 | |
| 146 | /** |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 147 | * \brief Cancel a scheduled event |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 148 | */ |
| 149 | void |
| 150 | cancelEvent(const EventId& eventId); |
| 151 | |
Alexander Afanasyev | 7ae4bf5 | 2014-07-11 17:12:41 -0700 | [diff] [blame] | 152 | /** |
| 153 | * \brief Cancel all scheduled events |
| 154 | */ |
| 155 | void |
| 156 | cancelAllEvents(); |
| 157 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 158 | private: |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 159 | /** |
| 160 | * \brief Schedule the next event on the deadline timer |
| 161 | */ |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 162 | void |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 163 | scheduleNext(); |
| 164 | |
Junxiao Shi | 86dfa53 | 2016-08-10 03:00:11 +0000 | [diff] [blame] | 165 | /** |
| 166 | * \brief Execute expired events |
| 167 | * \note If an event callback throws, the exception is propagated to the thread running the |
| 168 | * io_service. In case there are other expired events, they will be processed in the next |
| 169 | * invocation of this method. |
| 170 | */ |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 171 | void |
| 172 | executeEvent(const boost::system::error_code& code); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 173 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 174 | private: |
Junxiao Shi | 5bcab56 | 2017-07-16 17:19:14 +0000 | [diff] [blame] | 175 | unique_ptr<detail::MonotonicDeadlineTimer> m_deadlineTimer; |
Junxiao Shi | d50f2b4 | 2016-08-10 02:59:59 +0000 | [diff] [blame] | 176 | EventQueue m_queue; |
Yingdi Yu | f2a8209 | 2014-02-03 16:49:15 -0800 | [diff] [blame] | 177 | bool m_isEventExecuting; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
Alexander Afanasyev | 9a9952f | 2015-01-28 19:06:48 -0800 | [diff] [blame] | 180 | } // namespace scheduler |
| 181 | |
| 182 | using util::scheduler::Scheduler; |
| 183 | |
| 184 | } // namespace util |
| 185 | |
| 186 | // for backwards compatibility |
| 187 | using util::scheduler::Scheduler; |
| 188 | using util::scheduler::EventId; |
| 189 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 190 | } // namespace ndn |
| 191 | |
| 192 | #endif // NDN_UTIL_SCHEDULER_HPP |