11 #ifndef ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP 12 #define ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 18 #include "asio/detail/config.hpp" 20 #include "asio/error.hpp" 21 #include "asio/execution_context.hpp" 22 #include "asio/detail/bind_handler.hpp" 23 #include "asio/detail/fenced_block.hpp" 24 #include "asio/detail/memory.hpp" 25 #include "asio/detail/noncopyable.hpp" 26 #include "asio/detail/socket_ops.hpp" 27 #include "asio/detail/socket_types.hpp" 28 #include "asio/detail/timer_queue.hpp" 29 #include "asio/detail/timer_queue_ptime.hpp" 30 #include "asio/detail/timer_scheduler.hpp" 31 #include "asio/detail/wait_handler.hpp" 32 #include "asio/detail/wait_op.hpp" 34 #if defined(ASIO_WINDOWS_RUNTIME) 37 #endif // defined(ASIO_WINDOWS_RUNTIME) 39 #include "asio/detail/push_options.hpp" 44 template <
typename Time_Traits>
50 typedef typename Time_Traits::time_type time_type;
53 typedef typename Time_Traits::duration_type duration_type;
61 bool might_have_pending_waits;
69 scheduler_(asio::use_service<timer_scheduler>(context))
71 scheduler_.init_task();
72 scheduler_.add_timer_queue(timer_queue_);
78 scheduler_.remove_timer_queue(timer_queue_);
89 impl.expiry = time_type();
90 impl.might_have_pending_waits =
false;
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;
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;
136 move_construct(impl, other_impl);
144 move_assign(impl, other_service, other_impl);
150 if (!impl.might_have_pending_waits)
156 ASIO_HANDLER_OPERATION((scheduler_.context(),
157 "deadline_timer", &impl, 0,
"cancel"));
159 std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
160 impl.might_have_pending_waits =
false;
169 if (!impl.might_have_pending_waits)
175 ASIO_HANDLER_OPERATION((scheduler_.context(),
176 "deadline_timer", &impl, 0,
"cancel_one"));
178 std::size_t count = scheduler_.cancel_timer(
179 timer_queue_, impl.timer_data, 1);
181 impl.might_have_pending_waits =
false;
201 return Time_Traits::subtract(this->expiry(impl), Time_Traits::now());
208 std::size_t count = cancel(impl, ec);
209 impl.expiry = expiry_time;
218 return expires_at(impl,
219 Time_Traits::add(Time_Traits::now(), expiry_time), ec);
226 return expires_at(impl,
227 Time_Traits::add(Time_Traits::now(), expiry_time), ec);
233 time_type now = Time_Traits::now();
235 while (Time_Traits::less_than(now, impl.expiry) && !ec)
237 this->do_wait(Time_Traits::to_posix_duration(
238 Time_Traits::subtract(impl.expiry, now)), ec);
239 now = Time_Traits::now();
244 template <
typename Handler,
typename IoExecutor>
246 Handler& handler,
const IoExecutor& io_ex)
250 typename op::ptr p = { asio::detail::addressof(handler),
251 op::ptr::allocate(handler), 0 };
252 p.p =
new (p.v) op(handler, io_ex);
254 impl.might_have_pending_waits =
true;
256 ASIO_HANDLER_CREATION((scheduler_.context(),
257 *p.p,
"deadline_timer", &impl, 0,
"async_wait"));
259 scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
267 template <
typename Duration>
270 #if defined(ASIO_WINDOWS_RUNTIME) 271 std::this_thread::sleep_for(
272 std::chrono::seconds(timeout.total_seconds())
273 + std::chrono::microseconds(timeout.total_microseconds()));
275 #else // defined(ASIO_WINDOWS_RUNTIME) 277 tv.tv_sec = timeout.total_seconds();
278 tv.tv_usec = timeout.total_microseconds() % 1000000;
279 socket_ops::select(0, 0, 0, 0, &tv, ec);
280 #endif // defined(ASIO_WINDOWS_RUNTIME) 287 timer_scheduler& scheduler_;
293 #include "asio/detail/pop_options.hpp" 295 #endif // ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP Definition: deadline_timer_service.hpp:45
Definition: wait_handler.hpp:32
Definition: noncopyable.hpp:25
Definition: execution_context.hpp:386
A context for function object execution.
Definition: execution_context.hpp:105
Definition: deadline_timer_service.hpp:57
Definition: timer_queue.hpp:35
Class to represent an error code value.
Definition: error_code.hpp:80
execution_context & context()
Get the context object that owns the service.
Definition: execution_context.hpp:100
Definition: timer_queue.hpp:46
void shutdown()
Destroy all user-defined handler objects owned by the service.
Definition: deadline_timer_service.hpp:82
Definition: any_io_executor.hpp:28