2 // basic_deadline_timer.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_BASIC_DEADLINE_TIMER_HPP
12 #define BOOST_ASIO_BASIC_DEADLINE_TIMER_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 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
21 || defined(GENERATING_DOCUMENTATION)
24 #include <boost/asio/basic_io_object.hpp>
25 #include <boost/asio/detail/handler_type_requirements.hpp>
26 #include <boost/asio/detail/throw_error.hpp>
27 #include <boost/asio/error.hpp>
28 #include <boost/asio/time_traits.hpp>
30 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
31 # include <boost/asio/deadline_timer_service.hpp>
32 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
33 # include <boost/asio/detail/deadline_timer_service.hpp>
34 # define BOOST_ASIO_SVC_T detail::deadline_timer_service<TimeTraits>
35 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
37 #include <boost/asio/detail/push_options.hpp>
42 /// Provides waitable timer functionality.
44 * The basic_deadline_timer class template provides the ability to perform a
45 * blocking or asynchronous wait for a timer to expire.
47 * A deadline timer is always in one of two states: "expired" or "not expired".
48 * If the wait() or async_wait() function is called on an expired timer, the
49 * wait operation will complete immediately.
51 * Most applications will use the boost::asio::deadline_timer typedef.
54 * @e Distinct @e objects: Safe.@n
55 * @e Shared @e objects: Unsafe.
58 * Performing a blocking wait:
60 * // Construct a timer without setting an expiry time.
61 * boost::asio::deadline_timer timer(io_context);
63 * // Set an expiry time relative to now.
64 * timer.expires_from_now(boost::posix_time::seconds(5));
66 * // Wait for the timer to expire.
71 * Performing an asynchronous wait:
73 * void handler(const boost::system::error_code& error)
83 * // Construct a timer with an absolute expiry time.
84 * boost::asio::deadline_timer timer(io_context,
85 * boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));
87 * // Start an asynchronous wait.
88 * timer.async_wait(handler);
91 * @par Changing an active deadline_timer's expiry time
93 * Changing the expiry time of a timer while there are pending asynchronous
94 * waits causes those wait operations to be cancelled. To ensure that the action
95 * associated with the timer is performed only once, use something like this:
99 * void on_some_event()
101 * if (my_timer.expires_from_now(seconds(5)) > 0)
103 * // We managed to cancel the timer. Start new asynchronous wait.
104 * my_timer.async_wait(on_timeout);
108 * // Too late, timer has already expired!
112 * void on_timeout(const boost::system::error_code& e)
114 * if (e != boost::asio::error::operation_aborted)
116 * // Timer was not cancelled, take necessary action.
121 * @li The boost::asio::basic_deadline_timer::expires_from_now() function
122 * cancels any pending asynchronous waits, and returns the number of
123 * asynchronous waits that were cancelled. If it returns 0 then you were too
124 * late and the wait handler has already been executed, or will soon be
125 * executed. If it returns 1 then the wait handler was successfully cancelled.
127 * @li If a wait handler is cancelled, the boost::system::error_code passed to
128 * it contains the value boost::asio::error::operation_aborted.
130 template <typename Time,
131 typename TimeTraits = boost::asio::time_traits<Time>
132 BOOST_ASIO_SVC_TPARAM_DEF2(= deadline_timer_service<Time, TimeTraits>)>
133 class basic_deadline_timer
134 : BOOST_ASIO_SVC_ACCESS basic_io_object<BOOST_ASIO_SVC_T>
137 /// The type of the executor associated with the object.
138 typedef io_context::executor_type executor_type;
140 /// The time traits type.
141 typedef TimeTraits traits_type;
144 typedef typename traits_type::time_type time_type;
146 /// The duration type.
147 typedef typename traits_type::duration_type duration_type;
151 * This constructor creates a timer without setting an expiry time. The
152 * expires_at() or expires_from_now() functions must be called to set an
153 * expiry time before the timer can be waited on.
155 * @param io_context The io_context object that the timer will use to dispatch
156 * handlers for any asynchronous operations performed on the timer.
158 explicit basic_deadline_timer(boost::asio::io_context& io_context)
159 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
163 /// Constructor to set a particular expiry time as an absolute time.
165 * This constructor creates a timer and sets the expiry time.
167 * @param io_context The io_context object that the timer will use to dispatch
168 * handlers for any asynchronous operations performed on the timer.
170 * @param expiry_time The expiry time to be used for the timer, expressed
171 * as an absolute time.
173 basic_deadline_timer(boost::asio::io_context& io_context,
174 const time_type& expiry_time)
175 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
177 boost::system::error_code ec;
178 this->get_service().expires_at(this->get_implementation(), expiry_time, ec);
179 boost::asio::detail::throw_error(ec, "expires_at");
182 /// Constructor to set a particular expiry time relative to now.
184 * This constructor creates a timer and sets the expiry time.
186 * @param io_context The io_context object that the timer will use to dispatch
187 * handlers for any asynchronous operations performed on the timer.
189 * @param expiry_time The expiry time to be used for the timer, relative to
192 basic_deadline_timer(boost::asio::io_context& io_context,
193 const duration_type& expiry_time)
194 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
196 boost::system::error_code ec;
197 this->get_service().expires_from_now(
198 this->get_implementation(), expiry_time, ec);
199 boost::asio::detail::throw_error(ec, "expires_from_now");
202 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
203 /// Move-construct a basic_deadline_timer from another.
205 * This constructor moves a timer from one object to another.
207 * @param other The other basic_deadline_timer object from which the move will
210 * @note Following the move, the moved-from object is in the same state as if
211 * constructed using the @c basic_deadline_timer(io_context&) constructor.
213 basic_deadline_timer(basic_deadline_timer&& other)
214 : basic_io_object<BOOST_ASIO_SVC_T>(std::move(other))
218 /// Move-assign a basic_deadline_timer from another.
220 * This assignment operator moves a timer from one object to another. Cancels
221 * any outstanding asynchronous operations associated with the target object.
223 * @param other The other basic_deadline_timer object from which the move will
226 * @note Following the move, the moved-from object is in the same state as if
227 * constructed using the @c basic_deadline_timer(io_context&) constructor.
229 basic_deadline_timer& operator=(basic_deadline_timer&& other)
231 basic_io_object<BOOST_ASIO_SVC_T>::operator=(std::move(other));
234 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
236 /// Destroys the timer.
238 * This function destroys the timer, cancelling any outstanding asynchronous
239 * wait operations associated with the timer as if by calling @c cancel.
241 ~basic_deadline_timer()
245 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
246 // These functions are provided by basic_io_object<>.
247 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
248 #if !defined(BOOST_ASIO_NO_DEPRECATED)
249 /// (Deprecated: Use get_executor().) Get the io_context associated with the
252 * This function may be used to obtain the io_context object that the I/O
253 * object uses to dispatch handlers for asynchronous operations.
255 * @return A reference to the io_context object that the I/O object will use
256 * to dispatch handlers. Ownership is not transferred to the caller.
258 boost::asio::io_context& get_io_context()
260 return basic_io_object<BOOST_ASIO_SVC_T>::get_io_context();
263 /// (Deprecated: Use get_executor().) Get the io_context associated with the
266 * This function may be used to obtain the io_context object that the I/O
267 * object uses to dispatch handlers for asynchronous operations.
269 * @return A reference to the io_context object that the I/O object will use
270 * to dispatch handlers. Ownership is not transferred to the caller.
272 boost::asio::io_context& get_io_service()
274 return basic_io_object<BOOST_ASIO_SVC_T>::get_io_service();
276 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
278 /// Get the executor associated with the object.
279 executor_type get_executor() BOOST_ASIO_NOEXCEPT
281 return basic_io_object<BOOST_ASIO_SVC_T>::get_executor();
283 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
285 /// Cancel any asynchronous operations that are waiting on the timer.
287 * This function forces the completion of any pending asynchronous wait
288 * operations against the timer. The handler for each cancelled operation will
289 * be invoked with the boost::asio::error::operation_aborted error code.
291 * Cancelling the timer does not change the expiry time.
293 * @return The number of asynchronous operations that were cancelled.
295 * @throws boost::system::system_error Thrown on failure.
297 * @note If the timer has already expired when cancel() is called, then the
298 * handlers for asynchronous wait operations will:
300 * @li have already been invoked; or
302 * @li have been queued for invocation in the near future.
304 * These handlers can no longer be cancelled, and therefore are passed an
305 * error code that indicates the successful completion of the wait operation.
309 boost::system::error_code ec;
310 std::size_t s = this->get_service().cancel(this->get_implementation(), ec);
311 boost::asio::detail::throw_error(ec, "cancel");
315 /// Cancel any asynchronous operations that are waiting on the timer.
317 * This function forces the completion of any pending asynchronous wait
318 * operations against the timer. The handler for each cancelled operation will
319 * be invoked with the boost::asio::error::operation_aborted error code.
321 * Cancelling the timer does not change the expiry time.
323 * @param ec Set to indicate what error occurred, if any.
325 * @return The number of asynchronous operations that were cancelled.
327 * @note If the timer has already expired when cancel() is called, then the
328 * handlers for asynchronous wait operations will:
330 * @li have already been invoked; or
332 * @li have been queued for invocation in the near future.
334 * These handlers can no longer be cancelled, and therefore are passed an
335 * error code that indicates the successful completion of the wait operation.
337 std::size_t cancel(boost::system::error_code& ec)
339 return this->get_service().cancel(this->get_implementation(), ec);
342 /// Cancels one asynchronous operation that is waiting on the timer.
344 * This function forces the completion of one pending asynchronous wait
345 * operation against the timer. Handlers are cancelled in FIFO order. The
346 * handler for the cancelled operation will be invoked with the
347 * boost::asio::error::operation_aborted error code.
349 * Cancelling the timer does not change the expiry time.
351 * @return The number of asynchronous operations that were cancelled. That is,
354 * @throws boost::system::system_error Thrown on failure.
356 * @note If the timer has already expired when cancel_one() is called, then
357 * the handlers for asynchronous wait operations will:
359 * @li have already been invoked; or
361 * @li have been queued for invocation in the near future.
363 * These handlers can no longer be cancelled, and therefore are passed an
364 * error code that indicates the successful completion of the wait operation.
366 std::size_t cancel_one()
368 boost::system::error_code ec;
369 std::size_t s = this->get_service().cancel_one(
370 this->get_implementation(), ec);
371 boost::asio::detail::throw_error(ec, "cancel_one");
375 /// Cancels one asynchronous operation that is waiting on the timer.
377 * This function forces the completion of one pending asynchronous wait
378 * operation against the timer. Handlers are cancelled in FIFO order. The
379 * handler for the cancelled operation will be invoked with the
380 * boost::asio::error::operation_aborted error code.
382 * Cancelling the timer does not change the expiry time.
384 * @param ec Set to indicate what error occurred, if any.
386 * @return The number of asynchronous operations that were cancelled. That is,
389 * @note If the timer has already expired when cancel_one() is called, then
390 * the handlers for asynchronous wait operations will:
392 * @li have already been invoked; or
394 * @li have been queued for invocation in the near future.
396 * These handlers can no longer be cancelled, and therefore are passed an
397 * error code that indicates the successful completion of the wait operation.
399 std::size_t cancel_one(boost::system::error_code& ec)
401 return this->get_service().cancel_one(this->get_implementation(), ec);
404 /// Get the timer's expiry time as an absolute time.
406 * This function may be used to obtain the timer's current expiry time.
407 * Whether the timer has expired or not does not affect this value.
409 time_type expires_at() const
411 return this->get_service().expires_at(this->get_implementation());
414 /// Set the timer's expiry time as an absolute time.
416 * This function sets the expiry time. Any pending asynchronous wait
417 * operations will be cancelled. The handler for each cancelled operation will
418 * be invoked with the boost::asio::error::operation_aborted error code.
420 * @param expiry_time The expiry time to be used for the timer.
422 * @return The number of asynchronous operations that were cancelled.
424 * @throws boost::system::system_error Thrown on failure.
426 * @note If the timer has already expired when expires_at() is called, then
427 * the handlers for asynchronous wait operations will:
429 * @li have already been invoked; or
431 * @li have been queued for invocation in the near future.
433 * These handlers can no longer be cancelled, and therefore are passed an
434 * error code that indicates the successful completion of the wait operation.
436 std::size_t expires_at(const time_type& expiry_time)
438 boost::system::error_code ec;
439 std::size_t s = this->get_service().expires_at(
440 this->get_implementation(), expiry_time, ec);
441 boost::asio::detail::throw_error(ec, "expires_at");
445 /// Set the timer's expiry time as an absolute time.
447 * This function sets the expiry time. Any pending asynchronous wait
448 * operations will be cancelled. The handler for each cancelled operation will
449 * be invoked with the boost::asio::error::operation_aborted error code.
451 * @param expiry_time The expiry time to be used for the timer.
453 * @param ec Set to indicate what error occurred, if any.
455 * @return The number of asynchronous operations that were cancelled.
457 * @note If the timer has already expired when expires_at() is called, then
458 * the handlers for asynchronous wait operations will:
460 * @li have already been invoked; or
462 * @li have been queued for invocation in the near future.
464 * These handlers can no longer be cancelled, and therefore are passed an
465 * error code that indicates the successful completion of the wait operation.
467 std::size_t expires_at(const time_type& expiry_time,
468 boost::system::error_code& ec)
470 return this->get_service().expires_at(
471 this->get_implementation(), expiry_time, ec);
474 /// Get the timer's expiry time relative to now.
476 * This function may be used to obtain the timer's current expiry time.
477 * Whether the timer has expired or not does not affect this value.
479 duration_type expires_from_now() const
481 return this->get_service().expires_from_now(this->get_implementation());
484 /// Set the timer's expiry time relative to now.
486 * This function sets the expiry time. Any pending asynchronous wait
487 * operations will be cancelled. The handler for each cancelled operation will
488 * be invoked with the boost::asio::error::operation_aborted error code.
490 * @param expiry_time The expiry time to be used for the timer.
492 * @return The number of asynchronous operations that were cancelled.
494 * @throws boost::system::system_error Thrown on failure.
496 * @note If the timer has already expired when expires_from_now() is called,
497 * then the handlers for asynchronous wait operations will:
499 * @li have already been invoked; or
501 * @li have been queued for invocation in the near future.
503 * These handlers can no longer be cancelled, and therefore are passed an
504 * error code that indicates the successful completion of the wait operation.
506 std::size_t expires_from_now(const duration_type& expiry_time)
508 boost::system::error_code ec;
509 std::size_t s = this->get_service().expires_from_now(
510 this->get_implementation(), expiry_time, ec);
511 boost::asio::detail::throw_error(ec, "expires_from_now");
515 /// Set the timer's expiry time relative to now.
517 * This function sets the expiry time. Any pending asynchronous wait
518 * operations will be cancelled. The handler for each cancelled operation will
519 * be invoked with the boost::asio::error::operation_aborted error code.
521 * @param expiry_time The expiry time to be used for the timer.
523 * @param ec Set to indicate what error occurred, if any.
525 * @return The number of asynchronous operations that were cancelled.
527 * @note If the timer has already expired when expires_from_now() is called,
528 * then the handlers for asynchronous wait operations will:
530 * @li have already been invoked; or
532 * @li have been queued for invocation in the near future.
534 * These handlers can no longer be cancelled, and therefore are passed an
535 * error code that indicates the successful completion of the wait operation.
537 std::size_t expires_from_now(const duration_type& expiry_time,
538 boost::system::error_code& ec)
540 return this->get_service().expires_from_now(
541 this->get_implementation(), expiry_time, ec);
544 /// Perform a blocking wait on the timer.
546 * This function is used to wait for the timer to expire. This function
547 * blocks and does not return until the timer has expired.
549 * @throws boost::system::system_error Thrown on failure.
553 boost::system::error_code ec;
554 this->get_service().wait(this->get_implementation(), ec);
555 boost::asio::detail::throw_error(ec, "wait");
558 /// Perform a blocking wait on the timer.
560 * This function is used to wait for the timer to expire. This function
561 * blocks and does not return until the timer has expired.
563 * @param ec Set to indicate what error occurred, if any.
565 void wait(boost::system::error_code& ec)
567 this->get_service().wait(this->get_implementation(), ec);
570 /// Start an asynchronous wait on the timer.
572 * This function may be used to initiate an asynchronous wait against the
573 * timer. It always returns immediately.
575 * For each call to async_wait(), the supplied handler will be called exactly
576 * once. The handler will be called when:
578 * @li The timer has expired.
580 * @li The timer was cancelled, in which case the handler is passed the error
581 * code boost::asio::error::operation_aborted.
583 * @param handler The handler to be called when the timer expires. Copies
584 * will be made of the handler as required. The function signature of the
586 * @code void handler(
587 * const boost::system::error_code& error // Result of operation.
589 * Regardless of whether the asynchronous operation completes immediately or
590 * not, the handler will not be invoked from within this function. Invocation
591 * of the handler will be performed in a manner equivalent to using
592 * boost::asio::io_context::post().
594 template <typename WaitHandler>
595 BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
596 void (boost::system::error_code))
597 async_wait(BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
599 // If you get an error on the following line it means that your handler does
600 // not meet the documented type requirements for a WaitHandler.
601 BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
603 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
604 return this->get_service().async_wait(this->get_implementation(),
605 BOOST_ASIO_MOVE_CAST(WaitHandler)(handler));
606 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
607 async_completion<WaitHandler,
608 void (boost::system::error_code)> init(handler);
610 this->get_service().async_wait(this->get_implementation(),
611 init.completion_handler);
613 return init.result.get();
614 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
621 #include <boost/asio/detail/pop_options.hpp>
623 #if !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
624 # undef BOOST_ASIO_SVC_T
625 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
627 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
628 // || defined(GENERATING_DOCUMENTATION)
630 #endif // BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP