basic_waitable_timer.hpp 28 KB

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