basic_waitable_timer.hpp 25 KB

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