winrt_timer_scheduler.ipp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // detail/impl/winrt_timer_scheduler.ipp
  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_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_IPP
  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/bind_handler.hpp>
  18. #include <boost/asio/detail/winrt_timer_scheduler.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. winrt_timer_scheduler::winrt_timer_scheduler(execution_context& context)
  24. : execution_context_service_base<winrt_timer_scheduler>(context),
  25. scheduler_(use_service<scheduler_impl>(context)),
  26. mutex_(),
  27. event_(),
  28. timer_queues_(),
  29. thread_(),
  30. stop_thread_(false),
  31. shutdown_(false)
  32. {
  33. thread_ = thread(bind_handler(&winrt_timer_scheduler::call_run_thread, this));
  34. }
  35. winrt_timer_scheduler::~winrt_timer_scheduler()
  36. {
  37. shutdown();
  38. }
  39. void winrt_timer_scheduler::shutdown()
  40. {
  41. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  42. shutdown_ = true;
  43. stop_thread_ = true;
  44. event_.signal(lock);
  45. lock.unlock();
  46. thread_.join();
  47. op_queue<operation> ops;
  48. timer_queues_.get_all_timers(ops);
  49. scheduler_.abandon_operations(ops);
  50. }
  51. void winrt_timer_scheduler::notify_fork(execution_context::fork_event)
  52. {
  53. }
  54. void winrt_timer_scheduler::init_task()
  55. {
  56. }
  57. void winrt_timer_scheduler::run_thread()
  58. {
  59. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  60. while (!stop_thread_)
  61. {
  62. const long max_wait_duration = 5 * 60 * 1000000;
  63. long wait_duration = timer_queues_.wait_duration_usec(max_wait_duration);
  64. event_.wait_for_usec(lock, wait_duration);
  65. event_.clear(lock);
  66. op_queue<operation> ops;
  67. timer_queues_.get_ready_timers(ops);
  68. if (!ops.empty())
  69. {
  70. lock.unlock();
  71. scheduler_.post_deferred_completions(ops);
  72. lock.lock();
  73. }
  74. }
  75. }
  76. void winrt_timer_scheduler::call_run_thread(winrt_timer_scheduler* scheduler)
  77. {
  78. scheduler->run_thread();
  79. }
  80. void winrt_timer_scheduler::do_add_timer_queue(timer_queue_base& queue)
  81. {
  82. mutex::scoped_lock lock(mutex_);
  83. timer_queues_.insert(&queue);
  84. }
  85. void winrt_timer_scheduler::do_remove_timer_queue(timer_queue_base& queue)
  86. {
  87. mutex::scoped_lock lock(mutex_);
  88. timer_queues_.erase(&queue);
  89. }
  90. } // namespace detail
  91. } // namespace asio
  92. } // namespace boost
  93. #include <boost/asio/detail/pop_options.hpp>
  94. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  95. #endif // BOOST_ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_IPP