winrt_timer_scheduler.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // detail/impl/winrt_timer_scheduler.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_HPP
  11. #define BOOST_ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_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. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. template <typename Time_Traits>
  22. void winrt_timer_scheduler::add_timer_queue(timer_queue<Time_Traits>& queue)
  23. {
  24. do_add_timer_queue(queue);
  25. }
  26. // Remove a timer queue from the reactor.
  27. template <typename Time_Traits>
  28. void winrt_timer_scheduler::remove_timer_queue(timer_queue<Time_Traits>& queue)
  29. {
  30. do_remove_timer_queue(queue);
  31. }
  32. template <typename Time_Traits>
  33. void winrt_timer_scheduler::schedule_timer(timer_queue<Time_Traits>& queue,
  34. const typename Time_Traits::time_type& time,
  35. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op)
  36. {
  37. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  38. if (shutdown_)
  39. {
  40. io_context_.post_immediate_completion(op, false);
  41. return;
  42. }
  43. bool earliest = queue.enqueue_timer(time, timer, op);
  44. io_context_.work_started();
  45. if (earliest)
  46. event_.signal(lock);
  47. }
  48. template <typename Time_Traits>
  49. std::size_t winrt_timer_scheduler::cancel_timer(timer_queue<Time_Traits>& queue,
  50. typename timer_queue<Time_Traits>::per_timer_data& timer,
  51. std::size_t max_cancelled)
  52. {
  53. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  54. op_queue<operation> ops;
  55. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  56. lock.unlock();
  57. io_context_.post_deferred_completions(ops);
  58. return n;
  59. }
  60. template <typename Time_Traits>
  61. void winrt_timer_scheduler::move_timer(timer_queue<Time_Traits>& queue,
  62. typename timer_queue<Time_Traits>::per_timer_data& to,
  63. typename timer_queue<Time_Traits>::per_timer_data& from)
  64. {
  65. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  66. op_queue<operation> ops;
  67. queue.cancel_timer(to, ops);
  68. queue.move_timer(to, from);
  69. lock.unlock();
  70. scheduler_.post_deferred_completions(ops);
  71. }
  72. } // namespace detail
  73. } // namespace asio
  74. } // namespace boost
  75. #include <boost/asio/detail/pop_options.hpp>
  76. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  77. #endif // BOOST_ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_HPP