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