basic_waitable_timer.hpp 29 KB

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