basic_waitable_timer.hpp 28 KB

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