Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

635 lines
22KB

  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_context);
  49. *
  50. * // Set an expiry time relative to now.
  51. * timer.expires_after(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_context,
  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_after(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_after() 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_after() functions must be called to set an expiry
  136. * time before the timer can be waited on.
  137. *
  138. * @param io_context The io_context 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_context& io_context)
  142. : basic_io_object<WaitableTimerService>(io_context)
  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_context The io_context 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_context& io_context,
  156. const time_point& expiry_time)
  157. : basic_io_object<WaitableTimerService>(io_context)
  158. {
  159. asio::error_code ec;
  160. this->get_service().expires_at(this->get_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_context The io_context 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_context& io_context,
  174. const duration& expiry_time)
  175. : basic_io_object<WaitableTimerService>(io_context)
  176. {
  177. asio::error_code ec;
  178. this->get_service().expires_after(
  179. this->get_implementation(), expiry_time, ec);
  180. asio::detail::throw_error(ec, "expires_after");
  181. }
  182. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  183. /// Move-construct a basic_waitable_timer from another.
  184. /**
  185. * This constructor moves a timer from one object to another.
  186. *
  187. * @param other The other basic_waitable_timer object from which the move will
  188. * occur.
  189. *
  190. * @note Following the move, the moved-from object is in the same state as if
  191. * constructed using the @c basic_waitable_timer(io_context&) constructor.
  192. */
  193. basic_waitable_timer(basic_waitable_timer&& other)
  194. : basic_io_object<WaitableTimerService>(
  195. ASIO_MOVE_CAST(basic_waitable_timer)(other))
  196. {
  197. }
  198. /// Move-assign a basic_waitable_timer from another.
  199. /**
  200. * This assignment operator moves a timer from one object to another.
  201. *
  202. * @param other The other basic_waitable_timer object from which the move will
  203. * occur.
  204. *
  205. * @note Following the move, the moved-from object is in the same state as if
  206. * constructed using the @c basic_waitable_timer(io_context&) constructor.
  207. */
  208. basic_waitable_timer& operator=(basic_waitable_timer&& other)
  209. {
  210. basic_io_object<WaitableTimerService>::operator=(
  211. ASIO_MOVE_CAST(basic_waitable_timer)(other));
  212. return *this;
  213. }
  214. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  215. /// Cancel any asynchronous operations that are waiting on the timer.
  216. /**
  217. * This function forces the completion of any pending asynchronous wait
  218. * operations against the timer. The handler for each cancelled operation will
  219. * be invoked with the asio::error::operation_aborted error code.
  220. *
  221. * Cancelling the timer does not change the expiry time.
  222. *
  223. * @return The number of asynchronous operations that were cancelled.
  224. *
  225. * @throws asio::system_error Thrown on failure.
  226. *
  227. * @note If the timer has already expired when cancel() is called, then the
  228. * handlers for asynchronous wait operations will:
  229. *
  230. * @li have already been invoked; or
  231. *
  232. * @li have been queued for invocation in the near future.
  233. *
  234. * These handlers can no longer be cancelled, and therefore are passed an
  235. * error code that indicates the successful completion of the wait operation.
  236. */
  237. std::size_t cancel()
  238. {
  239. asio::error_code ec;
  240. std::size_t s = this->get_service().cancel(this->get_implementation(), ec);
  241. asio::detail::throw_error(ec, "cancel");
  242. return s;
  243. }
  244. /// Cancel any asynchronous operations that are waiting on the timer.
  245. /**
  246. * This function forces the completion of any pending asynchronous wait
  247. * operations against the timer. The handler for each cancelled operation will
  248. * be invoked with the asio::error::operation_aborted error code.
  249. *
  250. * Cancelling the timer does not change the expiry time.
  251. *
  252. * @param ec Set to indicate what error occurred, if any.
  253. *
  254. * @return The number of asynchronous operations that were cancelled.
  255. *
  256. * @note If the timer has already expired when cancel() is called, then the
  257. * handlers for asynchronous wait operations will:
  258. *
  259. * @li have already been invoked; or
  260. *
  261. * @li have been queued for invocation in the near future.
  262. *
  263. * These handlers can no longer be cancelled, and therefore are passed an
  264. * error code that indicates the successful completion of the wait operation.
  265. */
  266. std::size_t cancel(asio::error_code& ec)
  267. {
  268. return this->get_service().cancel(this->get_implementation(), ec);
  269. }
  270. /// Cancels one asynchronous operation that is waiting on the timer.
  271. /**
  272. * This function forces the completion of one pending asynchronous wait
  273. * operation against the timer. Handlers are cancelled in FIFO order. The
  274. * handler for the cancelled operation will be invoked with the
  275. * asio::error::operation_aborted error code.
  276. *
  277. * Cancelling the timer does not change the expiry time.
  278. *
  279. * @return The number of asynchronous operations that were cancelled. That is,
  280. * either 0 or 1.
  281. *
  282. * @throws asio::system_error Thrown on failure.
  283. *
  284. * @note If the timer has already expired when cancel_one() is called, then
  285. * the handlers for asynchronous wait operations will:
  286. *
  287. * @li have already been invoked; or
  288. *
  289. * @li have been queued for invocation in the near future.
  290. *
  291. * These handlers can no longer be cancelled, and therefore are passed an
  292. * error code that indicates the successful completion of the wait operation.
  293. */
  294. std::size_t cancel_one()
  295. {
  296. asio::error_code ec;
  297. std::size_t s = this->get_service().cancel_one(
  298. this->get_implementation(), ec);
  299. asio::detail::throw_error(ec, "cancel_one");
  300. return s;
  301. }
  302. /// Cancels one asynchronous operation that is waiting on the timer.
  303. /**
  304. * This function forces the completion of one pending asynchronous wait
  305. * operation against the timer. Handlers are cancelled in FIFO order. The
  306. * handler for the cancelled operation will be invoked with the
  307. * asio::error::operation_aborted error code.
  308. *
  309. * Cancelling the timer does not change the expiry time.
  310. *
  311. * @param ec Set to indicate what error occurred, if any.
  312. *
  313. * @return The number of asynchronous operations that were cancelled. That is,
  314. * either 0 or 1.
  315. *
  316. * @note If the timer has already expired when cancel_one() 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 cancel_one(asio::error_code& ec)
  327. {
  328. return this->get_service().cancel_one(this->get_implementation(), ec);
  329. }
  330. #if !defined(ASIO_NO_DEPRECATED)
  331. /// (Deprecated: Use expiry().) Get the timer's expiry time as an absolute
  332. /// time.
  333. /**
  334. * This function may be used to obtain the timer's current expiry time.
  335. * Whether the timer has expired or not does not affect this value.
  336. */
  337. time_point expires_at() const
  338. {
  339. return this->get_service().expires_at(this->get_implementation());
  340. }
  341. #endif // !defined(ASIO_NO_DEPRECATED)
  342. /// Get the timer's expiry time as an absolute time.
  343. /**
  344. * This function may be used to obtain the timer's current expiry time.
  345. * Whether the timer has expired or not does not affect this value.
  346. */
  347. time_point expiry() const
  348. {
  349. return this->get_service().expiry(this->get_implementation());
  350. }
  351. /// Set the timer's expiry time as an absolute time.
  352. /**
  353. * This function sets the expiry time. Any pending asynchronous wait
  354. * operations will be cancelled. The handler for each cancelled operation will
  355. * be invoked with the asio::error::operation_aborted error code.
  356. *
  357. * @param expiry_time The expiry time to be used for the timer.
  358. *
  359. * @return The number of asynchronous operations that were cancelled.
  360. *
  361. * @throws asio::system_error Thrown on failure.
  362. *
  363. * @note If the timer has already expired when expires_at() is called, then
  364. * the handlers for asynchronous wait operations will:
  365. *
  366. * @li have already been invoked; or
  367. *
  368. * @li have been queued for invocation in the near future.
  369. *
  370. * These handlers can no longer be cancelled, and therefore are passed an
  371. * error code that indicates the successful completion of the wait operation.
  372. */
  373. std::size_t expires_at(const time_point& expiry_time)
  374. {
  375. asio::error_code ec;
  376. std::size_t s = this->get_service().expires_at(
  377. this->get_implementation(), expiry_time, ec);
  378. asio::detail::throw_error(ec, "expires_at");
  379. return s;
  380. }
  381. /// Set the timer's expiry time as an absolute time.
  382. /**
  383. * This function sets the expiry time. Any pending asynchronous wait
  384. * operations will be cancelled. The handler for each cancelled operation will
  385. * be invoked with the asio::error::operation_aborted error code.
  386. *
  387. * @param expiry_time The expiry time to be used for the timer.
  388. *
  389. * @param ec Set to indicate what error occurred, if any.
  390. *
  391. * @return The number of asynchronous operations that were cancelled.
  392. *
  393. * @note If the timer has already expired when expires_at() is called, then
  394. * the handlers for asynchronous wait operations will:
  395. *
  396. * @li have already been invoked; or
  397. *
  398. * @li have been queued for invocation in the near future.
  399. *
  400. * These handlers can no longer be cancelled, and therefore are passed an
  401. * error code that indicates the successful completion of the wait operation.
  402. */
  403. std::size_t expires_at(const time_point& expiry_time,
  404. asio::error_code& ec)
  405. {
  406. return this->get_service().expires_at(
  407. this->get_implementation(), expiry_time, ec);
  408. }
  409. /// Set the timer's expiry time relative to now.
  410. /**
  411. * This function sets the expiry time. Any pending asynchronous wait
  412. * operations will be cancelled. The handler for each cancelled operation will
  413. * be invoked with the asio::error::operation_aborted error code.
  414. *
  415. * @param expiry_time The expiry time to be used for the timer.
  416. *
  417. * @return The number of asynchronous operations that were cancelled.
  418. *
  419. * @throws asio::system_error Thrown on failure.
  420. *
  421. * @note If the timer has already expired when expires_after() is called,
  422. * then the handlers for asynchronous wait operations will:
  423. *
  424. * @li have already been invoked; or
  425. *
  426. * @li have been queued for invocation in the near future.
  427. *
  428. * These handlers can no longer be cancelled, and therefore are passed an
  429. * error code that indicates the successful completion of the wait operation.
  430. */
  431. std::size_t expires_after(const duration& expiry_time)
  432. {
  433. asio::error_code ec;
  434. std::size_t s = this->get_service().expires_after(
  435. this->get_implementation(), expiry_time, ec);
  436. asio::detail::throw_error(ec, "expires_after");
  437. return s;
  438. }
  439. /// Set the timer's expiry time relative to now.
  440. /**
  441. * This function sets the expiry time. Any pending asynchronous wait
  442. * operations will be cancelled. The handler for each cancelled operation will
  443. * be invoked with the asio::error::operation_aborted error code.
  444. *
  445. * @param expiry_time The expiry time to be used for the timer.
  446. *
  447. * @param ec Set to indicate what error occurred, if any.
  448. *
  449. * @return The number of asynchronous operations that were cancelled.
  450. *
  451. * @note If the timer has already expired when expires_after() is called,
  452. * then the handlers for asynchronous wait operations will:
  453. *
  454. * @li have already been invoked; or
  455. *
  456. * @li have been queued for invocation in the near future.
  457. *
  458. * These handlers can no longer be cancelled, and therefore are passed an
  459. * error code that indicates the successful completion of the wait operation.
  460. */
  461. std::size_t expires_after(const duration& expiry_time,
  462. asio::error_code& ec)
  463. {
  464. return this->get_service().expires_after(
  465. this->get_implementation(), expiry_time, ec);
  466. }
  467. #if !defined(ASIO_NO_DEPRECATED)
  468. /// (Deprecated: Use expiry().) Get the timer's expiry time relative to now.
  469. /**
  470. * This function may be used to obtain the timer's current expiry time.
  471. * Whether the timer has expired or not does not affect this value.
  472. */
  473. duration expires_from_now() const
  474. {
  475. return this->get_service().expires_from_now(this->get_implementation());
  476. }
  477. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  478. /// to now.
  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 asio::error::operation_aborted error code.
  483. *
  484. * @param expiry_time The expiry time to be used for the timer.
  485. *
  486. * @return The number of asynchronous operations that were cancelled.
  487. *
  488. * @throws asio::system_error Thrown on failure.
  489. *
  490. * @note If the timer has already expired when expires_from_now() is called,
  491. * then 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_from_now(const duration& expiry_time)
  501. {
  502. asio::error_code ec;
  503. std::size_t s = this->get_service().expires_from_now(
  504. this->get_implementation(), expiry_time, ec);
  505. asio::detail::throw_error(ec, "expires_from_now");
  506. return s;
  507. }
  508. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  509. /// to now.
  510. /**
  511. * This function sets the expiry time. Any pending asynchronous wait
  512. * operations will be cancelled. The handler for each cancelled operation will
  513. * be invoked with the asio::error::operation_aborted error code.
  514. *
  515. * @param expiry_time The expiry time to be used for the timer.
  516. *
  517. * @param ec Set to indicate what error occurred, if any.
  518. *
  519. * @return The number of asynchronous operations that were cancelled.
  520. *
  521. * @note If the timer has already expired when expires_from_now() is called,
  522. * then the handlers for asynchronous wait operations will:
  523. *
  524. * @li have already been invoked; or
  525. *
  526. * @li have been queued for invocation in the near future.
  527. *
  528. * These handlers can no longer be cancelled, and therefore are passed an
  529. * error code that indicates the successful completion of the wait operation.
  530. */
  531. std::size_t expires_from_now(const duration& expiry_time,
  532. asio::error_code& ec)
  533. {
  534. return this->get_service().expires_from_now(
  535. this->get_implementation(), expiry_time, ec);
  536. }
  537. #endif // !defined(ASIO_NO_DEPRECATED)
  538. /// Perform a blocking wait on the timer.
  539. /**
  540. * This function is used to wait for the timer to expire. This function
  541. * blocks and does not return until the timer has expired.
  542. *
  543. * @throws asio::system_error Thrown on failure.
  544. */
  545. void wait()
  546. {
  547. asio::error_code ec;
  548. this->get_service().wait(this->get_implementation(), ec);
  549. asio::detail::throw_error(ec, "wait");
  550. }
  551. /// Perform a blocking wait on the timer.
  552. /**
  553. * This function is used to wait for the timer to expire. This function
  554. * blocks and does not return until the timer has expired.
  555. *
  556. * @param ec Set to indicate what error occurred, if any.
  557. */
  558. void wait(asio::error_code& ec)
  559. {
  560. this->get_service().wait(this->get_implementation(), ec);
  561. }
  562. /// Start an asynchronous wait on the timer.
  563. /**
  564. * This function may be used to initiate an asynchronous wait against the
  565. * timer. It always returns immediately.
  566. *
  567. * For each call to async_wait(), the supplied handler will be called exactly
  568. * once. The handler will be called when:
  569. *
  570. * @li The timer has expired.
  571. *
  572. * @li The timer was cancelled, in which case the handler is passed the error
  573. * code asio::error::operation_aborted.
  574. *
  575. * @param handler The handler to be called when the timer expires. Copies
  576. * will be made of the handler as required. The function signature of the
  577. * handler must be:
  578. * @code void handler(
  579. * const asio::error_code& error // Result of operation.
  580. * ); @endcode
  581. * Regardless of whether the asynchronous operation completes immediately or
  582. * not, the handler will not be invoked from within this function. Invocation
  583. * of the handler will be performed in a manner equivalent to using
  584. * asio::io_context::post().
  585. */
  586. template <typename WaitHandler>
  587. ASIO_INITFN_RESULT_TYPE(WaitHandler,
  588. void (asio::error_code))
  589. async_wait(ASIO_MOVE_ARG(WaitHandler) handler)
  590. {
  591. // If you get an error on the following line it means that your handler does
  592. // not meet the documented type requirements for a WaitHandler.
  593. ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  594. return this->get_service().async_wait(this->get_implementation(),
  595. ASIO_MOVE_CAST(WaitHandler)(handler));
  596. }
  597. };
  598. } // namespace asio
  599. #include "asio/detail/pop_options.hpp"
  600. #endif // ASIO_BASIC_WAITABLE_TIMER_HPP