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