select_reactor.hpp 3.7 KB

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