basic_waitable_timer.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. //
  2. // basic_waitable_timer.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef BOOST_ASIO_BASIC_WAITABLE_TIMER_HPP
  11. #define BOOST_ASIO_BASIC_WAITABLE_TIMER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstddef>
  17. #include <boost/asio/any_io_executor.hpp>
  18. #include <boost/asio/detail/chrono_time_traits.hpp>
  19. #include <boost/asio/detail/deadline_timer_service.hpp>
  20. #include <boost/asio/detail/handler_type_requirements.hpp>
  21. #include <boost/asio/detail/io_object_impl.hpp>
  22. #include <boost/asio/detail/non_const_lvalue.hpp>
  23. #include <boost/asio/detail/throw_error.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/wait_traits.hpp>
  26. #if defined(BOOST_ASIO_HAS_MOVE)
  27. # include <utility>
  28. #endif // defined(BOOST_ASIO_HAS_MOVE)
  29. #include <boost/asio/detail/push_options.hpp>
  30. namespace boost {
  31. namespace asio {
  32. #if !defined(BOOST_ASIO_BASIC_WAITABLE_TIMER_FWD_DECL)
  33. #define BOOST_ASIO_BASIC_WAITABLE_TIMER_FWD_DECL
  34. // Forward declaration with defaulted arguments.
  35. template <typename Clock,
  36. typename WaitTraits = boost::asio::wait_traits<Clock>,
  37. typename Executor = any_io_executor>
  38. class basic_waitable_timer;
  39. #endif // !defined(BOOST_ASIO_BASIC_WAITABLE_TIMER_FWD_DECL)
  40. /// Provides waitable timer functionality.
  41. /**
  42. * The basic_waitable_timer class template provides the ability to perform a
  43. * blocking or asynchronous wait for a timer to expire.
  44. *
  45. * A waitable timer is always in one of two states: "expired" or "not expired".
  46. * If the wait() or async_wait() function is called on an expired timer, the
  47. * wait operation will complete immediately.
  48. *
  49. * Most applications will use one of the boost::asio::steady_timer,
  50. * boost::asio::system_timer or boost::asio::high_resolution_timer typedefs.
  51. *
  52. * @note This waitable timer functionality is for use with the C++11 standard
  53. * library's @c &lt;chrono&gt; facility, or with the Boost.Chrono library.
  54. *
  55. * @par Thread Safety
  56. * @e Distinct @e objects: Safe.@n
  57. * @e Shared @e objects: Unsafe.
  58. *
  59. * @par Examples
  60. * Performing a blocking wait (C++11):
  61. * @code
  62. * // Construct a timer without setting an expiry time.
  63. * boost::asio::steady_timer timer(my_context);
  64. *
  65. * // Set an expiry time relative to now.
  66. * timer.expires_after(std::chrono::seconds(5));
  67. *
  68. * // Wait for the timer to expire.
  69. * timer.wait();
  70. * @endcode
  71. *
  72. * @par
  73. * Performing an asynchronous wait (C++11):
  74. * @code
  75. * void handler(const boost::system::error_code& error)
  76. * {
  77. * if (!error)
  78. * {
  79. * // Timer expired.
  80. * }
  81. * }
  82. *
  83. * ...
  84. *
  85. * // Construct a timer with an absolute expiry time.
  86. * boost::asio::steady_timer timer(my_context,
  87. * std::chrono::steady_clock::now() + std::chrono::seconds(60));
  88. *
  89. * // Start an asynchronous wait.
  90. * timer.async_wait(handler);
  91. * @endcode
  92. *
  93. * @par Changing an active waitable timer's expiry time
  94. *
  95. * Changing the expiry time of a timer while there are pending asynchronous
  96. * waits causes those wait operations to be cancelled. To ensure that the action
  97. * associated with the timer is performed only once, use something like this:
  98. * used:
  99. *
  100. * @code
  101. * void on_some_event()
  102. * {
  103. * if (my_timer.expires_after(seconds(5)) > 0)
  104. * {
  105. * // We managed to cancel the timer. Start new asynchronous wait.
  106. * my_timer.async_wait(on_timeout);
  107. * }
  108. * else
  109. * {
  110. * // Too late, timer has already expired!
  111. * }
  112. * }
  113. *
  114. * void on_timeout(const boost::system::error_code& e)
  115. * {
  116. * if (e != boost::asio::error::operation_aborted)
  117. * {
  118. * // Timer was not cancelled, take necessary action.
  119. * }
  120. * }
  121. * @endcode
  122. *
  123. * @li The boost::asio::basic_waitable_timer::expires_after() function
  124. * cancels any pending asynchronous waits, and returns the number of
  125. * asynchronous waits that were cancelled. If it returns 0 then you were too
  126. * late and the wait handler has already been executed, or will soon be
  127. * executed. If it returns 1 then the wait handler was successfully cancelled.
  128. *
  129. * @li If a wait handler is cancelled, the boost::system::error_code passed to
  130. * it contains the value boost::asio::error::operation_aborted.
  131. */
  132. template <typename Clock, typename WaitTraits, typename Executor>
  133. class basic_waitable_timer
  134. {
  135. public:
  136. /// The type of the executor associated with the object.
  137. typedef Executor executor_type;
  138. /// Rebinds the timer type to another executor.
  139. template <typename Executor1>
  140. struct rebind_executor
  141. {
  142. /// The timer type when rebound to the specified executor.
  143. typedef basic_waitable_timer<Clock, WaitTraits, Executor1> other;
  144. };
  145. /// The clock type.
  146. typedef Clock clock_type;
  147. /// The duration type of the clock.
  148. typedef typename clock_type::duration duration;
  149. /// The time point type of the clock.
  150. typedef typename clock_type::time_point time_point;
  151. /// The wait traits type.
  152. typedef WaitTraits traits_type;
  153. /// Constructor.
  154. /**
  155. * This constructor creates a timer without setting an expiry time. The
  156. * expires_at() or expires_after() functions must be called to set an expiry
  157. * time before the timer can be waited on.
  158. *
  159. * @param ex The I/O executor that the timer will use, by default, to
  160. * dispatch handlers for any asynchronous operations performed on the timer.
  161. */
  162. explicit basic_waitable_timer(const executor_type& ex)
  163. : impl_(0, ex)
  164. {
  165. }
  166. /// Constructor.
  167. /**
  168. * This constructor creates a timer without setting an expiry time. The
  169. * expires_at() or expires_after() functions must be called to set an expiry
  170. * time before the timer can be waited on.
  171. *
  172. * @param context An execution context which provides the I/O executor that
  173. * the timer will use, by default, to dispatch handlers for any asynchronous
  174. * operations performed on the timer.
  175. */
  176. template <typename ExecutionContext>
  177. explicit basic_waitable_timer(ExecutionContext& context,
  178. typename constraint<
  179. is_convertible<ExecutionContext&, execution_context&>::value
  180. >::type = 0)
  181. : impl_(0, 0, context)
  182. {
  183. }
  184. /// Constructor to set a particular expiry time as an absolute time.
  185. /**
  186. * This constructor creates a timer and sets the expiry time.
  187. *
  188. * @param ex The I/O executor object that the timer will use, by default, to
  189. * dispatch handlers for any asynchronous operations performed on the timer.
  190. *
  191. * @param expiry_time The expiry time to be used for the timer, expressed
  192. * as an absolute time.
  193. */
  194. basic_waitable_timer(const executor_type& ex, const time_point& expiry_time)
  195. : impl_(0, ex)
  196. {
  197. boost::system::error_code ec;
  198. impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
  199. boost::asio::detail::throw_error(ec, "expires_at");
  200. }
  201. /// Constructor to set a particular expiry time as an absolute time.
  202. /**
  203. * This constructor creates a timer and sets the expiry time.
  204. *
  205. * @param context An execution context which provides the I/O executor that
  206. * the timer will use, by default, to dispatch handlers for any asynchronous
  207. * operations performed on the timer.
  208. *
  209. * @param expiry_time The expiry time to be used for the timer, expressed
  210. * as an absolute time.
  211. */
  212. template <typename ExecutionContext>
  213. explicit basic_waitable_timer(ExecutionContext& context,
  214. const time_point& expiry_time,
  215. typename constraint<
  216. is_convertible<ExecutionContext&, execution_context&>::value
  217. >::type = 0)
  218. : impl_(0, 0, context)
  219. {
  220. boost::system::error_code ec;
  221. impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
  222. boost::asio::detail::throw_error(ec, "expires_at");
  223. }
  224. /// Constructor to set a particular expiry time relative to now.
  225. /**
  226. * This constructor creates a timer and sets the expiry time.
  227. *
  228. * @param ex The I/O executor that the timer will use, by default, to
  229. * dispatch handlers for any asynchronous operations performed on the timer.
  230. *
  231. * @param expiry_time The expiry time to be used for the timer, relative to
  232. * now.
  233. */
  234. basic_waitable_timer(const executor_type& ex, const duration& expiry_time)
  235. : impl_(0, ex)
  236. {
  237. boost::system::error_code ec;
  238. impl_.get_service().expires_after(
  239. impl_.get_implementation(), expiry_time, ec);
  240. boost::asio::detail::throw_error(ec, "expires_after");
  241. }
  242. /// Constructor to set a particular expiry time relative to now.
  243. /**
  244. * This constructor creates a timer and sets the expiry time.
  245. *
  246. * @param context An execution context which provides the I/O executor that
  247. * the timer will use, by default, to dispatch handlers for any asynchronous
  248. * operations performed on the timer.
  249. *
  250. * @param expiry_time The expiry time to be used for the timer, relative to
  251. * now.
  252. */
  253. template <typename ExecutionContext>
  254. explicit basic_waitable_timer(ExecutionContext& context,
  255. const duration& expiry_time,
  256. typename constraint<
  257. is_convertible<ExecutionContext&, execution_context&>::value
  258. >::type = 0)
  259. : impl_(0, 0, context)
  260. {
  261. boost::system::error_code ec;
  262. impl_.get_service().expires_after(
  263. impl_.get_implementation(), expiry_time, ec);
  264. boost::asio::detail::throw_error(ec, "expires_after");
  265. }
  266. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  267. /// Move-construct a basic_waitable_timer from another.
  268. /**
  269. * This constructor moves a timer from one object to another.
  270. *
  271. * @param other The other basic_waitable_timer object from which the move will
  272. * occur.
  273. *
  274. * @note Following the move, the moved-from object is in the same state as if
  275. * constructed using the @c basic_waitable_timer(const executor_type&)
  276. * constructor.
  277. */
  278. basic_waitable_timer(basic_waitable_timer&& other)
  279. : impl_(std::move(other.impl_))
  280. {
  281. }
  282. /// Move-assign a basic_waitable_timer from another.
  283. /**
  284. * This assignment operator moves a timer from one object to another. Cancels
  285. * any outstanding asynchronous operations associated with the target object.
  286. *
  287. * @param other The other basic_waitable_timer object from which the move will
  288. * occur.
  289. *
  290. * @note Following the move, the moved-from object is in the same state as if
  291. * constructed using the @c basic_waitable_timer(const executor_type&)
  292. * constructor.
  293. */
  294. basic_waitable_timer& operator=(basic_waitable_timer&& other)
  295. {
  296. impl_ = std::move(other.impl_);
  297. return *this;
  298. }
  299. // All timers have access to each other's implementations.
  300. template <typename Clock1, typename WaitTraits1, typename Executor1>
  301. friend class basic_waitable_timer;
  302. /// Move-construct a basic_waitable_timer from another.
  303. /**
  304. * This constructor moves a timer from one object to another.
  305. *
  306. * @param other The other basic_waitable_timer object from which the move will
  307. * occur.
  308. *
  309. * @note Following the move, the moved-from object is in the same state as if
  310. * constructed using the @c basic_waitable_timer(const executor_type&)
  311. * constructor.
  312. */
  313. template <typename Executor1>
  314. basic_waitable_timer(
  315. basic_waitable_timer<Clock, WaitTraits, Executor1>&& other,
  316. typename constraint<
  317. is_convertible<Executor1, Executor>::value
  318. >::type = 0)
  319. : impl_(std::move(other.impl_))
  320. {
  321. }
  322. /// Move-assign a basic_waitable_timer from another.
  323. /**
  324. * This assignment operator moves a timer from one object to another. Cancels
  325. * any outstanding asynchronous operations associated with the target object.
  326. *
  327. * @param other The other basic_waitable_timer object from which the move will
  328. * occur.
  329. *
  330. * @note Following the move, the moved-from object is in the same state as if
  331. * constructed using the @c basic_waitable_timer(const executor_type&)
  332. * constructor.
  333. */
  334. template <typename Executor1>
  335. typename constraint<
  336. is_convertible<Executor1, Executor>::value,
  337. basic_waitable_timer&
  338. >::type operator=(basic_waitable_timer<Clock, WaitTraits, Executor1>&& other)
  339. {
  340. basic_waitable_timer tmp(std::move(other));
  341. impl_ = std::move(tmp.impl_);
  342. return *this;
  343. }
  344. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  345. /// Destroys the timer.
  346. /**
  347. * This function destroys the timer, cancelling any outstanding asynchronous
  348. * wait operations associated with the timer as if by calling @c cancel.
  349. */
  350. ~basic_waitable_timer()
  351. {
  352. }
  353. /// Get the executor associated with the object.
  354. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  355. {
  356. return impl_.get_executor();
  357. }
  358. /// Cancel any asynchronous operations that are waiting on the timer.
  359. /**
  360. * This function forces the completion of any pending asynchronous wait
  361. * operations against the timer. The handler for each cancelled operation will
  362. * be invoked with the boost::asio::error::operation_aborted error code.
  363. *
  364. * Cancelling the timer does not change the expiry time.
  365. *
  366. * @return The number of asynchronous operations that were cancelled.
  367. *
  368. * @throws boost::system::system_error Thrown on failure.
  369. *
  370. * @note If the timer has already expired when cancel() is called, then the
  371. * handlers for asynchronous wait operations will:
  372. *
  373. * @li have already been invoked; or
  374. *
  375. * @li have been queued for invocation in the near future.
  376. *
  377. * These handlers can no longer be cancelled, and therefore are passed an
  378. * error code that indicates the successful completion of the wait operation.
  379. */
  380. std::size_t cancel()
  381. {
  382. boost::system::error_code ec;
  383. std::size_t s = impl_.get_service().cancel(impl_.get_implementation(), ec);
  384. boost::asio::detail::throw_error(ec, "cancel");
  385. return s;
  386. }
  387. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  388. /// (Deprecated: Use non-error_code overload.) Cancel any asynchronous
  389. /// operations that are waiting on the timer.
  390. /**
  391. * This function forces the completion of any pending asynchronous wait
  392. * operations against the timer. The handler for each cancelled operation will
  393. * be invoked with the boost::asio::error::operation_aborted error code.
  394. *
  395. * Cancelling the timer does not change the expiry time.
  396. *
  397. * @param ec Set to indicate what error occurred, if any.
  398. *
  399. * @return The number of asynchronous operations that were cancelled.
  400. *
  401. * @note If the timer has already expired when cancel() is called, then the
  402. * handlers for asynchronous wait operations will:
  403. *
  404. * @li have already been invoked; or
  405. *
  406. * @li have been queued for invocation in the near future.
  407. *
  408. * These handlers can no longer be cancelled, and therefore are passed an
  409. * error code that indicates the successful completion of the wait operation.
  410. */
  411. std::size_t cancel(boost::system::error_code& ec)
  412. {
  413. return impl_.get_service().cancel(impl_.get_implementation(), ec);
  414. }
  415. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  416. /// Cancels one asynchronous operation that is waiting on the timer.
  417. /**
  418. * This function forces the completion of one pending asynchronous wait
  419. * operation against the timer. Handlers are cancelled in FIFO order. The
  420. * handler for the cancelled operation will be invoked with the
  421. * boost::asio::error::operation_aborted error code.
  422. *
  423. * Cancelling the timer does not change the expiry time.
  424. *
  425. * @return The number of asynchronous operations that were cancelled. That is,
  426. * either 0 or 1.
  427. *
  428. * @throws boost::system::system_error Thrown on failure.
  429. *
  430. * @note If the timer has already expired when cancel_one() is called, then
  431. * the handlers for asynchronous wait operations will:
  432. *
  433. * @li have already been invoked; or
  434. *
  435. * @li have been queued for invocation in the near future.
  436. *
  437. * These handlers can no longer be cancelled, and therefore are passed an
  438. * error code that indicates the successful completion of the wait operation.
  439. */
  440. std::size_t cancel_one()
  441. {
  442. boost::system::error_code ec;
  443. std::size_t s = impl_.get_service().cancel_one(
  444. impl_.get_implementation(), ec);
  445. boost::asio::detail::throw_error(ec, "cancel_one");
  446. return s;
  447. }
  448. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  449. /// (Deprecated: Use non-error_code overload.) Cancels one asynchronous
  450. /// operation that is waiting on the timer.
  451. /**
  452. * This function forces the completion of one pending asynchronous wait
  453. * operation against the timer. Handlers are cancelled in FIFO order. The
  454. * handler for the cancelled operation will be invoked with the
  455. * boost::asio::error::operation_aborted error code.
  456. *
  457. * Cancelling the timer does not change the expiry time.
  458. *
  459. * @param ec Set to indicate what error occurred, if any.
  460. *
  461. * @return The number of asynchronous operations that were cancelled. That is,
  462. * either 0 or 1.
  463. *
  464. * @note If the timer has already expired when cancel_one() is called, then
  465. * the handlers for asynchronous wait operations will:
  466. *
  467. * @li have already been invoked; or
  468. *
  469. * @li have been queued for invocation in the near future.
  470. *
  471. * These handlers can no longer be cancelled, and therefore are passed an
  472. * error code that indicates the successful completion of the wait operation.
  473. */
  474. std::size_t cancel_one(boost::system::error_code& ec)
  475. {
  476. return impl_.get_service().cancel_one(impl_.get_implementation(), ec);
  477. }
  478. /// (Deprecated: Use expiry().) Get the timer's expiry time as an absolute
  479. /// time.
  480. /**
  481. * This function may be used to obtain the timer's current expiry time.
  482. * Whether the timer has expired or not does not affect this value.
  483. */
  484. time_point expires_at() const
  485. {
  486. return impl_.get_service().expires_at(impl_.get_implementation());
  487. }
  488. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  489. /// Get the timer's expiry time as an absolute time.
  490. /**
  491. * This function may be used to obtain the timer's current expiry time.
  492. * Whether the timer has expired or not does not affect this value.
  493. */
  494. time_point expiry() const
  495. {
  496. return impl_.get_service().expiry(impl_.get_implementation());
  497. }
  498. /// Set the timer's expiry time as an absolute time.
  499. /**
  500. * This function sets the expiry time. Any pending asynchronous wait
  501. * operations will be cancelled. The handler for each cancelled operation will
  502. * be invoked with the boost::asio::error::operation_aborted error code.
  503. *
  504. * @param expiry_time The expiry time to be used for the timer.
  505. *
  506. * @return The number of asynchronous operations that were cancelled.
  507. *
  508. * @throws boost::system::system_error Thrown on failure.
  509. *
  510. * @note If the timer has already expired when expires_at() is called, then
  511. * the handlers for asynchronous wait operations will:
  512. *
  513. * @li have already been invoked; or
  514. *
  515. * @li have been queued for invocation in the near future.
  516. *
  517. * These handlers can no longer be cancelled, and therefore are passed an
  518. * error code that indicates the successful completion of the wait operation.
  519. */
  520. std::size_t expires_at(const time_point& expiry_time)
  521. {
  522. boost::system::error_code ec;
  523. std::size_t s = impl_.get_service().expires_at(
  524. impl_.get_implementation(), expiry_time, ec);
  525. boost::asio::detail::throw_error(ec, "expires_at");
  526. return s;
  527. }
  528. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  529. /// (Deprecated: Use non-error_code overload.) Set the timer's expiry time as
  530. /// an absolute time.
  531. /**
  532. * This function sets the expiry time. Any pending asynchronous wait
  533. * operations will be cancelled. The handler for each cancelled operation will
  534. * be invoked with the boost::asio::error::operation_aborted error code.
  535. *
  536. * @param expiry_time The expiry time to be used for the timer.
  537. *
  538. * @param ec Set to indicate what error occurred, if any.
  539. *
  540. * @return The number of asynchronous operations that were cancelled.
  541. *
  542. * @note If the timer has already expired when expires_at() is called, then
  543. * the handlers for asynchronous wait operations will:
  544. *
  545. * @li have already been invoked; or
  546. *
  547. * @li have been queued for invocation in the near future.
  548. *
  549. * These handlers can no longer be cancelled, and therefore are passed an
  550. * error code that indicates the successful completion of the wait operation.
  551. */
  552. std::size_t expires_at(const time_point& expiry_time,
  553. boost::system::error_code& ec)
  554. {
  555. return impl_.get_service().expires_at(
  556. impl_.get_implementation(), expiry_time, ec);
  557. }
  558. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  559. /// Set the timer's expiry time relative to now.
  560. /**
  561. * This function sets the expiry time. Any pending asynchronous wait
  562. * operations will be cancelled. The handler for each cancelled operation will
  563. * be invoked with the boost::asio::error::operation_aborted error code.
  564. *
  565. * @param expiry_time The expiry time to be used for the timer.
  566. *
  567. * @return The number of asynchronous operations that were cancelled.
  568. *
  569. * @throws boost::system::system_error Thrown on failure.
  570. *
  571. * @note If the timer has already expired when expires_after() is called,
  572. * then the handlers for asynchronous wait operations will:
  573. *
  574. * @li have already been invoked; or
  575. *
  576. * @li have been queued for invocation in the near future.
  577. *
  578. * These handlers can no longer be cancelled, and therefore are passed an
  579. * error code that indicates the successful completion of the wait operation.
  580. */
  581. std::size_t expires_after(const duration& expiry_time)
  582. {
  583. boost::system::error_code ec;
  584. std::size_t s = impl_.get_service().expires_after(
  585. impl_.get_implementation(), expiry_time, ec);
  586. boost::asio::detail::throw_error(ec, "expires_after");
  587. return s;
  588. }
  589. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  590. /// (Deprecated: Use expiry().) Get the timer's expiry time relative to now.
  591. /**
  592. * This function may be used to obtain the timer's current expiry time.
  593. * Whether the timer has expired or not does not affect this value.
  594. */
  595. duration expires_from_now() const
  596. {
  597. return impl_.get_service().expires_from_now(impl_.get_implementation());
  598. }
  599. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  600. /// to now.
  601. /**
  602. * This function sets the expiry time. Any pending asynchronous wait
  603. * operations will be cancelled. The handler for each cancelled operation will
  604. * be invoked with the boost::asio::error::operation_aborted error code.
  605. *
  606. * @param expiry_time The expiry time to be used for the timer.
  607. *
  608. * @return The number of asynchronous operations that were cancelled.
  609. *
  610. * @throws boost::system::system_error Thrown on failure.
  611. *
  612. * @note If the timer has already expired when expires_from_now() is called,
  613. * then the handlers for asynchronous wait operations will:
  614. *
  615. * @li have already been invoked; or
  616. *
  617. * @li have been queued for invocation in the near future.
  618. *
  619. * These handlers can no longer be cancelled, and therefore are passed an
  620. * error code that indicates the successful completion of the wait operation.
  621. */
  622. std::size_t expires_from_now(const duration& expiry_time)
  623. {
  624. boost::system::error_code ec;
  625. std::size_t s = impl_.get_service().expires_from_now(
  626. impl_.get_implementation(), expiry_time, ec);
  627. boost::asio::detail::throw_error(ec, "expires_from_now");
  628. return s;
  629. }
  630. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  631. /// to now.
  632. /**
  633. * This function sets the expiry time. Any pending asynchronous wait
  634. * operations will be cancelled. The handler for each cancelled operation will
  635. * be invoked with the boost::asio::error::operation_aborted error code.
  636. *
  637. * @param expiry_time The expiry time to be used for the timer.
  638. *
  639. * @param ec Set to indicate what error occurred, if any.
  640. *
  641. * @return The number of asynchronous operations that were cancelled.
  642. *
  643. * @note If the timer has already expired when expires_from_now() is called,
  644. * then the handlers for asynchronous wait operations will:
  645. *
  646. * @li have already been invoked; or
  647. *
  648. * @li have been queued for invocation in the near future.
  649. *
  650. * These handlers can no longer be cancelled, and therefore are passed an
  651. * error code that indicates the successful completion of the wait operation.
  652. */
  653. std::size_t expires_from_now(const duration& expiry_time,
  654. boost::system::error_code& ec)
  655. {
  656. return impl_.get_service().expires_from_now(
  657. impl_.get_implementation(), expiry_time, ec);
  658. }
  659. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  660. /// Perform a blocking wait on the timer.
  661. /**
  662. * This function is used to wait for the timer to expire. This function
  663. * blocks and does not return until the timer has expired.
  664. *
  665. * @throws boost::system::system_error Thrown on failure.
  666. */
  667. void wait()
  668. {
  669. boost::system::error_code ec;
  670. impl_.get_service().wait(impl_.get_implementation(), ec);
  671. boost::asio::detail::throw_error(ec, "wait");
  672. }
  673. /// Perform a blocking wait on the timer.
  674. /**
  675. * This function is used to wait for the timer to expire. This function
  676. * blocks and does not return until the timer has expired.
  677. *
  678. * @param ec Set to indicate what error occurred, if any.
  679. */
  680. void wait(boost::system::error_code& ec)
  681. {
  682. impl_.get_service().wait(impl_.get_implementation(), ec);
  683. }
  684. /// Start an asynchronous wait on the timer.
  685. /**
  686. * This function may be used to initiate an asynchronous wait against the
  687. * timer. It always returns immediately.
  688. *
  689. * For each call to async_wait(), the supplied handler will be called exactly
  690. * once. The handler will be called when:
  691. *
  692. * @li The timer has expired.
  693. *
  694. * @li The timer was cancelled, in which case the handler is passed the error
  695. * code boost::asio::error::operation_aborted.
  696. *
  697. * @param handler The handler to be called when the timer expires. Copies
  698. * will be made of the handler as required. The function signature of the
  699. * handler must be:
  700. * @code void handler(
  701. * const boost::system::error_code& error // Result of operation.
  702. * ); @endcode
  703. * Regardless of whether the asynchronous operation completes immediately or
  704. * not, the handler will not be invoked from within this function. On
  705. * immediate completion, invocation of the handler will be performed in a
  706. * manner equivalent to using boost::asio::post().
  707. */
  708. template <
  709. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  710. WaitHandler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  711. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WaitHandler,
  712. void (boost::system::error_code))
  713. async_wait(
  714. BOOST_ASIO_MOVE_ARG(WaitHandler) handler
  715. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  716. {
  717. return async_initiate<WaitHandler, void (boost::system::error_code)>(
  718. initiate_async_wait(this), handler);
  719. }
  720. private:
  721. // Disallow copying and assignment.
  722. basic_waitable_timer(const basic_waitable_timer&) BOOST_ASIO_DELETED;
  723. basic_waitable_timer& operator=(
  724. const basic_waitable_timer&) BOOST_ASIO_DELETED;
  725. class initiate_async_wait
  726. {
  727. public:
  728. typedef Executor executor_type;
  729. explicit initiate_async_wait(basic_waitable_timer* self)
  730. : self_(self)
  731. {
  732. }
  733. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  734. {
  735. return self_->get_executor();
  736. }
  737. template <typename WaitHandler>
  738. void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler) const
  739. {
  740. // If you get an error on the following line it means that your handler
  741. // does not meet the documented type requirements for a WaitHandler.
  742. BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  743. detail::non_const_lvalue<WaitHandler> handler2(handler);
  744. self_->impl_.get_service().async_wait(
  745. self_->impl_.get_implementation(),
  746. handler2.value, self_->impl_.get_executor());
  747. }
  748. private:
  749. basic_waitable_timer* self_;
  750. };
  751. detail::io_object_impl<
  752. detail::deadline_timer_service<
  753. detail::chrono_time_traits<Clock, WaitTraits> >,
  754. executor_type > impl_;
  755. };
  756. } // namespace asio
  757. } // namespace boost
  758. #include <boost/asio/detail/pop_options.hpp>
  759. #endif // BOOST_ASIO_BASIC_WAITABLE_TIMER_HPP