basic_waitable_timer.hpp 24 KB

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