winrt_timer_scheduler.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // detail/impl/winrt_timer_scheduler.hpp
  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_HPP
  11. #define ASIO_DETAIL_IMPL_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 "asio/detail/config.hpp"
  16. #if defined(ASIO_WINDOWS_RUNTIME)
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. namespace detail {
  20. template <typename Time_Traits>
  21. void winrt_timer_scheduler::add_timer_queue(timer_queue<Time_Traits>& queue)
  22. {
  23. do_add_timer_queue(queue);
  24. }
  25. // Remove a timer queue from the reactor.
  26. template <typename Time_Traits>
  27. void winrt_timer_scheduler::remove_timer_queue(timer_queue<Time_Traits>& queue)
  28. {
  29. do_remove_timer_queue(queue);
  30. }
  31. template <typename Time_Traits>
  32. void winrt_timer_scheduler::schedule_timer(timer_queue<Time_Traits>& queue,
  33. const typename Time_Traits::time_type& time,
  34. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op)
  35. {
  36. asio::detail::mutex::scoped_lock lock(mutex_);
  37. if (shutdown_)
  38. {
  39. scheduler_.post_immediate_completion(op, false);
  40. return;
  41. }
  42. bool earliest = queue.enqueue_timer(time, timer, op);
  43. scheduler_.work_started();
  44. if (earliest)
  45. event_.signal(lock);
  46. }
  47. template <typename Time_Traits>
  48. std::size_t winrt_timer_scheduler::cancel_timer(timer_queue<Time_Traits>& queue,
  49. typename timer_queue<Time_Traits>::per_timer_data& timer,
  50. std::size_t max_cancelled)
  51. {
  52. asio::detail::mutex::scoped_lock lock(mutex_);
  53. op_queue<operation> ops;
  54. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  55. lock.unlock();
  56. scheduler_.post_deferred_completions(ops);
  57. return n;
  58. }
  59. template <typename Time_Traits>
  60. void winrt_timer_scheduler::move_timer(timer_queue<Time_Traits>& queue,
  61. typename timer_queue<Time_Traits>::per_timer_data& to,
  62. typename timer_queue<Time_Traits>::per_timer_data& from)
  63. {
  64. asio::detail::mutex::scoped_lock lock(mutex_);
  65. op_queue<operation> ops;
  66. queue.cancel_timer(to, ops);
  67. queue.move_timer(to, from);
  68. lock.unlock();
  69. scheduler_.post_deferred_completions(ops);
  70. }
  71. } // namespace detail
  72. } // namespace asio
  73. #include "asio/detail/pop_options.hpp"
  74. #endif // defined(ASIO_WINDOWS_RUNTIME)
  75. #endif // ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_HPP