winrt_timer_scheduler.ipp 2.9 KB

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