basic_waitable_timer.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. //
  2. // basic_waitable_timer.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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/detail/chrono_time_traits.hpp>
  18. #include <boost/asio/detail/deadline_timer_service.hpp>
  19. #include <boost/asio/detail/handler_type_requirements.hpp>
  20. #include <boost/asio/detail/io_object_impl.hpp>
  21. #include <boost/asio/detail/non_const_lvalue.hpp>
  22. #include <boost/asio/detail/throw_error.hpp>
  23. #include <boost/asio/error.hpp>
  24. #include <boost/asio/executor.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 = 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. 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_(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. typename enable_if<
  179. is_convertible<ExecutionContext&, execution_context&>::value
  180. >::type* = 0)
  181. : impl_(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_(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. typename enable_if<
  216. is_convertible<ExecutionContext&, execution_context&>::value
  217. >::type* = 0)
  218. : impl_(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_(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. typename enable_if<
  257. is_convertible<ExecutionContext&, execution_context&>::value
  258. >::type* = 0)
  259. : impl_(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. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  267. /// Move-construct a basic_waitable_timer from another.
  268. /**
  269. * This constructor moves a timer from one object to another.
  270. *
  271. * @param other The other basic_waitable_timer object from which the move will
  272. * occur.
  273. *
  274. * @note Following the move, the moved-from object is in the same state as if
  275. * constructed using the @c basic_waitable_timer(const executor_type&)
  276. * constructor.
  277. */
  278. basic_waitable_timer(basic_waitable_timer&& other)
  279. : impl_(std::move(other.impl_))
  280. {
  281. }
  282. /// Move-assign a basic_waitable_timer from another.
  283. /**
  284. * This assignment operator moves a timer from one object to another. Cancels
  285. * any outstanding asynchronous operations associated with the target object.
  286. *
  287. * @param other The other basic_waitable_timer object from which the move will
  288. * occur.
  289. *
  290. * @note Following the move, the moved-from object is in the same state as if
  291. * constructed using the @c basic_waitable_timer(const executor_type&)
  292. * constructor.
  293. */
  294. basic_waitable_timer& operator=(basic_waitable_timer&& other)
  295. {
  296. impl_ = std::move(other.impl_);
  297. return *this;
  298. }
  299. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  300. /// Destroys the timer.
  301. /**
  302. * This function destroys the timer, cancelling any outstanding asynchronous
  303. * wait operations associated with the timer as if by calling @c cancel.
  304. */
  305. ~basic_waitable_timer()
  306. {
  307. }
  308. /// Get the executor associated with the object.
  309. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  310. {
  311. return impl_.get_executor();
  312. }
  313. /// Cancel any asynchronous operations that are waiting on the timer.
  314. /**
  315. * This function forces the completion of any pending asynchronous wait
  316. * operations against the timer. The handler for each cancelled operation will
  317. * be invoked with the boost::asio::error::operation_aborted error code.
  318. *
  319. * Cancelling the timer does not change the expiry time.
  320. *
  321. * @return The number of asynchronous operations that were cancelled.
  322. *
  323. * @throws boost::system::system_error Thrown on failure.
  324. *
  325. * @note If the timer has already expired when cancel() is called, then the
  326. * handlers for asynchronous wait operations will:
  327. *
  328. * @li have already been invoked; or
  329. *
  330. * @li have been queued for invocation in the near future.
  331. *
  332. * These handlers can no longer be cancelled, and therefore are passed an
  333. * error code that indicates the successful completion of the wait operation.
  334. */
  335. std::size_t cancel()
  336. {
  337. boost::system::error_code ec;
  338. std::size_t s = impl_.get_service().cancel(impl_.get_implementation(), ec);
  339. boost::asio::detail::throw_error(ec, "cancel");
  340. return s;
  341. }
  342. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  343. /// (Deprecated: Use non-error_code overload.) Cancel any asynchronous
  344. /// operations that are waiting on the timer.
  345. /**
  346. * This function forces the completion of any pending asynchronous wait
  347. * operations against the timer. The handler for each cancelled operation will
  348. * be invoked with the boost::asio::error::operation_aborted error code.
  349. *
  350. * Cancelling the timer does not change the expiry time.
  351. *
  352. * @param ec Set to indicate what error occurred, if any.
  353. *
  354. * @return The number of asynchronous operations that were cancelled.
  355. *
  356. * @note If the timer has already expired when cancel() is called, then the
  357. * handlers for asynchronous wait operations will:
  358. *
  359. * @li have already been invoked; or
  360. *
  361. * @li have been queued for invocation in the near future.
  362. *
  363. * These handlers can no longer be cancelled, and therefore are passed an
  364. * error code that indicates the successful completion of the wait operation.
  365. */
  366. std::size_t cancel(boost::system::error_code& ec)
  367. {
  368. return impl_.get_service().cancel(impl_.get_implementation(), ec);
  369. }
  370. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  371. /// Cancels one asynchronous 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. * @return The number of asynchronous operations that were cancelled. That is,
  381. * either 0 or 1.
  382. *
  383. * @throws boost::system::system_error Thrown on failure.
  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()
  396. {
  397. boost::system::error_code ec;
  398. std::size_t s = impl_.get_service().cancel_one(
  399. impl_.get_implementation(), ec);
  400. boost::asio::detail::throw_error(ec, "cancel_one");
  401. return s;
  402. }
  403. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  404. /// (Deprecated: Use non-error_code overload.) Cancels one asynchronous
  405. /// operation that is waiting on the timer.
  406. /**
  407. * This function forces the completion of one pending asynchronous wait
  408. * operation against the timer. Handlers are cancelled in FIFO order. The
  409. * handler for the cancelled operation will be invoked with the
  410. * boost::asio::error::operation_aborted error code.
  411. *
  412. * Cancelling the timer does not change the expiry time.
  413. *
  414. * @param ec Set to indicate what error occurred, if any.
  415. *
  416. * @return The number of asynchronous operations that were cancelled. That is,
  417. * either 0 or 1.
  418. *
  419. * @note If the timer has already expired when cancel_one() is called, then
  420. * the handlers for asynchronous wait operations will:
  421. *
  422. * @li have already been invoked; or
  423. *
  424. * @li have been queued for invocation in the near future.
  425. *
  426. * These handlers can no longer be cancelled, and therefore are passed an
  427. * error code that indicates the successful completion of the wait operation.
  428. */
  429. std::size_t cancel_one(boost::system::error_code& ec)
  430. {
  431. return impl_.get_service().cancel_one(impl_.get_implementation(), ec);
  432. }
  433. /// (Deprecated: Use expiry().) Get the timer's expiry time as an absolute
  434. /// time.
  435. /**
  436. * This function may be used to obtain the timer's current expiry time.
  437. * Whether the timer has expired or not does not affect this value.
  438. */
  439. time_point expires_at() const
  440. {
  441. return impl_.get_service().expires_at(impl_.get_implementation());
  442. }
  443. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  444. /// Get the timer's expiry time as an absolute time.
  445. /**
  446. * This function may be used to obtain the timer's current expiry time.
  447. * Whether the timer has expired or not does not affect this value.
  448. */
  449. time_point expiry() const
  450. {
  451. return impl_.get_service().expiry(impl_.get_implementation());
  452. }
  453. /// Set the timer's expiry time as an absolute time.
  454. /**
  455. * This function sets the expiry time. Any pending asynchronous wait
  456. * operations will be cancelled. The handler for each cancelled operation will
  457. * be invoked with the boost::asio::error::operation_aborted error code.
  458. *
  459. * @param expiry_time The expiry time to be used for the timer.
  460. *
  461. * @return The number of asynchronous operations that were cancelled.
  462. *
  463. * @throws boost::system::system_error Thrown on failure.
  464. *
  465. * @note If the timer has already expired when expires_at() is called, then
  466. * the handlers for asynchronous wait operations will:
  467. *
  468. * @li have already been invoked; or
  469. *
  470. * @li have been queued for invocation in the near future.
  471. *
  472. * These handlers can no longer be cancelled, and therefore are passed an
  473. * error code that indicates the successful completion of the wait operation.
  474. */
  475. std::size_t expires_at(const time_point& expiry_time)
  476. {
  477. boost::system::error_code ec;
  478. std::size_t s = impl_.get_service().expires_at(
  479. impl_.get_implementation(), expiry_time, ec);
  480. boost::asio::detail::throw_error(ec, "expires_at");
  481. return s;
  482. }
  483. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  484. /// (Deprecated: Use non-error_code overload.) Set the timer's expiry time as
  485. /// an absolute time.
  486. /**
  487. * This function sets the expiry time. Any pending asynchronous wait
  488. * operations will be cancelled. The handler for each cancelled operation will
  489. * be invoked with the boost::asio::error::operation_aborted error code.
  490. *
  491. * @param expiry_time The expiry time to be used for the timer.
  492. *
  493. * @param ec Set to indicate what error occurred, if any.
  494. *
  495. * @return The number of asynchronous operations that were cancelled.
  496. *
  497. * @note If the timer has already expired when expires_at() is called, then
  498. * the handlers for asynchronous wait operations will:
  499. *
  500. * @li have already been invoked; or
  501. *
  502. * @li have been queued for invocation in the near future.
  503. *
  504. * These handlers can no longer be cancelled, and therefore are passed an
  505. * error code that indicates the successful completion of the wait operation.
  506. */
  507. std::size_t expires_at(const time_point& expiry_time,
  508. boost::system::error_code& ec)
  509. {
  510. return impl_.get_service().expires_at(
  511. impl_.get_implementation(), expiry_time, ec);
  512. }
  513. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  514. /// Set the timer's expiry time relative to now.
  515. /**
  516. * This function sets the expiry time. Any pending asynchronous wait
  517. * operations will be cancelled. The handler for each cancelled operation will
  518. * be invoked with the boost::asio::error::operation_aborted error code.
  519. *
  520. * @param expiry_time The expiry time to be used for the timer.
  521. *
  522. * @return The number of asynchronous operations that were cancelled.
  523. *
  524. * @throws boost::system::system_error Thrown on failure.
  525. *
  526. * @note If the timer has already expired when expires_after() is called,
  527. * then the handlers for asynchronous wait operations will:
  528. *
  529. * @li have already been invoked; or
  530. *
  531. * @li have been queued for invocation in the near future.
  532. *
  533. * These handlers can no longer be cancelled, and therefore are passed an
  534. * error code that indicates the successful completion of the wait operation.
  535. */
  536. std::size_t expires_after(const duration& expiry_time)
  537. {
  538. boost::system::error_code ec;
  539. std::size_t s = impl_.get_service().expires_after(
  540. impl_.get_implementation(), expiry_time, ec);
  541. boost::asio::detail::throw_error(ec, "expires_after");
  542. return s;
  543. }
  544. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  545. /// (Deprecated: Use expiry().) Get the timer's expiry time relative to now.
  546. /**
  547. * This function may be used to obtain the timer's current expiry time.
  548. * Whether the timer has expired or not does not affect this value.
  549. */
  550. duration expires_from_now() const
  551. {
  552. return impl_.get_service().expires_from_now(impl_.get_implementation());
  553. }
  554. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  555. /// to now.
  556. /**
  557. * This function sets the expiry time. Any pending asynchronous wait
  558. * operations will be cancelled. The handler for each cancelled operation will
  559. * be invoked with the boost::asio::error::operation_aborted error code.
  560. *
  561. * @param expiry_time The expiry time to be used for the timer.
  562. *
  563. * @return The number of asynchronous operations that were cancelled.
  564. *
  565. * @throws boost::system::system_error Thrown on failure.
  566. *
  567. * @note If the timer has already expired when expires_from_now() is called,
  568. * then the handlers for asynchronous wait operations will:
  569. *
  570. * @li have already been invoked; or
  571. *
  572. * @li have been queued for invocation in the near future.
  573. *
  574. * These handlers can no longer be cancelled, and therefore are passed an
  575. * error code that indicates the successful completion of the wait operation.
  576. */
  577. std::size_t expires_from_now(const duration& expiry_time)
  578. {
  579. boost::system::error_code ec;
  580. std::size_t s = impl_.get_service().expires_from_now(
  581. impl_.get_implementation(), expiry_time, ec);
  582. boost::asio::detail::throw_error(ec, "expires_from_now");
  583. return s;
  584. }
  585. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  586. /// to now.
  587. /**
  588. * This function sets the expiry time. Any pending asynchronous wait
  589. * operations will be cancelled. The handler for each cancelled operation will
  590. * be invoked with the boost::asio::error::operation_aborted error code.
  591. *
  592. * @param expiry_time The expiry time to be used for the timer.
  593. *
  594. * @param ec Set to indicate what error occurred, if any.
  595. *
  596. * @return The number of asynchronous operations that were cancelled.
  597. *
  598. * @note If the timer has already expired when expires_from_now() is called,
  599. * then the handlers for asynchronous wait operations will:
  600. *
  601. * @li have already been invoked; or
  602. *
  603. * @li have been queued for invocation in the near future.
  604. *
  605. * These handlers can no longer be cancelled, and therefore are passed an
  606. * error code that indicates the successful completion of the wait operation.
  607. */
  608. std::size_t expires_from_now(const duration& expiry_time,
  609. boost::system::error_code& ec)
  610. {
  611. return impl_.get_service().expires_from_now(
  612. impl_.get_implementation(), expiry_time, ec);
  613. }
  614. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  615. /// Perform a blocking wait on the timer.
  616. /**
  617. * This function is used to wait for the timer to expire. This function
  618. * blocks and does not return until the timer has expired.
  619. *
  620. * @throws boost::system::system_error Thrown on failure.
  621. */
  622. void wait()
  623. {
  624. boost::system::error_code ec;
  625. impl_.get_service().wait(impl_.get_implementation(), ec);
  626. boost::asio::detail::throw_error(ec, "wait");
  627. }
  628. /// Perform a blocking wait on the timer.
  629. /**
  630. * This function is used to wait for the timer to expire. This function
  631. * blocks and does not return until the timer has expired.
  632. *
  633. * @param ec Set to indicate what error occurred, if any.
  634. */
  635. void wait(boost::system::error_code& ec)
  636. {
  637. impl_.get_service().wait(impl_.get_implementation(), ec);
  638. }
  639. /// Start an asynchronous wait on the timer.
  640. /**
  641. * This function may be used to initiate an asynchronous wait against the
  642. * timer. It always returns immediately.
  643. *
  644. * For each call to async_wait(), the supplied handler will be called exactly
  645. * once. The handler will be called when:
  646. *
  647. * @li The timer has expired.
  648. *
  649. * @li The timer was cancelled, in which case the handler is passed the error
  650. * code boost::asio::error::operation_aborted.
  651. *
  652. * @param handler The handler to be called when the timer expires. Copies
  653. * will be made of the handler as required. The function signature of the
  654. * handler must be:
  655. * @code void handler(
  656. * const boost::system::error_code& error // Result of operation.
  657. * ); @endcode
  658. * Regardless of whether the asynchronous operation completes immediately or
  659. * not, the handler will not be invoked from within this function. On
  660. * immediate completion, invocation of the handler will be performed in a
  661. * manner equivalent to using boost::asio::post().
  662. */
  663. template <
  664. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  665. WaitHandler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  666. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WaitHandler,
  667. void (boost::system::error_code))
  668. async_wait(
  669. BOOST_ASIO_MOVE_ARG(WaitHandler) handler
  670. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  671. {
  672. return async_initiate<WaitHandler, void (boost::system::error_code)>(
  673. initiate_async_wait(this), handler);
  674. }
  675. private:
  676. // Disallow copying and assignment.
  677. basic_waitable_timer(const basic_waitable_timer&) BOOST_ASIO_DELETED;
  678. basic_waitable_timer& operator=(
  679. const basic_waitable_timer&) BOOST_ASIO_DELETED;
  680. class initiate_async_wait
  681. {
  682. public:
  683. typedef Executor executor_type;
  684. explicit initiate_async_wait(basic_waitable_timer* self)
  685. : self_(self)
  686. {
  687. }
  688. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  689. {
  690. return self_->get_executor();
  691. }
  692. template <typename WaitHandler>
  693. void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler) const
  694. {
  695. // If you get an error on the following line it means that your handler
  696. // does not meet the documented type requirements for a WaitHandler.
  697. BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  698. detail::non_const_lvalue<WaitHandler> handler2(handler);
  699. self_->impl_.get_service().async_wait(
  700. self_->impl_.get_implementation(), handler2.value,
  701. self_->impl_.get_implementation_executor());
  702. }
  703. private:
  704. basic_waitable_timer* self_;
  705. };
  706. detail::io_object_impl<
  707. detail::deadline_timer_service<
  708. detail::chrono_time_traits<Clock, WaitTraits> >,
  709. executor_type > impl_;
  710. };
  711. } // namespace asio
  712. } // namespace boost
  713. #include <boost/asio/detail/pop_options.hpp>
  714. #endif // BOOST_ASIO_BASIC_WAITABLE_TIMER_HPP