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.

deadline_timer_service.hpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // detail/deadline_timer_service.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 ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
  11. #define ASIO_DETAIL_DEADLINE_TIMER_SERVICE_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/error.hpp"
  18. #include "asio/execution_context.hpp"
  19. #include "asio/detail/bind_handler.hpp"
  20. #include "asio/detail/fenced_block.hpp"
  21. #include "asio/detail/memory.hpp"
  22. #include "asio/detail/noncopyable.hpp"
  23. #include "asio/detail/socket_ops.hpp"
  24. #include "asio/detail/socket_types.hpp"
  25. #include "asio/detail/timer_queue.hpp"
  26. #include "asio/detail/timer_queue_ptime.hpp"
  27. #include "asio/detail/timer_scheduler.hpp"
  28. #include "asio/detail/wait_handler.hpp"
  29. #include "asio/detail/wait_op.hpp"
  30. #if defined(ASIO_WINDOWS_RUNTIME)
  31. # include <chrono>
  32. # include <thread>
  33. #endif // defined(ASIO_WINDOWS_RUNTIME)
  34. #include "asio/detail/push_options.hpp"
  35. namespace asio {
  36. namespace detail {
  37. template <typename Time_Traits>
  38. class deadline_timer_service
  39. : public execution_context_service_base<deadline_timer_service<Time_Traits> >
  40. {
  41. public:
  42. // The time type.
  43. typedef typename Time_Traits::time_type time_type;
  44. // The duration type.
  45. typedef typename Time_Traits::duration_type duration_type;
  46. // The implementation type of the timer. This type is dependent on the
  47. // underlying implementation of the timer service.
  48. struct implementation_type
  49. : private asio::detail::noncopyable
  50. {
  51. time_type expiry;
  52. bool might_have_pending_waits;
  53. typename timer_queue<Time_Traits>::per_timer_data timer_data;
  54. };
  55. // Constructor.
  56. deadline_timer_service(execution_context& context)
  57. : execution_context_service_base<
  58. deadline_timer_service<Time_Traits> >(context),
  59. scheduler_(asio::use_service<timer_scheduler>(context))
  60. {
  61. scheduler_.init_task();
  62. scheduler_.add_timer_queue(timer_queue_);
  63. }
  64. // Destructor.
  65. ~deadline_timer_service()
  66. {
  67. scheduler_.remove_timer_queue(timer_queue_);
  68. }
  69. // Destroy all user-defined handler objects owned by the service.
  70. void shutdown()
  71. {
  72. }
  73. // Construct a new timer implementation.
  74. void construct(implementation_type& impl)
  75. {
  76. impl.expiry = time_type();
  77. impl.might_have_pending_waits = false;
  78. }
  79. // Destroy a timer implementation.
  80. void destroy(implementation_type& impl)
  81. {
  82. asio::error_code ec;
  83. cancel(impl, ec);
  84. }
  85. // Move-construct a new serial port implementation.
  86. void move_construct(implementation_type& impl,
  87. implementation_type& other_impl)
  88. {
  89. scheduler_.move_timer(timer_queue_, impl.timer_data, other_impl.timer_data);
  90. impl.expiry = other_impl.expiry;
  91. other_impl.expiry = time_type();
  92. impl.might_have_pending_waits = other_impl.might_have_pending_waits;
  93. other_impl.might_have_pending_waits = false;
  94. }
  95. // Move-assign from another serial port implementation.
  96. void move_assign(implementation_type& impl,
  97. deadline_timer_service& other_service,
  98. implementation_type& other_impl)
  99. {
  100. if (this != &other_service)
  101. if (impl.might_have_pending_waits)
  102. scheduler_.cancel_timer(timer_queue_, impl.timer_data);
  103. other_service.scheduler_.move_timer(other_service.timer_queue_,
  104. impl.timer_data, other_impl.timer_data);
  105. impl.expiry = other_impl.expiry;
  106. other_impl.expiry = time_type();
  107. impl.might_have_pending_waits = other_impl.might_have_pending_waits;
  108. other_impl.might_have_pending_waits = false;
  109. }
  110. // Cancel any asynchronous wait operations associated with the timer.
  111. std::size_t cancel(implementation_type& impl, asio::error_code& ec)
  112. {
  113. if (!impl.might_have_pending_waits)
  114. {
  115. ec = asio::error_code();
  116. return 0;
  117. }
  118. ASIO_HANDLER_OPERATION((scheduler_.context(),
  119. "deadline_timer", &impl, 0, "cancel"));
  120. std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
  121. impl.might_have_pending_waits = false;
  122. ec = asio::error_code();
  123. return count;
  124. }
  125. // Cancels one asynchronous wait operation associated with the timer.
  126. std::size_t cancel_one(implementation_type& impl,
  127. asio::error_code& ec)
  128. {
  129. if (!impl.might_have_pending_waits)
  130. {
  131. ec = asio::error_code();
  132. return 0;
  133. }
  134. ASIO_HANDLER_OPERATION((scheduler_.context(),
  135. "deadline_timer", &impl, 0, "cancel_one"));
  136. std::size_t count = scheduler_.cancel_timer(
  137. timer_queue_, impl.timer_data, 1);
  138. if (count == 0)
  139. impl.might_have_pending_waits = false;
  140. ec = asio::error_code();
  141. return count;
  142. }
  143. // Get the expiry time for the timer as an absolute time.
  144. time_type expiry(const implementation_type& impl) const
  145. {
  146. return impl.expiry;
  147. }
  148. // Get the expiry time for the timer as an absolute time.
  149. time_type expires_at(const implementation_type& impl) const
  150. {
  151. return impl.expiry;
  152. }
  153. // Get the expiry time for the timer relative to now.
  154. duration_type expires_from_now(const implementation_type& impl) const
  155. {
  156. return Time_Traits::subtract(this->expiry(impl), Time_Traits::now());
  157. }
  158. // Set the expiry time for the timer as an absolute time.
  159. std::size_t expires_at(implementation_type& impl,
  160. const time_type& expiry_time, asio::error_code& ec)
  161. {
  162. std::size_t count = cancel(impl, ec);
  163. impl.expiry = expiry_time;
  164. ec = asio::error_code();
  165. return count;
  166. }
  167. // Set the expiry time for the timer relative to now.
  168. std::size_t expires_after(implementation_type& impl,
  169. const duration_type& expiry_time, asio::error_code& ec)
  170. {
  171. return expires_at(impl,
  172. Time_Traits::add(Time_Traits::now(), expiry_time), ec);
  173. }
  174. // Set the expiry time for the timer relative to now.
  175. std::size_t expires_from_now(implementation_type& impl,
  176. const duration_type& expiry_time, asio::error_code& ec)
  177. {
  178. return expires_at(impl,
  179. Time_Traits::add(Time_Traits::now(), expiry_time), ec);
  180. }
  181. // Perform a blocking wait on the timer.
  182. void wait(implementation_type& impl, asio::error_code& ec)
  183. {
  184. time_type now = Time_Traits::now();
  185. ec = asio::error_code();
  186. while (Time_Traits::less_than(now, impl.expiry) && !ec)
  187. {
  188. this->do_wait(Time_Traits::to_posix_duration(
  189. Time_Traits::subtract(impl.expiry, now)), ec);
  190. now = Time_Traits::now();
  191. }
  192. }
  193. // Start an asynchronous wait on the timer.
  194. template <typename Handler, typename IoExecutor>
  195. void async_wait(implementation_type& impl,
  196. Handler& handler, const IoExecutor& io_ex)
  197. {
  198. // Allocate and construct an operation to wrap the handler.
  199. typedef wait_handler<Handler, IoExecutor> op;
  200. typename op::ptr p = { asio::detail::addressof(handler),
  201. op::ptr::allocate(handler), 0 };
  202. p.p = new (p.v) op(handler, io_ex);
  203. impl.might_have_pending_waits = true;
  204. ASIO_HANDLER_CREATION((scheduler_.context(),
  205. *p.p, "deadline_timer", &impl, 0, "async_wait"));
  206. scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
  207. p.v = p.p = 0;
  208. }
  209. private:
  210. // Helper function to wait given a duration type. The duration type should
  211. // either be of type boost::posix_time::time_duration, or implement the
  212. // required subset of its interface.
  213. template <typename Duration>
  214. void do_wait(const Duration& timeout, asio::error_code& ec)
  215. {
  216. #if defined(ASIO_WINDOWS_RUNTIME)
  217. std::this_thread::sleep_for(
  218. std::chrono::seconds(timeout.total_seconds())
  219. + std::chrono::microseconds(timeout.total_microseconds()));
  220. ec = asio::error_code();
  221. #else // defined(ASIO_WINDOWS_RUNTIME)
  222. ::timeval tv;
  223. tv.tv_sec = timeout.total_seconds();
  224. tv.tv_usec = timeout.total_microseconds() % 1000000;
  225. socket_ops::select(0, 0, 0, 0, &tv, ec);
  226. #endif // defined(ASIO_WINDOWS_RUNTIME)
  227. }
  228. // The queue of timers.
  229. timer_queue<Time_Traits> timer_queue_;
  230. // The object that schedules and executes timers. Usually a reactor.
  231. timer_scheduler& scheduler_;
  232. };
  233. } // namespace detail
  234. } // namespace asio
  235. #include "asio/detail/pop_options.hpp"
  236. #endif // ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP