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