winrt_timer_scheduler.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // detail/winrt_timer_scheduler.hpp
  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_WINRT_TIMER_SCHEDULER_HPP
  11. #define BOOST_ASIO_DETAIL_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 <cstddef>
  18. #include <boost/asio/detail/event.hpp>
  19. #include <boost/asio/detail/limits.hpp>
  20. #include <boost/asio/detail/mutex.hpp>
  21. #include <boost/asio/detail/op_queue.hpp>
  22. #include <boost/asio/detail/thread.hpp>
  23. #include <boost/asio/detail/timer_queue_base.hpp>
  24. #include <boost/asio/detail/timer_queue_set.hpp>
  25. #include <boost/asio/detail/wait_op.hpp>
  26. #include <boost/asio/io_context.hpp>
  27. #if defined(BOOST_ASIO_HAS_IOCP)
  28. # include <boost/asio/detail/thread.hpp>
  29. #endif // defined(BOOST_ASIO_HAS_IOCP)
  30. #include <boost/asio/detail/push_options.hpp>
  31. namespace boost {
  32. namespace asio {
  33. namespace detail {
  34. class winrt_timer_scheduler
  35. : public boost::asio::detail::service_base<winrt_timer_scheduler>
  36. {
  37. public:
  38. // Constructor.
  39. BOOST_ASIO_DECL winrt_timer_scheduler(boost::asio::io_context& io_context);
  40. // Destructor.
  41. BOOST_ASIO_DECL ~winrt_timer_scheduler();
  42. // Destroy all user-defined handler objects owned by the service.
  43. BOOST_ASIO_DECL void shutdown();
  44. // Recreate internal descriptors following a fork.
  45. BOOST_ASIO_DECL void notify_fork(
  46. boost::asio::io_context::fork_event fork_ev);
  47. // Initialise the task. No effect as this class uses its own thread.
  48. BOOST_ASIO_DECL void init_task();
  49. // Add a new timer queue to the reactor.
  50. template <typename Time_Traits>
  51. void add_timer_queue(timer_queue<Time_Traits>& queue);
  52. // Remove a timer queue from the reactor.
  53. template <typename Time_Traits>
  54. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  55. // Schedule a new operation in the given timer queue to expire at the
  56. // specified absolute time.
  57. template <typename Time_Traits>
  58. void schedule_timer(timer_queue<Time_Traits>& queue,
  59. const typename Time_Traits::time_type& time,
  60. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
  61. // Cancel the timer operations associated with the given token. Returns the
  62. // number of operations that have been posted or dispatched.
  63. template <typename Time_Traits>
  64. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  65. typename timer_queue<Time_Traits>::per_timer_data& timer,
  66. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  67. // Move the timer operations associated with the given timer.
  68. template <typename Time_Traits>
  69. void move_timer(timer_queue<Time_Traits>& queue,
  70. typename timer_queue<Time_Traits>::per_timer_data& to,
  71. typename timer_queue<Time_Traits>::per_timer_data& from);
  72. private:
  73. // Run the select loop in the thread.
  74. BOOST_ASIO_DECL void run_thread();
  75. // Entry point for the select loop thread.
  76. BOOST_ASIO_DECL static void call_run_thread(winrt_timer_scheduler* reactor);
  77. // Helper function to add a new timer queue.
  78. BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  79. // Helper function to remove a timer queue.
  80. BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  81. // The io_context implementation used to post completions.
  82. io_context_impl& io_context_;
  83. // Mutex used to protect internal variables.
  84. boost::asio::detail::mutex mutex_;
  85. // Event used to wake up background thread.
  86. boost::asio::detail::event event_;
  87. // The timer queues.
  88. timer_queue_set timer_queues_;
  89. // The background thread that is waiting for timers to expire.
  90. boost::asio::detail::thread* thread_;
  91. // Does the background thread need to stop.
  92. bool stop_thread_;
  93. // Whether the service has been shut down.
  94. bool shutdown_;
  95. };
  96. } // namespace detail
  97. } // namespace asio
  98. } // namespace boost
  99. #include <boost/asio/detail/pop_options.hpp>
  100. #include <boost/asio/detail/impl/winrt_timer_scheduler.hpp>
  101. #if defined(BOOST_ASIO_HEADER_ONLY)
  102. # include <boost/asio/detail/impl/winrt_timer_scheduler.ipp>
  103. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  104. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  105. #endif // BOOST_ASIO_DETAIL_WINRT_TIMER_SCHEDULER_HPP