basic_waitable_timer.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. /// The clock type.
  139. typedef Clock clock_type;
  140. /// The duration type of the clock.
  141. typedef typename clock_type::duration duration;
  142. /// The time point type of the clock.
  143. typedef typename clock_type::time_point time_point;
  144. /// The wait traits type.
  145. typedef WaitTraits traits_type;
  146. /// Constructor.
  147. /**
  148. * This constructor creates a timer without setting an expiry time. The
  149. * expires_at() or expires_after() functions must be called to set an expiry
  150. * time before the timer can be waited on.
  151. *
  152. * @param ex The I/O executor that the timer will use, by default, to
  153. * dispatch handlers for any asynchronous operations performed on the timer.
  154. */
  155. explicit basic_waitable_timer(const executor_type& ex)
  156. : impl_(ex)
  157. {
  158. }
  159. /// Constructor.
  160. /**
  161. * This constructor creates a timer without setting an expiry time. The
  162. * expires_at() or expires_after() functions must be called to set an expiry
  163. * time before the timer can be waited on.
  164. *
  165. * @param context An execution context which provides the I/O executor that
  166. * the timer will use, by default, to dispatch handlers for any asynchronous
  167. * operations performed on the timer.
  168. */
  169. template <typename ExecutionContext>
  170. explicit basic_waitable_timer(ExecutionContext& context,
  171. typename enable_if<
  172. is_convertible<ExecutionContext&, execution_context&>::value
  173. >::type* = 0)
  174. : impl_(context)
  175. {
  176. }
  177. /// Constructor to set a particular expiry time as an absolute time.
  178. /**
  179. * This constructor creates a timer and sets the expiry time.
  180. *
  181. * @param ex The I/O executor object that the timer will use, by default, to
  182. * dispatch handlers for any asynchronous operations performed on the timer.
  183. *
  184. * @param expiry_time The expiry time to be used for the timer, expressed
  185. * as an absolute time.
  186. */
  187. basic_waitable_timer(const executor_type& ex, const time_point& expiry_time)
  188. : impl_(ex)
  189. {
  190. boost::system::error_code ec;
  191. impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
  192. boost::asio::detail::throw_error(ec, "expires_at");
  193. }
  194. /// Constructor to set a particular expiry time as an absolute time.
  195. /**
  196. * This constructor creates a timer and sets the expiry time.
  197. *
  198. * @param context An execution context which provides the I/O executor that
  199. * the timer will use, by default, to dispatch handlers for any asynchronous
  200. * operations performed on the timer.
  201. *
  202. * @param expiry_time The expiry time to be used for the timer, expressed
  203. * as an absolute time.
  204. */
  205. template <typename ExecutionContext>
  206. explicit basic_waitable_timer(ExecutionContext& context,
  207. const time_point& expiry_time,
  208. typename enable_if<
  209. is_convertible<ExecutionContext&, execution_context&>::value
  210. >::type* = 0)
  211. : impl_(context)
  212. {
  213. boost::system::error_code ec;
  214. impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
  215. boost::asio::detail::throw_error(ec, "expires_at");
  216. }
  217. /// Constructor to set a particular expiry time relative to now.
  218. /**
  219. * This constructor creates a timer and sets the expiry time.
  220. *
  221. * @param ex The I/O executor that the timer will use, by default, to
  222. * dispatch handlers for any asynchronous operations performed on the timer.
  223. *
  224. * @param expiry_time The expiry time to be used for the timer, relative to
  225. * now.
  226. */
  227. basic_waitable_timer(const executor_type& ex, const duration& expiry_time)
  228. : impl_(ex)
  229. {
  230. boost::system::error_code ec;
  231. impl_.get_service().expires_after(
  232. impl_.get_implementation(), expiry_time, ec);
  233. boost::asio::detail::throw_error(ec, "expires_after");
  234. }
  235. /// Constructor to set a particular expiry time relative to now.
  236. /**
  237. * This constructor creates a timer and sets the expiry time.
  238. *
  239. * @param context An execution context which provides the I/O executor that
  240. * the timer will use, by default, to dispatch handlers for any asynchronous
  241. * operations performed on the timer.
  242. *
  243. * @param expiry_time The expiry time to be used for the timer, relative to
  244. * now.
  245. */
  246. template <typename ExecutionContext>
  247. explicit basic_waitable_timer(ExecutionContext& context,
  248. const duration& expiry_time,
  249. typename enable_if<
  250. is_convertible<ExecutionContext&, execution_context&>::value
  251. >::type* = 0)
  252. : impl_(context)
  253. {
  254. boost::system::error_code ec;
  255. impl_.get_service().expires_after(
  256. impl_.get_implementation(), expiry_time, ec);
  257. boost::asio::detail::throw_error(ec, "expires_after");
  258. }
  259. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  260. /// Move-construct a basic_waitable_timer from another.
  261. /**
  262. * This constructor moves a timer from one object to another.
  263. *
  264. * @param other The other basic_waitable_timer object from which the move will
  265. * occur.
  266. *
  267. * @note Following the move, the moved-from object is in the same state as if
  268. * constructed using the @c basic_waitable_timer(const executor_type&)
  269. * constructor.
  270. */
  271. basic_waitable_timer(basic_waitable_timer&& other)
  272. : impl_(std::move(other.impl_))
  273. {
  274. }
  275. /// Move-assign a basic_waitable_timer from another.
  276. /**
  277. * This assignment operator moves a timer from one object to another. Cancels
  278. * any outstanding asynchronous operations associated with the target object.
  279. *
  280. * @param other The other basic_waitable_timer object from which the move will
  281. * occur.
  282. *
  283. * @note Following the move, the moved-from object is in the same state as if
  284. * constructed using the @c basic_waitable_timer(const executor_type&)
  285. * constructor.
  286. */
  287. basic_waitable_timer& operator=(basic_waitable_timer&& other)
  288. {
  289. impl_ = std::move(other.impl_);
  290. return *this;
  291. }
  292. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  293. /// Destroys the timer.
  294. /**
  295. * This function destroys the timer, cancelling any outstanding asynchronous
  296. * wait operations associated with the timer as if by calling @c cancel.
  297. */
  298. ~basic_waitable_timer()
  299. {
  300. }
  301. /// Get the executor associated with the object.
  302. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  303. {
  304. return impl_.get_executor();
  305. }
  306. /// Cancel any asynchronous operations that are waiting on the timer.
  307. /**
  308. * This function forces the completion of any pending asynchronous wait
  309. * operations against the timer. The handler for each cancelled operation will
  310. * be invoked with the boost::asio::error::operation_aborted error code.
  311. *
  312. * Cancelling the timer does not change the expiry time.
  313. *
  314. * @return The number of asynchronous operations that were cancelled.
  315. *
  316. * @throws boost::system::system_error Thrown on failure.
  317. *
  318. * @note If the timer has already expired when cancel() is called, then the
  319. * handlers for asynchronous wait operations will:
  320. *
  321. * @li have already been invoked; or
  322. *
  323. * @li have been queued for invocation in the near future.
  324. *
  325. * These handlers can no longer be cancelled, and therefore are passed an
  326. * error code that indicates the successful completion of the wait operation.
  327. */
  328. std::size_t cancel()
  329. {
  330. boost::system::error_code ec;
  331. std::size_t s = impl_.get_service().cancel(impl_.get_implementation(), ec);
  332. boost::asio::detail::throw_error(ec, "cancel");
  333. return s;
  334. }
  335. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  336. /// (Deprecated: Use non-error_code overload.) Cancel any asynchronous
  337. /// operations that are waiting on the timer.
  338. /**
  339. * This function forces the completion of any pending asynchronous wait
  340. * operations against the timer. The handler for each cancelled operation will
  341. * be invoked with the boost::asio::error::operation_aborted error code.
  342. *
  343. * Cancelling the timer does not change the expiry time.
  344. *
  345. * @param ec Set to indicate what error occurred, if any.
  346. *
  347. * @return The number of asynchronous operations that were cancelled.
  348. *
  349. * @note If the timer has already expired when cancel() is called, then the
  350. * handlers for asynchronous wait operations will:
  351. *
  352. * @li have already been invoked; or
  353. *
  354. * @li have been queued for invocation in the near future.
  355. *
  356. * These handlers can no longer be cancelled, and therefore are passed an
  357. * error code that indicates the successful completion of the wait operation.
  358. */
  359. std::size_t cancel(boost::system::error_code& ec)
  360. {
  361. return impl_.get_service().cancel(impl_.get_implementation(), ec);
  362. }
  363. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  364. /// Cancels one asynchronous operation that is waiting on the timer.
  365. /**
  366. * This function forces the completion of one pending asynchronous wait
  367. * operation against the timer. Handlers are cancelled in FIFO order. The
  368. * handler for the cancelled operation will be invoked with the
  369. * boost::asio::error::operation_aborted error code.
  370. *
  371. * Cancelling the timer does not change the expiry time.
  372. *
  373. * @return The number of asynchronous operations that were cancelled. That is,
  374. * either 0 or 1.
  375. *
  376. * @throws boost::system::system_error Thrown on failure.
  377. *
  378. * @note If the timer has already expired when cancel_one() is called, then
  379. * the handlers for asynchronous wait operations will:
  380. *
  381. * @li have already been invoked; or
  382. *
  383. * @li have been queued for invocation in the near future.
  384. *
  385. * These handlers can no longer be cancelled, and therefore are passed an
  386. * error code that indicates the successful completion of the wait operation.
  387. */
  388. std::size_t cancel_one()
  389. {
  390. boost::system::error_code ec;
  391. std::size_t s = impl_.get_service().cancel_one(
  392. impl_.get_implementation(), ec);
  393. boost::asio::detail::throw_error(ec, "cancel_one");
  394. return s;
  395. }
  396. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  397. /// (Deprecated: Use non-error_code overload.) Cancels one asynchronous
  398. /// operation that is waiting on the timer.
  399. /**
  400. * This function forces the completion of one pending asynchronous wait
  401. * operation against the timer. Handlers are cancelled in FIFO order. The
  402. * handler for the cancelled operation will be invoked with the
  403. * boost::asio::error::operation_aborted error code.
  404. *
  405. * Cancelling the timer does not change the expiry time.
  406. *
  407. * @param ec Set to indicate what error occurred, if any.
  408. *
  409. * @return The number of asynchronous operations that were cancelled. That is,
  410. * either 0 or 1.
  411. *
  412. * @note If the timer has already expired when cancel_one() is called, then
  413. * the handlers for asynchronous wait operations will:
  414. *
  415. * @li have already been invoked; or
  416. *
  417. * @li have been queued for invocation in the near future.
  418. *
  419. * These handlers can no longer be cancelled, and therefore are passed an
  420. * error code that indicates the successful completion of the wait operation.
  421. */
  422. std::size_t cancel_one(boost::system::error_code& ec)
  423. {
  424. return impl_.get_service().cancel_one(impl_.get_implementation(), ec);
  425. }
  426. /// (Deprecated: Use expiry().) Get the timer's expiry time as an absolute
  427. /// time.
  428. /**
  429. * This function may be used to obtain the timer's current expiry time.
  430. * Whether the timer has expired or not does not affect this value.
  431. */
  432. time_point expires_at() const
  433. {
  434. return impl_.get_service().expires_at(impl_.get_implementation());
  435. }
  436. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  437. /// Get the timer's expiry time as an absolute time.
  438. /**
  439. * This function may be used to obtain the timer's current expiry time.
  440. * Whether the timer has expired or not does not affect this value.
  441. */
  442. time_point expiry() const
  443. {
  444. return impl_.get_service().expiry(impl_.get_implementation());
  445. }
  446. /// Set the timer's expiry time as an absolute time.
  447. /**
  448. * This function sets the expiry time. Any pending asynchronous wait
  449. * operations will be cancelled. The handler for each cancelled operation will
  450. * be invoked with the boost::asio::error::operation_aborted error code.
  451. *
  452. * @param expiry_time The expiry time to be used for the timer.
  453. *
  454. * @return The number of asynchronous operations that were cancelled.
  455. *
  456. * @throws boost::system::system_error Thrown on failure.
  457. *
  458. * @note If the timer has already expired when expires_at() is called, then
  459. * the handlers for asynchronous wait operations will:
  460. *
  461. * @li have already been invoked; or
  462. *
  463. * @li have been queued for invocation in the near future.
  464. *
  465. * These handlers can no longer be cancelled, and therefore are passed an
  466. * error code that indicates the successful completion of the wait operation.
  467. */
  468. std::size_t expires_at(const time_point& expiry_time)
  469. {
  470. boost::system::error_code ec;
  471. std::size_t s = impl_.get_service().expires_at(
  472. impl_.get_implementation(), expiry_time, ec);
  473. boost::asio::detail::throw_error(ec, "expires_at");
  474. return s;
  475. }
  476. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  477. /// (Deprecated: Use non-error_code overload.) Set the timer's expiry time as
  478. /// an absolute time.
  479. /**
  480. * This function sets the expiry time. Any pending asynchronous wait
  481. * operations will be cancelled. The handler for each cancelled operation will
  482. * be invoked with the boost::asio::error::operation_aborted error code.
  483. *
  484. * @param expiry_time The expiry time to be used for the timer.
  485. *
  486. * @param ec Set to indicate what error occurred, if any.
  487. *
  488. * @return The number of asynchronous operations that were cancelled.
  489. *
  490. * @note If the timer has already expired when expires_at() is called, then
  491. * the handlers for asynchronous wait operations will:
  492. *
  493. * @li have already been invoked; or
  494. *
  495. * @li have been queued for invocation in the near future.
  496. *
  497. * These handlers can no longer be cancelled, and therefore are passed an
  498. * error code that indicates the successful completion of the wait operation.
  499. */
  500. std::size_t expires_at(const time_point& expiry_time,
  501. boost::system::error_code& ec)
  502. {
  503. return impl_.get_service().expires_at(
  504. impl_.get_implementation(), expiry_time, ec);
  505. }
  506. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  507. /// Set the timer's expiry time relative to now.
  508. /**
  509. * This function sets the expiry time. Any pending asynchronous wait
  510. * operations will be cancelled. The handler for each cancelled operation will
  511. * be invoked with the boost::asio::error::operation_aborted error code.
  512. *
  513. * @param expiry_time The expiry time to be used for the timer.
  514. *
  515. * @return The number of asynchronous operations that were cancelled.
  516. *
  517. * @throws boost::system::system_error Thrown on failure.
  518. *
  519. * @note If the timer has already expired when expires_after() is called,
  520. * then the handlers for asynchronous wait operations will:
  521. *
  522. * @li have already been invoked; or
  523. *
  524. * @li have been queued for invocation in the near future.
  525. *
  526. * These handlers can no longer be cancelled, and therefore are passed an
  527. * error code that indicates the successful completion of the wait operation.
  528. */
  529. std::size_t expires_after(const duration& expiry_time)
  530. {
  531. boost::system::error_code ec;
  532. std::size_t s = impl_.get_service().expires_after(
  533. impl_.get_implementation(), expiry_time, ec);
  534. boost::asio::detail::throw_error(ec, "expires_after");
  535. return s;
  536. }
  537. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  538. /// (Deprecated: Use expiry().) Get the timer's expiry time relative to now.
  539. /**
  540. * This function may be used to obtain the timer's current expiry time.
  541. * Whether the timer has expired or not does not affect this value.
  542. */
  543. duration expires_from_now() const
  544. {
  545. return impl_.get_service().expires_from_now(impl_.get_implementation());
  546. }
  547. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  548. /// to now.
  549. /**
  550. * This function sets the expiry time. Any pending asynchronous wait
  551. * operations will be cancelled. The handler for each cancelled operation will
  552. * be invoked with the boost::asio::error::operation_aborted error code.
  553. *
  554. * @param expiry_time The expiry time to be used for the timer.
  555. *
  556. * @return The number of asynchronous operations that were cancelled.
  557. *
  558. * @throws boost::system::system_error Thrown on failure.
  559. *
  560. * @note If the timer has already expired when expires_from_now() is called,
  561. * then the handlers for asynchronous wait operations will:
  562. *
  563. * @li have already been invoked; or
  564. *
  565. * @li have been queued for invocation in the near future.
  566. *
  567. * These handlers can no longer be cancelled, and therefore are passed an
  568. * error code that indicates the successful completion of the wait operation.
  569. */
  570. std::size_t expires_from_now(const duration& expiry_time)
  571. {
  572. boost::system::error_code ec;
  573. std::size_t s = impl_.get_service().expires_from_now(
  574. impl_.get_implementation(), expiry_time, ec);
  575. boost::asio::detail::throw_error(ec, "expires_from_now");
  576. return s;
  577. }
  578. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  579. /// to now.
  580. /**
  581. * This function sets the expiry time. Any pending asynchronous wait
  582. * operations will be cancelled. The handler for each cancelled operation will
  583. * be invoked with the boost::asio::error::operation_aborted error code.
  584. *
  585. * @param expiry_time The expiry time to be used for the timer.
  586. *
  587. * @param ec Set to indicate what error occurred, if any.
  588. *
  589. * @return The number of asynchronous operations that were cancelled.
  590. *
  591. * @note If the timer has already expired when expires_from_now() is called,
  592. * then the handlers for asynchronous wait operations will:
  593. *
  594. * @li have already been invoked; or
  595. *
  596. * @li have been queued for invocation in the near future.
  597. *
  598. * These handlers can no longer be cancelled, and therefore are passed an
  599. * error code that indicates the successful completion of the wait operation.
  600. */
  601. std::size_t expires_from_now(const duration& expiry_time,
  602. boost::system::error_code& ec)
  603. {
  604. return impl_.get_service().expires_from_now(
  605. impl_.get_implementation(), expiry_time, ec);
  606. }
  607. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  608. /// Perform a blocking wait on the timer.
  609. /**
  610. * This function is used to wait for the timer to expire. This function
  611. * blocks and does not return until the timer has expired.
  612. *
  613. * @throws boost::system::system_error Thrown on failure.
  614. */
  615. void wait()
  616. {
  617. boost::system::error_code ec;
  618. impl_.get_service().wait(impl_.get_implementation(), ec);
  619. boost::asio::detail::throw_error(ec, "wait");
  620. }
  621. /// Perform a blocking wait on the timer.
  622. /**
  623. * This function is used to wait for the timer to expire. This function
  624. * blocks and does not return until the timer has expired.
  625. *
  626. * @param ec Set to indicate what error occurred, if any.
  627. */
  628. void wait(boost::system::error_code& ec)
  629. {
  630. impl_.get_service().wait(impl_.get_implementation(), ec);
  631. }
  632. /// Start an asynchronous wait on the timer.
  633. /**
  634. * This function may be used to initiate an asynchronous wait against the
  635. * timer. It always returns immediately.
  636. *
  637. * For each call to async_wait(), the supplied handler will be called exactly
  638. * once. The handler will be called when:
  639. *
  640. * @li The timer has expired.
  641. *
  642. * @li The timer was cancelled, in which case the handler is passed the error
  643. * code boost::asio::error::operation_aborted.
  644. *
  645. * @param handler The handler to be called when the timer expires. Copies
  646. * will be made of the handler as required. The function signature of the
  647. * handler must be:
  648. * @code void handler(
  649. * const boost::system::error_code& error // Result of operation.
  650. * ); @endcode
  651. * Regardless of whether the asynchronous operation completes immediately or
  652. * not, the handler will not be invoked from within this function. On
  653. * immediate completion, invocation of the handler will be performed in a
  654. * manner equivalent to using boost::asio::post().
  655. */
  656. template <typename WaitHandler>
  657. BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
  658. void (boost::system::error_code))
  659. async_wait(BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
  660. {
  661. return async_initiate<WaitHandler, void (boost::system::error_code)>(
  662. initiate_async_wait(), handler, this);
  663. }
  664. private:
  665. // Disallow copying and assignment.
  666. basic_waitable_timer(const basic_waitable_timer&) BOOST_ASIO_DELETED;
  667. basic_waitable_timer& operator=(
  668. const basic_waitable_timer&) BOOST_ASIO_DELETED;
  669. struct initiate_async_wait
  670. {
  671. template <typename WaitHandler>
  672. void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler,
  673. basic_waitable_timer* self) const
  674. {
  675. // If you get an error on the following line it means that your handler
  676. // does not meet the documented type requirements for a WaitHandler.
  677. BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  678. detail::non_const_lvalue<WaitHandler> handler2(handler);
  679. self->impl_.get_service().async_wait(
  680. self->impl_.get_implementation(), handler2.value,
  681. self->impl_.get_implementation_executor());
  682. }
  683. };
  684. detail::io_object_impl<
  685. detail::deadline_timer_service<
  686. detail::chrono_time_traits<Clock, WaitTraits> >,
  687. executor_type > impl_;
  688. };
  689. } // namespace asio
  690. } // namespace boost
  691. #include <boost/asio/detail/pop_options.hpp>
  692. #endif // BOOST_ASIO_BASIC_WAITABLE_TIMER_HPP