winrt_timer_scheduler.ipp 2.8 KB

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