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