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