select_reactor.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // detail/impl/select_reactor.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_SELECT_REACTOR_HPP
  11. #define ASIO_DETAIL_IMPL_SELECT_REACTOR_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_HAS_IOCP) \
  17. || (!defined(ASIO_HAS_DEV_POLL) \
  18. && !defined(ASIO_HAS_EPOLL) \
  19. && !defined(ASIO_HAS_KQUEUE) \
  20. && !defined(ASIO_WINDOWS_RUNTIME))
  21. #if defined(ASIO_HAS_IOCP)
  22. # include "asio/detail/win_iocp_io_context.hpp"
  23. #else // defined(ASIO_HAS_IOCP)
  24. # include "asio/detail/scheduler.hpp"
  25. #endif // defined(ASIO_HAS_IOCP)
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. namespace detail {
  29. inline void select_reactor::post_immediate_completion(
  30. operation* op, bool is_continuation) const
  31. {
  32. scheduler_.post_immediate_completion(op, is_continuation);
  33. }
  34. template <typename Time_Traits>
  35. void select_reactor::add_timer_queue(timer_queue<Time_Traits>& queue)
  36. {
  37. do_add_timer_queue(queue);
  38. }
  39. // Remove a timer queue from the reactor.
  40. template <typename Time_Traits>
  41. void select_reactor::remove_timer_queue(timer_queue<Time_Traits>& queue)
  42. {
  43. do_remove_timer_queue(queue);
  44. }
  45. template <typename Time_Traits>
  46. void select_reactor::schedule_timer(timer_queue<Time_Traits>& queue,
  47. const typename Time_Traits::time_type& time,
  48. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op)
  49. {
  50. asio::detail::mutex::scoped_lock lock(mutex_);
  51. if (shutdown_)
  52. {
  53. scheduler_.post_immediate_completion(op, false);
  54. return;
  55. }
  56. bool earliest = queue.enqueue_timer(time, timer, op);
  57. scheduler_.work_started();
  58. if (earliest)
  59. interrupter_.interrupt();
  60. }
  61. template <typename Time_Traits>
  62. std::size_t select_reactor::cancel_timer(timer_queue<Time_Traits>& queue,
  63. typename timer_queue<Time_Traits>::per_timer_data& timer,
  64. std::size_t max_cancelled)
  65. {
  66. asio::detail::mutex::scoped_lock lock(mutex_);
  67. op_queue<operation> ops;
  68. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  69. lock.unlock();
  70. scheduler_.post_deferred_completions(ops);
  71. return n;
  72. }
  73. template <typename Time_Traits>
  74. void select_reactor::cancel_timer_by_key(timer_queue<Time_Traits>& queue,
  75. typename timer_queue<Time_Traits>::per_timer_data* timer,
  76. void* cancellation_key)
  77. {
  78. mutex::scoped_lock lock(mutex_);
  79. op_queue<operation> ops;
  80. queue.cancel_timer_by_key(timer, ops, cancellation_key);
  81. lock.unlock();
  82. scheduler_.post_deferred_completions(ops);
  83. }
  84. template <typename Time_Traits>
  85. void select_reactor::move_timer(timer_queue<Time_Traits>& queue,
  86. typename timer_queue<Time_Traits>::per_timer_data& target,
  87. typename timer_queue<Time_Traits>::per_timer_data& source)
  88. {
  89. asio::detail::mutex::scoped_lock lock(mutex_);
  90. op_queue<operation> ops;
  91. queue.cancel_timer(target, ops);
  92. queue.move_timer(target, source);
  93. lock.unlock();
  94. scheduler_.post_deferred_completions(ops);
  95. }
  96. } // namespace detail
  97. } // namespace asio
  98. #include "asio/detail/pop_options.hpp"
  99. #endif // defined(ASIO_HAS_IOCP)
  100. // || (!defined(ASIO_HAS_DEV_POLL)
  101. // && !defined(ASIO_HAS_EPOLL)
  102. // && !defined(ASIO_HAS_KQUEUE)
  103. // && !defined(ASIO_WINDOWS_RUNTIME))
  104. #endif // ASIO_DETAIL_IMPL_SELECT_REACTOR_HPP