basic_waitable_timer.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. //
  2. // basic_waitable_timer.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_BASIC_WAITABLE_TIMER_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include "asio/basic_io_object.hpp"
  18. #include "asio/detail/handler_type_requirements.hpp"
  19. #include "asio/detail/throw_error.hpp"
  20. #include "asio/error.hpp"
  21. #include "asio/wait_traits.hpp"
  22. #include "asio/waitable_timer_service.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. /// Provides waitable timer functionality.
  26. /**
  27. * The basic_waitable_timer class template provides the ability to perform a
  28. * blocking or asynchronous wait for a timer to expire.
  29. *
  30. * A waitable timer is always in one of two states: "expired" or "not expired".
  31. * If the wait() or async_wait() function is called on an expired timer, the
  32. * wait operation will complete immediately.
  33. *
  34. * Most applications will use one of the asio::steady_timer,
  35. * asio::system_timer or asio::high_resolution_timer typedefs.
  36. *
  37. * @note This waitable timer functionality is for use with the C++11 standard
  38. * library's @c &lt;chrono&gt; facility, or with the Boost.Chrono library.
  39. *
  40. * @par Thread Safety
  41. * @e Distinct @e objects: Safe.@n
  42. * @e Shared @e objects: Unsafe.
  43. *
  44. * @par Examples
  45. * Performing a blocking wait (C++11):
  46. * @code
  47. * // Construct a timer without setting an expiry time.
  48. * asio::steady_timer timer(io_service);
  49. *
  50. * // Set an expiry time relative to now.
  51. * timer.expires_from_now(std::chrono::seconds(5));
  52. *
  53. * // Wait for the timer to expire.
  54. * timer.wait();
  55. * @endcode
  56. *
  57. * @par
  58. * Performing an asynchronous wait (C++11):
  59. * @code
  60. * void handler(const asio::error_code& error)
  61. * {
  62. * if (!error)
  63. * {
  64. * // Timer expired.
  65. * }
  66. * }
  67. *
  68. * ...
  69. *
  70. * // Construct a timer with an absolute expiry time.
  71. * asio::steady_timer timer(io_service,
  72. * std::chrono::steady_clock::now() + std::chrono::seconds(60));
  73. *
  74. * // Start an asynchronous wait.
  75. * timer.async_wait(handler);
  76. * @endcode
  77. *
  78. * @par Changing an active waitable timer's expiry time
  79. *
  80. * Changing the expiry time of a timer while there are pending asynchronous
  81. * waits causes those wait operations to be cancelled. To ensure that the action
  82. * associated with the timer is performed only once, use something like this:
  83. * used:
  84. *
  85. * @code
  86. * void on_some_event()
  87. * {
  88. * if (my_timer.expires_from_now(seconds(5)) > 0)
  89. * {
  90. * // We managed to cancel the timer. Start new asynchronous wait.
  91. * my_timer.async_wait(on_timeout);
  92. * }
  93. * else
  94. * {
  95. * // Too late, timer has already expired!
  96. * }
  97. * }
  98. *
  99. * void on_timeout(const asio::error_code& e)
  100. * {
  101. * if (e != asio::error::operation_aborted)
  102. * {
  103. * // Timer was not cancelled, take necessary action.
  104. * }
  105. * }
  106. * @endcode
  107. *
  108. * @li The asio::basic_waitable_timer::expires_from_now() function
  109. * cancels any pending asynchronous waits, and returns the number of
  110. * asynchronous waits that were cancelled. If it returns 0 then you were too
  111. * late and the wait handler has already been executed, or will soon be
  112. * executed. If it returns 1 then the wait handler was successfully cancelled.
  113. *
  114. * @li If a wait handler is cancelled, the asio::error_code passed to
  115. * it contains the value asio::error::operation_aborted.
  116. */
  117. template <typename Clock,
  118. typename WaitTraits = asio::wait_traits<Clock>,
  119. typename WaitableTimerService = waitable_timer_service<Clock, WaitTraits> >
  120. class basic_waitable_timer
  121. : public basic_io_object<WaitableTimerService>
  122. {
  123. public:
  124. /// The clock type.
  125. typedef Clock clock_type;
  126. /// The duration type of the clock.
  127. typedef typename clock_type::duration duration;
  128. /// The time point type of the clock.
  129. typedef typename clock_type::time_point time_point;
  130. /// The wait traits type.
  131. typedef WaitTraits traits_type;
  132. /// Constructor.
  133. /**
  134. * This constructor creates a timer without setting an expiry time. The
  135. * expires_at() or expires_from_now() functions must be called to set an
  136. * expiry time before the timer can be waited on.
  137. *
  138. * @param io_service The io_service object that the timer will use to dispatch
  139. * handlers for any asynchronous operations performed on the timer.
  140. */
  141. explicit basic_waitable_timer(asio::io_service& io_service)
  142. : basic_io_object<WaitableTimerService>(io_service)
  143. {
  144. }
  145. /// Constructor to set a particular expiry time as an absolute time.
  146. /**
  147. * This constructor creates a timer and sets the expiry time.
  148. *
  149. * @param io_service The io_service object that the timer will use to dispatch
  150. * handlers for any asynchronous operations performed on the timer.
  151. *
  152. * @param expiry_time The expiry time to be used for the timer, expressed
  153. * as an absolute time.
  154. */
  155. basic_waitable_timer(asio::io_service& io_service,
  156. const time_point& expiry_time)
  157. : basic_io_object<WaitableTimerService>(io_service)
  158. {
  159. asio::error_code ec;
  160. this->service.expires_at(this->implementation, expiry_time, ec);
  161. asio::detail::throw_error(ec, "expires_at");
  162. }
  163. /// Constructor to set a particular expiry time relative to now.
  164. /**
  165. * This constructor creates a timer and sets the expiry time.
  166. *
  167. * @param io_service The io_service object that the timer will use to dispatch
  168. * handlers for any asynchronous operations performed on the timer.
  169. *
  170. * @param expiry_time The expiry time to be used for the timer, relative to
  171. * now.
  172. */
  173. basic_waitable_timer(asio::io_service& io_service,
  174. const duration& expiry_time)
  175. : basic_io_object<WaitableTimerService>(io_service)
  176. {
  177. asio::error_code ec;
  178. this->service.expires_from_now(this->implementation, expiry_time, ec);
  179. asio::detail::throw_error(ec, "expires_from_now");
  180. }
  181. /// Cancel any asynchronous operations that are waiting on the timer.
  182. /**
  183. * This function forces the completion of any pending asynchronous wait
  184. * operations against the timer. The handler for each cancelled operation will
  185. * be invoked with the asio::error::operation_aborted error code.
  186. *
  187. * Cancelling the timer does not change the expiry time.
  188. *
  189. * @return The number of asynchronous operations that were cancelled.
  190. *
  191. * @throws asio::system_error Thrown on failure.
  192. *
  193. * @note If the timer has already expired when cancel() is called, then the
  194. * handlers for asynchronous wait operations will:
  195. *
  196. * @li have already been invoked; or
  197. *
  198. * @li have been queued for invocation in the near future.
  199. *
  200. * These handlers can no longer be cancelled, and therefore are passed an
  201. * error code that indicates the successful completion of the wait operation.
  202. */
  203. std::size_t cancel()
  204. {
  205. asio::error_code ec;
  206. std::size_t s = this->service.cancel(this->implementation, ec);
  207. asio::detail::throw_error(ec, "cancel");
  208. return s;
  209. }
  210. /// Cancel any asynchronous operations that are waiting on the timer.
  211. /**
  212. * This function forces the completion of any pending asynchronous wait
  213. * operations against the timer. The handler for each cancelled operation will
  214. * be invoked with the asio::error::operation_aborted error code.
  215. *
  216. * Cancelling the timer does not change the expiry time.
  217. *
  218. * @param ec Set to indicate what error occurred, if any.
  219. *
  220. * @return The number of asynchronous operations that were cancelled.
  221. *
  222. * @note If the timer has already expired when cancel() is called, then the
  223. * handlers for asynchronous wait operations will:
  224. *
  225. * @li have already been invoked; or
  226. *
  227. * @li have been queued for invocation in the near future.
  228. *
  229. * These handlers can no longer be cancelled, and therefore are passed an
  230. * error code that indicates the successful completion of the wait operation.
  231. */
  232. std::size_t cancel(asio::error_code& ec)
  233. {
  234. return this->service.cancel(this->implementation, ec);
  235. }
  236. /// Cancels one asynchronous operation that is waiting on the timer.
  237. /**
  238. * This function forces the completion of one pending asynchronous wait
  239. * operation against the timer. Handlers are cancelled in FIFO order. The
  240. * handler for the cancelled operation will be invoked with the
  241. * asio::error::operation_aborted error code.
  242. *
  243. * Cancelling the timer does not change the expiry time.
  244. *
  245. * @return The number of asynchronous operations that were cancelled. That is,
  246. * either 0 or 1.
  247. *
  248. * @throws asio::system_error Thrown on failure.
  249. *
  250. * @note If the timer has already expired when cancel_one() is called, then
  251. * the handlers for asynchronous wait operations will:
  252. *
  253. * @li have already been invoked; or
  254. *
  255. * @li have been queued for invocation in the near future.
  256. *
  257. * These handlers can no longer be cancelled, and therefore are passed an
  258. * error code that indicates the successful completion of the wait operation.
  259. */
  260. std::size_t cancel_one()
  261. {
  262. asio::error_code ec;
  263. std::size_t s = this->service.cancel_one(this->implementation, ec);
  264. asio::detail::throw_error(ec, "cancel_one");
  265. return s;
  266. }
  267. /// Cancels one asynchronous operation that is waiting on the timer.
  268. /**
  269. * This function forces the completion of one pending asynchronous wait
  270. * operation against the timer. Handlers are cancelled in FIFO order. The
  271. * handler for the cancelled operation will be invoked with the
  272. * asio::error::operation_aborted error code.
  273. *
  274. * Cancelling the timer does not change the expiry time.
  275. *
  276. * @param ec Set to indicate what error occurred, if any.
  277. *
  278. * @return The number of asynchronous operations that were cancelled. That is,
  279. * either 0 or 1.
  280. *
  281. * @note If the timer has already expired when cancel_one() is called, then
  282. * the handlers for asynchronous wait operations will:
  283. *
  284. * @li have already been invoked; or
  285. *
  286. * @li have been queued for invocation in the near future.
  287. *
  288. * These handlers can no longer be cancelled, and therefore are passed an
  289. * error code that indicates the successful completion of the wait operation.
  290. */
  291. std::size_t cancel_one(asio::error_code& ec)
  292. {
  293. return this->service.cancel_one(this->implementation, ec);
  294. }
  295. /// Get the timer's expiry time as an absolute time.
  296. /**
  297. * This function may be used to obtain the timer's current expiry time.
  298. * Whether the timer has expired or not does not affect this value.
  299. */
  300. time_point expires_at() const
  301. {
  302. return this->service.expires_at(this->implementation);
  303. }
  304. /// Set the timer's expiry time as an absolute time.
  305. /**
  306. * This function sets the expiry time. Any pending asynchronous wait
  307. * operations will be cancelled. The handler for each cancelled operation will
  308. * be invoked with the asio::error::operation_aborted error code.
  309. *
  310. * @param expiry_time The expiry time to be used for the timer.
  311. *
  312. * @return The number of asynchronous operations that were cancelled.
  313. *
  314. * @throws asio::system_error Thrown on failure.
  315. *
  316. * @note If the timer has already expired when expires_at() is called, then
  317. * the handlers for asynchronous wait operations will:
  318. *
  319. * @li have already been invoked; or
  320. *
  321. * @li have been queued for invocation in the near future.
  322. *
  323. * These handlers can no longer be cancelled, and therefore are passed an
  324. * error code that indicates the successful completion of the wait operation.
  325. */
  326. std::size_t expires_at(const time_point& expiry_time)
  327. {
  328. asio::error_code ec;
  329. std::size_t s = this->service.expires_at(
  330. this->implementation, expiry_time, ec);
  331. asio::detail::throw_error(ec, "expires_at");
  332. return s;
  333. }
  334. /// Set the timer's expiry time as an absolute time.
  335. /**
  336. * This function sets the expiry time. Any pending asynchronous wait
  337. * operations will be cancelled. The handler for each cancelled operation will
  338. * be invoked with the asio::error::operation_aborted error code.
  339. *
  340. * @param expiry_time The expiry time to be used for the timer.
  341. *
  342. * @param ec Set to indicate what error occurred, if any.
  343. *
  344. * @return The number of asynchronous operations that were cancelled.
  345. *
  346. * @note If the timer has already expired when expires_at() is called, then
  347. * the handlers for asynchronous wait operations will:
  348. *
  349. * @li have already been invoked; or
  350. *
  351. * @li have been queued for invocation in the near future.
  352. *
  353. * These handlers can no longer be cancelled, and therefore are passed an
  354. * error code that indicates the successful completion of the wait operation.
  355. */
  356. std::size_t expires_at(const time_point& expiry_time,
  357. asio::error_code& ec)
  358. {
  359. return this->service.expires_at(this->implementation, expiry_time, ec);
  360. }
  361. /// Get the timer's expiry time relative to now.
  362. /**
  363. * This function may be used to obtain the timer's current expiry time.
  364. * Whether the timer has expired or not does not affect this value.
  365. */
  366. duration expires_from_now() const
  367. {
  368. return this->service.expires_from_now(this->implementation);
  369. }
  370. /// Set the timer's expiry time relative to now.
  371. /**
  372. * This function sets the expiry time. Any pending asynchronous wait
  373. * operations will be cancelled. The handler for each cancelled operation will
  374. * be invoked with the asio::error::operation_aborted error code.
  375. *
  376. * @param expiry_time The expiry time to be used for the timer.
  377. *
  378. * @return The number of asynchronous operations that were cancelled.
  379. *
  380. * @throws asio::system_error Thrown on failure.
  381. *
  382. * @note If the timer has already expired when expires_from_now() is called,
  383. * then the handlers for asynchronous wait operations will:
  384. *
  385. * @li have already been invoked; or
  386. *
  387. * @li have been queued for invocation in the near future.
  388. *
  389. * These handlers can no longer be cancelled, and therefore are passed an
  390. * error code that indicates the successful completion of the wait operation.
  391. */
  392. std::size_t expires_from_now(const duration& expiry_time)
  393. {
  394. asio::error_code ec;
  395. std::size_t s = this->service.expires_from_now(
  396. this->implementation, expiry_time, ec);
  397. asio::detail::throw_error(ec, "expires_from_now");
  398. return s;
  399. }
  400. /// Set the timer's expiry time relative to now.
  401. /**
  402. * This function sets the expiry time. Any pending asynchronous wait
  403. * operations will be cancelled. The handler for each cancelled operation will
  404. * be invoked with the asio::error::operation_aborted error code.
  405. *
  406. * @param expiry_time The expiry time to be used for the timer.
  407. *
  408. * @param ec Set to indicate what error occurred, if any.
  409. *
  410. * @return The number of asynchronous operations that were cancelled.
  411. *
  412. * @note If the timer has already expired when expires_from_now() is called,
  413. * then 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 expires_from_now(const duration& expiry_time,
  423. asio::error_code& ec)
  424. {
  425. return this->service.expires_from_now(
  426. this->implementation, expiry_time, ec);
  427. }
  428. /// Perform a blocking wait on the timer.
  429. /**
  430. * This function is used to wait for the timer to expire. This function
  431. * blocks and does not return until the timer has expired.
  432. *
  433. * @throws asio::system_error Thrown on failure.
  434. */
  435. void wait()
  436. {
  437. asio::error_code ec;
  438. this->service.wait(this->implementation, ec);
  439. asio::detail::throw_error(ec, "wait");
  440. }
  441. /// Perform a blocking wait on the timer.
  442. /**
  443. * This function is used to wait for the timer to expire. This function
  444. * blocks and does not return until the timer has expired.
  445. *
  446. * @param ec Set to indicate what error occurred, if any.
  447. */
  448. void wait(asio::error_code& ec)
  449. {
  450. this->service.wait(this->implementation, ec);
  451. }
  452. /// Start an asynchronous wait on the timer.
  453. /**
  454. * This function may be used to initiate an asynchronous wait against the
  455. * timer. It always returns immediately.
  456. *
  457. * For each call to async_wait(), the supplied handler will be called exactly
  458. * once. The handler will be called when:
  459. *
  460. * @li The timer has expired.
  461. *
  462. * @li The timer was cancelled, in which case the handler is passed the error
  463. * code asio::error::operation_aborted.
  464. *
  465. * @param handler The handler to be called when the timer expires. Copies
  466. * will be made of the handler as required. The function signature of the
  467. * handler must be:
  468. * @code void handler(
  469. * const asio::error_code& error // Result of operation.
  470. * ); @endcode
  471. * Regardless of whether the asynchronous operation completes immediately or
  472. * not, the handler will not be invoked from within this function. Invocation
  473. * of the handler will be performed in a manner equivalent to using
  474. * asio::io_service::post().
  475. */
  476. template <typename WaitHandler>
  477. ASIO_INITFN_RESULT_TYPE(WaitHandler,
  478. void (asio::error_code))
  479. async_wait(ASIO_MOVE_ARG(WaitHandler) handler)
  480. {
  481. // If you get an error on the following line it means that your handler does
  482. // not meet the documented type requirements for a WaitHandler.
  483. ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  484. return this->service.async_wait(this->implementation,
  485. ASIO_MOVE_CAST(WaitHandler)(handler));
  486. }
  487. };
  488. } // namespace asio
  489. #include "asio/detail/pop_options.hpp"
  490. #endif // ASIO_BASIC_WAITABLE_TIMER_HPP