winrt_timer_scheduler.hpp 2.8 KB

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