]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/deadline_timer_service.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / asio / detail / deadline_timer_service.hpp
1 //
2 // detail/deadline_timer_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <cstddef>
20 #include <boost/asio/error.hpp>
21 #include <boost/asio/execution_context.hpp>
22 #include <boost/asio/detail/bind_handler.hpp>
23 #include <boost/asio/detail/fenced_block.hpp>
24 #include <boost/asio/detail/memory.hpp>
25 #include <boost/asio/detail/noncopyable.hpp>
26 #include <boost/asio/detail/socket_ops.hpp>
27 #include <boost/asio/detail/socket_types.hpp>
28 #include <boost/asio/detail/timer_queue.hpp>
29 #include <boost/asio/detail/timer_queue_ptime.hpp>
30 #include <boost/asio/detail/timer_scheduler.hpp>
31 #include <boost/asio/detail/wait_handler.hpp>
32 #include <boost/asio/detail/wait_op.hpp>
33
34 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
35 # include <chrono>
36 # include <thread>
37 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
38
39 #include <boost/asio/detail/push_options.hpp>
40
41 namespace boost {
42 namespace asio {
43 namespace detail {
44
45 template <typename Time_Traits>
46 class deadline_timer_service
47 : public execution_context_service_base<deadline_timer_service<Time_Traits> >
48 {
49 public:
50 // The time type.
51 typedef typename Time_Traits::time_type time_type;
52
53 // The duration type.
54 typedef typename Time_Traits::duration_type duration_type;
55
56 // The implementation type of the timer. This type is dependent on the
57 // underlying implementation of the timer service.
58 struct implementation_type
59 : private boost::asio::detail::noncopyable
60 {
61 time_type expiry;
62 bool might_have_pending_waits;
63 typename timer_queue<Time_Traits>::per_timer_data timer_data;
64 };
65
66 // Constructor.
67 deadline_timer_service(execution_context& context)
68 : execution_context_service_base<
69 deadline_timer_service<Time_Traits> >(context),
70 scheduler_(boost::asio::use_service<timer_scheduler>(context))
71 {
72 scheduler_.init_task();
73 scheduler_.add_timer_queue(timer_queue_);
74 }
75
76 // Destructor.
77 ~deadline_timer_service()
78 {
79 scheduler_.remove_timer_queue(timer_queue_);
80 }
81
82 // Destroy all user-defined handler objects owned by the service.
83 void shutdown()
84 {
85 }
86
87 // Construct a new timer implementation.
88 void construct(implementation_type& impl)
89 {
90 impl.expiry = time_type();
91 impl.might_have_pending_waits = false;
92 }
93
94 // Destroy a timer implementation.
95 void destroy(implementation_type& impl)
96 {
97 boost::system::error_code ec;
98 cancel(impl, ec);
99 }
100
101 // Move-construct a new serial port implementation.
102 void move_construct(implementation_type& impl,
103 implementation_type& other_impl)
104 {
105 scheduler_.move_timer(timer_queue_, impl.timer_data, other_impl.timer_data);
106
107 impl.expiry = other_impl.expiry;
108 other_impl.expiry = time_type();
109
110 impl.might_have_pending_waits = other_impl.might_have_pending_waits;
111 other_impl.might_have_pending_waits = false;
112 }
113
114 // Move-assign from another serial port implementation.
115 void move_assign(implementation_type& impl,
116 deadline_timer_service& other_service,
117 implementation_type& other_impl)
118 {
119 if (this != &other_service)
120 if (impl.might_have_pending_waits)
121 scheduler_.cancel_timer(timer_queue_, impl.timer_data);
122
123 other_service.scheduler_.move_timer(other_service.timer_queue_,
124 impl.timer_data, other_impl.timer_data);
125
126 impl.expiry = other_impl.expiry;
127 other_impl.expiry = time_type();
128
129 impl.might_have_pending_waits = other_impl.might_have_pending_waits;
130 other_impl.might_have_pending_waits = false;
131 }
132
133 // Cancel any asynchronous wait operations associated with the timer.
134 std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
135 {
136 if (!impl.might_have_pending_waits)
137 {
138 ec = boost::system::error_code();
139 return 0;
140 }
141
142 BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
143 "deadline_timer", &impl, 0, "cancel"));
144
145 std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
146 impl.might_have_pending_waits = false;
147 ec = boost::system::error_code();
148 return count;
149 }
150
151 // Cancels one asynchronous wait operation associated with the timer.
152 std::size_t cancel_one(implementation_type& impl,
153 boost::system::error_code& ec)
154 {
155 if (!impl.might_have_pending_waits)
156 {
157 ec = boost::system::error_code();
158 return 0;
159 }
160
161 BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
162 "deadline_timer", &impl, 0, "cancel_one"));
163
164 std::size_t count = scheduler_.cancel_timer(
165 timer_queue_, impl.timer_data, 1);
166 if (count == 0)
167 impl.might_have_pending_waits = false;
168 ec = boost::system::error_code();
169 return count;
170 }
171
172 // Get the expiry time for the timer as an absolute time.
173 time_type expiry(const implementation_type& impl) const
174 {
175 return impl.expiry;
176 }
177
178 // Get the expiry time for the timer as an absolute time.
179 time_type expires_at(const implementation_type& impl) const
180 {
181 return impl.expiry;
182 }
183
184 // Get the expiry time for the timer relative to now.
185 duration_type expires_from_now(const implementation_type& impl) const
186 {
187 return Time_Traits::subtract(this->expiry(impl), Time_Traits::now());
188 }
189
190 // Set the expiry time for the timer as an absolute time.
191 std::size_t expires_at(implementation_type& impl,
192 const time_type& expiry_time, boost::system::error_code& ec)
193 {
194 std::size_t count = cancel(impl, ec);
195 impl.expiry = expiry_time;
196 ec = boost::system::error_code();
197 return count;
198 }
199
200 // Set the expiry time for the timer relative to now.
201 std::size_t expires_after(implementation_type& impl,
202 const duration_type& expiry_time, boost::system::error_code& ec)
203 {
204 return expires_at(impl,
205 Time_Traits::add(Time_Traits::now(), expiry_time), ec);
206 }
207
208 // Set the expiry time for the timer relative to now.
209 std::size_t expires_from_now(implementation_type& impl,
210 const duration_type& expiry_time, boost::system::error_code& ec)
211 {
212 return expires_at(impl,
213 Time_Traits::add(Time_Traits::now(), expiry_time), ec);
214 }
215
216 // Perform a blocking wait on the timer.
217 void wait(implementation_type& impl, boost::system::error_code& ec)
218 {
219 time_type now = Time_Traits::now();
220 ec = boost::system::error_code();
221 while (Time_Traits::less_than(now, impl.expiry) && !ec)
222 {
223 this->do_wait(Time_Traits::to_posix_duration(
224 Time_Traits::subtract(impl.expiry, now)), ec);
225 now = Time_Traits::now();
226 }
227 }
228
229 // Start an asynchronous wait on the timer.
230 template <typename Handler, typename IoExecutor>
231 void async_wait(implementation_type& impl,
232 Handler& handler, const IoExecutor& io_ex)
233 {
234 // Allocate and construct an operation to wrap the handler.
235 typedef wait_handler<Handler, IoExecutor> op;
236 typename op::ptr p = { boost::asio::detail::addressof(handler),
237 op::ptr::allocate(handler), 0 };
238 p.p = new (p.v) op(handler, io_ex);
239
240 impl.might_have_pending_waits = true;
241
242 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
243 *p.p, "deadline_timer", &impl, 0, "async_wait"));
244
245 scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
246 p.v = p.p = 0;
247 }
248
249 private:
250 // Helper function to wait given a duration type. The duration type should
251 // either be of type boost::posix_time::time_duration, or implement the
252 // required subset of its interface.
253 template <typename Duration>
254 void do_wait(const Duration& timeout, boost::system::error_code& ec)
255 {
256 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
257 std::this_thread::sleep_for(
258 std::chrono::seconds(timeout.total_seconds())
259 + std::chrono::microseconds(timeout.total_microseconds()));
260 ec = boost::system::error_code();
261 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
262 ::timeval tv;
263 tv.tv_sec = timeout.total_seconds();
264 tv.tv_usec = timeout.total_microseconds() % 1000000;
265 socket_ops::select(0, 0, 0, 0, &tv, ec);
266 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
267 }
268
269 // The queue of timers.
270 timer_queue<Time_Traits> timer_queue_;
271
272 // The object that schedules and executes timers. Usually a reactor.
273 timer_scheduler& scheduler_;
274 };
275
276 } // namespace detail
277 } // namespace asio
278 } // namespace boost
279
280 #include <boost/asio/detail/pop_options.hpp>
281
282 #endif // BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP