winrt_timer_scheduler.ipp 2.9 KB

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