select_reactor.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // detail/impl/select_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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 TimeTraits, typename Allocator>
  36. void select_reactor::add_timer_queue(
  37. timer_queue<TimeTraits, Allocator>& queue)
  38. {
  39. do_add_timer_queue(queue);
  40. }
  41. // Remove a timer queue from the reactor.
  42. template <typename TimeTraits, typename Allocator>
  43. void select_reactor::remove_timer_queue(
  44. timer_queue<TimeTraits, Allocator>& queue)
  45. {
  46. do_remove_timer_queue(queue);
  47. }
  48. template <typename TimeTraits, typename Allocator>
  49. void select_reactor::schedule_timer(
  50. timer_queue<TimeTraits, Allocator>& queue,
  51. const typename TimeTraits::time_type& time,
  52. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  53. wait_op* op)
  54. {
  55. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  56. if (shutdown_)
  57. {
  58. scheduler_.post_immediate_completion(op, false);
  59. return;
  60. }
  61. bool earliest = queue.enqueue_timer(time, timer, op);
  62. scheduler_.work_started();
  63. if (earliest)
  64. interrupter_.interrupt();
  65. }
  66. template <typename TimeTraits, typename Allocator>
  67. std::size_t select_reactor::cancel_timer(
  68. timer_queue<TimeTraits, Allocator>& queue,
  69. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  70. std::size_t max_cancelled)
  71. {
  72. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  73. op_queue<operation> ops;
  74. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  75. lock.unlock();
  76. scheduler_.post_deferred_completions(ops);
  77. return n;
  78. }
  79. template <typename TimeTraits, typename Allocator>
  80. void select_reactor::cancel_timer_by_key(
  81. timer_queue<TimeTraits, Allocator>& queue,
  82. typename timer_queue<TimeTraits, Allocator>::per_timer_data* timer,
  83. void* cancellation_key)
  84. {
  85. mutex::scoped_lock lock(mutex_);
  86. op_queue<operation> ops;
  87. queue.cancel_timer_by_key(timer, ops, cancellation_key);
  88. lock.unlock();
  89. scheduler_.post_deferred_completions(ops);
  90. }
  91. template <typename TimeTraits, typename Allocator>
  92. void select_reactor::move_timer(timer_queue<TimeTraits, Allocator>& queue,
  93. typename timer_queue<TimeTraits, Allocator>::per_timer_data& target,
  94. typename timer_queue<TimeTraits, Allocator>::per_timer_data& source)
  95. {
  96. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  97. op_queue<operation> ops;
  98. queue.cancel_timer(target, ops);
  99. queue.move_timer(target, source);
  100. lock.unlock();
  101. scheduler_.post_deferred_completions(ops);
  102. }
  103. } // namespace detail
  104. } // namespace asio
  105. } // namespace boost
  106. #include <boost/asio/detail/pop_options.hpp>
  107. #endif // defined(BOOST_ASIO_HAS_IOCP)
  108. // || (!defined(BOOST_ASIO_HAS_DEV_POLL)
  109. // && !defined(BOOST_ASIO_HAS_EPOLL)
  110. // && !defined(BOOST_ASIO_HAS_KQUEUE)
  111. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME))
  112. #endif // BOOST_ASIO_DETAIL_IMPL_SELECT_REACTOR_HPP