basic_waitable_timer.hpp 28 KB

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