2 // detail/deadline_timer_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
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>
34 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
37 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
39 #include <boost/asio/detail/push_options.hpp>
45 template <typename Time_Traits>
46 class deadline_timer_service
47 : public execution_context_service_base<deadline_timer_service<Time_Traits> >
51 typedef typename Time_Traits::time_type time_type;
54 typedef typename Time_Traits::duration_type duration_type;
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
62 bool might_have_pending_waits;
63 typename timer_queue<Time_Traits>::per_timer_data timer_data;
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))
72 scheduler_.init_task();
73 scheduler_.add_timer_queue(timer_queue_);
77 ~deadline_timer_service()
79 scheduler_.remove_timer_queue(timer_queue_);
82 // Destroy all user-defined handler objects owned by the service.
87 // Construct a new timer implementation.
88 void construct(implementation_type& impl)
90 impl.expiry = time_type();
91 impl.might_have_pending_waits = false;
94 // Destroy a timer implementation.
95 void destroy(implementation_type& impl)
97 boost::system::error_code ec;
101 // Move-construct a new serial port implementation.
102 void move_construct(implementation_type& impl,
103 implementation_type& other_impl)
105 scheduler_.move_timer(timer_queue_, impl.timer_data, other_impl.timer_data);
107 impl.expiry = other_impl.expiry;
108 other_impl.expiry = time_type();
110 impl.might_have_pending_waits = other_impl.might_have_pending_waits;
111 other_impl.might_have_pending_waits = false;
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)
119 if (this != &other_service)
120 if (impl.might_have_pending_waits)
121 scheduler_.cancel_timer(timer_queue_, impl.timer_data);
123 other_service.scheduler_.move_timer(other_service.timer_queue_,
124 impl.timer_data, other_impl.timer_data);
126 impl.expiry = other_impl.expiry;
127 other_impl.expiry = time_type();
129 impl.might_have_pending_waits = other_impl.might_have_pending_waits;
130 other_impl.might_have_pending_waits = false;
133 // Cancel any asynchronous wait operations associated with the timer.
134 std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
136 if (!impl.might_have_pending_waits)
138 ec = boost::system::error_code();
142 BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
143 "deadline_timer", &impl, 0, "cancel"));
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();
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)
155 if (!impl.might_have_pending_waits)
157 ec = boost::system::error_code();
161 BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
162 "deadline_timer", &impl, 0, "cancel_one"));
164 std::size_t count = scheduler_.cancel_timer(
165 timer_queue_, impl.timer_data, 1);
167 impl.might_have_pending_waits = false;
168 ec = boost::system::error_code();
172 // Get the expiry time for the timer as an absolute time.
173 time_type expiry(const implementation_type& impl) const
178 // Get the expiry time for the timer as an absolute time.
179 time_type expires_at(const implementation_type& impl) const
184 // Get the expiry time for the timer relative to now.
185 duration_type expires_from_now(const implementation_type& impl) const
187 return Time_Traits::subtract(this->expiry(impl), Time_Traits::now());
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)
194 std::size_t count = cancel(impl, ec);
195 impl.expiry = expiry_time;
196 ec = boost::system::error_code();
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)
204 return expires_at(impl,
205 Time_Traits::add(Time_Traits::now(), expiry_time), ec);
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)
212 return expires_at(impl,
213 Time_Traits::add(Time_Traits::now(), expiry_time), ec);
216 // Perform a blocking wait on the timer.
217 void wait(implementation_type& impl, boost::system::error_code& ec)
219 time_type now = Time_Traits::now();
220 ec = boost::system::error_code();
221 while (Time_Traits::less_than(now, impl.expiry) && !ec)
223 this->do_wait(Time_Traits::to_posix_duration(
224 Time_Traits::subtract(impl.expiry, now)), ec);
225 now = Time_Traits::now();
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)
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);
240 impl.might_have_pending_waits = true;
242 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
243 *p.p, "deadline_timer", &impl, 0, "async_wait"));
245 scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
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)
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)
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)
269 // The queue of timers.
270 timer_queue<Time_Traits> timer_queue_;
272 // The object that schedules and executes timers. Usually a reactor.
273 timer_scheduler& scheduler_;
276 } // namespace detail
280 #include <boost/asio/detail/pop_options.hpp>
282 #endif // BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP