2 // detail/deadline_timer_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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/io_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 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(boost::asio::io_context& io_context)
68 : service_base<deadline_timer_service<Time_Traits> >(io_context),
69 scheduler_(boost::asio::use_service<timer_scheduler>(io_context))
71 scheduler_.init_task();
72 scheduler_.add_timer_queue(timer_queue_);
76 ~deadline_timer_service()
78 scheduler_.remove_timer_queue(timer_queue_);
81 // Destroy all user-defined handler objects owned by the service.
86 // Construct a new timer implementation.
87 void construct(implementation_type& impl)
89 impl.expiry = time_type();
90 impl.might_have_pending_waits = false;
93 // Destroy a timer implementation.
94 void destroy(implementation_type& impl)
96 boost::system::error_code ec;
100 // Move-construct a new serial port implementation.
101 void move_construct(implementation_type& impl,
102 implementation_type& other_impl)
104 scheduler_.move_timer(timer_queue_, impl.timer_data, other_impl.timer_data);
106 impl.expiry = other_impl.expiry;
107 other_impl.expiry = time_type();
109 impl.might_have_pending_waits = other_impl.might_have_pending_waits;
110 other_impl.might_have_pending_waits = false;
113 // Move-assign from another serial port implementation.
114 void move_assign(implementation_type& impl,
115 deadline_timer_service& other_service,
116 implementation_type& other_impl)
118 if (this != &other_service)
119 if (impl.might_have_pending_waits)
120 scheduler_.cancel_timer(timer_queue_, impl.timer_data);
122 other_service.scheduler_.move_timer(other_service.timer_queue_,
123 impl.timer_data, other_impl.timer_data);
125 impl.expiry = other_impl.expiry;
126 other_impl.expiry = time_type();
128 impl.might_have_pending_waits = other_impl.might_have_pending_waits;
129 other_impl.might_have_pending_waits = false;
132 // Cancel any asynchronous wait operations associated with the timer.
133 std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
135 if (!impl.might_have_pending_waits)
137 ec = boost::system::error_code();
141 BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
142 "deadline_timer", &impl, 0, "cancel"));
144 std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
145 impl.might_have_pending_waits = false;
146 ec = boost::system::error_code();
150 // Cancels one asynchronous wait operation associated with the timer.
151 std::size_t cancel_one(implementation_type& impl,
152 boost::system::error_code& ec)
154 if (!impl.might_have_pending_waits)
156 ec = boost::system::error_code();
160 BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
161 "deadline_timer", &impl, 0, "cancel_one"));
163 std::size_t count = scheduler_.cancel_timer(
164 timer_queue_, impl.timer_data, 1);
166 impl.might_have_pending_waits = false;
167 ec = boost::system::error_code();
171 // Get the expiry time for the timer as an absolute time.
172 time_type expiry(const implementation_type& impl) const
177 // Get the expiry time for the timer as an absolute time.
178 time_type expires_at(const implementation_type& impl) const
183 // Get the expiry time for the timer relative to now.
184 duration_type expires_from_now(const implementation_type& impl) const
186 return Time_Traits::subtract(this->expiry(impl), Time_Traits::now());
189 // Set the expiry time for the timer as an absolute time.
190 std::size_t expires_at(implementation_type& impl,
191 const time_type& expiry_time, boost::system::error_code& ec)
193 std::size_t count = cancel(impl, ec);
194 impl.expiry = expiry_time;
195 ec = boost::system::error_code();
199 // Set the expiry time for the timer relative to now.
200 std::size_t expires_after(implementation_type& impl,
201 const duration_type& expiry_time, boost::system::error_code& ec)
203 return expires_at(impl,
204 Time_Traits::add(Time_Traits::now(), expiry_time), ec);
207 // Set the expiry time for the timer relative to now.
208 std::size_t expires_from_now(implementation_type& impl,
209 const duration_type& expiry_time, boost::system::error_code& ec)
211 return expires_at(impl,
212 Time_Traits::add(Time_Traits::now(), expiry_time), ec);
215 // Perform a blocking wait on the timer.
216 void wait(implementation_type& impl, boost::system::error_code& ec)
218 time_type now = Time_Traits::now();
219 ec = boost::system::error_code();
220 while (Time_Traits::less_than(now, impl.expiry) && !ec)
222 this->do_wait(Time_Traits::to_posix_duration(
223 Time_Traits::subtract(impl.expiry, now)), ec);
224 now = Time_Traits::now();
228 // Start an asynchronous wait on the timer.
229 template <typename Handler>
230 void async_wait(implementation_type& impl, Handler& handler)
232 // Allocate and construct an operation to wrap the handler.
233 typedef wait_handler<Handler> op;
234 typename op::ptr p = { boost::asio::detail::addressof(handler),
235 op::ptr::allocate(handler), 0 };
236 p.p = new (p.v) op(handler);
238 impl.might_have_pending_waits = true;
240 BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
241 *p.p, "deadline_timer", &impl, 0, "async_wait"));
243 scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
248 // Helper function to wait given a duration type. The duration type should
249 // either be of type boost::posix_time::time_duration, or implement the
250 // required subset of its interface.
251 template <typename Duration>
252 void do_wait(const Duration& timeout, boost::system::error_code& ec)
254 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
255 std::this_thread::sleep_for(
256 std::chrono::seconds(timeout.total_seconds())
257 + std::chrono::microseconds(timeout.total_microseconds()));
258 ec = boost::system::error_code();
259 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
261 tv.tv_sec = timeout.total_seconds();
262 tv.tv_usec = timeout.total_microseconds() % 1000000;
263 socket_ops::select(0, 0, 0, 0, &tv, ec);
264 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
267 // The queue of timers.
268 timer_queue<Time_Traits> timer_queue_;
270 // The object that schedules and executes timers. Usually a reactor.
271 timer_scheduler& scheduler_;
274 } // namespace detail
278 #include <boost/asio/detail/pop_options.hpp>
280 #endif // BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP